zml/moe: Mosaic TPU MoE backend.#571
Open
loupicaaa wants to merge 8 commits into
Open
Conversation
gwenzek
reviewed
Jun 4, 2026
| const valid_mask = zml.Tensor.arange(.{ .end = x.dim(.s) }, .i64) | ||
| .withTags(.{.s}) | ||
| .cmp(.LT, token_index.convert(.i64)); | ||
| recurrent_value = valid_mask.broad(value.shape()).select(recurrent_value, zml.Tensor.zeroes(recurrent_value.shape())); |
Contributor
There was a problem hiding this comment.
you can drop the zml.Tensor here to make it a bit easier to read.
| .cmp(.LT, token_index.convert(.i64)); | ||
| recurrent_value = valid_mask.broad(value.shape()).select(recurrent_value, zml.Tensor.zeroes(recurrent_value.shape())); | ||
| recurrent_beta = valid_mask.broad(beta.shape()).select(recurrent_beta, zml.Tensor.zeroes(recurrent_beta.shape())); | ||
| g = valid_mask.broad(g.shape()).select(g, zml.Tensor.zeroes(g.shape())); |
Contributor
There was a problem hiding this comment.
This is a mouthful maybe we should have a helper function to mask along side a given axis until some upper bound.
e8553d7 to
3f279cc
Compare
43e5679 to
f9fb9e8
Compare
hugomano
reviewed
Jun 12, 2026
| experts: zml.Sharding, | ||
|
|
||
| pub fn init(platform: *zml.Platform) !Shardings { | ||
| if (platform.target == .tpu) { |
Contributor
There was a problem hiding this comment.
use switch over platform.target without else prong
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR add the TPU backend for MoE. It uses the GMM Expert parallelism kernel to perform the grouped gemms on gate_up and down weights (these weights must have a precise shape to run optimally).
This backend is currently optimized for small batches. A specialized mosaic tpu kernel will be introduced to handle bigger ones.
This implementation come with few fixes concerning the TPU sharding and the Qwen 3.5 moe model which uses the backend :
n.b. : The kv head repetition to satisfy the sharding will be handled a more generic way in a next PR