Skip to content

Add tensor parallel support for GPT-J - #48783

Closed
Siddhant231xyz wants to merge 2 commits into
huggingface:mainfrom
Siddhant231xyz:tp-gptj
Closed

Siddhant231xyz wants to merge 2 commits into
huggingface:mainfrom
Siddhant231xyz:tp-gptj

Conversation

@Siddhant231xyz

@Siddhant231xyz Siddhant231xyz commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

CPU CI GPU run-slow

What does this PR do?

  • GPTJAttention split/merge heads with -1 so column-sharded q/k/v projections work.
  • GPTJConfig.base_model_tp_plan (attn q/k/v colwise, out_proj rowwise, mlp fc_in colwise, fc_out rowwise) + lm_head plan.
  • GPTJModel._input_embed_layer = "wte" so the tied input embedding is vocab-sharded.

Part of #34789. Depends on #48779; this branch includes that commit and will be rebased once it lands.

Verification

  • tests/models/gptj: 112 passed.
  • 2-rank CPU/gloo mesh: sharded forward matches single-process forward (max abs diff 2e-7), 18/25 params sharded.

AI disclosure

I used an AI coding assistant (Claude Code) to draft this change and the verification script. I have read and understand every changed line, ran the tests listed above locally, and take responsibility for the code.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline and the Pull Request checks?
  • Was this discussed/approved via a Github issue or the forum? Maintainer invited PRs on Add Tensor Parallel support for ALL models #34789 ("just open a PR and link it to this issue").
  • Did you make sure to update the documentation with your changes according to the guidelines? No doc change needed: TP support is defined by base_model_tp_plan on the config.
  • Did you write any new necessary tests? Covered by the existing test_tp_plan_matches_params and the model test suite; 2-rank numerical check described above.

Who can review?

@ArthurZucker @Cyrilvallez

PreTrainedConfig injected a hard-coded {'embed_tokens': 'embedding_rowwise'} into base_model_tp_plan whenever tie_word_embeddings is set, which produces an invalid plan entry for models whose embedding attribute has another name (GPT-2 style 'wte', GPT-NeoX 'embed_in', ...), as noted in the TODO.

Move the rule into ParallelInterface mixin plan initialization, keyed on the model's _input_embed_layer (default 'embed_tokens'), so the config stays declarative and models with differently named embeddings get vocab-parallel tied embeddings too.

No behaviour change for existing models: tests/tensor_parallel + test_tensor_parallel_mixin pass (16), tp_plan tests for llama/qwen3_moe pass, and a tied-embedding Llama on a 2-rank CPU mesh still shards 15/20 params with identical logits.

Related: huggingface#34789
- Make GPTJAttention head-count agnostic (split/merge heads with -1) so column-sharded q/k/v projections work.
- Add base_model_tp_plan to GPTJConfig (attn q/k/v colwise, out_proj rowwise, mlp fc_in colwise, fc_out rowwise) and a colwise_gather_output plan for lm_head.
- Declare _input_embed_layer = 'wte' on GPTJModel so the tied input embedding is vocab-sharded.

Verified on a 2-rank CPU/gloo mesh: sharded forward matches single-process forward (max abs diff 2e-7), 18/25 params sharded. tests/models/gptj: 112 passed.

Related: huggingface#34789
@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: gptj

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 34790946183:2
Result: success | Jobs: 16 | Tests: 189,179 | Failures: 0 | Duration: 15h 37m

@ArthurZucker

Copy link
Copy Markdown
Collaborator

Thanks for this! Main has moved under you: embedding sharding for TP was reworked in #49570 (distributed/mixin.py now adds embedding_rowwise for the input embeddings by default), so the configuration_utils.py / mixin.py changes here conflict and are mostly no longer needed. The GPT-J base_model_tp_plan itself is still missing on main, though. Closing this one for now; feel free to open a fresh PR against main if it's still needed. Thanks!

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.

2 participants