|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, InfiniTensor. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +#include "torch_kernel_helper.h" |
| 9 | +#include "tiling/platform/platform_ascendc.h" |
| 10 | +#include "aclrtlaunch_add_rms_norm.h" |
| 11 | + |
| 12 | +namespace ascend_kernel { |
| 13 | + |
| 14 | +std::vector<at::Tensor> add_rms_norm(const at::Tensor &x1, |
| 15 | + const at::Tensor &x2, |
| 16 | + const at::Tensor &weight, double eps) { |
| 17 | + // Input validation. |
| 18 | + TORCH_CHECK(x1.dim() > 0, |
| 19 | + "add_rms_norm: x1 must have at least 1 dimension"); |
| 20 | + TORCH_CHECK(x1.sizes() == x2.sizes(), |
| 21 | + "add_rms_norm: x1 and x2 must have the same shape"); |
| 22 | + TORCH_CHECK(x1.scalar_type() == x2.scalar_type(), |
| 23 | + "add_rms_norm: x1 and x2 must have the same dtype"); |
| 24 | + TORCH_CHECK(x1.scalar_type() == at::kHalf || |
| 25 | + x1.scalar_type() == at::kFloat, |
| 26 | + "add_rms_norm: only float16 and float32 are supported, got ", |
| 27 | + x1.scalar_type()); |
| 28 | + TORCH_CHECK(weight.dim() == 1, |
| 29 | + "add_rms_norm: weight must be 1-dimensional"); |
| 30 | + TORCH_CHECK(weight.size(0) == x1.size(-1), |
| 31 | + "add_rms_norm: weight size (", weight.size(0), |
| 32 | + ") must match input last dim (", x1.size(-1), ")"); |
| 33 | + |
| 34 | + int64_t dimLength = x1.size(-1); |
| 35 | + int64_t totalRows = x1.numel() / dimLength; |
| 36 | + |
| 37 | + if (totalRows == 0 || dimLength == 0) { |
| 38 | + return {at::empty_like(x1), at::empty_like(x1)}; |
| 39 | + } |
| 40 | + |
| 41 | + at::Tensor inp1 = x1.contiguous(); |
| 42 | + at::Tensor inp2 = x2.contiguous(); |
| 43 | + int64_t dtypeSize = inp1.element_size(); |
| 44 | + |
| 45 | + // Hardware parameters. |
| 46 | + auto ascendc_platform = |
| 47 | + platform_ascendc::PlatformAscendCManager::GetInstance(); |
| 48 | + int64_t coreNum = |
| 49 | + static_cast<int64_t>(ascendc_platform->GetCoreNumAiv()); |
| 50 | + uint64_t ubSize; |
| 51 | + ascendc_platform->GetCoreMemSize(platform_ascendc::CoreMemType::UB, |
| 52 | + ubSize); |
| 53 | + int64_t ubSizeLimit = static_cast<int64_t>(ubSize); |
| 54 | + |
| 55 | + // Alignment (32-byte boundary). |
| 56 | + int64_t alignElements = 32 / dtypeSize; |
| 57 | + int64_t dimLengthAlign = |
| 58 | + ((dimLength + alignElements - 1) / alignElements) * alignElements; |
| 59 | + |
| 60 | + // UB capacity check. |
| 61 | + // fp16: inQ_x1(×2×2) + inQ_x2(×2×2) + outQ_y(×2×2) + outQ_xout(×2×2) |
| 62 | + // + fp32Buf1(×4) + fp32Buf2(×4) + weight(×4) = 16 + 12 = 28 |
| 63 | + // fp32: inQ_x1(×2×4) + inQ_x2(×2×4) + outQ_y(×2×4) + outQ_xout(×2×4) |
| 64 | + // + weight(×4) = 32 + 4 = 36 |
| 65 | + int64_t bufferCoefficient = (dtypeSize == 2) ? 28 : 36; |
| 66 | + int64_t maxDimLength = |
| 67 | + (ubSizeLimit - 1024) / bufferCoefficient; |
| 68 | + int64_t fpAlignElements = 32 / 4; |
| 69 | + maxDimLength = |
| 70 | + (maxDimLength / fpAlignElements) * fpAlignElements; |
| 71 | + TORCH_CHECK(dimLengthAlign <= maxDimLength, |
| 72 | + "add_rms_norm: dimLength ", dimLength, |
| 73 | + " (aligned ", dimLengthAlign, |
| 74 | + ") exceeds UB capacity (max ", maxDimLength, ")"); |
| 75 | + |
| 76 | + // Padding. |
| 77 | + at::Tensor kernelInput1; |
| 78 | + at::Tensor kernelInput2; |
| 79 | + |
| 80 | + if (dimLength != dimLengthAlign) { |
| 81 | + kernelInput1 = inp1.reshape({totalRows, dimLength}); |
| 82 | + kernelInput1 = at::constant_pad_nd( |
| 83 | + kernelInput1, {0, dimLengthAlign - dimLength}, 0.0); |
| 84 | + kernelInput1 = kernelInput1.contiguous(); |
| 85 | + |
| 86 | + kernelInput2 = inp2.reshape({totalRows, dimLength}); |
| 87 | + kernelInput2 = at::constant_pad_nd( |
| 88 | + kernelInput2, {0, dimLengthAlign - dimLength}, 0.0); |
| 89 | + kernelInput2 = kernelInput2.contiguous(); |
| 90 | + } else { |
| 91 | + kernelInput1 = |
| 92 | + inp1.reshape({totalRows, dimLengthAlign}).contiguous(); |
| 93 | + kernelInput2 = |
| 94 | + inp2.reshape({totalRows, dimLengthAlign}).contiguous(); |
| 95 | + } |
| 96 | + |
| 97 | + at::Tensor kernelOutputY = at::empty_like(kernelInput1); |
| 98 | + at::Tensor kernelOutputXOut = at::empty_like(kernelInput1); |
| 99 | + |
| 100 | + // Weight: always pass as fp32, padded to `dimLengthAlign`. |
| 101 | + at::Tensor weightFloat = weight.contiguous().to(at::kFloat); |
| 102 | + |
| 103 | + if (dimLength != dimLengthAlign) { |
| 104 | + weightFloat = at::constant_pad_nd( |
| 105 | + weightFloat, {0, dimLengthAlign - dimLength}, 0.0); |
| 106 | + } |
| 107 | + |
| 108 | + weightFloat = weightFloat.contiguous(); |
| 109 | + |
| 110 | + // Block-level tiling (distribute rows across cores). |
| 111 | + int64_t usedCoreNum = std::min(totalRows, coreNum); |
| 112 | + int64_t formerLength = |
| 113 | + (totalRows + usedCoreNum - 1) / usedCoreNum; |
| 114 | + int64_t tailLength = formerLength - 1; |
| 115 | + int64_t formerNum = totalRows - tailLength * usedCoreNum; |
| 116 | + uint32_t blockDim = static_cast<uint32_t>(usedCoreNum); |
| 117 | + |
| 118 | + // All EXEC_KERNEL_CMD args must be lvalues. |
| 119 | + float epsFloat = static_cast<float>(eps); |
| 120 | + int64_t dtypeSizeVal = dtypeSize; |
| 121 | + |
| 122 | + EXEC_KERNEL_CMD(add_rms_norm, blockDim, |
| 123 | + kernelInput1, kernelInput2, weightFloat, |
| 124 | + kernelOutputY, kernelOutputXOut, |
| 125 | + totalRows, dimLength, dimLengthAlign, |
| 126 | + formerNum, formerLength, tailLength, |
| 127 | + epsFloat, dtypeSizeVal); |
| 128 | + |
| 129 | + // Remove padding and reshape back to original shape. |
| 130 | + at::Tensor outputY = kernelOutputY; |
| 131 | + at::Tensor outputXOut = kernelOutputXOut; |
| 132 | + |
| 133 | + if (dimLength != dimLengthAlign) { |
| 134 | + outputY = outputY.narrow(-1, 0, dimLength).contiguous(); |
| 135 | + outputXOut = outputXOut.narrow(-1, 0, dimLength).contiguous(); |
| 136 | + } |
| 137 | + |
| 138 | + outputY = outputY.reshape(x1.sizes()); |
| 139 | + outputXOut = outputXOut.reshape(x1.sizes()); |
| 140 | + |
| 141 | + return {outputY, outputXOut}; |
| 142 | +} |
| 143 | + |
| 144 | +} // namespace ascend_kernel |
0 commit comments