torchtitan icon indicating copy to clipboard operation
torchtitan copied to clipboard

Discussion: Deprecate ModelSpec and migrate its fields

Open curiouscurrent opened this issue 4 months ago • 4 comments

Hi! I noticed the TODO about deprecating ModelSpec in torchtitan/torchtitan/protocols/model_spec.py and wanted to make sure I understand the current architecture before I start working on it. From tracing the codebase, this is my understanding of the current structure:

Current

Trainer
│
└── config (Trainer.Config)
    │
    └── model_spec (ModelSpec)
        │
        ├── name
        ├── flavor
        ├── model ───────────────────────► BaseModel.Config
        ├── parallelize_fn
        ├── pipelining_fn
        ├── post_optimizer_build_fn
        ├── state_dict_adapter
        └── traverse()
                                         │
                                         ▼
                                BaseModel.Config
                                │
                                ├── hidden_size
                                ├── num_layers
                                ├── vocab_size
                                ├── max_seq_len
                                ├── attention
                                ├── ...
                                └── traverse() (inherited)

My interpretation of the TODO

Trainer
│
└── config (Trainer.Config)
    │
    ├── model ───────────────────────► BaseModel.Config
    ├── parallelize_fn
    ├── pipelining_fn
    ├── post_optimizer_build_fn
    ├── state_dict_adapter
    ├── name
    └── flavor
                                        │
                                        ▼
                               BaseModel.Config
                               │
                               ├── hidden_size
                               ├── num_layers
                               ├── vocab_size
                               ├── max_seq_len
                               ├── attention
                               ├── ...
                               └── traverse() (inherited)

(ModelSpec wrapper removed)

My understanding is that ModelSpec currently acts as an intermediate wrapper that bundles:

  1. BaseModel.Config
  2. metadata (name, flavor)
  3. runtime callbacks (parallelize_fn, pipelining_fn, post_optimizer_build_fn, state_dict_adapter)
  4. a custom traverse() that forwards traversal into self.model.

My interpretation of the TODO is that the goal is to remove this intermediate wrapper while preserving the existing behavior. I just wanted to confirm whether my understanding is correct before I start working on it.

I also had a couple of questions regarding the intended migration:

  1. Which ModelSpec fields are expected to move into Trainer.Config, and which (if any) should move into BaseModel.Config?

  2. Should model_registry() continue returning an object after this refactor, or is its API expected to change as part of removing ModelSpec?

  3. Is ModelSpec.traverse() expected to disappear entirely once the wrapper is removed?

If my understanding matches the intended design, I'd be happy to work on the refactor.

curiouscurrent avatar Jul 20 '26 22:07 curiouscurrent

@tianyu-l

curiouscurrent avatar Jul 20 '26 22:07 curiouscurrent

@wwwjn @fegin @felipemello1

curiouscurrent avatar Jul 20 '26 23:07 curiouscurrent

Thanks for your interest. This one is a bit tricky that I might want to work on it by myself.

tianyu-l avatar Jul 21 '26 00:07 tianyu-l

Makes sense, thanks for the clarification @tianyu-l . If there are any follow-up tasks or parts of the refactor I can help with, I'd love to contribute.

curiouscurrent avatar Jul 21 '26 07:07 curiouscurrent