Skip to content

config: the lazy imports that buy nothing become eager - #36975

Merged
ch-wan merged 1 commit into
mainfrom
cheng/gc-r4e-eager-imports
Aug 29, 2026
Merged

config: the lazy imports that buy nothing become eager#36975
ch-wan merged 1 commit into
mainfrom
cheng/gc-r4e-eager-imports

Conversation

@ch-wan

@ch-wan ch-wan commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

The series

Stacked, each PR based on the one above it. Read them in order; every boundary is
self-sufficient and green on its own.

  1. #36896 — the resolution pipeline's dispatcher leaves the record
  2. #36972 — the callbacks into the record go to zero
  3. #36973 — six more runtime readers ask the bags
  4. #36974 — the dead record parameters go
  5. #36975 — the lazy imports that buy nothing become eager ← you are here
  • #36925 — CI vehicle — runs the whole series against main, not for merge

Motivation

arg_groups.overrides is already imported at module scope by thirty-five files, server_args
among them. Importing it is neither a cycle nor a cost — by the time any resolution step runs, the
module is loaded. Ninety-three from ...overrides import statements were nonetheless written
inside the functions that use them, which reads as though laziness were required when mostly it
was habit.

Eight of the twenty files this touches had the lazy form before this series, so it is not only a
cleanup after it.

Modifications

The statements that buy nothing become module-scope imports.

Twenty-three more were mixed: a name the tests patch, plus names riding along on the same
statement that nothing patches. Splitting those frees thirty-six more riders. The _dsa_* pass
callables in model_hook are the shape of it —

from sglang.srt.arg_groups.overrides import (
    _dsa_kv_cache_dtype_default,      # only ever an argument
    _dsa_split_backend_resolution,    # only ever an argument
    run_post_process_pass,            # patched through the module
)

— three names, one reason. The two passes are handed to run_post_process_pass and never looked
up through the module, so they were lazy only by association. The seam stays a call-site import;
they go to the top.

model_hook and moe_hook are the exception: their import block also carries the two properties
PR 2 lifted, so the two changes cannot be separated by hunk without an intermediate commit that
does not compile. Their split lands in PR 2 with the move it is entangled with, and the counts
below are for the whole series.

What is left is load-bearing, for exactly two reasons and nothing else. They are worth writing
down because they decide where the next one goes.

The module is a test seam. use_mla_backend, attention_backends_of, model_config_of and
supports_mamba_cache_extra_buffer are patched as patch("...overrides.<name>"). The four
declaration paths — declare_resolution, declare_late_resolution, declare_direct_writes,
run_post_process_pass — are wrapped by setattr on the module in
test_no_entry_changes_after_it_is_recorded, which watches every declaration a resolution
makes.

A from ... import name is a copy. Hoisting these turns one seam into six or twenty: a test
that patches the source module reaches none of the copies, and a test that patches one consumer
reaches only that one. Hoisting the declarers is what this commit tried first, and the census
guard failed exactly as it was designed to — it recorded nothing, because the wrapper it installed
on the module was no longer what the callers called.

Import order. runtime_context is imported by utils.common, which overrides needs, so it
sits below overrides and cannot import it at module scope. That one is a cycle, not a
preference; it was found by the import smoke check, not by reasoning.

That leaves a rule a reader can check:

function-local overrides import count
the module is a patched or watched seam 54
import order (runtime_context) 8
neither 0

Accuracy Tests

The 62-shape resolution probe is byte-identical to the base commit. A dummy resolution is
unchanged at ~9 ms and loads the same six arg_groups modules — which is the point: the laziness
these statements were written for was already provided by the families' own import placement, not
by the overrides import.

605 registered config tests pass, test_no_entry_changes_after_it_is_recorded among them — it is
the guard that fails the moment a declarer is hoisted.

Two properties worth stating because a hoist can break them silently: every newly module-bound name
is actually used in its file (no unused hoist), and no function still imports locally a name the
same file now binds at module scope (no UnboundLocal from late shadowing). Both hold by AST over
the commit. The newly eager modules are also off the overridesutils.common
runtime_context path, which is the cycle the keep-list's second reason protects.

Speed Tests and Profiling

None beyond the dummy-resolution timing above.

Checklist

Review and Merge Process

Mechanical apart from the keep-list, and the table above is the whole of it: eight names stay lazy
because the module is where tests patch or watch them, runtime_context stays lazy because of
import order, and nothing else stays lazy at all. A reviewer can check the claim by re-running the
same scan.


CI States

Latest PR Test (Base): 🚫 Run #33249957988
Latest PR Test (Extra): 🚫 Run #33249957857
Latest PR Test (AMD ROCm 7.2): 🚫 Run #33249958152

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 29, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-08-29T06:39:01.939566Z 24e78d7 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@ch-wan
ch-wan force-pushed the cheng/gc-r4e-eager-imports branch from 24e78d7 to 81c7523 Compare August 29, 2026 07:13
@github-actions github-actions Bot added documentation Improvements or additions to documentation quant LLM Quantization lora Multi-modal multi-modal language model hicache Hierarchical Caching for SGLang npu diffusion SGLang Diffusion unified-radix-cache labels Aug 29, 2026
@ch-wan
ch-wan force-pushed the cheng/gc-r4d-dead-params branch 2 times, most recently from 5a127e6 to 22962c3 Compare August 29, 2026 07:37
@ch-wan
ch-wan force-pushed the cheng/gc-r4e-eager-imports branch from 81c7523 to 5284d7f Compare August 29, 2026 07:37
@ch-wan
ch-wan force-pushed the cheng/gc-r4d-dead-params branch from 22962c3 to 64840a1 Compare August 29, 2026 08:11
@ch-wan
ch-wan force-pushed the cheng/gc-r4e-eager-imports branch from 5284d7f to ceb011e Compare August 29, 2026 08:11
@ch-wan
ch-wan force-pushed the cheng/gc-r4d-dead-params branch from 64840a1 to 639f853 Compare August 29, 2026 08:32
@ch-wan
ch-wan force-pushed the cheng/gc-r4e-eager-imports branch from ceb011e to a04c86f Compare August 29, 2026 08:32
`arg_groups.overrides` is imported at module scope by thirty-five files
already, `server_args` among them, so importing it is not a cycle and not a
cost -- by the time any resolution step runs, the module is loaded. Ninety-three
`from ...overrides import` statements were nonetheless written inside the
functions that use them.

The ones that buy nothing become module-scope imports, including the twenty-three
that were **mixed**: a name a test patches, plus names riding along on the same
statement that nothing patches. Splitting those frees the riders -- the `_dsa_*`
pass callables are the shape of it, only ever handed to `run_post_process_pass`
as arguments, never looked up through the module.

Three groups were kept lazy for a test seam, which is the wrong way round, and
each is fixed at the test rather than in the code the runtime executes:

- The four declaration paths. `test_no_entry_changes_after_it_is_recorded`
  wrapped `declare_resolution`, `declare_late_resolution`,
  `declare_direct_writes` and `run_post_process_pass` by `setattr` on the
  module, purely to snapshot the stash after each append. The property it
  checks belongs to the stash, so the seam is now the stash: a list that
  snapshots on append, installed by a test-local `ServerArgs` subclass that
  survives the pipeline's own reset of it. The guard is strictly stronger --
  it records at the append rather than after the call -- and still fails on an
  injected in-place edit.
- `use_mla_backend`. Eight cases patched the answer; it is
  `model_config_of(server_args).attention_arch == AttentionArch.MLA`, so the
  fixtures now state the model instead. Five of the patches were redundant
  outright -- their stand-in already answered "not MLA" -- and the rest set
  `_model_config.attention_arch`, which is the input the value is derived from.
- `model_config_of` had no patch site at all and was lazy on a hunch.

What is left is load-bearing, for exactly two reasons and nothing else:
`attention_backends_of` (3 patch sites) and `supports_mamba_cache_extra_buffer`
(2) are still patched through the module, and `runtime_context` is imported by
`utils.common`, which `overrides` needs, so it sits *below* `overrides` and
cannot import it at module scope -- a cycle, not a preference.

A function-local `overrides` import that is neither of those: zero.

A dummy resolution is unchanged at ~9 ms and loads the same six `arg_groups`
modules; the 62-shape resolution probe is byte-identical to the base commit.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@PaddyXj PaddyXj mentioned this pull request Aug 31, 2026
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deepseek diffusion SGLang Diffusion documentation Improvements or additions to documentation hicache Hierarchical Caching for SGLang jit-kernel lora Multi-modal multi-modal language model npu quant LLM Quantization speculative-decoding unified-radix-cache

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant