Commit 7cd7d18
[minor] Refactor TE fused-norm handling in GPTModelExporter (#1061)
### What does this PR do?
Type of change: Refactor (no behavior change)
Extract a `_get_fused_norm_weight` helper method in `GPTModelExporter`
to consolidate the repeated
TE fused-norm detection logic that previously appeared as three separate
multi-condition `elif` blocks
in `_get_transformer_layer_state_dict` (attention and MLP paths) and
`_get_mamba_layer_state_dict`.
Changes:
- Add `_get_fused_norm_weight(module)` that checks `"fused_norm" in
self.rules` and returns `getattr(module, "layer_norm_weight", None)`
- Replace double `hasattr` chains with `getattr(..., None)` chaining —
`getattr` already returns `None` for missing attributes
- Remove redundant `isinstance(layer.norm, IdentityOp)` in the Mamba
`elif` (guaranteed by being an `elif` branch)
- Use walrus operator (`:=`) to capture `norm_weight` without repeating
the attribute traversal on the call line
### Before / After
Before (one of three nearly-identical blocks):
```python
elif (
hasattr(layer.self_attention, "linear_qkv")
and hasattr(layer.self_attention.linear_qkv, "layer_norm_weight")
and layer.self_attention.linear_qkv.layer_norm_weight is not None
and "fused_norm" in self.rules
):
self.rules["fused_norm"](layer.self_attention.linear_qkv.layer_norm_weight, layer_id)
```
After:
```python
elif (
norm_weight := self._get_fused_norm_weight(
getattr(layer.self_attention, "linear_qkv", None)
)
) is not None:
self.rules["fused_norm"](norm_weight, layer_id)
```
### Testing
No behavior change — existing tests cover all paths.
- Is this change backward compatible?: ✅ Pure refactor, no API or logic
change
- Did you write any new necessary tests?: N/A
- Did you update Changelog?: N/A (minor refactor)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Refactor**
* Centralized and simplified fused-normalization handling in model
export, reducing duplicated checks and streamlining control flow while
preserving existing behavior and compatibility. Improved maintainability
and consistency across export paths.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Signed-off-by: James Shen <yueshen@nvidia.com>
Signed-off-by: Yue Shen <yueshen@nvidia.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent de55e8a commit 7cd7d18
1 file changed
Lines changed: 20 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
419 | 419 | | |
420 | 420 | | |
421 | 421 | | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
422 | 432 | | |
423 | 433 | | |
424 | 434 | | |
425 | 435 | | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
432 | 441 | | |
433 | 442 | | |
434 | 443 | | |
| |||
470 | 479 | | |
471 | 480 | | |
472 | 481 | | |
473 | | - | |
474 | | - | |
475 | | - | |
476 | | - | |
| 482 | + | |
| 483 | + | |
477 | 484 | | |
478 | | - | |
| 485 | + | |
479 | 486 | | |
480 | 487 | | |
481 | 488 | | |
| |||
555 | 562 | | |
556 | 563 | | |
557 | 564 | | |
558 | | - | |
559 | | - | |
560 | | - | |
561 | | - | |
562 | | - | |
563 | | - | |
| 565 | + | |
564 | 566 | | |
565 | | - | |
| 567 | + | |
566 | 568 | | |
567 | 569 | | |
568 | 570 | | |
| |||
0 commit comments