|
| 1 | +# Copyright 2026 DeepMind Technologies Limited. All Rights Reserved. |
| 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 | +# http://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 | + |
| 16 | +"""The Linear Softmax Cross-Entropy Loss PyTorch Op API.""" |
| 17 | + |
| 18 | +import jax |
| 19 | +from tokamax._src.ops.linear_softmax_cross_entropy_loss import pallas_mosaic_tpu_kernel as tokamax_kernel |
| 20 | +import torch |
| 21 | +import torch_tpu._internal.pallas.pallas |
| 22 | + |
| 23 | + |
| 24 | +def linear_softmax_cross_entropy_loss_jax( |
| 25 | + x: jax.Array, |
| 26 | + labels: jax.Array, |
| 27 | + weights: jax.Array, |
| 28 | + b_block_size: int, |
| 29 | + h_block_size: int, |
| 30 | + v_block_size: int, |
| 31 | + reduction: str, |
| 32 | + preferred_element_type: str, |
| 33 | +) -> tuple[jax.Array, jax.Array]: |
| 34 | + """Wrapper for tokamax kernel with type hints for torch_tpu's jax_op.""" |
| 35 | + dtype = ( |
| 36 | + jax.numpy.float32 |
| 37 | + if preferred_element_type == "float32" |
| 38 | + else jax.numpy.bfloat16 |
| 39 | + ) |
| 40 | + return tokamax_kernel.linear_softmax_cross_entropy_loss_fwd_pallas_mosaic_tpu( |
| 41 | + x, |
| 42 | + labels, |
| 43 | + weights, |
| 44 | + b_block_size=b_block_size, |
| 45 | + h_block_size=h_block_size, |
| 46 | + v_block_size=v_block_size, |
| 47 | + reduction=reduction, |
| 48 | + preferred_element_type=dtype, |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def linear_softmax_cross_entropy_loss_backward_jax( |
| 53 | + dout: jax.Array, |
| 54 | + lse: jax.Array, |
| 55 | + x: jax.Array, |
| 56 | + labels: jax.Array, |
| 57 | + weights: jax.Array, |
| 58 | + b_block_size: int, |
| 59 | + h_block_size: int, |
| 60 | + v_block_size: int, |
| 61 | + reduction: str, |
| 62 | + preferred_element_type: str, |
| 63 | +) -> tuple[jax.Array, jax.Array]: |
| 64 | + """Wrapper for tokamax kernel with type hints for torch_tpu...jaxop.""" |
| 65 | + dtype = ( |
| 66 | + jax.numpy.float32 |
| 67 | + if preferred_element_type == "float32" |
| 68 | + else jax.numpy.bfloat16 |
| 69 | + ) |
| 70 | + return tokamax_kernel.linear_softmax_cross_entropy_loss_bwd_pallas_mosaic_tpu( |
| 71 | + dout, |
| 72 | + lse, |
| 73 | + x, |
| 74 | + labels, |
| 75 | + weights, |
| 76 | + b_block_size=b_block_size, |
| 77 | + h_block_size=h_block_size, |
| 78 | + v_block_size=v_block_size, |
| 79 | + reduction=reduction, |
| 80 | + preferred_element_type=dtype, |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +# pylint: disable=protected-access |
| 85 | +linear_softmax_cross_entropy_loss: torch._library.custom_ops.CustomOpDef = ( |
| 86 | + torch_tpu._internal.pallas.pallas.jax_op( |
| 87 | + "tokamax::linear_softmax_cross_entropy_loss", |
| 88 | + linear_softmax_cross_entropy_loss_jax, |
| 89 | + ) |
| 90 | +) |
| 91 | + |
| 92 | +# pylint: disable=protected-access |
| 93 | +linear_softmax_cross_entropy_loss_backward: ( |
| 94 | + torch._library.custom_ops.CustomOpDef |
| 95 | +) = torch_tpu._internal.pallas.pallas.jax_op( |
| 96 | + "tokamax::linear_softmax_cross_entropy_loss_backward", |
| 97 | + linear_softmax_cross_entropy_loss_backward_jax, |
| 98 | +) |
| 99 | + |
| 100 | + |
| 101 | +def setup_context(ctx, inputs, output): |
| 102 | + """Callback for torch register_autograd.""" |
| 103 | + ( |
| 104 | + x, |
| 105 | + labels, |
| 106 | + weights, |
| 107 | + b_block_size, |
| 108 | + h_block_size, |
| 109 | + v_block_size, |
| 110 | + reduction, |
| 111 | + preferred_element_type, |
| 112 | + ) = inputs |
| 113 | + loss, lse = output |
| 114 | + del loss # Unused |
| 115 | + ctx.save_for_backward(x, labels, weights, lse) |
| 116 | + ctx.b_block_size = b_block_size |
| 117 | + ctx.h_block_size = h_block_size |
| 118 | + ctx.v_block_size = v_block_size |
| 119 | + ctx.reduction = reduction |
| 120 | + ctx.preferred_element_type = preferred_element_type |
| 121 | + |
| 122 | + |
| 123 | +def backward(ctx, d_loss, d_lse): |
| 124 | + """Callback for torch register_autograd.""" |
| 125 | + del d_lse # Unused |
| 126 | + x, labels, weights, lse = ctx.saved_tensors |
| 127 | + grad_x, grad_w = linear_softmax_cross_entropy_loss_backward( |
| 128 | + d_loss, |
| 129 | + lse, |
| 130 | + x, |
| 131 | + labels, |
| 132 | + weights, |
| 133 | + ctx.b_block_size, |
| 134 | + ctx.h_block_size, |
| 135 | + ctx.v_block_size, |
| 136 | + ctx.reduction, |
| 137 | + ctx.preferred_element_type, |
| 138 | + ) |
| 139 | + return grad_x, None, grad_w, None, None, None, None, None |
| 140 | + |
| 141 | + |
| 142 | +linear_softmax_cross_entropy_loss.register_autograd( |
| 143 | + backward, setup_context=setup_context |
| 144 | +) |
0 commit comments