Skip to content

Refact server#111

Open
Yunnglin wants to merge 25 commits intomainfrom
refact_server
Open

Refact server#111
Yunnglin wants to merge 25 commits intomainfrom
refact_server

Conversation

@Yunnglin
Copy link
Collaborator

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR information

Write the detail information belongs to this PR.

Experiment results

Paste your experiment result here(if needed).

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant architectural refactoring of the server, merging the functionalities of Tinker and Twinkle into a single, unified system. The changes aim to enhance maintainability and reduce redundancy by centralizing common utilities, establishing a dedicated gateway for client API routing, and modularizing the server's core components into distinct, manageable packages. This new structure simplifies deployment, improves code organization, and sets the foundation for future development.

Highlights

  • Unified Server Architecture: The server has been refactored to unify the previously separate Tinker and Twinkle server implementations into a single, cohesive Ray Serve deployment.
  • Centralized Common Utilities: Shared modules such as datum, io_utils, router, and serialize have been moved to a new src/twinkle/server/common directory, reducing code duplication.
  • Dedicated Gateway Service: A new src/twinkle/server/gateway service has been introduced to act as a unified entry point, handling API routing for both Tinker and Twinkle clients.
  • Modularized Application Components: Core application components (model, processor, and sampler) are now organized into distinct packages, each with dedicated Tinker and Twinkle API handlers.
  • Simplified Server Launcher: The ServerLauncher has been updated to remove the server_type argument, streamlining server configuration and reflecting the single server approach.
  • Client-Side Type Definitions: Explicit Pydantic models have been introduced in twinkle_client/types to define clear API request/response structures for server, model, processor, sampler, and training-related functionalities.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/twinkle/server/main.py
    • Simplified command-line arguments and usage examples by removing server type specific options.
  • src/twinkle/server/common/init.py
    • Added new initialization file for the common utilities package.
  • src/twinkle/server/common/datum.py
    • Renamed from src/twinkle/server/tinker/common/datum.py.
  • src/twinkle/server/common/io_utils.py
    • Added new file, merging functionalities from previous Tinker and Twinkle IO utilities.
  • src/twinkle/server/common/router.py
    • Renamed from src/twinkle/server/tinker/common/router.py.
  • src/twinkle/server/common/serialize.py
    • Renamed from src/twinkle/server/twinkle/common/serialize.py.
  • src/twinkle/server/gateway/init.py
    • Added new initialization file for the gateway package.
  • src/twinkle/server/gateway/proxy.py
    • Renamed from src/twinkle/server/tinker/proxy.py and updated for unified routing with Tinker endpoints prefixed.
  • src/twinkle/server/gateway/server.py
    • Added new file, implementing the unified gateway server for both Tinker and Twinkle clients.
  • src/twinkle/server/gateway/tinker_gateway_handlers.py
    • Added new file, containing Tinker-specific gateway handlers.
  • src/twinkle/server/gateway/twinkle_gateway_handlers.py
    • Added new file, containing Twinkle-specific gateway handlers.
  • src/twinkle/server/launcher.py
    • Updated ServerLauncher to remove the server_type argument and simplify builder resolution for a unified server.
  • src/twinkle/server/model/init.py
    • Added new initialization file for the model package.
  • src/twinkle/server/model/app.py
    • Added new file, implementing the unified model management application.
  • src/twinkle/server/model/backends/init.py
    • Renamed from src/twinkle/server/twinkle/common/__init__.py.
  • src/twinkle/server/model/backends/common.py
    • Renamed from src/twinkle/server/tinker/common/compat_base.py and updated for shared backend model helpers.
  • src/twinkle/server/model/backends/megatron_model.py
    • Renamed from src/twinkle/server/tinker/common/megatron_model.py and updated imports.
  • src/twinkle/server/model/backends/transformers_model.py
    • Renamed from src/twinkle/server/tinker/common/transformers_model.py and updated to handle both Tinker and Twinkle I/O.
  • src/twinkle/server/model/tinker_handlers.py
    • Added new file, containing Tinker-compatible model handlers.
  • src/twinkle/server/model/twinkle_handlers.py
    • Added new file, containing Twinkle-native model handlers.
  • src/twinkle/server/processor/init.py
    • Added new initialization file for the processor package.
  • src/twinkle/server/processor/app.py
    • Renamed from src/twinkle/server/twinkle/processor.py and updated for unified processor management.
  • src/twinkle/server/sampler/init.py
    • Added new initialization file for the sampler package.
  • src/twinkle/server/sampler/app.py
    • Added new file, implementing the unified sampler management application.
  • src/twinkle/server/sampler/tinker_handlers.py
    • Added new file, containing Tinker-compatible sampler handlers.
  • src/twinkle/server/sampler/twinkle_handlers.py
    • Added new file, containing Twinkle-native sampler handlers.
  • src/twinkle/server/tinker/init.py
    • Removed the Tinker server package.
  • src/twinkle/server/tinker/common/init.py
    • Removed the Tinker common utilities package.
  • src/twinkle/server/tinker/common/io_utils.py
    • Removed the Tinker-specific IO utilities.
  • src/twinkle/server/tinker/model.py
    • Removed the Tinker model management server.
  • src/twinkle/server/tinker/sampler.py
    • Removed the Tinker sampler server.
  • src/twinkle/server/tinker/server.py
    • Removed the Tinker-compatible server implementation.
  • src/twinkle/server/twinkle/init.py
    • Removed the Twinkle server package.
  • src/twinkle/server/twinkle/common/io_utils.py
    • Removed the Twinkle-specific IO utilities.
  • src/twinkle/server/twinkle/common/transformers_model.py
    • Removed the Twinkle-specific Transformers model compatibility wrapper.
  • src/twinkle/server/twinkle/model.py
    • Removed the Twinkle model management server.
  • src/twinkle/server/twinkle/sampler.py
    • Removed the Twinkle sampler server.
  • src/twinkle/server/twinkle/server.py
    • Removed the Twinkle REST API server.
  • src/twinkle/server/utils/task_queue.py
    • Added a new schedule_task_and_wait method for synchronous task execution.
  • src/twinkle_client/http/http_utils.py
    • Updated import path for serialize_object to reflect the new common utilities location.
  • src/twinkle_client/manager.py
    • Updated import paths for Checkpoint, Cursor, and TrainingRun to use the new client-side type definitions.
  • src/twinkle_client/types/init.py
    • Added new initialization file for the client types package.
  • src/twinkle_client/types/model.py
    • Added new file, defining Pydantic models for Twinkle model management endpoints.
  • src/twinkle_client/types/processor.py
    • Added new file, defining Pydantic models for Twinkle processor endpoints.
  • src/twinkle_client/types/sampler.py
    • Added new file, defining Pydantic models for Twinkle sampler endpoints.
  • src/twinkle_client/types/server.py
    • Added new file, defining Pydantic models for Twinkle server health and error endpoints.
  • src/twinkle_client/types/training.py
    • Added new file, defining Pydantic models for Twinkle training runs and checkpoints.
Activity
  • The author, Yunnglin, has initiated a significant refactoring effort to unify the server architecture.
  • The changes involve extensive file reorganization, including renaming, moving, and creating new files to establish a modular and centralized server structure.
  • The server launcher has been updated to reflect the new single server type, simplifying its configuration.
  • New client-side Pydantic models have been introduced to standardize API request and response structures, improving clarity and consistency.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a significant and well-executed refactoring of the server architecture. By unifying the tinker and twinkle server implementations, it greatly improves code structure, reduces duplication, and enhances maintainability. Key improvements include a unified gateway, model, and sampler application, consolidation of common utilities, and decoupling of client-side data types. The new structure is much cleaner and provides a solid foundation for future development. I've added a couple of minor suggestions to improve code style by moving inline imports to the top of their respective files.

Note: Security Review did not run due to the size of the PR.

@Yunnglin Yunnglin marked this pull request as ready for review March 12, 2026 13:21
Copilot AI review requested due to automatic review settings March 12, 2026 13:21
@Yunnglin
Copy link
Collaborator Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a major and well-executed refactoring of the server architecture, unifying the Tinker and Twinkle protocols under a single gateway. Key improvements include moving resource lifecycle management to be server-side and session-based, introducing typed Pydantic models for API requests and responses, and simplifying the client-side experience with a new TwinkleClient manager. The code is now better organized and more robust. I have one suggestion regarding a client-side dependency on server code.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors Twinkle’s server/client surface to a unified “gateway + model + sampler + processor” architecture that serves both Tinker-compatible (/tinker/*) and Twinkle-native (/twinkle/*) endpoints, and introduces session-based lifecycle management (session header propagated from client to server) for keeping adapters/processors alive without per-resource heartbeats.

Changes:

  • Introduces shared twinkle_client.types Pydantic models and updates client wrappers to return typed responses.
  • Replaces the legacy twinkle.server.twinkle/* and twinkle.server.tinker/* app modules with unified deployments under twinkle.server.gateway, twinkle.server.model, twinkle.server.sampler, and twinkle.server.processor.
  • Adds session ID propagation (X-Twinkle-Session-Id) and updates server state/task-queue codepaths to async APIs.

Reviewed changes

Copilot reviewed 104 out of 111 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/twinkle_client/utils/patch_tinker.py Switches env var naming for the injected tinker client token.
src/twinkle_client/types/training.py Adds shared training-run/checkpoint Pydantic models for Twinkle-native APIs.
src/twinkle_client/types/session.py Adds session create/heartbeat request/response models.
src/twinkle_client/types/server.py Adds basic health/error + checkpoint-path response models.
src/twinkle_client/types/sampler.py Adds sampler request/response Pydantic models (sample, set_template, add_adapter).
src/twinkle_client/types/processor.py Adds processor request/response Pydantic models (create/heartbeat/call).
src/twinkle_client/types/model.py Adds model-management request/response Pydantic models.
src/twinkle_client/types/checkpoint.py Adds ResolvedLoadPath model and moves it into client types for reuse.
src/twinkle_client/types/init.py Re-exports types for convenient import twinkle_client.types as types.
src/twinkle_client/sampler/vllm_sampler.py Updates sampler client routes and returns typed models instead of raw dicts.
src/twinkle_client/processor/base.py Updates processor client routes to new unified processor service paths.
src/twinkle_client/http/utils.py Replaces contextvars with global config + adds session-id support + forces /api/v1 suffix.
src/twinkle_client/http/http_utils.py Adds session header propagation + improves error surfacing with server detail.
src/twinkle_client/http/heartbeat.py Updates processor heartbeat endpoint routing to new unified processor service.
src/twinkle_client/http/init.py Updates exported HTTP helpers to match new utils API (session-id support).
src/twinkle_client/dataset/base.py Updates dataset processor client routes to new unified processor service.
src/twinkle_client/dataset/packing_dataset.py Same as above for packing dataset client.
src/twinkle_client/dataset/lazy_dataset.py Same as above for lazy dataset client.
src/twinkle_client/dataset/iterable_packing_dataset.py Same as above for iterable packing dataset client.
src/twinkle_client/dataset/iterable_dataset.py Same as above for iterable dataset client.
src/twinkle_client/dataloader/dataloader.py Updates dataloader processor client routes to new unified processor service.
src/twinkle_client/init.py Introduces init_twinkle_client() creating a server-side session + background heartbeat.
src/twinkle/server/utils/validation.py Stores session id from request headers and exposes get_session_id_from_request().
src/twinkle/server/utils/task_queue.py Makes server-state future status updates async + adds schedule_task_and_wait().
src/twinkle/server/utils/state/server_state.py Converts several ServerStateProxy APIs to async (touch_session, futures, replica availability).
src/twinkle/server/utils/processor_manager.py Adds a session-based processor lifecycle manager mixin with per-token limits.
src/twinkle/server/utils/checkpoint_base.py Renames/reshapes checkpoint persistence base; reuses shared ResolvedLoadPath type.
src/twinkle/server/utils/adapter_manager.py Switches adapter expiry to session-based expiration + adds max lifetime enforcement.
src/twinkle/server/utils/init.py Updates exports to the new module names (checkpoint_base, processor_manager).
src/twinkle/server/twinkle/server.py Removes legacy Twinkle-only gateway server implementation.
src/twinkle/server/twinkle/sampler.py Removes legacy Twinkle-only sampler implementation.
src/twinkle/server/twinkle/processor.py Removes legacy Twinkle-only processor implementation.
src/twinkle/server/twinkle/common/transformers_model.py Removes legacy Twinkle-only compat wrapper (moved into unified backends).
src/twinkle/server/twinkle/init.py Removes legacy lazy-module entrypoint for twinkle server subpackage.
src/twinkle/server/tinker/sampler.py Removes legacy Tinker-only sampler implementation.
src/twinkle/server/tinker/common/transformers_model.py Removes legacy Tinker-only transformers compat wrapper (moved into unified backends).
src/twinkle/server/tinker/common/megatron_model.py Removes legacy Tinker-only megatron compat wrapper (moved into unified backends).
src/twinkle/server/tinker/common/io_utils.py Removes legacy Tinker-only checkpoint IO utilities (replaced by common managers).
src/twinkle/server/tinker/common/init.py Removes legacy tinker common exports.
src/twinkle/server/tinker/init.py Removes legacy lazy-module entrypoint for tinker server subpackage.
src/twinkle/server/sampler/twinkle_handlers.py Adds Twinkle-native sampler endpoints on the unified sampler deployment.
src/twinkle/server/sampler/tinker_handlers.py Adds Tinker-compatible sampler endpoint on the unified sampler deployment.
src/twinkle/server/sampler/app.py Adds unified SamplerManagement deployment wiring both tinker + twinkle routes.
src/twinkle/server/sampler/init.py Exposes build_sampler_app.
src/twinkle/server/processor/twinkle_handlers.py Adds Twinkle-native processor endpoints (create/call) with session-based lifecycle.
src/twinkle/server/processor/app.py Adds unified ProcessorManagement deployment using ProcessorManagerMixin.
src/twinkle/server/processor/init.py Exposes build_processor_app.
src/twinkle/server/model/backends/transformers_model.py Adds unified transformers backend supporting both Tinker and Twinkle I/O styles.
src/twinkle/server/model/backends/megatron_model.py Adds unified megatron backend supporting Tinker I/O style.
src/twinkle/server/model/backends/common.py Centralizes shared backend helpers (collect/metrics cleanup/output shaping).
src/twinkle/server/model/backends/init.py Initializes backend package.
src/twinkle/server/model/app.py Adds unified ModelManagement deployment wiring both tinker + twinkle routes.
src/twinkle/server/model/init.py Exposes build_model_app.
src/twinkle/server/gateway/twinkle_gateway_handlers.py Adds Twinkle-native management endpoints (sessions, runs, checkpoints, weights_info, path).
src/twinkle/server/gateway/server.py Adds unified gateway server deployment wiring both Tinker + Twinkle APIs.
src/twinkle/server/gateway/proxy.py Updates internal proxy routing to hit /tinker/* paths on unified deployments.
src/twinkle/server/gateway/init.py Exposes build_server_app.
src/twinkle/server/common/twinkle_checkpoint.py Refactors Twinkle checkpoint/run managers to use shared client Pydantic models.
src/twinkle/server/common/tinker_checkpoint.py Introduces Tinker checkpoint/run managers using tinker.types.
src/twinkle/server/common/serialize.py Moves/centralizes serialization utilities under twinkle.server.common.
src/twinkle/server/common/router.py Makes replica-capacity query async for compatibility with async ServerStateProxy.
src/twinkle/server/common/datum.py Adds/keeps common datum helpers (copyright header update).
src/twinkle/server/common/checkpoint_factory.py Adds factory for choosing tinker vs twinkle checkpoint managers by client type.
src/twinkle/server/common/init.py Exposes common utilities/factories for server subpackages.
src/twinkle/server/main.py Simplifies CLI help/args toward unified server behavior.
src/twinkle/preprocessor/init.py Adds GSM8KProcessor export.
src/twinkle/infra/_ray/ray_helper.py Adjusts Ray task resource request for helper placement group operation.
src/twinkle/hub/hub.py Modifies hub push behavior to validate success (but changes return behavior).
setup.cfg Extends flake8 ignore list (adds E125).
cookbook/transformers/sp_fsdp_dense.py Updates example model id to Qwen3.5-4B.
cookbook/transformers/fsdp2.py Updates example model id to Qwen3.5-4B.
cookbook/client/twinkle/transformer/server_config.yaml Removes older twinkle-only server config example.
cookbook/client/twinkle/transformer/server.py Removes older twinkle-only server launcher example.
cookbook/client/twinkle/megatron/server_config.yaml Removes older twinkle-only megatron server config example.
cookbook/client/twinkle/megatron/server.py Removes older twinkle-only megatron server launcher example.
cookbook/client/twinkle/self_host/self_congnition.py Updates training loop example to new typed responses / combined step API.
cookbook/client/twinkle/self_host/sample.py Updates sampling example to new typed sampler response.
cookbook/client/twinkle/self_host/grpo.py Updates GRPO example to GSM8K + new typed save/sample/metric APIs.
cookbook/client/tinker/self_host/self_cognition.py Updates import paths to unified twinkle.server.common helpers.
cookbook/client/tinker/self_host/sample.py Adds new tinker-compatible sampling example script.
cookbook/client/tinker/self_host/lora.py Adds new tinker-compatible LoRA training example script.
cookbook/client/tinker/modelscope_service/server.py Removes legacy modelscope-service server launcher example.
cookbook/client/tinker/modelscope/self_cognition.py Updates import paths to unified twinkle.server.common helpers.
cookbook/client/tinker/modelscope/sample.py Adds new modelscope-hosted tinker-compatible sampling example script.
cookbook/client/server/transformer/server_config.yaml Updates tinker-compatible server config and adds processor service app.
cookbook/client/server/transformer/server.py Adds/updates server launcher for cookbook config.
cookbook/client/server/megatron/server_config_4b.yaml Updates megatron server config (processor service added).
cookbook/client/server/megatron/server_config.yaml Updates megatron server config (processor service added).
cookbook/client/server/megatron/server.py Switches default config file to server_config_4b.yaml.
README_ZH.md Updates documentation import paths to unified server common helpers.
README.md Updates documentation import paths to unified server common helpers.

You can also share your feedback on Copilot code review. Take the survey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants