Skip to content

feat: stream S3 safetensors with Run:ai Model Streamer 0.17.0 - #5241

Open
Reinerzhou wants to merge 5 commits into
InternLM:mainfrom
Reinerzhou:feat/runai-streamer-017
Open

Reinerzhou wants to merge 5 commits into
InternLM:mainfrom
Reinerzhou:feat/runai-streamer-017

Conversation

@Reinerzhou

@Reinerzhou Reinerzhou commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Stream S3 safetensors through the official, unmodified runai-model-streamer==0.17.0 and runai-model-streamer-s3==0.17.0 wheels. requirements/serve.txt lists their S3 dependency chain for serving images that install LMDeploy with pip --no-deps, including python-dateutil>=2.1,<3.0.0 to satisfy botocore; the existing requirements_cuda.txt includes serve.txt, so the CUDA all extra also includes it. PyTorch remains in LMDeploy's CUDA runtime dependencies.
  • Cache only model metadata, keyed by the effective S3 endpoint, model URI, and remote metadata signature. A cache hit checks metadata ETags/sizes/timestamps; changed metadata gets a new cache directory while old versions remain available to running services. Indexed checkpoints derive shard URIs from model.safetensors.index.json without a separate weight listing during streaming. Existing MTP drafts select only their own tensors and shards; local and S3 loading share this selection rule. Unindexed checkpoints read only root model.safetensors.
  • Support --load-format runai_streamer, --model-loader-extra-config, and PytorchEngineConfig.model_loader_extra_config. Loader overrides are passed through engine configuration and restored after each stream. Ray workers receive non-secret S3 settings and reconstruct metadata in their cache directory. Custom endpoints use AWS_ENDPOINT_URL_S3 for boto3 metadata requests, leaving STS unchanged.
  • Validate unsupported S3 loader/backend combinations before CLI model download and reject independent S3 draft loading when only one target TP rank would enter Run:ai's rank discovery. The multiprocessing launcher now surfaces this error before spawning its engine process. Add English and Chinese documentation with generic S3 examples. This PR adds no model architecture or speculative decoding method.

Validation

With the official Run:ai 0.17.0 wheels, pre-commit and the relevant local weight-loader/MemDecode tests passed 36/36 on 79f382d1. Current-head CI passed: lint, unit tests, PR function tests, and Chinese documentation.

Real S3 GLM-5.3-Flash serving was rerun on current head 79f382d1, one H200 node, TP4, memory_limit=0. Each configuration passed /health, chat, and the same 64-question GSM8K subset (temperature=1.0, max_tokens=10240). All 64 requests succeeded without errors in every run. Startup is time to health, not a controlled performance benchmark.

Run:ai distributed concurrency Startup GSM8K correct
false 2 271 s 63/64
false 4 271 s 63/64
false 8 291 s 63/64
true 2 180 s 63/64
true 4 170 s 63/64
true 8 170 s 62/64

All scores exceeded 95% on this subset. The final run answered one fewer question correctly than the earlier run; sampling used temperature=1.0.

On the current code, a local Llama-3.1-8B-Instruct target with an independent EAGLE3 draft on test-only MinIO S3 started at TP1, returned HTTP 200, and produced 59 output tokens; metrics recorded 15 draft proposals and 45 accepted draft tokens. The publicly available draft was distributed as .bin, so its tensors were converted to safetensors for this test only. The unsupported target-TP2/draft-TP1 case returned the intended ValueError directly, before engine spawn. On MinIO, replacing config.json at the same S3 URI selected a new metadata cache while preserving the old cache; an anonymous public S3 cache hit also succeeded without downloading weights. On current head 79f382d1, public S3 Qwen3-1.7B at TP2 (distributed=false, concurrency=4) also passed health and Chat Completions; the request finished with the correct answer. Multi-node serving and controlled comparative startup performance remain unverified.

Scope and known limits

  • S3 safetensors are supported in the PyTorch engine. The loader passes owned=True in both modes. distributed=false streams each rank to CPU; distributed=true uses Run:ai CUDA rank sharing and preserves identical shard order across ranks.
  • The requested official 0.17.0 release is pinned. Its upstream rank-discovery deadlock occurs when only some ranks of an existing distributed process group make a non-distributed read while others wait elsewhere; it is not a concurrency=1 condition. This PR rejects the identified independent-S3-draft/target-TP>1 case before worker startup. A follow-up can move to a later official 0.17.x release after approval and compatibility tests.
  • This PR does not add DeepSeek-V4.1-Flash or its bundled DSpark implementation.

@Reinerzhou
Reinerzhou force-pushed the feat/runai-streamer-017 branch from e6b5438 to e2388a4 Compare December 4, 2026 06:41
@Reinerzhou
Reinerzhou marked this pull request as draft December 4, 2026 06:52
@Reinerzhou
Reinerzhou force-pushed the feat/runai-streamer-017 branch 4 times, most recently from c055331 to 96e0851 Compare December 4, 2026 07:51
@Reinerzhou Reinerzhou changed the title feat: stream S3 safetensors with Run:ai Model Streamer 0.17 feat: stream S3 safetensors with Run:ai Model Streamer 0.17.0 Dec 4, 2026
@Reinerzhou
Reinerzhou force-pushed the feat/runai-streamer-017 branch 8 times, most recently from 25e03a5 to ea1aeca Compare December 4, 2026 08:39
@Reinerzhou
Reinerzhou marked this pull request as ready for review December 4, 2026 08:43
os.getenv('RUNAI_STREAMER_MEMORY_LIMIT', 'default'))
with SafetensorsStreamer() as streamer:
try:
streamer.stream_files(files,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

With target TP > 1 and a non-DeepSeek EAGLE3 draft on S3, build_spec_agent() creates a proposer only on the TP leader; follower ranks skip draft loading. Run:ai 0.17.0 performs global rank-discovery collectives even with is_distributed=False, so this call waits for ranks that never participate and startup times out. A two-process CPU/Gloo check reproduced the timeout, while loading on both ranks succeeded. Reject this configuration before worker startup or arrange collective participation; documenting the upstream limitation does not prevent this existing speculative-decoding path from encountering it.

@silencelamb silencelamb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed ea1aeca with the official Run:ai 0.17.0 wheels. All 19 tests in the two new weight-loader test files passed locally. I recommend fixing the cache identity and S3 endpoint handling before merging.

Comment thread lmdeploy/utils.py Outdated
cache_root = download_dir or os.getenv('LMDEPLOY_RUNAI_CACHE_DIR') or os.path.join(tempfile.gettempdir(),
'lmdeploy_runai')
os.makedirs(cache_root, exist_ok=True)
model_dir = os.path.join(cache_root, hashlib.sha256(model_uri.encode()).hexdigest())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P1] Include the effective S3 endpoint in the metadata cache key. Two independent S3 endpoints can serve different checkpoints at the same bucket and prefix. I loaded the same URI through two local S3 endpoints with a shared cache using the official 0.17.0 wheels. The second load reused endpoint A's config.json and streamed endpoint B's tensors. This can pair the wrong configuration or tokenizer with otherwise loadable weights. Resolve one effective S3 endpoint for both readers and include it alongside the URI in the cache key.

Comment thread lmdeploy/utils.py Outdated
env['RUNAI_STREAMER_S3_ENDPOINT'] = env['AWS_ENDPOINT_URL']
if 'AWS_ENDPOINT_URL' not in env and 'RUNAI_STREAMER_S3_ENDPOINT' in env:
# Run:ai 0.17.0 pull_files uses boto3, which reads AWS_ENDPOINT_URL.
env['AWS_ENDPOINT_URL'] = env['RUNAI_STREAMER_S3_ENDPOINT']

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P2] Scope the custom endpoint to S3. Setting RUNAI_STREAMER_S3_ENDPOINT also sets AWS_ENDPOINT_URL, which botocore honors for other services. I verified that a newly constructed STS client is redirected to the S3 endpoint. This can break AssumeRole credential resolution when the custom endpoint only serves S3. Use an S3-specific endpoint such as AWS_ENDPOINT_URL_S3 for the metadata client and pass it to Ray workers where needed.

Comment thread lmdeploy/utils.py
with open(os.path.join(staging, _RUNAI_SOURCE_FILE), 'w', encoding='utf-8') as f:
json.dump({'model_uri': model_uri}, f)
os.rename(staging, model_dir)
return model_dir

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P2] Preserve the source model name for chat-template selection. get_chat_template() uses the model path for built-in name matches. I loaded identical Vicuna metadata and a tokenizer without a chat template from a named local directory and from this S3 cache. The local path selected Vicuna, while the hash-only path fell back to BaseChatTemplate and removed the user and assistant prompt delimiters. Use the original model name for matching and the metadata cache path for tokenizer loading.

Comment thread lmdeploy/utils.py Outdated

if extra_config is not None:
env['LMDEPLOY_RUNAI_DISTRIBUTED'] = str(distributed).lower()
if concurrency is not None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P2] Scope loader overrides to the engine that supplied them. concurrency and memory_limit are written to os.environ and are left in place when a later engine omits them. I confirmed that applying explicit overrides and then applying an empty configuration retains both values. In a process hosting more than one Python pipeline, starting a second pipeline with defaults can therefore reuse the first pipeline's S3 tuning. Carry the resolved settings with the engine to the worker that opens the streamer.

Comment thread lmdeploy/cli/serve.py Outdated
else get_max_batch_size(args.device)
backend = args.backend
is_s3_model = is_runai_model(args.model_path)
if args.load_format == 'runai_streamer' and not is_s3_model:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P2] Validate the load format and backend before downloading. The CLI entry point resolves args.model_path with get_model() before reaching this check. I intercepted get_model() for lmdeploy serve api_server org/model --load-format runai_streamer and confirmed it is called first. With an uncached Hugging Face repo, this can download the entire checkpoint before rejecting the command. Validate the option combination against the original input before model resolution.

Comment thread lmdeploy/utils.py

def is_runai_model(model_path: str) -> bool:
"""Return whether a path is an S3 URI or its metadata cache."""
return model_path.startswith('s3://') or get_runai_model_uri(model_path) is not None

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P3] Preserve local pathlib.Path compatibility. autoget_backend_config(Path(...)) selects the backend at the base revision and raises AttributeError at this line on the PR head. Normalize path-like inputs before applying the S3 prefix check.

Comment thread lmdeploy/messages.py Outdated
eager_mode: bool = False
custom_module_map: dict[str, str] = None
download_dir: str = None
model_loader_extra_config: dict[str, Any] | None = None

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P3] Preserve the public config's positional argument order. Inserting this field before revision shifts the existing constructor arguments. I verified that an argument passed in the old revision slot is stored in model_loader_extra_config and leaves revision=None. Make the new field keyword-only or append it after the existing fields.

@Reinerzhou
Reinerzhou force-pushed the feat/runai-streamer-017 branch from ea1aeca to bd3429b Compare December 4, 2026 10:34
@Reinerzhou
Reinerzhou force-pushed the feat/runai-streamer-017 branch from bd3429b to 0b7d1f7 Compare December 4, 2026 11:51

This branch has not been deployed

No deployments
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.

3 participants