Skip to content

Update sample and server#38

Merged
Yunnglin merged 10 commits intodevfrom
update_sample
Feb 7, 2026
Merged

Update sample and server#38
Yunnglin merged 10 commits intodevfrom
update_sample

Conversation

@Yunnglin
Copy link
Copy Markdown
Collaborator

@Yunnglin Yunnglin commented Feb 6, 2026

No description provided.

Copilot AI review requested due to automatic review settings February 6, 2026 05:14
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @Yunnglin, 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 streamlines the server setup and management process by introducing a unified server launcher and a dedicated command-line interface. It enhances the modularity of the codebase by relocating common utilities to a shared package and significantly improves checkpoint handling to support diverse model loading scenarios, including direct integration with model hubs. These changes aim to simplify development, deployment, and maintenance of Twinkle servers and related applications.

Highlights

  • Unified Server Launching: Introduced a new launch_server function and ServerLauncher class to centralize and simplify the startup logic for both 'tinker' and 'twinkle' server types. This significantly reduces boilerplate code in server scripts.
  • New CLI for Server Management: A new command-line interface (python -m twinkle.server) has been added, allowing users to launch and configure Twinkle servers directly from a YAML configuration file or with minimal command-line arguments.
  • Refactored Common Utilities: Several common utility modules, including adapter management, task queue, and rate limiter, have been moved from twinkle.server.tinker.common to a more general twinkle.server.utils package, improving modularity and reusability across different server implementations.
  • Enhanced Checkpoint Management: The checkpoint management system has been improved to support loading models from both local twinkle:// paths and remote hub model IDs. Checkpoint metadata now includes more detailed information like base model, LoRA configuration, and user metadata.
  • Updated Sample Configurations and Scripts: All example server configuration YAMLs and associated Python scripts have been updated to utilize the new launch_server mechanism and reflect changes in import_path conventions and model IDs.

🧠 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
  • cookbook/client/tinker/megatron/server.py
    • Refactored server startup logic to use the new launch_server function, removing extensive manual Ray Serve setup.
  • cookbook/client/tinker/megatron/server_config.yaml
    • Added server_type: tinker configuration.
    • Simplified import_path values (e.g., main:build_server_app to server).
    • Removed commented-out example model configurations.
  • cookbook/client/tinker/transformer/lora.py
    • Added dotenv and os imports for environment variable handling.
    • Modified init_tinker_compat_client to pass api_key from environment variable.
    • Updated base_model from Qwen/Qwen2.5-7B-Instruct to Qwen/Qwen2.5-0.5B-Instruct.
    • Added comments to support resuming from twinkle path or model ID.
    • Added a print statement for resuming training.
    • Added comments regarding api_key and publishing checkpoints.
  • cookbook/client/tinker/transformer/self_congnition.py
    • Updated base_model from Qwen/Qwen2.5-7B-Instruct to Qwen/Qwen2.5-0.5B-Instruct.
  • cookbook/client/tinker/transformer/server.py
    • Refactored server startup logic to use the new launch_server function.
  • cookbook/client/tinker/transformer/server_config.yaml
    • Added server_type: tinker configuration.
    • Simplified import_path values.
    • Updated model IDs to Qwen2.5-0.5B-Instruct.
    • Removed commented-out sampler and model configurations.
  • cookbook/client/twinkle/megatron/server.py
    • Refactored server startup logic to use the new launch_server function.
  • cookbook/client/twinkle/megatron/server_config.yaml
    • Added server_type: twinkle configuration.
    • Simplified import_path values.
  • cookbook/client/twinkle/transformer/lora.py
    • Added comments for saving and uploading models.
    • Updated hub_model_id from AlexEz/twinkle-self-cognition-2 to AlexEz/twinkle-self-cognition.
  • cookbook/client/twinkle/transformer/server.py
    • Refactored server startup logic to use the new launch_server function.
  • cookbook/client/twinkle/transformer/server_config.yaml
    • Added server_type: twinkle configuration.
    • Simplified import_path values.
    • Added adapter_config with per_token_adapter_limit and adapter_timeout.
  • cookbook/remote/tinker/ascend/server.py
    • Refactored server startup logic to use the new launch_server function.
  • cookbook/remote/tinker/ascend/server_config.yaml
    • Added server_type: tinker configuration.
    • Simplified import_path values.
  • cookbook/remote/tinker/server.py
    • Refactored server startup logic to use the new launch_server function.
  • cookbook/remote/tinker/server_config.yaml
    • Added server_type: tinker configuration.
    • Simplified import_path values.
  • cookbook/remote/twinkle/server.py
    • Refactored server startup logic to use the new launch_server function.
  • cookbook/remote/twinkle/server_config.yaml
    • Added server_type: twinkle configuration.
    • Simplified import_path values.
  • src/twinkle/hub/hub.py
    • Refactored HubOperation methods to use a new _get_hub_class helper for dynamic hub selection.
    • Added a new download_file method to HubOperation, MSHub, and HFHub for downloading specific files.
  • src/twinkle/server/init.py
    • Imported ServerLauncher and launch_server from the new launcher.py module.
    • Updated __all__ to include new launcher components.
  • src/twinkle/server/main.py
    • Added a new CLI entry point for launching Twinkle servers, supporting YAML configuration and server type specification.
  • src/twinkle/server/launcher.py
    • Added a new ServerLauncher class and launch_server function to provide a unified server startup mechanism.
    • Implemented logic for Ray/Serve initialization, application deployment from config, and builder function resolution.
  • src/twinkle/server/tinker/init.py
    • Added __all__ export list for better module control.
  • src/twinkle/server/tinker/common/init.py
    • Removed imports for TwinkleCompatTransformersModel, TwinkleCompatMegatronModel, RateLimiter, TaskQueue related classes, as these have been relocated.
  • src/twinkle/server/tinker/common/adapter_manager.py -> src/twinkle/server/utils/adapter_manager.py
    • Renamed and moved adapter_manager.py to twinkle.server.utils, making it a shared utility.
  • src/twinkle/server/tinker/common/io_utils.py
    • Imported ResolvedLoadPath.
    • Added _transform_checkpoint_fields to TrainingRunManager for compatibility with different checkpoint path formats.
    • Modified _create_checkpoint to include additional metadata (e.g., base_model, is_lora).
    • Modified _parse_checkpoint to handle twinkle_path to tinker_path transformations.
  • src/twinkle/server/tinker/common/megatron_model.py
    • Updated load method to use checkpoint_manager.resolve_load_path for unified handling of twinkle:// paths and hub model IDs.
  • src/twinkle/server/tinker/common/rate_limiter.py -> src/twinkle/server/utils/rate_limiter.py
    • Renamed and moved rate_limiter.py to twinkle.server.utils.
  • src/twinkle/server/tinker/common/task_queue.py -> src/twinkle/server/utils/task_queue.py
    • Renamed and moved task_queue.py to twinkle.server.utils.
  • src/twinkle/server/tinker/common/transformers_model.py
    • Updated load method to use checkpoint_manager.resolve_load_path.
  • src/twinkle/server/tinker/model.py
    • Updated imports to use TaskQueueMixin, TaskQueueConfig, and AdapterManagerMixin from twinkle.server.utils.
    • Imported TwinkleCompatTransformersModel locally within __init__ to avoid circular dependencies.
  • src/twinkle/server/tinker/sampler.py
    • Updated imports to use TaskQueueMixin and TaskQueueConfig from twinkle.server.utils.
  • src/twinkle/server/tinker/server.py
    • Updated imports to use QueueState from twinkle.server.utils.
    • Added _modelscope_config_lock to prevent race conditions during ModelScope login operations.
    • Improved error handling for retrieving ModelScope username.
  • src/twinkle/server/twinkle/common/init.py
    • Removed numerous imports, indicating relocation of these utilities to twinkle.server.utils.io_utils.
  • src/twinkle/server/twinkle/common/io_utils.py
    • Imported ResolvedLoadPath.
    • Modified _create_checkpoint to include additional metadata.
    • Modified _parse_checkpoint to handle tinker_path to twinkle_path transformations.
    • Added a get method to CheckpointManager for backwards compatibility in retrieving checkpoint metadata.
  • src/twinkle/server/twinkle/model.py
    • Removed threading and time imports.
    • Imported AdapterManagerMixin from twinkle.server.utils and get_logger.
    • The ModelManagement class now inherits from AdapterManagerMixin, centralizing adapter lifecycle management.
    • Refactored adapter management logic to use the mixin's methods.
    • Modified load method to use checkpoint_manager.resolve_load_path for unified path resolution.
  • src/twinkle/server/utils/init.py
    • Added imports for RateLimiter, TaskStatus, QueueState, TaskQueueConfig, TaskQueueMixin, and AdapterManagerMixin, consolidating common utilities.
  • src/twinkle/server/utils/io_utils.py
    • Imported HubOperation.
    • The BaseCheckpoint model now includes new fields for base_model, is_lora, lora_rank, train_unembed, train_mlp, train_attn, and user_metadata.
    • Added ResolvedLoadPath class to standardize checkpoint path resolution.
    • Modified _create_checkpoint signature to accept new metadata fields.
    • Added get_weights_info and _get_weights_info_from_hub to BaseCheckpointManager to support fetching weights information from model hubs.
    • Introduced resolve_load_path to BaseCheckpointManager for unifying local and hub path resolution.
  • src/twinkle_client/utils/patch_tinker.py
    • Added a patch to ParsedCheckpointTinkerPath.from_tinker_path to enable support for both tinker:// and twinkle:// prefixes for checkpoint paths.
Activity
  • The pull request was created by Yunnglin.
  • The pull request description is empty, indicating that the intent and high-level changes need to be inferred from the code modifications.
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
Copy Markdown
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 valuable refactoring of the server launching mechanism and checkpoint handling. The introduction of ServerLauncher and a unified launch_server function greatly simplifies the server startup scripts by centralizing boilerplate code. The refactoring of io_utils to handle different path formats (twinkle://, hub IDs) via resolve_load_path is a major improvement for flexibility and robustness. Moving shared utilities like AdapterManagerMixin and TaskQueueMixin to a common utils directory improves code structure and reusability. The addition of a CLI entry point is also a great enhancement.

While the overall changes are excellent, I've found a critical bug in the new CLI module and a regression in the server launcher's deployment option handling. I've also noted a minor style issue. Please see my detailed comments.

Comment on lines +864 to +866
from pathlib import Path as PathLib
if not PathLib(checkpoint_dir).exists():
raise ValueError(f"Checkpoint directory not found: {checkpoint_dir}")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The import from pathlib import Path as PathLib is performed inside the resolve_load_path method. It's better practice to move all imports to the top of the file for consistency and readability. Since Path is already imported at the top of the file, you can remove this local import and use Path directly.

            if not Path(checkpoint_dir).exists():
                raise ValueError(f"Checkpoint directory not found: {checkpoint_dir}")

Copy link
Copy Markdown

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 pull request updates the sample and server code with significant refactoring and new features for the Twinkle distributed training framework. The changes introduce a unified server launcher, modularize task queue and adapter management into mixins, add support for loading models from both local twinkle:// paths and hub model IDs, and simplify server startup scripts across all cookbook examples.

Changes:

  • Added unified server launcher with CLI support for both tinker and twinkle server types
  • Refactored adapter management and task queue functionality into reusable mixin classes
  • Enhanced checkpoint loading to support both twinkle:// paths and hub model IDs
  • Added new hub download_file method and refactored hub operations for better code reuse

Reviewed changes

Copilot reviewed 35 out of 38 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/twinkle_client/utils/patch_tinker.py Added support for both 'tinker://' and 'twinkle://' prefixes in checkpoint paths
src/twinkle/server/utils/task_queue.py New task queue management mixin with rate limiting and status tracking
src/twinkle/server/utils/rate_limiter.py New sliding window rate limiter for RPS/TPS limits with memory cleanup
src/twinkle/server/utils/adapter_manager.py New adapter lifecycle management mixin with automatic timeout
src/twinkle/server/utils/io_utils.py Added resolve_load_path method and hub integration for checkpoint loading
src/twinkle/server/twinkle/model.py Integrated adapter manager mixin, removed manual threading code
src/twinkle/server/tinker/server.py Added lock for ModelScope config file operations
src/twinkle/server/tinker/model.py Integrated task queue and adapter manager mixins
src/twinkle/server/tinker/common/transformers_model.py Simplified checkpoint loading using resolve_load_path
src/twinkle/server/tinker/common/megatron_model.py Simplified checkpoint loading using resolve_load_path
src/twinkle/server/tinker/common/io_utils.py Added checkpoint field transformations for backwards compatibility
src/twinkle/server/launcher.py New unified server launcher supporting YAML config and CLI
src/twinkle/server/main.py New CLI entry point for server launching
src/twinkle/server/init.py Added exports for launcher functionality
src/twinkle/hub/hub.py Added download_file method and refactored for code reuse
cookbook/*/server.py Simplified all server scripts to use new launch_server function
cookbook/*/server_config.yaml Updated import_path to use simple names (server, model, sampler)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +403 to +405
ret = self.model.load(
name=resolved.checkpoint_name,
output_dir=resolved.checkpoint_dir,
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

When resolved.checkpoint_dir is None (for hub model IDs), passing output_dir=None to self.model.load() could cause issues depending on how the model's load method handles this parameter. The load method should either handle None gracefully or this code should conditionally pass output_dir only when it's not None.

Copilot uses AI. Check for mistakes.
parsed_args = parser.parse_args(args)

# Setup logging
setup_logging(parsed_args.log_level)
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

The function setup_logging is called but never defined. This will cause a NameError at runtime when the CLI is executed.

Copilot uses AI. Check for mistakes.
self.state.pop_config(user_key)

# Initialize adapter manager from mixin
self._init_adapter_manager(**adapter_config)
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

The code attempts to unpack adapter_config dictionary with **adapter_config, but adapter_config could be None (as it defaults to None in the function signature at line 142). This will cause a TypeError when adapter_config is not provided. Should add a check or use **(adapter_config or {}) instead.

Copilot uses AI. Check for mistakes.
return parser


def main(args: list[str] | None = None) -> int:
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

The type annotation list[str] | None uses Python 3.10+ syntax. For Python 3.9 compatibility, this should be Optional[List[str]] or use from __future__ import annotations (which is present, so this is fine). However, ensure the minimum Python version requirement is documented as 3.10+ if this syntax is used consistently, or use Union and List from typing for broader compatibility.

Copilot uses AI. Check for mistakes.
Comment on lines +4 to 5
from twinkle.server import launch_server

Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

The module 'twinkle.server' imports itself.

Suggested change
from twinkle.server import launch_server

Copilot uses AI. Check for mistakes.
"""
from __future__ import annotations

import os
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

Import of 'os' is not used.

Suggested change
import os

Copilot uses AI. Check for mistakes.
import os
import time
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Union
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

Import of 'List' is not used.

Suggested change
from typing import Any, Callable, Dict, List, Optional, Union
from typing import Any, Callable, Dict, Optional, Union

Copilot uses AI. Check for mistakes.
Comment on lines +190 to +191
except Exception:
pass
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

'except' clause does nothing but pass and there is no explanatory comment.

Suggested change
except Exception:
pass
except Exception as exc:
# Ignore shutdown errors but log for debugging purposes
logger.debug("Failed to shutdown existing Ray Serve instance: %s", exc)

Copilot uses AI. Check for mistakes.
Yunnglin and others added 4 commits February 6, 2026 14:05
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@Yunnglin Yunnglin merged commit 1dfcf43 into dev Feb 7, 2026
0 of 3 checks passed
@Yunnglin Yunnglin deleted the update_sample branch February 10, 2026 07:52
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