Skip to content

Fix tiled MLP for ZeRO-3 and FSDP - #1000

Merged
vasqu merged 2 commits into
huggingface:mainfrom
kashif:fix-tiled-mlp-zero3-fsdp
Jun 29, 2026
Merged

vasqu merged 2 commits into
huggingface:mainfrom
kashif:fix-tiled-mlp-zero3-fsdp

Conversation

@kashif

@kashif kashif commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

The tiled MLP kernel passes compute_params through but never uses it, and computes num_shards locally on each rank. Both bite under sharded-parameter backends:

  • DeepSpeed ZeRO-3 crashes: the per-shard recompute fires ZeRO's gradient hook multiple times for the same parameter, so the same ds_id lands in the reduction bucket twice and it asserts.
  • ZeRO-3 / FSDP can deadlock: if ranks see different sequence lengths they pick different num_shards, so they issue a different number of weight all-gathers and hang.

This ports the fixes that DeepSpeed's own TiledMLP uses:

  • In the backward, defer ZeRO-3 reduction by setting param.ds_grad_is_ready = False for every shard except the last (gated on the ds_id marker, so non-ZeRO backends are untouched).
  • In apply_tiled_mlp, all-reduce num_shards to the per-rank max when distributed, so every rank runs the same number of shards.

Same fix is going into linkedin/Liger-Kernel. Verified there on 2x H100: tiled gradients match a non-tiled reference under FSDP2 and DeepSpeed ZeRO-3 (with and without gradient checkpointing). No change for single-GPU or non-distributed runs.

@github-actions

Copy link
Copy Markdown

Hi @kashif, thanks for your interest in contributing!

This project requires that pull request authors are vouched, and you are not in the list of vouched users.

This PR will be closed automatically. See https://github.com/huggingface/kernels-community/blob/main/CONTRIBUTING.md for more details.

@github-actions github-actions Bot closed this Jun 29, 2026
@vasqu vasqu reopened this Jun 29, 2026
@vasqu vasqu self-assigned this Jun 29, 2026
@vasqu

vasqu commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

/kernel-bot build liger-kernels

@github-actions

github-actions Bot commented Jun 29, 2026 •

Copy link
Copy Markdown

Build request processed.

Command: /kernel-bot build liger-kernels
Mode: build only
Target branch: pr-1000
PR head SHA: e156b7a70cf24a5e29ce92d738d5f79503bb9096
Workflows: build.yaml, build-mac.yaml, build-windows.yaml

Dispatched (1):

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🤗

@vasqu
vasqu merged commit 90dbeee into huggingface:main Jun 29, 2026
3 of 4 checks passed
@vasqu

vasqu commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

/kernel-bot merge-and-upload liger-kernels

@github-actions

github-actions Bot commented Jun 29, 2026 •

Copy link
Copy Markdown

Build request processed.

Command: /kernel-bot merge-and-upload liger-kernels
Mode: merge, build and upload
Target branch: ``
PR head SHA: e156b7a70cf24a5e29ce92d738d5f79503bb9096
Workflows: `build.yaml, build-mac.yaml, build-windows.yaml`

Merge result: PR is already merged. Continuing with build/upload.

Dispatched (1):

@kashif
kashif deleted the fix-tiled-mlp-zero3-fsdp branch June 29, 2026 18:19
@danieldk

Copy link
Copy Markdown
Member

And congrats on making PR #1000 @kashif :)

@kashif

kashif commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

thanks @danieldk happy to help!

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.

3 participants