|
| 1 | +# Copyright 2023–2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Token sorting for MoE layers.""" |
| 16 | + |
| 17 | +import functools |
| 18 | + |
| 19 | +import jax |
| 20 | +import jax.numpy as jnp |
| 21 | + |
| 22 | + |
| 23 | +@functools.partial(jax.custom_vjp, nondiff_argnums=(2,)) |
| 24 | +def route( |
| 25 | + tokens: jax.Array, |
| 26 | + selected_experts: jax.Array, |
| 27 | + use_custom_mosaic_kernel: bool, |
| 28 | +) -> jax.Array: |
| 29 | + """Route tokens to selected experts.""" |
| 30 | + return _route_fwd(tokens, selected_experts, use_custom_mosaic_kernel)[0] |
| 31 | + |
| 32 | + |
| 33 | +def _route_fwd( |
| 34 | + tokens: jax.Array, |
| 35 | + selected_experts: jax.Array, |
| 36 | + use_custom_mosaic_kernel: bool, |
| 37 | +) -> tuple[jax.Array, jax.Array]: |
| 38 | + return ( |
| 39 | + _route_impl(tokens, selected_experts, use_custom_mosaic_kernel), |
| 40 | + selected_experts, |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def _route_bwd( |
| 45 | + use_custom_mosaic_kernel: bool, |
| 46 | + residuals: jax.Array, |
| 47 | + grads: jax.Array, |
| 48 | +) -> tuple[jax.Array, None]: |
| 49 | + selected_experts = residuals |
| 50 | + return _unroute_impl(grads, selected_experts, use_custom_mosaic_kernel), None |
| 51 | + |
| 52 | + |
| 53 | +route.defvjp(_route_fwd, _route_bwd) |
| 54 | + |
| 55 | + |
| 56 | +@functools.partial(jax.custom_vjp, nondiff_argnums=(2,)) |
| 57 | +def unroute( |
| 58 | + tokens: jax.Array, |
| 59 | + selected_experts: jax.Array, |
| 60 | + use_custom_mosaic_kernel: bool, |
| 61 | +) -> jax.Array: |
| 62 | + return _unroute_fwd(tokens, selected_experts, use_custom_mosaic_kernel)[0] |
| 63 | + |
| 64 | + |
| 65 | +def _unroute_fwd( |
| 66 | + tokens: jax.Array, |
| 67 | + selected_experts: jax.Array, |
| 68 | + use_custom_mosaic_kernel: bool, |
| 69 | +) -> tuple[jax.Array, jax.Array]: |
| 70 | + return ( |
| 71 | + _unroute_impl(tokens, selected_experts, use_custom_mosaic_kernel), |
| 72 | + selected_experts, |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +def _unroute_bwd( |
| 77 | + use_custom_mosaic_kernel: bool, residuals: jax.Array, grads: jax.Array |
| 78 | +) -> tuple[jax.Array, None]: |
| 79 | + selected_experts = residuals |
| 80 | + return _route_impl(grads, selected_experts, use_custom_mosaic_kernel), None |
| 81 | + |
| 82 | + |
| 83 | +unroute.defvjp(_unroute_fwd, _unroute_bwd) |
| 84 | + |
| 85 | + |
| 86 | +def _route_impl( |
| 87 | + tokens: jax.Array, |
| 88 | + selected_experts: jax.Array, |
| 89 | + use_custom_mosaic_kernel: bool, |
| 90 | +) -> jax.Array: |
| 91 | + """Gather `tokens` according to `selected_experts`.""" |
| 92 | + assert ( |
| 93 | + tokens.shape[0] == selected_experts.shape[0] |
| 94 | + and selected_experts.ndim == 2 |
| 95 | + ), f"{tokens.shape=}, {selected_experts.shape=}" |
| 96 | + if use_custom_mosaic_kernel: |
| 97 | + raise NotImplementedError("Custom Mosaic kernel not implemented.") |
| 98 | + inds = jnp.argsort(jnp.ravel(selected_experts)) // selected_experts.shape[1] |
| 99 | + return _sort_impl(tokens, inds, use_custom_mosaic_kernel) |
| 100 | + |
| 101 | + |
| 102 | +def _unroute_impl( |
| 103 | + tokens: jax.Array, |
| 104 | + selected_experts: jax.Array, |
| 105 | + use_custom_mosaic_kernel: bool, |
| 106 | +) -> jax.Array: |
| 107 | + assert ( |
| 108 | + tokens.shape[0] == selected_experts.shape[0] * selected_experts.shape[1] |
| 109 | + and selected_experts.ndim == 2 |
| 110 | + ) |
| 111 | + inds = jnp.argsort(jnp.argsort(jnp.ravel(selected_experts))) |
| 112 | + return jnp.sum( |
| 113 | + jnp.reshape( |
| 114 | + _sort_impl(tokens, inds, use_custom_mosaic_kernel), |
| 115 | + (-1, selected_experts.shape[1]) + tokens.shape[1:], |
| 116 | + ), |
| 117 | + axis=1, |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +def _sort_impl( |
| 122 | + tokens: jax.Array, inds: jax.Array, use_custom_mosaic_kernel: bool |
| 123 | +) -> jax.Array: |
| 124 | + if use_custom_mosaic_kernel: |
| 125 | + raise NotImplementedError("Custom Mosaic kernel not implemented.") |
| 126 | + else: |
| 127 | + return tokens[inds, ...] |
0 commit comments