Merged
新增MhcPreSinkhorn正反向算子graph_convert文件 #8382
taochangminAdd on Jul 7
新增MhcPreSinkhorn正反向算子graph_convert文件 #8382
Merged
Merged fromtaochangmin/ops-transformermasterintocann/ops-transformermaster
Total: 5 files changed+280-2
| @@ -378,7 +378,7 @@ public: | |||
| 378 | rowOuterIdx * tilingData->stage2RowFactor * tilingData->hcMult * tilingData->d + | 378 | rowOuterIdx * tilingData->stage2RowFactor * tilingData->hcMult * tilingData->d + |
| 379 | dLoopIdx * tilingData->dFactor; | 379 | dLoopIdx * tilingData->dFactor; |
| 380 | CopyIn(xGm[xGmOffset], | 380 | CopyIn(xGm[xGmOffset], |
| 381 | - xLocal, tilingData->stage2RowFactor * tilingData->hcMult, curDFactor, | 381 | + xLocal, curRowFactor * tilingData->hcMult, curDFactor, |
| 382 | tilingData->d - curDFactor); | 382 | tilingData->d - curDFactor); |
| 383 | xQue.template EnQue(xLocal); | 383 | xQue.template EnQue(xLocal); |
| 384 | xLocal = xQue.template DeQue<T>(); | 384 | xLocal = xQue.template DeQue<T>(); |
| @@ -430,7 +430,7 @@ public: | |||
| 430 | rowOuterIdx * tilingData->stage2RowFactor * tilingData->hcMult * tilingData->d + | 430 | rowOuterIdx * tilingData->stage2RowFactor * tilingData->hcMult * tilingData->d + |
| 431 | dLoopIdx * tilingData->dFactor; | 431 | dLoopIdx * tilingData->dFactor; |
| 432 | CopyIn(xGm[xGmOffset], | 432 | CopyIn(xGm[xGmOffset], |
| 433 | - xLocal, tilingData->stage2RowFactor * tilingData->hcMult, curDFactor, | 433 | + xLocal, curRowFactor * tilingData->hcMult, curDFactor, |
| 434 | tilingData->d - curDFactor); | 434 | tilingData->d - curDFactor); |
| 435 | xQue.template EnQue(xLocal); | 435 | xQue.template EnQue(xLocal); |
| 436 | xLocal = xQue.template DeQue<T>(); | 436 | xLocal = xQue.template DeQue<T>(); |
| @@ -27,6 +27,7 @@ from .lightning_indexer import lightning_indexer, lightning_indexer_metadata | |||
| 27 | from .quant_lightning_indexer import quant_lightning_indexer, quant_lightning_indexer_metadata | 27 | from .quant_lightning_indexer import quant_lightning_indexer, quant_lightning_indexer_metadata |
| 28 | from .mhc_post import mhc_post | 28 | from .mhc_post import mhc_post |
| 29 | from .mhc_pre_sinkhorn import mhc_pre_sinkhorn | 29 | from .mhc_pre_sinkhorn import mhc_pre_sinkhorn |
| 30 | +from .mhc_pre_sinkhorn_backward import mhc_pre_sinkhorn_backward | ||
| 30 | from .kv_compress_epilog import kv_compress_epilog | 31 | from .kv_compress_epilog import kv_compress_epilog |
| 31 | from .indexer_quant_cache import indexer_quant_cache | 32 | from .indexer_quant_cache import indexer_quant_cache |
| 32 | from .compressor import compressor | 33 | from .compressor import compressor |
| @@ -0,0 +1,133 @@ | |||
| 1 | +# ----------------------------------------------------------------------------------------------------------- | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | +# ----------------------------------------------------------------------------------------------------------- | ||
| 10 | +# GE Converter for Graph Mode | ||
| 11 | + | ||
| 12 | +try: | ||
| 13 | + import torch | ||
| 14 | + import torch_npu | ||
| 15 | + import torchair | ||
| 16 | + from torch.library import impl | ||
| 17 | + from torchair._ge_concrete_graph import ge_apis as ge | ||
| 18 | + from torchair.ge._ge_graph import Tensor, TensorSpec | ||
| 19 | + from torchair._ge_concrete_graph.fx2ge_converter import declare_supported, register_fx_node_ge_converter | ||
| 20 | + from torchair._ge_concrete_graph.supported_declaration import Support | ||
| 21 | + from typing import Any, Dict, List, Tuple, Union, Callable, Optional | ||
| 22 | + from torchair._ge_concrete_graph.ge_ir_pb2 import GraphDef, OpDef, TensorDescriptor, TensorDef | ||
| 23 | + from torchair.ge._ge_graph import get_default_ge_graph, next_unique_name | ||
| 24 | + from torchair.ge._ge_graph import auto_convert_to_tensor | ||
| 25 | + from torchair.ge._ge_graph import Tensor, TensorSpec, DataType, TensorType | ||
| 26 | + from torchair.ge._ge_graph import compat_as_bytes, compat_as_bytes_list | ||
| 27 | + from torchair.ge._ge_graph import trans_to_list_list_int, trans_to_list_list_float | ||
| 28 | + from torchair.ge._ge_graph import get_invalid_desc | ||
| 29 | + from torchair._ge_concrete_graph.compat_ir import ge_op, IrDef | ||
| 30 | + from torchair.ge import attr | ||
| 31 | + _TORCHAIR_AVAILABLE = True | ||
| 32 | +except ImportError: | ||
| 33 | + _TORCHAIR_AVAILABLE = False | ||
| 34 | + | ||
| 35 | +if _TORCHAIR_AVAILABLE: | ||
| 36 | + @auto_convert_to_tensor([False, False, False, False], [False, False, False, False]) | ||
| 37 | + def MhcPreSinkhorn(x: Tensor, phi: Tensor, alpha: Tensor, bias: Tensor, *, | ||
| 38 | + hc_mult: int = 4, num_iters: int = 20, hc_eps: float = 1e-6, | ||
| 39 | + norm_eps: float = 1e-6, need_backward: bool = True, | ||
| 40 | + dependencies=[], node_name=None): | ||
| 41 | + """REG_OP(MhcPreSinkhorn)\n | ||
| 42 | + .INPUT(x, TensorType({DT_BF16, DT_FLOAT16}))\n | ||
| 43 | + .INPUT(phi, TensorType({DT_FLOAT}))\n | ||
| 44 | + .INPUT(alpha, TensorType({DT_FLOAT}))\n | ||
| 45 | + .INPUT(bias, TensorType({DT_FLOAT}))\n | ||
| 46 | + .OUTPUT(hin, TensorType({DT_BF16, DT_FLOAT16}))\n | ||
| 47 | + .OUTPUT(hPost, TensorType({DT_FLOAT}))\n | ||
| 48 | + .OUTPUT(hRes, TensorType({DT_FLOAT}))\n | ||
| 49 | + .OUTPUT(hPre, TensorType({DT_FLOAT}))\n | ||
| 50 | + .OUTPUT(hcBeforeNorm, TensorType({DT_FLOAT}))\n | ||
| 51 | + .OUTPUT(invRms, TensorType({DT_FLOAT}))\n | ||
| 52 | + .OUTPUT(sumOut, TensorType({DT_FLOAT}))\n | ||
| 53 | + .OUTPUT(normOut, TensorType({DT_FLOAT}))\n | ||
| 54 | + .ATTR(hc_mult, Int, 4)\n | ||
| 55 | + .ATTR(num_iters, Int, 20)\n | ||
| 56 | + .ATTR(hc_eps, Float, 1e-6)\n | ||
| 57 | + .ATTR(norm_eps, Float, 1e-6)\n | ||
| 58 | + .ATTR(need_backward, Bool, true)\n | ||
| 59 | + """ | ||
| 60 | + inputs = { | ||
| 61 | + "x": x, | ||
| 62 | + "phi": phi, | ||
| 63 | + "alpha": alpha, | ||
| 64 | + "bias": bias, | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + attrs = { | ||
| 68 | + "hc_mult": attr.Int(hc_mult), | ||
| 69 | + "num_iters": attr.Int(num_iters), | ||
| 70 | + "hc_eps": attr.Float(hc_eps), | ||
| 71 | + "norm_eps": attr.Float(norm_eps), | ||
| 72 | + "need_backward": attr.Bool(need_backward), | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + outputs = [ | ||
| 76 | + "hin", | ||
| 77 | + "hPost", | ||
| 78 | + "hRes", | ||
| 79 | + "hPre", | ||
| 80 | + "hcBeforeNorm", | ||
| 81 | + "invRms", | ||
| 82 | + "sumOut", | ||
| 83 | + "normOut", | ||
| 84 | + ] | ||
| 85 | + | ||
| 86 | + return ge_op( | ||
| 87 | + op_type="MhcPreSinkhorn", | ||
| 88 | + inputs=inputs, | ||
| 89 | + attrs=attrs, | ||
| 90 | + outputs=outputs, | ||
| 91 | + dependencies=dependencies, | ||
| 92 | + ir=IrDef("MhcPreSinkhorn") \ | ||
| 93 | + .input("x", "DT_BF16, DT_FLOAT16") \ | ||
| 94 | + .input("phi", "DT_FLOAT") \ | ||
| 95 | + .input("alpha", "DT_FLOAT") \ | ||
| 96 | + .input("bias", "DT_FLOAT") \ | ||
| 97 | + .attr("hc_mult", attr.Int(4)) \ | ||
| 98 | + .attr("num_iters", attr.Int(20)) \ | ||
| 99 | + .attr("hc_eps", attr.Float(1e-6)) \ | ||
| 100 | + .attr("norm_eps", attr.Float(1e-6)) \ | ||
| 101 | + .attr("need_backward", attr.Bool(True)) \ | ||
| 102 | + .output("hin", "DT_BF16, DT_FLOAT16") \ | ||
| 103 | + .output("hPost", "DT_FLOAT") \ | ||
| 104 | + .output("hRes", "DT_FLOAT") \ | ||
| 105 | + .output("hPre", "DT_FLOAT") \ | ||
| 106 | + .output("hcBeforeNorm", "DT_FLOAT") \ | ||
| 107 | + .output("invRms", "DT_FLOAT") \ | ||
| 108 | + .output("sumOut", "DT_FLOAT") \ | ||
| 109 | + .output("normOut", "DT_FLOAT") | ||
| 110 | + ) | ||
| 111 | + | ||
| 112 | + @register_fx_node_ge_converter(torch.ops.cann_ops_transformer.mhc_pre_sinkhorn.default) | ||
| 113 | + def convert_mhc_pre_sinkhorn( | ||
| 114 | + x: Tensor, | ||
| 115 | + phi: Tensor, | ||
| 116 | + alpha: Tensor, | ||
| 117 | + bias: Tensor, | ||
| 118 | + hc_mult: int, | ||
| 119 | + num_iters: int, | ||
| 120 | + hc_eps: float, | ||
| 121 | + norm_eps: float, | ||
| 122 | + out_flag: bool, | ||
| 123 | + meta_outputs: TensorSpec = None): | ||
| 124 | + | ||
| 125 | + return MhcPreSinkhorn(x=x, | ||
| 126 | + phi=phi, | ||
| 127 | + alpha=alpha, | ||
| 128 | + bias=bias, | ||
| 129 | + hc_mult=hc_mult, | ||
| 130 | + num_iters=num_iters, | ||
| 131 | + hc_eps=hc_eps, | ||
| 132 | + norm_eps=norm_eps, | ||
| 133 | + need_backward=out_flag) | ||
Atorch_extension/cann_ops_transformer/ops/graph_convert/graph_convert_mhc_pre_sinkhorn_backward.py+144-0
| @@ -0,0 +1,144 @@ | |||
| 1 | +# ----------------------------------------------------------------------------------------------------------- | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | +# ----------------------------------------------------------------------------------------------------------- | ||
| 10 | +# GE Converter for Graph Mode | ||
| 11 | + | ||
| 12 | +try: | ||
| 13 | + import torch | ||
| 14 | + import torch_npu | ||
| 15 | + import torchair | ||
| 16 | + from torch.library import impl | ||
| 17 | + from torchair._ge_concrete_graph import ge_apis as ge | ||
| 18 | + from torchair.ge._ge_graph import Tensor, TensorSpec | ||
| 19 | + from torchair._ge_concrete_graph.fx2ge_converter import declare_supported, register_fx_node_ge_converter | ||
| 20 | + from torchair._ge_concrete_graph.supported_declaration import Support | ||
| 21 | + from typing import Any, Dict, List, Tuple, Union, Callable, Optional | ||
| 22 | + from torchair._ge_concrete_graph.ge_ir_pb2 import GraphDef, OpDef, TensorDescriptor, TensorDef | ||
| 23 | + from torchair.ge._ge_graph import get_default_ge_graph, next_unique_name | ||
| 24 | + from torchair.ge._ge_graph import auto_convert_to_tensor | ||
| 25 | + from torchair.ge._ge_graph import Tensor, TensorSpec, DataType, TensorType | ||
| 26 | + from torchair.ge._ge_graph import compat_as_bytes, compat_as_bytes_list | ||
| 27 | + from torchair.ge._ge_graph import trans_to_list_list_int, trans_to_list_list_float | ||
| 28 | + from torchair.ge._ge_graph import get_invalid_desc | ||
| 29 | + from torchair._ge_concrete_graph.compat_ir import ge_op, IrDef | ||
| 30 | + from torchair.ge import attr | ||
| 31 | + _TORCHAIR_AVAILABLE = True | ||
| 32 | +except ImportError: | ||
| 33 | + _TORCHAIR_AVAILABLE = False | ||
| 34 | + | ||
| 35 | +if _TORCHAIR_AVAILABLE: | ||
| 36 | + @auto_convert_to_tensor( | ||
| 37 | + [False, False, False, False, False, False, False, False, False, False, False, False], | ||
| 38 | + [False, False, False, False, False, False, False, False, False, False, False, False]) | ||
| 39 | + def MhcPreSinkhornBackward(grad_hin: Tensor, grad_h_post: Tensor, grad_h_res: Tensor, | ||
| 40 | + x: Tensor, phi: Tensor, alpha: Tensor, bias: Tensor, | ||
| 41 | + h_pre: Tensor, hc_before_norm: Tensor, inv_rms: Tensor, | ||
| 42 | + sum_out: Tensor, norm_out: Tensor, *, | ||
| 43 | + hc_eps: float = 1e-6, dependencies=[], node_name=None): | ||
| 44 | + """REG_OP(MhcPreSinkhornBackward)\n | ||
| 45 | + .INPUT(grad_hin, TensorType({DT_BF16, DT_FLOAT16}))\n | ||
| 46 | + .INPUT(grad_h_post, TensorType({DT_FLOAT}))\n | ||
| 47 | + .INPUT(grad_h_res, TensorType({DT_FLOAT}))\n | ||
| 48 | + .INPUT(x, TensorType({DT_BF16, DT_FLOAT16}))\n | ||
| 49 | + .INPUT(phi, TensorType({DT_FLOAT}))\n | ||
| 50 | + .INPUT(alpha, TensorType({DT_FLOAT}))\n | ||
| 51 | + .INPUT(bias, TensorType({DT_FLOAT}))\n | ||
| 52 | + .INPUT(h_pre, TensorType({DT_FLOAT}))\n | ||
| 53 | + .INPUT(hc_before_norm, TensorType({DT_FLOAT}))\n | ||
| 54 | + .INPUT(inv_rms, TensorType({DT_FLOAT}))\n | ||
| 55 | + .INPUT(sum_out, TensorType({DT_FLOAT}))\n | ||
| 56 | + .INPUT(norm_out, TensorType({DT_FLOAT}))\n | ||
| 57 | + .OUTPUT(grad_x, TensorType({DT_BF16, DT_FLOAT16}))\n | ||
| 58 | + .OUTPUT(grad_phi, TensorType({DT_FLOAT}))\n | ||
| 59 | + .OUTPUT(grad_alpha, TensorType({DT_FLOAT}))\n | ||
| 60 | + .OUTPUT(grad_bias, TensorType({DT_FLOAT}))\n | ||
| 61 | + .ATTR(hc_eps, Float, 1e-6)\n | ||
| 62 | + """ | ||
| 63 | + inputs = { | ||
| 64 | + "grad_hin": grad_hin, | ||
| 65 | + "grad_h_post": grad_h_post, | ||
| 66 | + "grad_h_res": grad_h_res, | ||
| 67 | + "x": x, | ||
| 68 | + "phi": phi, | ||
| 69 | + "alpha": alpha, | ||
| 70 | + "bias": bias, | ||
| 71 | + "h_pre": h_pre, | ||
| 72 | + "hc_before_norm": hc_before_norm, | ||
| 73 | + "inv_rms": inv_rms, | ||
| 74 | + "sum_out": sum_out, | ||
| 75 | + "norm_out": norm_out, | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + attrs = { | ||
| 79 | + "hc_eps": attr.Float(hc_eps), | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + outputs = [ | ||
| 83 | + "grad_x", | ||
| 84 | + "grad_phi", | ||
| 85 | + "grad_alpha", | ||
| 86 | + "grad_bias", | ||
| 87 | + ] | ||
| 88 | + | ||
| 89 | + return ge_op( | ||
| 90 | + op_type="MhcPreSinkhornBackward", | ||
| 91 | + inputs=inputs, | ||
| 92 | + attrs=attrs, | ||
| 93 | + outputs=outputs, | ||
| 94 | + dependencies=dependencies, | ||
| 95 | + ir=IrDef("MhcPreSinkhornBackward") \ | ||
| 96 | + .input("grad_hin", "DT_BF16, DT_FLOAT16") \ | ||
| 97 | + .input("grad_h_post", "DT_FLOAT") \ | ||
| 98 | + .input("grad_h_res", "DT_FLOAT") \ | ||
| 99 | + .input("x", "DT_BF16, DT_FLOAT16") \ | ||
| 100 | + .input("phi", "DT_FLOAT") \ | ||
| 101 | + .input("alpha", "DT_FLOAT") \ | ||
| 102 | + .input("bias", "DT_FLOAT") \ | ||
| 103 | + .input("h_pre", "DT_FLOAT") \ | ||
| 104 | + .input("hc_before_norm", "DT_FLOAT") \ | ||
| 105 | + .input("inv_rms", "DT_FLOAT") \ | ||
| 106 | + .input("sum_out", "DT_FLOAT") \ | ||
| 107 | + .input("norm_out", "DT_FLOAT") \ | ||
| 108 | + .attr("hc_eps", attr.Float(1e-6)) \ | ||
| 109 | + .output("grad_x", "DT_BF16, DT_FLOAT16") \ | ||
| 110 | + .output("grad_phi", "DT_FLOAT") \ | ||
| 111 | + .output("grad_alpha", "DT_FLOAT") \ | ||
| 112 | + .output("grad_bias", "DT_FLOAT") | ||
| 113 | + ) | ||
| 114 | + | ||
| 115 | + @register_fx_node_ge_converter(torch.ops.cann_ops_transformer.mhc_pre_sinkhorn_backward.default) | ||
| 116 | + def convert_mhc_pre_sinkhorn_backward( | ||
| 117 | + grad_hin: Tensor, | ||
| 118 | + grad_h_post: Tensor, | ||
| 119 | + grad_h_res: Tensor, | ||
| 120 | + x: Tensor, | ||
| 121 | + phi: Tensor, | ||
| 122 | + alpha: Tensor, | ||
| 123 | + bias: Tensor, | ||
| 124 | + h_pre: Tensor, | ||
| 125 | + hc_before_norm: Tensor, | ||
| 126 | + inv_rms: Tensor, | ||
| 127 | + sum_out: Tensor, | ||
| 128 | + norm_out: Tensor, | ||
| 129 | + hc_eps: float, | ||
| 130 | + meta_outputs: TensorSpec = None): | ||
| 131 | + | ||
| 132 | + return MhcPreSinkhornBackward(grad_hin=grad_hin, | ||
| 133 | + grad_h_post=grad_h_post, | ||
| 134 | + grad_h_res=grad_h_res, | ||
| 135 | + x=x, | ||
| 136 | + phi=phi, | ||
| 137 | + alpha=alpha, | ||
| 138 | + bias=bias, | ||
| 139 | + h_pre=h_pre, | ||
| 140 | + hc_before_norm=hc_before_norm, | ||
| 141 | + inv_rms=inv_rms, | ||
| 142 | + sum_out=sum_out, | ||
| 143 | + norm_out=norm_out, | ||
| 144 | + hc_eps=hc_eps) | ||