Skip to content

TransformerLayer implementation is overly complex and difficult to follow #2903

Done

Description

@Hukongtao

class TransformerLayer(torch.nn.Module):

I understand that you want to implement a single TransformerLayer class to support various model architectures.

However, this design leads to an issue: the code inside the TransformerLayer class has become overly complex, with too many parameters and a large number of if-else statements. For beginners, it is really difficult to figure out how to configure the parameters to fit their own model structures.

I'm curious why you didn't design it like the Hugging Face Transformers framework, where one model corresponds to one file:
https://github.com/huggingface/transformers/tree/main/src/transformers/models

This kind of design is much more user-friendly, because users only need to focus on the code they care about.

Activity

  1. added
    question
    on Jun 24, 2026
  2. pggPL commented on Dec 1, 2026

    @pggPL
    Collaborator

    Thanks for the feedback — that's a very fair point. Historically TransformerLayer grew as a single configurable layer to cover standard Transformer variants, while TE focused primarily on lower-level building blocks.

    We agree that model-specific implementations are much easier to read and configure (similar to Hugging Face transformers/models), and we're moving in that direction by introducing a transformer_engine.pytorch.models namespace with dedicated per-model layers. The first PR introducing this structure — with DeepSeekV3Layer (MultiLatentAttention + DeepSeekV3MoE) under transformer_engine/pytorch/models/deepseek_v3/ — is already open here: #3471.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions