Skip to content

Fix per-layer SwiGLU clipping in BailingHybrid - #50574

Open
icavan wants to merge 10 commits into
huggingface:mainfrom
icavan:icavan/ling-swiglu-clipping
Open

icavan wants to merge 10 commits into
huggingface:mainfrom
icavan:icavan/ling-swiglu-clipping

Conversation

@icavan

@icavan icavan commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

🤗 Transformers CI

What does this PR do?

BailingHybrid currently ignores the per-layer SwiGLU limits in the published Ling checkpoint configs. This applies the routed and shared expert limits independently: the gate is clipped after SiLU, and the up projection is bounded symmetrically. Missing or zero limits retain the unclipped behavior.

Ling reuses the Step3p7 expert and shared-MLP forwards. Each module selects its limit from the config using its layer index. FP8 replacement uses the same config rather than copying clipping attributes from the original module. Ling's published config keys and zero values are preserved; limit_after_act is exposed as a fixed config-class property, without a new serialized field. Existing pre-activation and SwiGLU-OAI formulas retain their behavior. SonicMoE rejects routed clipping at its backend entry point because its fused gate does not implement this formula.

Split from #49737 following review. This PR does not include the Ling 3.1 MLA or MTP changes.

Validation

  • Kept one new SM90+ FP8 test in the existing integration class, using MemoryCleanupMixin and a two-expert dummy model. It saves and loads a complete tiny hybrid model through the FP8 quantizer, executes model(input_ids), and compares fixed expected logits. The test passed on GPU without skipping.
  • Separate development checks confirmed that wrong clipping order, missing routed/shared limits and incorrect layer selection are caught by that output assertion.
  • Tiny FP8 model comparisons against the previous PR head (3ecbf80f1) cover direct and low-rank Q projections, FP8 shared projections, cached/cacheless logits, generated tokens, module dtypes and native-format save/reload. Outputs match exactly. Expert gate compilation with fullgraph=True also matches eager execution.
  • Published Ling 3.0/3.1 configs retain their serialized fields and values, and meta-model parameter shapes/dtypes are unchanged. Full CPU regression across the five relevant model/kernel/quantization modules passed: 356 tests and 2029 subtests, with 355 conditional skips.
  • Step3p7 and Ling expert forward/gradient comparisons passed for eager, batched and grouped dispatch on CPU and CUDA.
  • Modular consistency, config attributes, imports/init, Ruff and diff checks passed. Existing model test bodies are unchanged.

AI assistance was used for implementation, review and testing.

@Rocketknight1

Copy link
Copy Markdown
Member

cc @Cyrilvallez since you were the reviewer on #49737 !

@Cyrilvallez

Copy link
Copy Markdown
Member

cc @vasqu @IlyasMoutawwakil do you think we want to support such a feature for fp8?

@vasqu vasqu 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.

Some quick comments

@vasqu vasqu 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.

Looking better thanks, some smaller details mostly let's get the attr within their inits not directly under the class

Comment thread tests/kernels/test_finegrained_fp8.py Resolved
@icavan
icavan marked this pull request as draft November 19, 2026 03:41
@icavan
icavan requested a review from vasqu November 19, 2026 07:37
@icavan
icavan marked this pull request as ready for review November 19, 2026 08:04
@icavan

icavan commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Hi @vasqu, would you mind taking another look at this PR when you have a chance? Thanks again for the detailed feedback and your time!

@vasqu vasqu 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.

Sorry to backtrack a bit but the forward definitely gets more ugly. This is mostly about style not a functional issue overall re my comments here

Comment on lines +849 to +850
# Declare the clipping order for replacement expert implementations.
self.limit_after_act = True

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.

lets add it as config value no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed that the clipping order should be discoverable from the config. Ling and Step3p7 now expose limit_after_act as a read-only property on their config classes, and BF16/FP8 initialize from it. Post-activation clipping is fixed by these architectures, and the released checkpoint configs do not declare a separate flag. This keeps it available through the config without adding a serialized field or making the order independently configurable. Would that work for these released checkpoints? For future checkpoint formats, I'd prefer to make this explicit in the published config from the outset. Leaving this thread open for your thoughts.

@icavan

icavan commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Sorry to backtrack a bit but the forward definitely gets more ugly. This is mostly about style not a functional issue overall re my comments here

@vasqu Thanks for taking another look. Agreed that the clipping order should be discoverable from the config. For the existing Ling and Step3p7 checkpoints, post-activation clipping is part of the architecture, and their configs don't declare a separate flag.

Would you be okay with exposing limit_after_act as a fixed, read-only property on their config classes for these existing models? Both BF16 and FP8 could read it through the same config interface without introducing a new serialized field.

For future Ling checkpoint releases, I plan to make this metadata explicit in config.json, following the standardized naming we agree on here.

@icavan
icavan marked this pull request as draft November 24, 2026 04:17
@icavan

icavan commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks again for the detailed review, @vasqu. I've pushed a cleanup that restores constructor-only infinity normalization, moves per-layer limit selection into the child modules, and reuses the Step3p7 shared-MLP forward. FP8 now reads the settings from the config, and only the complete tiny-model FP8 output test remains among the added tests.

The GPU integration test passed, and I checked separately that losing the clipping order, either threshold, or the layer index makes its output assertion fail. I've updated the description and resolved the addressed threads, leaving the two config-property discussions open for your thoughts.

When you have a chance, could you please take another look? Thanks for helping simplify this.

@icavan
icavan marked this pull request as ready for review November 24, 2026 06:11

@vasqu vasqu 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.

Last comments looks good now

self.limit = (config.swiglu_limits_shared[layer_idx] if config.swiglu_limits_shared else 0) or float("inf")
# Zero or a missing per-layer limit disables clipping.
self.limit = (
config.swiglu_limits_shared[layer_idx] if layer_idx is not None and config.swiglu_limits_shared else 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.

layer idx check is maybe a bit overkill no?

Comment on lines +234 to +237
@property
def limit_after_act(self) -> bool:
"""Clipping order is fixed by the architecture and is not serialized."""
return True

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.

yep im fine with a property on this, it should only be true anyways so setting it shouldnt be done in these cases anyways then



class BailingHybridExperts(DeepseekV3Experts):
@use_experts_implementation

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.

hmm maybe we just forgot but imo the step model should be able to use the decorator as well, can you check? so here we dont need the extra decorator declared

Comment on lines +745 to +754
# swiglu_limit is a scalar shared across layers; swiglu_limits is a per-layer list.
# Post-activation clipping prefers the list, falling back to the scalar if absent.
# Zero/None disables clipping in this path; other gate formulas keep their scalar semantics.
self.limit_after_act = getattr(config, "limit_after_act", False)
self.limit = None
if self.limit_after_act:
limits = getattr(config, "swiglu_limits", None)
if limits is not None and layer_idx is None:
raise ValueError("Per-layer SwiGLU limits require a layer index.")
self.limit = (limits[layer_idx] if limits is not None else self.swiglu_limit) or float("inf")

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.

id remove the comment tbh + limits could exist outside of limit after act no?

@require_torch_gpu
@require_kernels
@require_cuda_capability_at_least(9, 0)
class BailingHybridFp8IntegrationTest(unittest.TestCase):

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.

there is already BailingHybridIntegrationTest, can we fit it in there? maybe a smaller dummy model if possible?

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.

memory cleanup mixin then

@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: bailing_hybrid, step3p7

has_bias: bool = False,
has_gate: bool = True,
expert_dtype: str = "fp8",
layer_idx: int | None = None,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the fp8 experts don't need all this, just pass the right swiglu_limit from the original module and set it if given else use the one from the config

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.

5 participants