|
| 1 | +# Copyright 2026 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import torch |
| 9 | + |
| 10 | +from executorch.backends.arm.tosa.dialect.lib import TosaValueError |
| 11 | +from executorch.backends.arm.tosa.dialect.ops_registration import register_fake_tosa_op |
| 12 | +from executorch.backends.arm.tosa.specification import ( |
| 13 | + get_context_spec, |
| 14 | + TosaSpecification, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@register_fake_tosa_op( |
| 19 | + "CAST_TO_BLOCK_SCALED(Tensor input, SymInt block_size, ScalarType output_dtype) -> (Tensor, Tensor)", |
| 20 | + [TosaSpecification.create_from_string("TOSA-1.1+FP")], |
| 21 | +) |
| 22 | +def CAST_TO_BLOCK_SCALED( |
| 23 | + input: torch.Tensor, |
| 24 | + block_size: int, |
| 25 | + output_dtype: torch.dtype, |
| 26 | +) -> tuple[torch.Tensor, torch.Tensor]: |
| 27 | + tosa_spec = get_context_spec() |
| 28 | + |
| 29 | + if not tosa_spec.support_float() or not tosa_spec.support_extension("mxfp"): |
| 30 | + raise TosaValueError( |
| 31 | + f"TOSA spec {tosa_spec} doesn't support MXFP block-scaled casts", |
| 32 | + op="CAST_TO_BLOCK_SCALED", |
| 33 | + ) |
| 34 | + |
| 35 | + if input.dtype not in (torch.float32, torch.bfloat16): |
| 36 | + raise TosaValueError( |
| 37 | + f"Unsupported input dtype {input.dtype} for CAST_TO_BLOCK_SCALED", |
| 38 | + op="CAST_TO_BLOCK_SCALED", |
| 39 | + ) |
| 40 | + if input.dtype == torch.bfloat16 and not ( |
| 41 | + tosa_spec.support_extension("bf16") or tosa_spec.support_extension("mxfp") |
| 42 | + ): |
| 43 | + raise TosaValueError( |
| 44 | + f"TOSA spec {tosa_spec} doesn't support bf16", |
| 45 | + op="CAST_TO_BLOCK_SCALED", |
| 46 | + ) |
| 47 | + |
| 48 | + if input.ndim < 1: |
| 49 | + raise TosaValueError( |
| 50 | + "CAST_TO_BLOCK_SCALED requires rank >= 1", |
| 51 | + op="CAST_TO_BLOCK_SCALED", |
| 52 | + ) |
| 53 | + if block_size != 32: |
| 54 | + raise TosaValueError( |
| 55 | + f"Unsupported block_size {block_size} (must be 32)", |
| 56 | + op="CAST_TO_BLOCK_SCALED", |
| 57 | + ) |
| 58 | + if input.shape[-1] % block_size != 0: |
| 59 | + raise TosaValueError( |
| 60 | + f"Last dim {input.shape[-1]} must be divisible by block_size {block_size}", |
| 61 | + op="CAST_TO_BLOCK_SCALED", |
| 62 | + ) |
| 63 | + |
| 64 | + scale_tensor_dtype = torch.float8_e8m0fnu |
| 65 | + if output_dtype not in (torch.float8_e4m3fn, torch.float8_e5m2): |
| 66 | + raise TosaValueError( |
| 67 | + f"Unsupported block-scaled output dtype {output_dtype}", |
| 68 | + op="CAST_TO_BLOCK_SCALED", |
| 69 | + ) |
| 70 | + scale_shape = (*input.shape[:-1], input.shape[-1] // block_size) |
| 71 | + output_data = torch.empty_like(input, dtype=output_dtype) |
| 72 | + output_scale = input.new_empty(scale_shape, dtype=scale_tensor_dtype) |
| 73 | + return output_data, output_scale |
0 commit comments