|
| 1 | +import functools |
| 2 | + |
| 3 | +import ninetoothed |
| 4 | +import ninetoothed.language as ntl |
| 5 | +import torch |
| 6 | +from ninetoothed import Tensor |
| 7 | + |
| 8 | + |
| 9 | +def arrangement(input, output, dim): |
| 10 | + assert input.ndim == output.ndim |
| 11 | + |
| 12 | + def create_axis_tile_shape(dim, dim_block): |
| 13 | + return tuple(1 for _ in range(dim)) + (dim_block,) + tuple(1 for _ in range(input.ndim - dim - 1)) |
| 14 | + |
| 15 | + inner_block_shape = create_axis_tile_shape(dim, input.shape[dim]) |
| 16 | + outer_block_shape = create_axis_tile_shape(dim, -1) |
| 17 | + |
| 18 | + def arrange(input): |
| 19 | + input_arranged = input.tile(inner_block_shape).tile(outer_block_shape) |
| 20 | + |
| 21 | + input_arranged.dtype = input_arranged.dtype.squeeze( |
| 22 | + tuple(d for d in range(input.ndim) if d != dim) |
| 23 | + ) |
| 24 | + input_arranged.dtype.dtype = input_arranged.dtype.dtype.squeeze( |
| 25 | + tuple(d for d in range(input.ndim) if d != dim) |
| 26 | + ) |
| 27 | + return input_arranged |
| 28 | + |
| 29 | + input_arranged = arrange(input) |
| 30 | + output_arranged = arrange(output) |
| 31 | + |
| 32 | + return input_arranged, output_arranged |
| 33 | + |
| 34 | + |
| 35 | +def application(input, output): |
| 36 | + for i in range(input.shape[0]): |
| 37 | + input_i = input[i] |
| 38 | + row_minus_max = input_i - ntl.max(input_i) |
| 39 | + numerator = ntl.exp(ntl.cast(row_minus_max, ntl.float32)) |
| 40 | + denominator = ntl.sum(numerator) |
| 41 | + output[i] = numerator / denominator # noqa: F841 |
| 42 | + |
| 43 | + |
| 44 | +def softmax(input, dim, output=None): |
| 45 | + if output is None: |
| 46 | + output = torch.empty_like(input) |
| 47 | + |
| 48 | + kernel = _make(input.ndim, dim) |
| 49 | + |
| 50 | + kernel(input, output) |
| 51 | + |
| 52 | + return output |
| 53 | + |
| 54 | + |
| 55 | +@functools.cache |
| 56 | +def _make(ndim, dim): |
| 57 | + return ninetoothed.make( |
| 58 | + functools.partial(arrangement, dim=dim), |
| 59 | + application, |
| 60 | + ( |
| 61 | + Tensor(ndim, other=float("-inf"), shape_options={"constexpr": True}), |
| 62 | + Tensor(ndim), |
| 63 | + ), |
| 64 | + ) |
0 commit comments