Skip to content

[Fix] minicpmv: Preserve chaining on delegated methods - #41391

Closed
losewayy wants to merge 2 commits into
sgl-project:mainfrom
losewayy:fix/minicpmv-getattr-chain
Closed

losewayy wants to merge 2 commits into
sgl-project:mainfrom
losewayy:fix/minicpmv-getattr-chain

Conversation

@losewayy

@losewayy losewayy commented Sep 27, 2026 •

Copy link
Copy Markdown

Motivation

MiniCPMV delegates every undefined attribute to the inner self.minicpmv module, so model.eval()/train()/to()/cuda() returned the inner module instead of the wrapper — breaking method chaining. Concretely, loader.py has ~16 sites returning model.eval(); for MiniCPM-V 2.6/4.0/4.5 the returned object is the inner MiniCPMBaseModel, which lacks the load_weights defined on the wrapper — so the RL weight-update path (weight_updater.py → get_model().load_weights(...)) receives a module without that method.

Continues the work from cjhgo's #7281, rebased and extended onto current master.

Modifications

  • __getattr__("minicpmv") now raises AttributeError instead of returning None — the old code made hasattr(obj, "minicpmv") wrongly return True before __init__ assigned it (and returning None would mask infinite recursion if the guard were ever reordered).
  • Delegated callables get wrapped: if the call result is self.minicpmv, it is rewritten to self. The guard not isinstance(attr, nn.Module) keeps submodule attributes like model.llm/model.vpm passing through as real modules — nn.Module is callable, so without the guard they would be wrapped into closures. functools.wraps keeps __name__ and inspect.signature intact on wrapped methods.
  • The one-off eval() override in MiniCPMV4_5 is removed — it was a semantic no-op (nn.Module.eval() already returns self), and the general wrapper now covers chaining for all versions including MiniCPMV4_6ForConditionalGeneration.

Testing

Extracted the real __getattr__/__call__ source from the modified file via ast (the module can't be imported without the full sglang runtime) and grafted it onto a stub wrapping a real torch.nn.Module, 27 checks covering: eval()/train()/to()/ float()/half()/cuda() all return the wrapper and mutate the inner module; train(mode) propagates the flag; eval().train().eval() chains; load_state_dict returns _IncompatibleKeys untouched; submodule attributes (model.llm) stay real modules with working .weight; non-callable attributes pass through by identity; hasattr(w, "minicpmv") is False before init and True after; __call__ forwards to the inner module.

Known pre-existing quirks (unchanged by this PR): setattr on the wrapper writes to the wrapper's __dict__ rather than delegating to the inner module, and Python <3.11 pickle edge cases — both behave identically on master.

First-time contributor note: CI needs a maintainer to apply the run-ci label.


CI States

Latest PR Test (Base): ❌ Run #39474630317
Latest PR Test (Extra): ❌ Run #39474630519
Latest PR Test (AMD ROCm 10): ❌ Run #39474630268

@losewayy

Copy link
Copy Markdown
Author

Went over this again while it sits at the gate, and checked current main too. The same delegation pattern is in mistral.py, so I'm flagging it here instead of adding it to this PR.

  • Mistral3ForConditionalGeneration (python/sglang/srt/models/mistral.py:91, inner module set on line 102) delegates the same way on line 151 and also doesn't rewrite the result. So its eval()/train()/to() return the inner LlavaForConditionalGeneration and lose the wrapper's load_weights, which is the one that normalizes the v5 names. Happy to send a separate PR for that too.
  • Line 154's hasattr is dead code. Python doesn't have that protocol, hasattr goes through getattr.

None of the test jobs here have run: only lint, check-changes and the label/pr-body helpers got through, everything else is blocked by pr-gate (Missing required label 'run-ci'), so nothing has been exercised by CI yet. Rebased onto current master, and it continues cjhgo's #7281, which got two approvals back in 2025 and then went idle after a lint request; that one is superseded by this.

@mickqian could you add the run-ci label when you get a chance? It's +20/-7 in one file and lint is green.

@losewayy

losewayy commented Oct 6, 2026 •

Copy link
Copy Markdown
Author

@Fridge003, could you review this MiniCPMV fix when you have a chance, or advise me on the right next step? The required test jobs are still blocked because the run-ci label is missing, so there are no CI test results yet. Thanks.

MiniCPMV delegates every undefined attribute to the inner self.minicpmv
module, so methods like eval()/train()/to()/cuda() returned the inner
module instead of the wrapper, breaking method chaining. Wrap delegated
callables so a result identical to self.minicpmv is rewritten to self,
and raise AttributeError for the 'minicpmv' name instead of returning
None so hasattr()/getattr() behave correctly before __init__ assigns it.

Drop the redundant eval() override on MiniCPMV4_5: nn.Module.eval()
already returns self, and the wrapper now rewrites it anyway.

Continues the work from cjhgo's sgl-project#7281, rebased and extended to current
master.
@losewayy
losewayy force-pushed the fix/minicpmv-getattr-chain branch from c385b4e to 54a9c93 Compare October 20, 2026 18:25
@github-actions

This comment has been minimized.

@losewayy losewayy changed the title Fix MiniCPMV.__getattr__ to preserve chaining on delegated methods [Fix] minicpmv: Preserve chaining on delegated methods Oct 20, 2026
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.

1 participant