Skip to content

refactor(glm): reuse DeepseekV3 base implementation via template methods#604

Open
wtsung wants to merge 1 commit into
lightseekorg:mainfrom
wtsung:weicong_clean_up_glm
Open

refactor(glm): reuse DeepseekV3 base implementation via template methods#604
wtsung wants to merge 1 commit into
lightseekorg:mainfrom
wtsung:weicong_clean_up_glm

Conversation

@wtsung

@wtsung wtsung commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Refactor GlmMoeDsaDecoderLayer and GlmMoeDsaModel to fully leverage inheritance from their DeepseekV3 base classes.

This change introduces two template methods in the DeepseekV3 implementation so that subclasses only need to customize the components that differ, eliminating approximately 170 lines of duplicated code without changing behavior.

Changes

deepseek_v3.py

Introduce two extension points for subclasses:

  • Add DeepseekV3DecoderLayer._create_attn() to allow subclasses to customize the attention implementation.
  • Add DeepseekV3Model._create_decoder_layer() to allow subclasses to customize decoder layer construction.

glm5.py

Simplify the GLM-specific subclasses by relying on the inherited implementation wherever possible.

GlmMoeDsaDecoderLayer

  • Call super().__init__() instead of duplicating the parent initialization.
  • Override only _create_attn() to instantiate GlmMoeDsaAttention.
  • Remove duplicated implementations of forward(), forward_mlp(), and initialization logic, all of which are identical to DeepseekV3DecoderLayer.

GlmMoeDsaModel

  • Override only _create_decoder_layer() to construct GlmMoeDsaDecoderLayer.
  • Remove the duplicated __init__() implementation and inherit the base initialization directly.

Additional cleanup

  • Inline single-use static helper methods.
  • Initialize _retired_decode_workspaces in __init__().
  • Remove the unused parameter from _resolve_decode_q_len().
  • Rename _qkv_width to qkv_width.

Verification

The refactor is behavior-preserving:

  • The removed forward() and forward_mlp() implementations in GlmMoeDsaDecoderLayer are identical to those inherited from DeepseekV3DecoderLayer.
  • The removed GlmMoeDsaModel.__init__() performs the same initialization sequence as DeepseekV3Model.__init__().
  • The only functional customization retained in the GLM subclasses is the creation of the attention module and decoder layer via the newly introduced template methods.

@wtsung wtsung requested a review from a team as a code owner July 7, 2026 15:58
@wtsung wtsung force-pushed the weicong_clean_up_glm branch from 566de33 to 0b3f5bb Compare July 7, 2026 16:04
@lightseek-bot lightseek-bot requested review from syuoni and yweng0828 July 7, 2026 18:19
@wtsung wtsung force-pushed the weicong_clean_up_glm branch from 0b3f5bb to ec44a6b Compare July 8, 2026 14:19
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