IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Python function

moe_forward

moe_forward()​

max.nn.common_layers.moe_exchange.moe_forward(mlp, h, *args, **kwargs)

source

Runs a feed-forward block on h and returns its output placed as h.

The movement follows from how h lies on its mesh and from whether the block SumsOverDevices:

  • Rows held per replica, a per-row block: the block runs on them as they are.
  • Rows held per replica, a summing block: the rows are all-gathered, the block runs on the whole batch, and each replica keeps its own rows of the completed output.
  • Replicated rows, a per-row block: each device runs an even share of the rows, and the outputs are gathered.
  • Replicated rows, a summing block: the block runs and completes its output.

Parameters:

  • mlp (Module[..., Tensor]) – The feed-forward block, dense or MoE.
  • h (Tensor) – The normalized hidden states, (rows, hidden).
  • *args (object) – Passed to mlp after the hidden states.
  • **kwargs (object) – Passed to mlp, for example an expert-parallel MoE’s ep buffers.

Returns:

The block’s output, with the mapping and shard shapes of h.

Raises:

  • NotImplementedError – If the block sums over devices, or the rows have to move, on a mesh of more than one axis.
  • TypeError – If a block that runs per row returns partial sums.

Return type:

Tensor