Merged
TensorAPI新增conv2D功能 #4239
ca1kwAdd on Jul 10
TensorAPI新增conv2D功能 #4239
Merged
Merged fromca1kw/asc-devkitmasterintocann/asc-devkitmaster
Total: 20 files changed+1042-27
| @@ -0,0 +1,85 @@ | |||
| 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 | + | ||
| 11 | +#if !defined(ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS) | ||
| 12 | +#warning \ | ||
| 13 | + "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/nc1hwc02nc1hwc0.h is an internal header file and must not be used directly. Functions or variables defined in this file maybe removed in the future. Please use "#include "tensor_api/tensor.h"" and use public functions or variables defined in interface headers files." | ||
| 14 | +#define ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS | ||
| 15 | +#define UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC | ||
| 16 | +#endif | ||
| 17 | + | ||
| 18 | +/*! | ||
| 19 | + * \file nc1hwc02nc1hwc0.h | ||
| 20 | + * \brief GM(NC1HWC0) -> L1(NC1HWC0) copy for conv feature maps. | ||
| 21 | + */ | ||
| 22 | +#ifndef IMPL_TENSOR_API_ARCH_CUBE_GM_TO_L1_COPY_IMPL_NC1HWC02NC1HWC0_H | ||
| 23 | +#define IMPL_TENSOR_API_ARCH_CUBE_GM_TO_L1_COPY_IMPL_NC1HWC02NC1HWC0_H | ||
| 24 | + | ||
| 25 | +#include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/instruction.h" | ||
| 26 | + | ||
| 27 | +namespace AscendC { | ||
| 28 | +namespace Te { | ||
| 29 | + | ||
| 30 | +// GM(NC1HWC0) -> L1(NC1HWC0). Dispatched by CopyGM2L1Routing when both src/dst carry the | ||
| 31 | +// NC1HWC0LayoutPtn tag. W is loaded fully when contiguous (one burst per C1 of H*W*C0); | ||
| 32 | +// otherwise a per-H loop emits C1 bursts of W*C0 each. | ||
| 33 | +class CopyGmToCbufNC1HWC02NC1HWC0 { | ||
| 34 | +public: | ||
| 35 | + template <const CopyGM2L1Trait& trait, typename T, typename U> | ||
| 36 | + __aicore__ inline static void Run(const T& dst, const U& src) | ||
| 37 | + { | ||
| 38 | + using type = typename U::elementType; | ||
| 39 | + auto srcLayout = src.Layout(); | ||
| 40 | + auto dstLayout = dst.Layout(); | ||
| 41 | + | ||
| 42 | + // (N, C1, H, W, C0): C1 axis stride = H*W*C0, H axis stride = W*C0. | ||
| 43 | + uint8_t cacheMode = src.Engine().GetCacheMode(); | ||
| 44 | + uint32_t c1 = Get<1>(dstLayout.Shape()); | ||
| 45 | + uint32_t rowElems = Get<2>(dstLayout.Stride()); // W*C0 (dst packed H stride) | ||
| 46 | + | ||
| 47 | + uint64_t srcStride = Get<1>(srcLayout.Stride()) * sizeof(type); // burst_src_stride | ||
| 48 | + uint32_t dstStride = Get<1>(dstLayout.Stride()) * sizeof(type); // burst_dst_stride | ||
| 49 | + if constexpr (IsB4Type<type>) { | ||
| 50 | + srcStride = srcStride >> 1; | ||
| 51 | + dstStride = dstStride >> 1; | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + if (Get<2>(srcLayout.Stride()) == rowElems) { | ||
| 55 | + // W fully loaded: src H rows contiguous, one burst per C1 of H*W*C0. | ||
| 56 | + uint32_t blockLen = Get<1>(dstLayout.Stride()) * sizeof(type); | ||
| 57 | + if constexpr (IsB4Type<type>) { | ||
| 58 | + blockLen = blockLen >> 1; | ||
| 59 | + } | ||
| 60 | + CopyGmToCbufAlignV2Base::DataCopy(dst, src, c1, blockLen, 0, 0, cacheMode, srcStride, dstStride); | ||
| 61 | + } else { | ||
| 62 | + // W not fully loaded (sliced src): per-H loop, C1 bursts of W*C0 each. | ||
| 63 | + uint32_t h = Get<2>(dstLayout.Shape()); | ||
| 64 | + uint32_t blockLen = rowElems * sizeof(type); | ||
| 65 | + if constexpr (IsB4Type<type>) { | ||
| 66 | + blockLen = blockLen >> 1; | ||
| 67 | + } | ||
| 68 | + for (uint32_t i = 0; i < h; ++i) { | ||
| 69 | + auto srcH = src(MakeCoord(0, 0, i, 0, 0)); | ||
| 70 | + auto dstH = dst(MakeCoord(0, 0, i, 0, 0)); | ||
| 71 | + CopyGmToCbufAlignV2Base::DataCopy(dstH, srcH, c1, blockLen, 0, 0, cacheMode, srcStride, dstStride); | ||
| 72 | + } | ||
| 73 | + } | ||
| 74 | + } | ||
| 75 | +}; | ||
| 76 | + | ||
| 77 | +} // namespace Te | ||
| 78 | +} // namespace AscendC | ||
| 79 | + | ||
| 80 | +#endif // IMPL_TENSOR_API_ARCH_CUBE_GM_TO_L1_COPY_IMPL_NC1HWC02NC1HWC0_H | ||
| 81 | + | ||
| 82 | +#if defined(UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC) | ||
| 83 | +#undef ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS | ||
| 84 | +#undef UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC | ||
| 85 | +#endif | ||
| @@ -0,0 +1,81 @@ | |||
| 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 | + | ||
| 11 | +#if !defined(ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS) | ||
| 12 | +#warning \ | ||
| 13 | + "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/nchw2nc1hwc0.h is an internal header file and must not be used directly. Functions or variables defined in this file maybe removed in the future. Please use "#include "tensor_api/tensor.h"" and use public functions or variables defined in interface headers files." | ||
这么一大坨,每个文件都写是很容易出现低级的拼写错误的,如果用一个统一的宏定义,然后这里只需要写一行即可。 ![]() ![]() | |||
| 14 | +#define ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS | ||
| 15 | +#define UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC | ||
| 16 | +#endif | ||
| 17 | + | ||
| 18 | +/*! | ||
| 19 | + * \file nchw2nc1hwc0.h | ||
| 20 | + * \brief GM(NCHW) -> L1(NC1HWC0) copy via the dn2nz DMA path. | ||
| 21 | + */ | ||
| 22 | +#ifndef IMPL_TENSOR_API_ARCH_CUBE_GM_TO_L1_COPY_IMPL_NCHW2NC1HWC0_H | ||
| 23 | +#define IMPL_TENSOR_API_ARCH_CUBE_GM_TO_L1_COPY_IMPL_NCHW2NC1HWC0_H | ||
| 24 | + | ||
| 25 | +#include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/instruction.h" | ||
| 26 | + | ||
| 27 | +namespace AscendC { | ||
| 28 | +namespace Te { | ||
| 29 | + | ||
| 30 | +// GM(NCHW) -> L1(NC1HWC0). NCHW is the HW<->C transpose of NHWC, so it uses the dn2nz DMA path | ||
| 31 | +// (column-major src) instead of nd2nz. Maps: dnNum=H, nValue=W, dValue=C. Src strides come straight | ||
| 32 | +// from the NCHW layout (row-major (C*H*W, H*W, W, 1)): the H*W axis (Stride[1]) is the dnNum step, | ||
| 33 | +// so loop1SrcStride = H*W*sizeof = Stride[1]*sizeof; the W axis (Stride[2]=W) is the nValue step, | ||
| 34 | +// so loop4SrcStride = W*sizeof = Stride[2]*sizeof. Dst strides: loop2DstStride=1, loop3DstStride=H*W, | ||
| 35 | +// loop4DstStride=W. H*W is the only value not directly readable from the dst stride (which carries | ||
| 36 | +// C0), so it is the single product computed here. | ||
| 37 | +class CopyGmToCbufNCHW2NC1HWC0 { | ||
同上面,Cbuf -> L1 ![]() ![]() | |||
| 38 | +public: | ||
| 39 | + template <const CopyGM2L1Trait& trait, typename T, typename U> | ||
| 40 | + __aicore__ inline static void Run(const T& dst, const U& src) | ||
| 41 | + { | ||
| 42 | + using type = typename U::elementType; | ||
| 43 | + auto srcLayout = src.Layout(); | ||
| 44 | + auto dstLayout = dst.Layout(); | ||
| 45 | + | ||
| 46 | + // src NCHW (N, C, H, W): dnNum=H, nValue=W, dValue=C. | ||
| 47 | + uint16_t dnNum = Get<2>(srcLayout.Shape()); // srcH | ||
| 48 | + uint16_t nValue = Get<3>(srcLayout.Shape()); // srcW | ||
| 49 | + uint32_t dValue = Get<1>(srcLayout.Shape()); // srcC | ||
| 50 | + // NCHW row-major stride: (C*H*W, H*W, W, 1) -> Stride[1]=H*W (dnNum step), Stride[2]=W (nValue step). | ||
| 51 | + uint64_t loop1SrcStride = static_cast<uint64_t>(Get<1>(srcLayout.Stride())) * sizeof(type); // H*W*sizeof | ||
| 52 | + uint64_t loop4SrcStride = static_cast<uint64_t>(Get<2>(srcLayout.Stride())) * sizeof(type); // W*sizeof | ||
| 53 | + | ||
| 54 | + // dst NC1HWC0 (N, C1, H, W, C0): loop4DstStride = dstW = Shape[3]; loop3DstStride = dstH*dstW | ||
| 55 | + // = Shape[2]*Shape[3] (no stride carries the bare H*W product). | ||
| 56 | + uint16_t loop2DstStride = 1; | ||
| 57 | + uint16_t loop4DstStride = Get<3>(dstLayout.Shape()); // dstW | ||
| 58 | + uint16_t loop3DstStride = Get<2>(dstLayout.Shape()) * Get<3>(dstLayout.Shape()); // dstH*dstW | ||
| 59 | + | ||
| 60 | + if constexpr (IsB4Type<type>) { | ||
| 61 | + dValue = dValue >> 1; | ||
| 62 | + loop4SrcStride = loop4SrcStride >> 1; | ||
| 63 | + loop1SrcStride = loop1SrcStride >> 1; | ||
| 64 | + } | ||
| 65 | + uint8_t cacheMode = src.Engine().GetCacheMode(); | ||
| 66 | + | ||
| 67 | + CopyGmToCbufMultiDn2nzInstr::DataCopy( | ||
| 68 | + dst, src, dnNum, loop2DstStride, loop3DstStride, loop4DstStride, loop1SrcStride, cacheMode, nValue, dValue, | ||
| 69 | + loop4SrcStride, false); | ||
| 70 | + } | ||
| 71 | +}; | ||
| 72 | + | ||
| 73 | +} // namespace Te | ||
| 74 | +} // namespace AscendC | ||
| 75 | + | ||
| 76 | +#endif // IMPL_TENSOR_API_ARCH_CUBE_GM_TO_L1_COPY_IMPL_NCHW2NC1HWC0_H | ||
| 77 | + | ||
| 78 | +#if defined(UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC) | ||
| 79 | +#undef ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS | ||
| 80 | +#undef UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC | ||
| 81 | +#endif | ||
| @@ -0,0 +1,74 @@ | |||
| 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 | + | ||
| 11 | +#if !defined(ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS) | ||
| 12 | +#warning \ | ||
| 13 | + "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/nhwc2nc1hwc0.h is an internal header file and must not be used directly. Functions or variables defined in this file maybe removed in the future. Please use "#include "tensor_api/tensor.h"" and use public functions or variables defined in interface headers files." | ||
| 14 | +#define ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS | ||
| 15 | +#define UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC | ||
| 16 | +#endif | ||
| 17 | + | ||
| 18 | +/*! | ||
| 19 | + * \file nhwc2nc1hwc0.h | ||
| 20 | + * \brief GM(NHWC) -> L1(NC1HWC0) copy via the nd2nz DMA path. | ||
| 21 | + */ | ||
| 22 | +#ifndef IMPL_TENSOR_API_ARCH_CUBE_GM_TO_L1_COPY_IMPL_NHWC2NC1HWC0_H | ||
| 23 | +#define IMPL_TENSOR_API_ARCH_CUBE_GM_TO_L1_COPY_IMPL_NHWC2NC1HWC0_H | ||
| 24 | + | ||
| 25 | +#include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/instruction.h" | ||
| 26 | + | ||
| 27 | +namespace AscendC { | ||
| 28 | +namespace Te { | ||
| 29 | + | ||
| 30 | +class CopyGmToCbufNHWC2NC1HWC0 { | ||
| 31 | +public: | ||
| 32 | + template <const CopyGM2L1Trait& trait, typename T, typename U> | ||
| 33 | + __aicore__ inline static void Run(const T& dst, const U& src) | ||
| 34 | + { | ||
| 35 | + using type = typename U::elementType; | ||
| 36 | + auto srcLayout = src.Layout(); | ||
| 37 | + auto dstLayout = dst.Layout(); | ||
| 38 | + | ||
| 39 | + // src NHWC (N, H, W, C): take the H/W/C axes. N is the leading batch axis (kept as N=1 here). | ||
| 40 | + uint16_t ndNum = Get<1>(srcLayout.Shape()); // srcH | ||
| 41 | + uint16_t nValue = Get<2>(srcLayout.Shape()); // srcW | ||
| 42 | + uint32_t dValue = Get<3>(srcLayout.Shape()); // srcC | ||
| 43 | + // NHWC row-major stride: (H*W*C, W*C, C, 1) -> stride[1]=W*C, stride[2]=C. | ||
| 44 | + uint64_t loop4SrcStride = static_cast<uint64_t>(Get<1>(srcLayout.Stride())) * sizeof(type); // srcW*srcC | ||
| 45 | + uint64_t loop1SrcStride = static_cast<uint64_t>(Get<2>(srcLayout.Stride())) * sizeof(type); // srcC | ||
| 46 | + | ||
| 47 | + // dst NC1HWC0 (N, C1, H, W, C0): loop4DstStride = dstW = Shape[3]; loop3DstStride = dstH*dstW | ||
| 48 | + // = Shape[2]*Shape[3] (no stride carries the bare H*W product). | ||
| 49 | + uint16_t loop2DstStride = 1; | ||
| 50 | + uint16_t loop4DstStride = Get<3>(dstLayout.Shape()); // dstW | ||
| 51 | + uint16_t loop3DstStride = Get<2>(dstLayout.Shape()) * Get<3>(dstLayout.Shape()); // dstH*dstW | ||
| 52 | + | ||
| 53 | + if constexpr (IsB4Type<type>) { | ||
| 54 | + dValue = dValue >> 1; | ||
| 55 | + loop4SrcStride = loop4SrcStride >> 1; | ||
| 56 | + loop1SrcStride = loop1SrcStride >> 1; | ||
| 57 | + } | ||
| 58 | + uint8_t cacheMode = src.Engine().GetCacheMode(); | ||
| 59 | + | ||
| 60 | + CopyGmToCbufMultiNd2nzInstr::DataCopy( | ||
| 61 | + dst, src, ndNum, loop2DstStride, loop3DstStride, loop4DstStride, loop1SrcStride, cacheMode, nValue, dValue, | ||
| 62 | + loop4SrcStride, false); | ||
| 63 | + } | ||
| 64 | +}; | ||
| 65 | + | ||
| 66 | +} // namespace Te | ||
| 67 | +} // namespace AscendC | ||
| 68 | + | ||
| 69 | +#endif // IMPL_TENSOR_API_ARCH_CUBE_GM_TO_L1_COPY_IMPL_NHWC2NC1HWC0_H | ||
| 70 | + | ||
| 71 | +#if defined(UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC) | ||
| 72 | +#undef ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS | ||
| 73 | +#undef UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC | ||
| 74 | +#endif | ||
| @@ -27,6 +27,9 @@ | |||
| 27 | #include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/nd2nd.h" | 27 | #include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/nd2nd.h" |
| 28 | #include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/nd2nz.h" | 28 | #include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/nd2nz.h" |
| 29 | #include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/nd2zn.h" | 29 | #include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/nd2zn.h" |
| 30 | +#include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/nc1hwc02nc1hwc0.h" | ||
| 31 | +#include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/nchw2nc1hwc0.h" | ||
| 32 | +#include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/nhwc2nc1hwc0.h" | ||
| 30 | #include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/nz2nz.h" | 33 | #include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/nz2nz.h" |
| 31 | #include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/zn2zn.h" | 34 | #include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/zn2zn.h" |
| 32 | #include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/scalea_nd2zz.h" | 35 | #include "impl/tensor_api/arch/cube/gm_to_l1/copy_impl/scalea_nd2zz.h" |
| @@ -41,10 +44,10 @@ namespace Te { | |||
| 41 | 44 | ||
| 42 | class CopyGM2L1Ignore { | 45 | class CopyGM2L1Ignore { |
| 43 | public: | 46 | public: |
| 44 | - template <const CopyGM2L1Trait& trait, typename... Args> | 47 | + template <const CopyGM2L1Trait& trait, typename T, typename U> |
| 45 | - __aicore__ inline static void Run(const Args&... args) | 48 | + __aicore__ inline static void Run(const T& dst, const U& src) |
| 46 | { | 49 | { |
| 47 | - static_assert(Std::is_same_v<Args..., void>, "CopyGM2L1Ignore should not be called"); | 50 | + static_assert(!Std::is_same_v<T, T>, "CopyGM2L1: unsupported layout pattern combination."); |
| 48 | } | 51 | } |
| 49 | }; | 52 | }; |
| 50 | 53 | ||
| @@ -143,6 +146,21 @@ struct CopyGM2L1Routing<Version, NNLayoutPtn, NNLayoutPtn> { | |||
| 143 | using type = CopyGmToCbufScaleBNn2Nn; | 146 | using type = CopyGmToCbufScaleBNn2Nn; |
| 144 | }; | 147 | }; |
| 145 | 148 | ||
| 149 | +template <uint32_t Version> | ||
| 150 | +struct CopyGM2L1Routing<Version, NC1HWC0LayoutPtn, NC1HWC0LayoutPtn> { | ||
| 151 | + using type = CopyGmToCbufNC1HWC02NC1HWC0; | ||
| 152 | +}; | ||
| 153 | + | ||
| 154 | +template <uint32_t Version> | ||
| 155 | +struct CopyGM2L1Routing<Version, NC1HWC0LayoutPtn, NHWCLayoutPtn> { | ||
| 156 | + using type = CopyGmToCbufNHWC2NC1HWC0; | ||
| 157 | +}; | ||
| 158 | + | ||
| 159 | +template <uint32_t Version> | ||
| 160 | +struct CopyGM2L1Routing<Version, NC1HWC0LayoutPtn, NCHWLayoutPtn> { | ||
| 161 | + using type = CopyGmToCbufNCHW2NC1HWC0; | ||
| 162 | +}; | ||
| 163 | + | ||
| 146 | } // namespace Te | 164 | } // namespace Te |
| 147 | } // namespace AscendC | 165 | } // namespace AscendC |
| 148 | 166 | ||
| @@ -0,0 +1,72 @@ | |||
| 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 | + | ||
| 11 | +#if !defined(ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS) | ||
| 12 | +#warning \ | ||
| 13 | + "impl/tensor_api/arch/cube/l0c_to_gm/copy_impl/nz2nchw.h is an internal header file and must not be used directly. Functions or variables defined in this file maybe removed in the future. Please use "#include "tensor_api/tensor.h"" and use public functions or variables defined in interface headers files." | ||
| 14 | +#define ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS | ||
| 15 | +#define UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC | ||
| 16 | +#endif | ||
| 17 | + | ||
| 18 | +/*! | ||
| 19 | + * \file nz2nchw.h | ||
| 20 | + * \brief L0C(NZ) -> GM(NCHW) copy via the nz2dn fixpipe path (no quant). | ||
| 21 | + */ | ||
| 22 | +#ifndef IMPL_TENSOR_API_ARCH_CUBE_L0C_TO_GM_COPY_IMPL_NZ2NCHW_H | ||
| 23 | +#define IMPL_TENSOR_API_ARCH_CUBE_L0C_TO_GM_COPY_IMPL_NZ2NCHW_H | ||
| 24 | + | ||
| 25 | +#include "impl/tensor_api/arch/cube/l0c_to_gm/copy_impl/instruction.h" | ||
| 26 | + | ||
| 27 | +namespace AscendC { | ||
| 28 | +namespace Te { | ||
| 29 | + | ||
| 30 | +class DataCopyL0C2GMNZ2NCHW { | ||
| 31 | +public: | ||
| 32 | + template <const CopyL0C2GMTrait& trait, typename T, typename U> | ||
| 33 | + __aicore__ inline static void Run(const T& dst, const U& src, const FixpipeParams& params) | ||
| 34 | + { | ||
| 35 | + constexpr QuantMode_t quantPre = GetQuantMode<trait.roundMode, T, U>(); | ||
| 36 | + CheckDataType::CheckL0C2GmDataType<quantPre, T, U>(); | ||
| 37 | + | ||
| 38 | + auto dstLayout = dst.Layout(); | ||
| 39 | + auto srcLayout = src.Layout(); | ||
| 40 | + | ||
| 41 | + // nz2dn needs the channel register (src NZ C0 stride) set, mirroring the DN path's | ||
| 42 | + // EmitSetRegister non-ND branch. The generic SetRegisterImpl would treat NCHW as neither | ||
| 43 | + // ND nor DN and emit SetRegister(1,0,0) without SetChannelPara, so set it explicitly here. | ||
| 44 | + SetRegisterInstr::SetRegister(1u, 0u, 0u, GetElement<AttrInfo::Stride, AttrInfo::Column, 0>(srcLayout)); | ||
| 45 | + | ||
| 46 | + // dst NCHW (N, C, H, W), N == 1. NCHW memory equals DNExt (H*W, C) column-major, so this | ||
| 47 | + // mirrors the validated DN path: nSize = C = Shape[1] (column count), mSize = H*W = | ||
| 48 | + // Shape[2]*Shape[3] (row count), dstStride = H*W (the DN column stride = row count = Stride[1]). | ||
| 49 | + uint32_t nSize = Get<1>(dstLayout.Shape()); | ||
| 50 | + uint32_t mSize = Get<2>(dstLayout.Shape()) * Get<3>(dstLayout.Shape()); | ||
| 51 | + uint32_t srcStride = GetElement<AttrInfo::Stride, AttrInfo::Column, 1>(srcLayout) / FRACTAL_FIXED; | ||
| 52 | + uint32_t dstStride = Get<1>(dstLayout.Stride()); | ||
| 53 | + | ||
| 54 | + uint8_t cacheMode = dst.Engine().GetCacheMode(); | ||
| 55 | + bool reluEn = trait.enableRelu; | ||
| 56 | + uint8_t unitFlag = params.unitFlag; | ||
| 57 | + bool isChannelSplit = trait.enableChannelSplit; | ||
| 58 | + | ||
| 59 | + CopyMatrixCcToGmInstr::DataCopy<quantPre, T, U>( | ||
| 60 | + dst, src, nSize, mSize, srcStride, dstStride, cacheMode, reluEn, unitFlag, isChannelSplit, false, true); | ||
| 61 | + } | ||
| 62 | +}; | ||
| 63 | + | ||
| 64 | +} // namespace Te | ||
| 65 | +} // namespace AscendC | ||
| 66 | + | ||
| 67 | +#endif // IMPL_TENSOR_API_ARCH_CUBE_L0C_TO_GM_COPY_IMPL_NZ2NCHW_H | ||
| 68 | + | ||
| 69 | +#if defined(UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC) | ||
| 70 | +#undef ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS | ||
| 71 | +#undef UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC | ||
| 72 | +#endif | ||
| @@ -0,0 +1,66 @@ | |||
| 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 | + | ||
| 11 | +#if !defined(ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS) | ||
| 12 | +#warning \ | ||
| 13 | + "impl/tensor_api/arch/cube/l0c_to_gm/copy_impl/nz2nhwc.h is an internal header file and must not be used directly. Functions or variables defined in this file maybe removed in the future. Please use "#include "tensor_api/tensor.h"" and use public functions or variables defined in interface headers files." | ||
| 14 | +#define ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS | ||
| 15 | +#define UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC | ||
| 16 | +#endif | ||
| 17 | + | ||
| 18 | +/*! | ||
| 19 | + * \file nz2nhwc.h | ||
| 20 | + * \brief L0C(NZ) -> GM(NHWC) copy via the nz2nd fixpipe path (no quant). | ||
| 21 | + */ | ||
| 22 | +#ifndef IMPL_TENSOR_API_ARCH_CUBE_L0C_TO_GM_COPY_IMPL_NZ2NHWC_H | ||
| 23 | +#define IMPL_TENSOR_API_ARCH_CUBE_L0C_TO_GM_COPY_IMPL_NZ2NHWC_H | ||
| 24 | + | ||
| 25 | +#include "impl/tensor_api/arch/cube/l0c_to_gm/copy_impl/instruction.h" | ||
| 26 | + | ||
| 27 | +namespace AscendC { | ||
| 28 | +namespace Te { | ||
| 29 | + | ||
| 30 | +class DataCopyL0C2GMNZ2NHWC { | ||
| 31 | +public: | ||
| 32 | + template <const CopyL0C2GMTrait& trait, typename T, typename U> | ||
| 33 | + __aicore__ inline static void Run(const T& dst, const U& src, const FixpipeParams& params) | ||
| 34 | + { | ||
| 35 | + constexpr QuantMode_t quantPre = GetQuantMode<trait.roundMode, T, U>(); | ||
| 36 | + CheckDataType::CheckL0C2GmDataType<quantPre, T, U>(); | ||
| 37 | + SetRegisterImpl<T, U>(dst, src); | ||
| 38 | + | ||
| 39 | + auto dstLayout = dst.Layout(); | ||
| 40 | + auto srcLayout = src.Layout(); | ||
| 41 | + | ||
| 42 | + // dstStride = C = Stride[2] (row-major NHWC stride is (H*W*C, W*C, C, 1)). | ||
| 43 | + uint32_t nSize = Get<3>(dstLayout.Shape()); | ||
| 44 | + uint32_t mSize = Get<1>(dstLayout.Shape()) * Get<2>(dstLayout.Shape()); | ||
| 45 | + uint32_t srcStride = GetElement<AttrInfo::Stride, AttrInfo::Column, 1>(srcLayout) / FRACTAL_FIXED; | ||
| 46 | + uint32_t dstStride = Get<2>(dstLayout.Stride()); | ||
| 47 | + | ||
| 48 | + uint8_t cacheMode = dst.Engine().GetCacheMode(); | ||
| 49 | + bool reluEn = trait.enableRelu; | ||
| 50 | + uint8_t unitFlag = params.unitFlag; | ||
| 51 | + bool isChannelSplit = trait.enableChannelSplit; | ||
| 52 | + | ||
| 53 | + CopyMatrixCcToGmInstr::DataCopy<quantPre, T, U>( | ||
| 54 | + dst, src, nSize, mSize, srcStride, dstStride, cacheMode, reluEn, unitFlag, isChannelSplit, true, false); | ||
| 55 | + } | ||
| 56 | +}; | ||
| 57 | + | ||
| 58 | +} // namespace Te | ||
| 59 | +} // namespace AscendC | ||
| 60 | + | ||
| 61 | +#endif // IMPL_TENSOR_API_ARCH_CUBE_L0C_TO_GM_COPY_IMPL_NZ2NHWC_H | ||
| 62 | + | ||
| 63 | +#if defined(UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC) | ||
| 64 | +#undef ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS | ||
| 65 | +#undef UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC | ||
| 66 | +#endif | ||
| @@ -24,6 +24,8 @@ | |||
| 24 | 24 | ||
| 25 | #include "impl/tensor_api/arch/cube/utils/l0c2out_utils.h" | 25 | #include "impl/tensor_api/arch/cube/utils/l0c2out_utils.h" |
| 26 | #include "impl/tensor_api/arch/cube/l0c_to_gm/copy_impl/data_copy.h" | 26 | #include "impl/tensor_api/arch/cube/l0c_to_gm/copy_impl/data_copy.h" |
| 27 | +#include "impl/tensor_api/arch/cube/l0c_to_gm/copy_impl/nz2nchw.h" | ||
| 28 | +#include "impl/tensor_api/arch/cube/l0c_to_gm/copy_impl/nz2nhwc.h" | ||
| 27 | 29 | ||
| 28 | namespace AscendC { | 30 | namespace AscendC { |
| 29 | namespace Te { | 31 | namespace Te { |
| @@ -64,6 +66,20 @@ struct CopyL0C2GMRouting<Version, NZLayoutPtn, NZLayoutPtn> { | |||
| 64 | using type = DataCopyL0C2GM; | 66 | using type = DataCopyL0C2GM; |
| 65 | }; | 67 | }; |
| 66 | 68 | ||
| 69 | +template <uint32_t Version> | ||
| 70 | +struct CopyL0C2GMRouting<Version, NC1HWC0LayoutPtn, NZLayoutPtn> { | ||
| 71 | + using type = DataCopyL0C2GM; | ||
| 72 | +}; | ||
| 73 | + | ||
| 74 | +template <uint32_t Version> | ||
| 75 | +struct CopyL0C2GMRouting<Version, NHWCLayoutPtn, NZLayoutPtn> { | ||
| 76 | + using type = DataCopyL0C2GMNZ2NHWC; | ||
| 77 | +}; | ||
| 78 | + | ||
| 79 | +template <uint32_t Version> | ||
| 80 | +struct CopyL0C2GMRouting<Version, NCHWLayoutPtn, NZLayoutPtn> { | ||
| 81 | + using type = DataCopyL0C2GMNZ2NCHW; | ||
| 82 | +}; | ||
| 67 | } // namespace Te | 83 | } // namespace Te |
| 68 | } // namespace AscendC | 84 | } // namespace AscendC |
| 69 | 85 | ||
| @@ -38,19 +38,12 @@ using CopyL12L0AModeSet = TupleMap< | |||
| 38 | Std::tuple<Std::tuple<_1, _0>, CopyMode::NORMAL>, Std::tuple<Std::tuple<_1, _1>, CopyMode::NORMAL>, | 38 | Std::tuple<Std::tuple<_1, _0>, CopyMode::NORMAL>, Std::tuple<Std::tuple<_1, _1>, CopyMode::NORMAL>, |
| 39 | Std::tuple<Std::tuple<_0, _0>, CopyMode::TRANS>, Std::tuple<Std::tuple<_0, _1>, CopyMode::TRANS_B8B4>>; | 39 | Std::tuple<Std::tuple<_0, _0>, CopyMode::TRANS>, Std::tuple<Std::tuple<_0, _1>, CopyMode::TRANS_B8B4>>; |
| 40 | 40 | ||
| 41 | -struct CopyL12L0A { | 41 | +struct CopyL12L0ABase { |
| 42 | public: | 42 | public: |
| 43 | - template <typename Tp, const Tp& traits, typename... Args> | 43 | + template < |
| 44 | - __aicore__ inline static void Copy(const Args&... args) | 44 | + const CopyL12L0ATrait& trait = DEFAULT_COPY_L1_TO_L0A_TRAIT, typename T, typename U, typename PadT = int16_t> |
| 45 | - { | 45 | + __aicore__ inline static void LoadDataImpl( |
| 46 | - if ASCEND_IS_AIC { | 46 | + const T& dst, const U& src, const Img2ColParams<PadT>& params = DEFAULT_IMG2COL_PARAMS) |
| 47 | - LoadData<traits, Args...>(args...); | ||
| 48 | - } | ||
| 49 | - } | ||
| 50 | - | ||
| 51 | -private: | ||
| 52 | - template <const CopyL12L0ATrait& trait = DEFAULT_COPY_L1_TO_L0A_TRAIT, typename T, typename U> | ||
| 53 | - __aicore__ inline static void LoadData(const T& dst, const U& src) | ||
| 54 | { | 47 | { |
| 55 | using dstPos = GetMemLocation<T>; | 48 | using dstPos = GetMemLocation<T>; |
| 56 | using srcPos = GetMemLocation<U>; | 49 | using srcPos = GetMemLocation<U>; |
| @@ -62,14 +55,44 @@ private: | |||
| 62 | using SrcLayout = typename U::layoutType; | 55 | using SrcLayout = typename U::layoutType; |
| 63 | using DstPattern = GetLayoutPattern<DstLayout>; | 56 | using DstPattern = GetLayoutPattern<DstLayout>; |
| 64 | using SrcPattern = GetLayoutPattern<SrcLayout>; | 57 | using SrcPattern = GetLayoutPattern<SrcLayout>; |
| 65 | - constexpr auto isB8B4Type = sizeof(typename T::elementType) == 1; | 58 | + if constexpr (Std::is_same_v<SrcPattern, NC1HWC0LayoutPtn>) { |
| 66 | - constexpr auto noTrans = Std::is_same_v<DstPattern, SrcPattern>; | 59 | + // Img2Col: NZ<-NC1HWC0 for conv feature maps; params drives filter/stride/pad. |
| 67 | - using CopyL12L0AMode = | 60 | + using CopyL12L0AImpl = |
| 68 | - typename CopyL12L0AModeSet::template Get<Std::tuple<Std::Int<noTrans>, Std::Int<isB8B4Type>>>; | 61 | + typename CopyL12L0ARouting<CURRENT_ARCH_VERSION, DstPattern, SrcPattern, CopyMode::NORMAL>::type; |
| 69 | - static_assert(!Std::is_same_v<CopyL12L0AMode, Std::ignore_t>, "Unsupported CopyL12L0AMode."); | 62 | + CopyL12L0AImpl::template Run<trait, T, U, PadT>(dst, src, params); |
| 70 | - using CopyL12L0AImpl = | 63 | + } else { |
| 71 | - typename CopyL12L0ARouting<CURRENT_ARCH_VERSION, DstPattern, SrcPattern, CopyL12L0AMode>::type; | 64 | + // Common load (NZ<-NZ / NZ<-ZN): params unused. |
| 72 | - CopyL12L0AImpl::template Run<trait, T, U>(dst, src); | 65 | + constexpr auto isB8B4Type = sizeof(typename T::elementType) == 1; |
| 66 | + constexpr auto noTrans = Std::is_same_v<DstPattern, SrcPattern>; | ||
| 67 | + using CopyL12L0AMode = | ||
| 68 | + typename CopyL12L0AModeSet::template Get<Std::tuple<Std::Int<noTrans>, Std::Int<isB8B4Type>>>; | ||
| 69 | + static_assert(!Std::is_same_v<CopyL12L0AMode, Std::ignore_t>, "Unsupported CopyL12L0AMode."); | ||
| 70 | + using CopyL12L0AImpl = | ||
| 71 | + typename CopyL12L0ARouting<CURRENT_ARCH_VERSION, DstPattern, SrcPattern, CopyL12L0AMode>::type; | ||
| 72 | + CopyL12L0AImpl::template Run<trait, T, U>(dst, src); | ||
| 73 | + } | ||
| 74 | + } | ||
| 75 | +}; | ||
| 76 | + | ||
| 77 | +struct CopyL12L0A : public CopyL12L0ABase { | ||
| 78 | +public: | ||
| 79 | + template <typename Tp, const Tp& trait, typename... Args> | ||
| 80 | + __aicore__ inline static void Copy(const Args&... args) | ||
| 81 | + { | ||
| 82 | + if ASCEND_IS_AIC { | ||
| 83 | + LoadDataImpl<trait>(args...); | ||
| 84 | + } | ||
| 85 | + } | ||
| 86 | +}; | ||
| 87 | + | ||
| 88 | +struct CopyL12L0AWith : public CopyL12L0ABase { | ||
| 89 | +public: | ||
| 90 | + template <typename Tp, const Tp& trait, typename... Args> | ||
| 91 | + __aicore__ inline static void Copy(const Args&... args) | ||
| 92 | + { | ||
| 93 | + if ASCEND_IS_AIC { | ||
| 94 | + LoadDataImpl<trait>(args...); | ||
| 95 | + } | ||
| 73 | } | 96 | } |
| 74 | }; | 97 | }; |
| 75 | 98 | ||
| @@ -0,0 +1,65 @@ | |||
| 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 | + | ||
| 11 | +#if !defined(ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS) | ||
| 12 | +#warning \ | ||
| 13 | + "impl/tensor_api/arch/cube/l1_to_l0a/copy_impl/img2col.h is an internal header file and must not be used directly. Functions or variables defined in this file maybe removed in the future. Please use "#include "tensor_api/tensor.h"" and use public functions or variables defined in interface headers files." | ||
| 14 | +#define ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS | ||
| 15 | +#define UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC | ||
| 16 | +#endif | ||
| 17 | + | ||
| 18 | +/*! | ||
| 19 | + * \file img2col.h | ||
| 20 | + * \brief | ||
| 21 | + */ | ||
| 22 | +#ifndef IMPL_TENSOR_API_ARCH_CUBE_L1_TO_L0A_COPY_IMPL_IMG2COL_H | ||
| 23 | +#define IMPL_TENSOR_API_ARCH_CUBE_L1_TO_L0A_COPY_IMPL_IMG2COL_H | ||
| 24 | + | ||
| 25 | +#include "impl/tensor_api/arch/cube/l1_to_l0a/copy_impl/instruction.h" | ||
| 26 | + | ||
| 27 | +namespace AscendC { | ||
| 28 | +namespace Te { | ||
| 29 | +class LoadDataL12L0AImg2Col { | ||
| 30 | +public: | ||
| 31 | + template <const CopyL12L0ATrait& trait, typename DstTensor, typename SrcTensor, typename PadT> | ||
| 32 | + __aicore__ inline static void Run(const DstTensor& dst, const SrcTensor& src, const Img2ColParams<PadT>& params) | ||
| 33 | + { | ||
| 34 | + // PadT (the padValue type) should match the L0A dst element type so the padding register bit | ||
| 35 | + // pattern is correct; the caller picks it when constructing Img2ColParams<PadT>. | ||
| 36 | + auto dstLayout = dst.Layout(); | ||
| 37 | + auto srcLayout = src.Layout(); | ||
| 38 | + | ||
| 39 | + uint16_t kStartPt = 0; | ||
| 40 | + uint16_t mStartPt = 0; | ||
| 41 | + | ||
| 42 | + // src L1 NC1HWC0 (N, C1, H, W, C0): l1H/l1W are the feature-map dims, channelSize = C1*C0. | ||
| 43 | + uint16_t l1H = Get<2>(srcLayout.Shape()); | ||
| 44 | + uint16_t l1W = Get<3>(srcLayout.Shape()); | ||
| 45 | + uint16_t channelSize = Get<1>(srcLayout.Shape()) * Get<4>(srcLayout.Shape()); | ||
| 46 | + | ||
| 47 | + LoadCbufToCaImg2Col::SetFMatrix(l1H, l1W, params.padList); | ||
| 48 | + LoadCbufToCaImg2Col::SetPadding(params.padValue); | ||
| 49 | + LoadCbufToCaImg2Col::SetRepeat(static_cast<uint16_t>(Std::ceil_division(params.mExtension, FRACTAL_FIXED))); | ||
| 50 | + LoadCbufToCaImg2Col::LoadData( | ||
| 51 | + dst, src, params.kExtension, params.mExtension, kStartPt, mStartPt, params.strideW, params.strideH, | ||
| 52 | + params.filterW, params.filterH, params.dilationFilterW, params.dilationFilterH, params.filterSizeW, | ||
| 53 | + params.filterSizeH, params.transpose, params.fMatrixCtrl, channelSize); | ||
| 54 | + } | ||
| 55 | +}; | ||
| 56 | + | ||
| 57 | +} // namespace Te | ||
| 58 | +} // namespace AscendC | ||
| 59 | + | ||
| 60 | +#endif // IMPL_TENSOR_API_ARCH_CUBE_L1_TO_L0A_COPY_IMPL_IMG2COL_H | ||
| 61 | + | ||
| 62 | +#if defined(UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC) | ||
| 63 | +#undef ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS | ||
| 64 | +#undef UNDEF_ASCENDC_TENSOR_API_INCLUDE_COMPILER_INTERNAL_HEADERS_ASCENDC | ||
| 65 | +#endif | ||
| @@ -31,6 +31,8 @@ namespace Te { | |||
| 31 | 31 | ||
| 32 | struct CopyL12L0ATrait {}; | 32 | struct CopyL12L0ATrait {}; |
| 33 | 33 | ||
| 34 | +constexpr Img2ColParams<int16_t> DEFAULT_IMG2COL_PARAMS{}; | ||
| 35 | + | ||
| 34 | class LoadCbufToCa { | 36 | class LoadCbufToCa { |
| 35 | public: | 37 | public: |
| 36 | template <bool transpose, typename T, typename U, typename... Params> | 38 | template <bool transpose, typename T, typename U, typename... Params> |
| @@ -56,6 +58,68 @@ private: | |||
| 56 | } | 58 | } |
| 57 | }; | 59 | }; |
| 58 | 60 | ||
| 61 | +class LoadCbufToCaImg2Col { | ||
| 62 | +public: | ||
| 63 | + __aicore__ inline static void SetFMatrix(uint16_t l1H, uint16_t l1W, const uint8_t padList[4]) | ||
| 64 | + { | ||
| 65 | + if ASCEND_IS_AIV { | ||
| 66 | + return; | ||
| 67 | + } | ||
| 68 | + asc_l13d_fmatrix_config config; | ||
| 69 | + config.l1_height = l1H; | ||
| 70 | + config.l1_width = l1W; | ||
| 71 | + config.padding_left_size = padList[0]; | ||
| 72 | + config.padding_right_size = padList[1]; | ||
| 73 | + config.padding_top_size = padList[2]; | ||
| 74 | + config.padding_bottom_size = padList[3]; | ||
| 75 | + asc_set_l13d_fmatrix(config); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + template <typename T> | ||
| 79 | + __aicore__ inline static void SetPadding(T padValue) | ||
| 80 | + { | ||
| 81 | + if ASCEND_IS_AIV { | ||
| 82 | + return; | ||
| 83 | + } | ||
| 84 | + asc_set_l13d_padding(padValue); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + __aicore__ inline static void SetRepeat(uint16_t dstStride) | ||
| 88 | + { | ||
| 89 | + if ASCEND_IS_AIV { | ||
| 90 | + return; | ||
| 91 | + } | ||
| 92 | + asc_load3d_v2_config config; | ||
| 93 | + config.rpt_stride = 0; | ||
| 94 | + config.rpt_time = 1; | ||
| 95 | + config.rpt_mode = 0; | ||
| 96 | + config.config |= static_cast<uint64_t>(dstStride) << 32; | ||
| 97 | + asc_set_l13d_rpt(config); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + template <typename T, typename U, typename... Params> | ||
| 101 | + __aicore__ inline static void LoadData(const T& dst, const U& src, const Params&... params) | ||
| 102 | + { | ||
| 103 | + LoadCbufToCaImg2ColImpl(dst.Data().Get(), src.Data().Get(), params...); | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | +private: | ||
| 107 | + template <typename T> | ||
| 108 | + __aicore__ inline static void LoadCbufToCaImg2ColImpl( | ||
| 109 | + __ca__ T* dst, __cbuf__ T* src, uint16_t kExtension, uint16_t mExtension, uint16_t kStartPt, uint16_t mStartPt, | ||
| 110 | + uint8_t strideW, uint8_t strideH, uint8_t filterW, uint8_t filterH, uint8_t dilationFilterW, | ||
| 111 | + uint8_t dilationFilterH, bool filterSizeW, bool filterSizeH, bool transpose, bool fMatrixCtrl, | ||
| 112 | + uint16_t channelSize) | ||
| 113 | + { | ||
| 114 | + if ASCEND_IS_AIV { | ||
| 115 | + return; | ||
| 116 | + } | ||
| 117 | + asc_copy_l12l0a( | ||
| 118 | + dst, src, kExtension, mExtension, kStartPt, mStartPt, strideW, strideH, filterW, filterH, dilationFilterW, | ||
| 119 | + dilationFilterH, filterSizeW, filterSizeH, transpose, fMatrixCtrl, channelSize); | ||
| 120 | + } | ||
| 121 | +}; | ||
| 122 | + | ||
| 59 | } // namespace Te | 123 | } // namespace Te |
| 60 | } // namespace AscendC | 124 | } // namespace AscendC |
| 61 | 125 | ||
| @@ -26,6 +26,7 @@ | |||
| 26 | #include "impl/tensor_api/arch/cube/l1_to_l0a/copy_impl/nz2nz.h" | 26 | #include "impl/tensor_api/arch/cube/l1_to_l0a/copy_impl/nz2nz.h" |
| 27 | #include "impl/tensor_api/arch/cube/l1_to_l0a/copy_impl/zn2nz.h" | 27 | #include "impl/tensor_api/arch/cube/l1_to_l0a/copy_impl/zn2nz.h" |
| 28 | #include "impl/tensor_api/arch/cube/l1_to_l0a/copy_impl/zn2nzb8b4.h" | 28 | #include "impl/tensor_api/arch/cube/l1_to_l0a/copy_impl/zn2nzb8b4.h" |
| 29 | +#include "impl/tensor_api/arch/cube/l1_to_l0a/copy_impl/img2col.h" | ||
| 29 | 30 | ||
| 30 | namespace AscendC { | 31 | namespace AscendC { |
| 31 | namespace Te { | 32 | namespace Te { |
| @@ -59,6 +60,13 @@ struct CopyL12L0ARouting<Version, NZLayoutPtn, ZNLayoutPtn, CopyMode::TRANS_B8B4 | |||
| 59 | using type = LoadDataL12L0AZN2NZB8B4; | 60 | using type = LoadDataL12L0AZN2NZB8B4; |
| 60 | }; | 61 | }; |
| 61 | 62 | ||
| 63 | +// Img2Col: L1(NC1HWC0) -> L0A(NZ) for conv feature maps. Dispatched when src carries the | ||
| 64 | +// NC1HWC0LayoutPtn tag and params is bound via CopyL12L0A.with(Img2ColParams). | ||
| 65 | +template <uint32_t Version> | ||
| 66 | +struct CopyL12L0ARouting<Version, NZLayoutPtn, NC1HWC0LayoutPtn, CopyMode::NORMAL> { | ||
| 67 | + using type = LoadDataL12L0AImg2Col; | ||
| 68 | +}; | ||
| 69 | + | ||
| 62 | } // namespace Te | 70 | } // namespace Te |
| 63 | } // namespace AscendC | 71 | } // namespace AscendC |
| 64 | #endif // IMPL_TENSOR_API_ARCH_CUBE_L1_TO_L0A_ROUTING_H | 72 | #endif // IMPL_TENSOR_API_ARCH_CUBE_L1_TO_L0A_ROUTING_H |
| @@ -53,6 +53,12 @@ inline constexpr bool IsL0COutDNFormatV = | |||
| 53 | template <typename TensorType> | 53 | template <typename TensorType> |
| 54 | inline constexpr bool IsL0COutNZFormatV = IsSatisfiedPtnFormatV<TensorType, NZLayoutPtn>; | 54 | inline constexpr bool IsL0COutNZFormatV = IsSatisfiedPtnFormatV<TensorType, NZLayoutPtn>; |
| 55 | 55 | ||
| 56 | +// NC1HWC0 (N, C1, H, W, C0): a flat 5-dim layout with no pattern tag (depth 5 AND rank 5). Batched | ||
| 57 | +// NZ/ND/DN dst layouts are also depth 5 but nest to rank 2, so the rank check keeps them apart. | ||
| 58 | +template <typename TensorType> | ||
| 59 | +inline constexpr bool IsL0COutNC1HWC0V = | ||
| 60 | + TensorType::layoutType::depth == FIVE_DIM_DATA && TensorType::layoutType::rank == FIVE_DIM_DATA; | ||
| 61 | + | ||
| 56 | template <typename DstTensorType, typename SrcTensorType> | 62 | template <typename DstTensorType, typename SrcTensorType> |
| 57 | inline constexpr bool IsL0COutBatchNZ2NZV = | 63 | inline constexpr bool IsL0COutBatchNZ2NZV = |
| 58 | IsL0COutNZFormatV<DstTensorType> && IsL0COutNZFormatV<SrcTensorType> && | 64 | IsL0COutNZFormatV<DstTensorType> && IsL0COutNZFormatV<SrcTensorType> && |
| @@ -89,7 +95,17 @@ template <typename T, typename U, typename DstLayout, typename SrcLayout> | |||
| 89 | __aicore__ inline static constexpr L0COutCopyParams MakeL0COutCopyParams( | 95 | __aicore__ inline static constexpr L0COutCopyParams MakeL0COutCopyParams( |
| 90 | const DstLayout& dstLayout, const SrcLayout& srcLayout) | 96 | const DstLayout& dstLayout, const SrcLayout& srcLayout) |
| 91 | { | 97 | { |
| 92 | - if constexpr (IsL0COutBatchNZ2NZV<T, U>) { | 98 | + if constexpr (IsL0COutNC1HWC0V<T>) { |
| 99 | + // dst NC1HWC0 (N, C1, H, W, C0), N == 1: nSize = Cout = C1*C0, mSize = Ho*Wo = H*W. | ||
| 100 | + // dstStride is the C1-axis stride (= H*W*C0), i.e. the C0-block step without the NZ 16-row | ||
| 101 | + // alignment padding. src is the non-batch NZ, so srcStride follows the plain NZ path. | ||
| 102 | + auto dstShape = dstLayout.Shape(); | ||
| 103 | + return { | ||
| 104 | + static_cast<uint32_t>(Get<1>(dstShape) * Get<4>(dstShape)), | ||
| 105 | + static_cast<uint32_t>(Get<2>(dstShape) * Get<3>(dstShape)), | ||
| 106 | + static_cast<uint32_t>(GetElement<AttrInfo::Stride, AttrInfo::Column, 1>(srcLayout) / FRACTAL_FIXED), | ||
| 107 | + static_cast<uint32_t>(Get<1>(dstLayout.Stride()))}; | ||
| 108 | + } else if constexpr (IsL0COutBatchNZ2NZV<T, U>) { | ||
| 93 | auto srcNoBatchLayout = RemoveBatchDim(srcLayout); | 109 | auto srcNoBatchLayout = RemoveBatchDim(srcLayout); |
| 94 | auto dstNoBatchLayout = RemoveBatchDim(dstLayout); | 110 | auto dstNoBatchLayout = RemoveBatchDim(dstLayout); |
| 95 | return { | 111 | return { |
| @@ -29,11 +29,47 @@ namespace AscendC { | |||
| 29 | namespace Te { | 29 | namespace Te { |
| 30 | 30 | ||
| 31 | template <typename Traits> | 31 | template <typename Traits> |
| 32 | -struct CopyTraits<CopyL12L0A, Traits> : public CopyTraits<CopyL12L0A, Traits, CopyL12L0A, Traits> {}; | 32 | +struct CopyTraits<CopyL12L0A, Traits> : public CopyTraits<CopyL12L0A, Traits, CopyL12L0AWith, Traits> { |
| 33 | + using BaseType = CopyTraits<CopyL12L0A, Traits, CopyL12L0AWith, Traits>; | ||
| 34 | + using TraitType = typename BaseType::TraitType; | ||
| 35 | + using BaseType::with; | ||
| 36 | + | ||
| 37 | + template <typename PadT> | ||
| 38 | + __aicore__ inline constexpr auto with(const Img2ColParams<PadT>& params) const | ||
| 39 | + { | ||
| 40 | + return CopyTraits<CopyL12L0AWith, Traits, PadT>{params}; | ||
| 41 | + } | ||
| 42 | +}; | ||
| 33 | 43 | ||
| 34 | template <> | 44 | template <> |
| 35 | struct CopyTraits<CopyL12L0A> : public CopyTraits<CopyL12L0A, CopyL12L0ATraitDefault> {}; | 45 | struct CopyTraits<CopyL12L0A> : public CopyTraits<CopyL12L0A, CopyL12L0ATraitDefault> {}; |
| 36 | 46 | ||
| 47 | +template <typename TraitStruct, typename PadT> | ||
| 48 | +struct CopyTraits<CopyL12L0AWith, TraitStruct, PadT> { | ||
| 49 | + using TraitType = typename TraitStruct::TraitType; | ||
| 50 | + static constexpr const TraitType defaultTrait = TraitStruct::value; | ||
| 51 | + | ||
| 52 | + template <const TraitType& trait = defaultTrait, typename... Args> | ||
| 53 | + __aicore__ inline void CopyUnpack(const Args&... args) const | ||
| 54 | + { | ||
| 55 | + CopyL12L0AWith::Copy<TraitType, trait, Args...>(args..., params); | ||
| 56 | + } | ||
| 57 | + Img2ColParams<PadT> params; | ||
| 58 | +}; | ||
| 59 | + | ||
| 60 | +template <typename TraitStruct> | ||
| 61 | +struct CopyTraits<CopyL12L0AWith, TraitStruct> { | ||
| 62 | + using TraitType = typename TraitStruct::TraitType; | ||
| 63 | + static constexpr const TraitType defaultTrait = TraitStruct::value; | ||
| 64 | + | ||
| 65 | + template <const TraitType& trait = defaultTrait, typename... Args> | ||
| 66 | + __aicore__ inline void CopyUnpack(const Args&... args) const | ||
| 67 | + { | ||
| 68 | + CopyL12L0AWith::Copy<TraitType, trait, Args...>(args..., params); | ||
| 69 | + } | ||
| 70 | + Img2ColParams<int16_t> params{}; | ||
| 71 | +}; | ||
| 72 | + | ||
| 37 | } // namespace Te | 73 | } // namespace Te |
| 38 | } // namespace AscendC | 74 | } // namespace AscendC |
| 39 | 75 | ||
| @@ -33,7 +33,9 @@ using LayoutFormatSet = TupleMap< | |||
| 33 | Std::tuple<DNExtLayoutPtn, MakeDNExtFrameLayout>, Std::tuple<NNLayoutPtn, MakeNnFrameLayout>, | 33 | Std::tuple<DNExtLayoutPtn, MakeDNExtFrameLayout>, Std::tuple<NNLayoutPtn, MakeNnFrameLayout>, |
| 34 | Std::tuple<ZZLayoutPtn, MakeZzFrameLayout>, Std::tuple<ZNLayoutPtn, MakeZnFrameLayout>, | 34 | Std::tuple<ZZLayoutPtn, MakeZzFrameLayout>, Std::tuple<ZNLayoutPtn, MakeZnFrameLayout>, |
| 35 | Std::tuple<ScaleANDLayoutPtn, MakeScaleANDFrameLayout>, Std::tuple<ScaleADNLayoutPtn, MakeScaleADNFrameLayout>, | 35 | Std::tuple<ScaleANDLayoutPtn, MakeScaleANDFrameLayout>, Std::tuple<ScaleADNLayoutPtn, MakeScaleADNFrameLayout>, |
| 36 | - Std::tuple<ScaleBNDLayoutPtn, MakeScaleBNDFrameLayout>, Std::tuple<ScaleBDNLayoutPtn, MakeScaleBDNFrameLayout>>; | 36 | + Std::tuple<ScaleBNDLayoutPtn, MakeScaleBNDFrameLayout>, Std::tuple<ScaleBDNLayoutPtn, MakeScaleBDNFrameLayout>, |
| 37 | + Std::tuple<NCHWLayoutPtn, MakeNCHWFrameLayout>, Std::tuple<NHWCLayoutPtn, MakeNHWCFrameLayout>, | ||
| 38 | + Std::tuple<NC1HWC0LayoutPtn, MakeNC1HWC0FrameLayout>>; | ||
| 37 | 39 | ||
| 38 | template <typename T, typename C0> | 40 | template <typename T, typename C0> |
| 39 | struct LayoutTrait { | 41 | struct LayoutTrait { |
| @@ -55,7 +57,10 @@ using FormatTraitSet = TupleMap< | |||
| 55 | Std::tuple<ScaleANDLayoutPtn, LayoutTrait<Std::ignore_t, _2>>, | 57 | Std::tuple<ScaleANDLayoutPtn, LayoutTrait<Std::ignore_t, _2>>, |
| 56 | Std::tuple<ScaleADNLayoutPtn, LayoutTrait<Std::ignore_t, _2>>, | 58 | Std::tuple<ScaleADNLayoutPtn, LayoutTrait<Std::ignore_t, _2>>, |
| 57 | Std::tuple<ScaleBNDLayoutPtn, LayoutTrait<Std::ignore_t, _2>>, | 59 | Std::tuple<ScaleBNDLayoutPtn, LayoutTrait<Std::ignore_t, _2>>, |
| 58 | - Std::tuple<ScaleBDNLayoutPtn, LayoutTrait<Std::ignore_t, _2>>>; | 60 | + Std::tuple<ScaleBDNLayoutPtn, LayoutTrait<Std::ignore_t, _2>>, |
| 61 | + Std::tuple<NCHWLayoutPtn, LayoutTrait<Std::ignore_t, _1>>, | ||
| 62 | + Std::tuple<NHWCLayoutPtn, LayoutTrait<Std::ignore_t, _1>>, | ||
| 63 | + Std::tuple<NC1HWC0LayoutPtn, LayoutTrait<Std::ignore_t, _1>>>; | ||
| 59 | 64 | ||
| 60 | template <typename T, typename = void> | 65 | template <typename T, typename = void> |
| 61 | struct IsFrameLayoutTrait : Std::false_type {}; | 66 | struct IsFrameLayoutTrait : Std::false_type {}; |
| @@ -284,6 +284,48 @@ struct MakeScaleBDNFrameLayout { | |||
| 284 | } | 284 | } |
| 285 | }; | 285 | }; |
| 286 | 286 | ||
| 287 | +// Convolution feature-map patterns. These are flat (non-fractal) row-major layouts: shape carries | ||
| 288 | +// the logical dims in their natural order, stride is the row-major contiguous product (last dim = 1, | ||
| 289 | +// each earlier dim = product of all later dims). C0 is supplied by the caller, not derived from the | ||
| 290 | +// trait, so the stride stays a plain contiguous layout regardless of element type. | ||
| 291 | + | ||
| 292 | +struct NCHWLayoutPtn {}; | ||
| 293 | +struct NHWCLayoutPtn {}; | ||
| 294 | +struct NC1HWC0LayoutPtn {}; | ||
pattern相关对外,include ![]() ![]() | |||
| 295 | + | ||
| 296 | +struct MakeNCHWFrameLayout { | ||
| 297 | + template <typename TraitType, typename N, typename C, typename H, typename W> | ||
| 298 | + __aicore__ inline static auto Make(N n, C c, H h, W w) | ||
| 299 | + { | ||
| 300 | + auto shape = MakeShape(n, c, h, w); | ||
| 301 | + auto stride = MakeStride(c * h * w, h * w, w, _1{}); | ||
| 302 | + using LayoutT = Layout<decltype(shape), decltype(stride), Std::tuple<NCHWLayoutPtn, TraitType>>; | ||
| 303 | + return LayoutT(shape, stride); | ||
| 304 | + } | ||
| 305 | +}; | ||
| 306 | + | ||
| 307 | +struct MakeNHWCFrameLayout { | ||
| 308 | + template <typename TraitType, typename N, typename H, typename W, typename C> | ||
| 309 | + __aicore__ inline static auto Make(N n, H h, W w, C c) | ||
| 310 | + { | ||
| 311 | + auto shape = MakeShape(n, h, w, c); | ||
| 312 | + auto stride = MakeStride(h * w * c, w * c, c, _1{}); | ||
| 313 | + using LayoutT = Layout<decltype(shape), decltype(stride), Std::tuple<NHWCLayoutPtn, TraitType>>; | ||
| 314 | + return LayoutT(shape, stride); | ||
| 315 | + } | ||
| 316 | +}; | ||
| 317 | + | ||
| 318 | +struct MakeNC1HWC0FrameLayout { | ||
| 319 | + template <typename TraitType, typename N, typename C1, typename H, typename W, typename C0> | ||
| 320 | + __aicore__ inline static auto Make(N n, C1 c1, H h, W w, C0 c0) | ||
| 321 | + { | ||
| 322 | + auto shape = MakeShape(n, c1, h, w, c0); | ||
| 323 | + auto stride = MakeStride(c1 * h * w * c0, h * w * c0, w * c0, c0, _1{}); | ||
| 324 | + using LayoutT = Layout<decltype(shape), decltype(stride), Std::tuple<NC1HWC0LayoutPtn, TraitType>>; | ||
| 325 | + return LayoutT(shape, stride); | ||
| 326 | + } | ||
| 327 | +}; | ||
| 328 | + | ||
| 287 | } // namespace Te | 329 | } // namespace Te |
| 288 | } // namespace AscendC | 330 | } // namespace AscendC |
| 289 | 331 | ||
| @@ -79,6 +79,24 @@ struct FixpipeParams { | |||
| 79 | bool subBlockId = false; | 79 | bool subBlockId = false; |
| 80 | }; | 80 | }; |
| 81 | 81 | ||
| 82 | +template <typename T> | ||
| 83 | +struct Img2ColParams { | ||
| 84 | + uint16_t mExtension = 0; | ||
| 85 | + uint16_t kExtension = 0; | ||
| 86 | + uint8_t padList[4] = {0, 0, 0, 0}; | ||
| 87 | + uint8_t strideW = 1; | ||
| 88 | + uint8_t strideH = 1; | ||
| 89 | + uint8_t filterW = 1; | ||
| 90 | + uint8_t filterH = 1; | ||
| 91 | + uint8_t dilationFilterW = 1; | ||
| 92 | + uint8_t dilationFilterH = 1; | ||
| 93 | + bool filterSizeW = false; | ||
| 94 | + bool filterSizeH = false; | ||
| 95 | + bool transpose = false; | ||
| 96 | + bool fMatrixCtrl = false; | ||
enable_xxx定义 ![]() ![]() | |||
| 97 | + T padValue = 0; | ||
| 98 | +}; | ||
| 99 | + | ||
| 82 | } // namespace Te | 100 | } // namespace Te |
| 83 | } // namespace AscendC | 101 | } // namespace AscendC |
| 84 | 102 | ||
| @@ -521,3 +521,46 @@ TEST_F(Tensor_Api_Frame_Layout, ScaleAndZzNnBatchFrameLayout) | |||
| 521 | EXPECT_EQ(AscendC::Std::get<0>(AscendC::Std::get<1>(GetShape<1>(zz))), 2); | 521 | EXPECT_EQ(AscendC::Std::get<0>(AscendC::Std::get<1>(GetShape<1>(zz))), 2); |
| 522 | EXPECT_EQ(AscendC::Std::get<1>(AscendC::Std::get<1>(GetShape<1>(zz))), 32); | 522 | EXPECT_EQ(AscendC::Std::get<1>(AscendC::Std::get<1>(GetShape<1>(zz))), 32); |
| 523 | } | 523 | } |
| 524 | + | ||
| 525 | +TEST_F(Tensor_Api_Frame_Layout, ConvFeatureMapLayouts) | ||
| 526 | +{ | ||
| 527 | + using namespace AscendC::Te; | ||
| 528 | + | ||
| 529 | + // NCHW(N,C,H,W): row-major contiguous stride (C*H*W, H*W, W, 1). | ||
| 530 | + auto nchw = MakeFrameLayout<NCHWLayoutPtn>(2, 3, 4, 5); | ||
| 531 | + EXPECT_EQ(AscendC::Std::get<0>(GetShape(nchw)), 2); | ||
| 532 | + EXPECT_EQ(AscendC::Std::get<1>(GetShape(nchw)), 3); | ||
| 533 | + EXPECT_EQ(AscendC::Std::get<2>(GetShape(nchw)), 4); | ||
| 534 | + EXPECT_EQ(AscendC::Std::get<3>(GetShape(nchw)), 5); | ||
| 535 | + EXPECT_EQ(AscendC::Std::get<0>(GetStride(nchw)), 60); | ||
| 536 | + EXPECT_EQ(AscendC::Std::get<1>(GetStride(nchw)), 20); | ||
| 537 | + EXPECT_EQ(AscendC::Std::get<2>(GetStride(nchw)), 5); | ||
| 538 | + EXPECT_EQ(AscendC::Std::get<3>(GetStride(nchw)), 1); | ||
| 539 | + static_assert(AscendC::Std::is_same_v<GetLayoutPattern<decltype(nchw)>, NCHWLayoutPtn>); | ||
| 540 | + | ||
| 541 | + // NHWC(N,H,W,C): row-major contiguous stride (H*W*C, W*C, C, 1). | ||
| 542 | + auto nhwc = MakeFrameLayout<NHWCLayoutPtn>(2, 4, 5, 3); | ||
| 543 | + EXPECT_EQ(AscendC::Std::get<0>(GetShape(nhwc)), 2); | ||
| 544 | + EXPECT_EQ(AscendC::Std::get<1>(GetShape(nhwc)), 4); | ||
| 545 | + EXPECT_EQ(AscendC::Std::get<2>(GetShape(nhwc)), 5); | ||
| 546 | + EXPECT_EQ(AscendC::Std::get<3>(GetShape(nhwc)), 3); | ||
| 547 | + EXPECT_EQ(AscendC::Std::get<0>(GetStride(nhwc)), 60); | ||
| 548 | + EXPECT_EQ(AscendC::Std::get<1>(GetStride(nhwc)), 15); | ||
| 549 | + EXPECT_EQ(AscendC::Std::get<2>(GetStride(nhwc)), 3); | ||
| 550 | + EXPECT_EQ(AscendC::Std::get<3>(GetStride(nhwc)), 1); | ||
| 551 | + static_assert(AscendC::Std::is_same_v<GetLayoutPattern<decltype(nhwc)>, NHWCLayoutPtn>); | ||
| 552 | + | ||
| 553 | + // NC1HWC0(N,C1,H,W,C0): C0 supplied by caller, row-major contiguous stride. | ||
| 554 | + auto nc1hwc0 = MakeFrameLayout<NC1HWC0LayoutPtn>(2, 3, 4, 5, 16); | ||
| 555 | + EXPECT_EQ(AscendC::Std::get<0>(GetShape(nc1hwc0)), 2); | ||
| 556 | + EXPECT_EQ(AscendC::Std::get<1>(GetShape(nc1hwc0)), 3); | ||
| 557 | + EXPECT_EQ(AscendC::Std::get<2>(GetShape(nc1hwc0)), 4); | ||
| 558 | + EXPECT_EQ(AscendC::Std::get<3>(GetShape(nc1hwc0)), 5); | ||
| 559 | + EXPECT_EQ(AscendC::Std::get<4>(GetShape(nc1hwc0)), 16); | ||
| 560 | + EXPECT_EQ(AscendC::Std::get<0>(GetStride(nc1hwc0)), 960); | ||
| 561 | + EXPECT_EQ(AscendC::Std::get<1>(GetStride(nc1hwc0)), 320); | ||
| 562 | + EXPECT_EQ(AscendC::Std::get<2>(GetStride(nc1hwc0)), 80); | ||
| 563 | + EXPECT_EQ(AscendC::Std::get<3>(GetStride(nc1hwc0)), 16); | ||
| 564 | + EXPECT_EQ(AscendC::Std::get<4>(GetStride(nc1hwc0)), 1); | ||
| 565 | + static_assert(AscendC::Std::is_same_v<GetLayoutPattern<decltype(nc1hwc0)>, NC1HWC0LayoutPtn>); | ||
| 566 | +} | ||
| @@ -356,6 +356,76 @@ TEST_GM2L1_BATCH(half, Nz2Nz, NZ, NZ, 3, 32, 64) | |||
| 356 | // ZN -> ZN batched. | 356 | // ZN -> ZN batched. |
| 357 | TEST_GM2L1_BATCH(half, Zn2Zn, ZN, ZN, 3, 32, 64) | 357 | TEST_GM2L1_BATCH(half, Zn2Zn, ZN, ZN, 3, 32, 64) |
| 358 | 358 | ||
| 359 | +// NC1HWC0 (N, C1, H, W, C0) flat 5D layout, contiguous. Built with the NC1HWC0LayoutPtn tag so | ||
| 360 | +// CopyGM2L1Routing dispatches to CopyGmToCbufNC1HWC02NC1HWC0. | ||
| 361 | +template <typename T, size_t C0> | ||
| 362 | +auto MakeNc1hwc0(int n, int c1, int h, int w) | ||
| 363 | +{ | ||
| 364 | + return MakeFrameLayout<NC1HWC0LayoutPtn>(n, c1, h, w, static_cast<int>(C0)); | ||
| 365 | +} | ||
| 366 | + | ||
| 367 | +// NC1HWC0 gm->l1 copy, W fully loaded: C1 bursts of H*W*C0, golden per-C1 block. | ||
| 368 | +#define TEST_GM2L1_NC1HWC0_INNER(type, N, C1, H, W, counter) \ | ||
| 369 | + TEST_F(TensorApiGm2L1, TEST_GM2L1_CONCAT_(CopyGm2L1NC1HWC0, ND2ND, type, counter)) \ | ||
| 370 | + { \ | ||
| 371 | + using T = type; \ | ||
| 372 | + constexpr size_t C0 = IsB4Type<T> ? 64 : 32 / sizeof(T); \ | ||
| 373 | + const int kBlockElems = (H) * (W) * C0; /* per-C1 block = H*W*C0 */ \ | ||
| 374 | + auto gmNc1hwc0 = MakeNc1hwc0<T, C0>((N), (C1), (H), (W)); \ | ||
| 375 | + auto l1Nc1hwc0 = MakeNc1hwc0<T, C0>((N), (C1), (H), (W)); \ | ||
| 376 | + auto gmA = MakeTensor(MakeMemPtr<Location::GM>(reinterpret_cast<T*>(src0Gm)), gmNc1hwc0); \ | ||
| 377 | + auto l1ATensor = MakeTensor(MakeMemPtr<Location::L1>(reinterpret_cast<T*>(l1ABuf)), l1Nc1hwc0); \ | ||
| 378 | + auto atomCopy = MakeCopy(CopyGM2L1{}, CopyGM2L1TraitDefault{}); \ | ||
| 379 | + InitializeData<T>(); \ | ||
| 380 | + atomCopy.Call(l1ATensor, gmA); \ | ||
| 381 | + for (int b = 0; b < (N) * (C1); ++b) { \ | ||
| 382 | + auto gmB = MakeTensor( \ | ||
| 383 | + MakeMemPtr<Location::GM>(reinterpret_cast<T*>(src0Gm) + b * kBlockElems), MakeND<T>(1, kBlockElems)); \ | ||
| 384 | + auto l1G = MakeTensor( \ | ||
| 385 | + MakeMemPtr<Location::L1>(reinterpret_cast<T*>(l1ABufGolden) + b * kBlockElems), \ | ||
| 386 | + MakeND<T>(1, kBlockElems)); \ | ||
| 387 | + DataCopyGm2L1Sim(l1G, gmB); \ | ||
| 388 | + } \ | ||
| 389 | + EXPECT_TRUE(std::equal(l1ABuf, l1ABuf + L1Size, l1ABufGolden)); \ | ||
| 390 | + } | ||
| 391 | +#define TEST_GM2L1_NC1HWC0(type, N, C1, H, W) TEST_GM2L1_NC1HWC0_INNER(type, N, C1, H, W, __COUNTER__) | ||
| 392 | + | ||
| 393 | +TEST_GM2L1_NC1HWC0(half, 1, 3, 4, 4) | ||
| 394 | +TEST_GM2L1_NC1HWC0(half, 1, 1, 8, 2) | ||
| 395 | +TEST_GM2L1_NC1HWC0(uint32_t, 1, 2, 4, 4) | ||
| 396 | + | ||
| 397 | +// NC1HWC0 gm->l1 copy, W not fully loaded: GM built with srcW, sliced to loadW; per-(C1,H) block. | ||
| 398 | +#define TEST_GM2L1_NC1HWC0_PARTW_INNER(type, N, C1, H, srcW, loadW, counter) \ | ||
| 399 | + TEST_F(TensorApiGm2L1, TEST_GM2L1_CONCAT_(CopyGm2L1NC1HWC0PartW, ND2ND, type, counter)) \ | ||
| 400 | + { \ | ||
| 401 | + using T = type; \ | ||
| 402 | + constexpr size_t C0 = IsB4Type<T> ? 64 : 32 / sizeof(T); \ | ||
| 403 | + const int kSrcRow = (srcW) * C0; \ | ||
| 404 | + const int kDstRow = (loadW) * C0; \ | ||
| 405 | + auto gmFull = MakeNc1hwc0<T, C0>((N), (C1), (H), (srcW)); \ | ||
| 406 | + auto l1Nc1hwc0 = MakeNc1hwc0<T, C0>((N), (C1), (H), (loadW)); \ | ||
| 407 | + auto gmFullT = MakeTensor(MakeMemPtr<Location::GM>(reinterpret_cast<T*>(src0Gm)), gmFull); \ | ||
| 408 | + auto gmA = gmFullT.Slice(MakeCoord(0, 0, 0, 0, 0), MakeShape((N), (C1), (H), (loadW), C0)); \ | ||
| 409 | + auto l1ATensor = MakeTensor(MakeMemPtr<Location::L1>(reinterpret_cast<T*>(l1ABuf)), l1Nc1hwc0); \ | ||
| 410 | + auto atomCopy = MakeCopy(CopyGM2L1{}, CopyGM2L1TraitDefault{}); \ | ||
| 411 | + InitializeData<T>(); \ | ||
| 412 | + atomCopy.Call(l1ATensor, gmA); \ | ||
| 413 | + for (int b = 0; b < (N) * (C1) * (H); ++b) { \ | ||
| 414 | + auto gmB = MakeTensor( \ | ||
| 415 | + MakeMemPtr<Location::GM>(reinterpret_cast<T*>(src0Gm) + b * kSrcRow), MakeND<T>(1, kDstRow)); \ | ||
| 416 | + auto l1G = MakeTensor( \ | ||
| 417 | + MakeMemPtr<Location::L1>(reinterpret_cast<T*>(l1ABufGolden) + b * kDstRow), MakeND<T>(1, kDstRow)); \ | ||
| 418 | + DataCopyGm2L1Sim(l1G, gmB); \ | ||
| 419 | + } \ | ||
| 420 | + EXPECT_TRUE(std::equal(l1ABuf, l1ABuf + L1Size, l1ABufGolden)); \ | ||
| 421 | + } | ||
| 422 | +#define TEST_GM2L1_NC1HWC0_PARTW(type, N, C1, H, srcW, loadW) \ | ||
| 423 | + TEST_GM2L1_NC1HWC0_PARTW_INNER(type, N, C1, H, srcW, loadW, __COUNTER__) | ||
| 424 | + | ||
| 425 | +TEST_GM2L1_NC1HWC0_PARTW(half, 1, 3, 4, 8, 4) | ||
| 426 | +TEST_GM2L1_NC1HWC0_PARTW(half, 1, 2, 4, 6, 2) | ||
| 427 | +TEST_GM2L1_NC1HWC0_PARTW(uint32_t, 1, 2, 4, 8, 4) | ||
| 428 | + | ||
| 359 | // ND2ND | 429 | // ND2ND |
| 360 | // constraint: col small to big: dst column stride % 32B = 0, col same: no constraint | 430 | // constraint: col small to big: dst column stride % 32B = 0, col same: no constraint |
| 361 | // constraint: or support src shape is 1 dim(include 2d continuous, src stride equals to dst stride) | 431 | // constraint: or support src shape is 1 dim(include 2d continuous, src stride equals to dst stride) |
| @@ -2792,3 +2862,83 @@ TEST_F(TensorApiGm2L1, CopyGm2L1Batch_ND2ND_Compact_HalfType) | |||
| 2792 | constexpr uint64_t expectStride = static_cast<uint64_t>(M) * N * sizeof(T); | 2862 | constexpr uint64_t expectStride = static_cast<uint64_t>(M) * N * sizeof(T); |
| 2793 | EXPECT_GM2L1_BATCH_LAST_CALL(B, expectBlockLen, expectStride, expectStride); | 2863 | EXPECT_GM2L1_BATCH_LAST_CALL(B, expectBlockLen, expectStride, expectStride); |
| 2794 | } | 2864 | } |
| 2865 | + | ||
| 2866 | +// GM(NHWC) -> L1(NC1HWC0) via the nd2nz DMA path. Verifies the captured DMA parameters: | ||
| 2867 | +// ndNum=H, nValue=W, dValue=C; src strides read straight from the NHWC layout | ||
| 2868 | +// (loop1SrcStride=C*sizeof, loop4SrcStride=W*C*sizeof); dst strides loop2DstStride=1, | ||
| 2869 | +// loop3DstStride=H*W, loop4DstStride=W. | ||
| 2870 | +TEST_F(TensorApiGm2L1, CopyGm2L1NHWC2NC1HWC0) | ||
| 2871 | +{ | ||
| 2872 | + using T = half; | ||
| 2873 | + constexpr uint32_t N = 1; | ||
| 2874 | + constexpr uint32_t H = 4; | ||
| 2875 | + constexpr uint32_t W = 4; | ||
| 2876 | + constexpr uint32_t C0 = 16; | ||
| 2877 | + constexpr uint32_t C = 32; | ||
| 2878 | + constexpr uint32_t C1 = C / C0; // 2 | ||
| 2879 | + | ||
| 2880 | + auto gmNhwc = MakeFrameLayout<NHWCLayoutPtn>( | ||
| 2881 | + static_cast<int32_t>(N), static_cast<int32_t>(H), static_cast<int32_t>(W), static_cast<int32_t>(C)); | ||
| 2882 | + auto l1Nc1hwc0 = MakeFrameLayout<NC1HWC0LayoutPtn>( | ||
| 2883 | + static_cast<int32_t>(N), static_cast<int32_t>(C1), static_cast<int32_t>(H), static_cast<int32_t>(W), | ||
| 2884 | + static_cast<int32_t>(C0)); | ||
| 2885 | + auto gmA = MakeTensor(MakeMemPtr<Location::GM>(reinterpret_cast<T*>(src0Gm)), gmNhwc); | ||
| 2886 | + auto l1A = MakeTensor(MakeMemPtr<Location::L1>(reinterpret_cast<T*>(l1ABuf)), l1Nc1hwc0); | ||
| 2887 | + | ||
| 2888 | + MakeCopy(CopyGM2L1{}, CopyGM2L1TraitDefault{}).Call(l1A, gmA); | ||
| 2889 | + | ||
| 2890 | + ASSERT_EQ(gGm2L1ND2NzCaptures.size(), 1); | ||
| 2891 | + ASSERT_EQ(gGm2L1NzParaCaptures.size(), 1); | ||
| 2892 | + const auto& nd2nz = gGm2L1ND2NzCaptures.back(); | ||
| 2893 | + const auto& nzPara = gGm2L1NzParaCaptures.back(); | ||
| 2894 | + | ||
| 2895 | + EXPECT_EQ(nzPara.ndNum, H); // ndNum = srcH | ||
| 2896 | + EXPECT_EQ(nd2nz.nValue, W); // nValue = srcW | ||
| 2897 | + EXPECT_EQ(nd2nz.dValue, C); // dValue = srcC | ||
| 2898 | + EXPECT_EQ(nd2nz.loop1SrcStride, C * sizeof(T)); // srcC * sizeof(type) | ||
| 2899 | + EXPECT_EQ(nd2nz.loop4SrcStride, W * C * sizeof(T)); // srcW * srcC * sizeof(type) | ||
| 2900 | + EXPECT_EQ(nzPara.loop2DstStride, 1); | ||
| 2901 | + EXPECT_EQ(nzPara.loop3DstStride, H * W); // dstH * dstW | ||
| 2902 | + EXPECT_EQ(nzPara.loop4DstStride, W); // dstW | ||
| 2903 | + EXPECT_FALSE(nd2nz.enableSmallC0); | ||
| 2904 | +} | ||
| 2905 | + | ||
| 2906 | +// GM(NCHW) -> L1(NC1HWC0) via the dn2nz DMA path (NCHW is the HW<->C transpose of NHWC). Verifies | ||
| 2907 | +// the captured DMA parameters: dnNum=H, nValue=W, dValue=C; src strides read straight from the NCHW | ||
| 2908 | +// layout (loop1SrcStride=H*W*sizeof=Stride[1]*sizeof, loop4SrcStride=W*sizeof=Stride[2]*sizeof); | ||
| 2909 | +// dst strides loop2DstStride=1, loop3DstStride=H*W, loop4DstStride=W. | ||
| 2910 | +TEST_F(TensorApiGm2L1, CopyGm2L1NCHW2NC1HWC0) | ||
| 2911 | +{ | ||
| 2912 | + using T = half; | ||
| 2913 | + constexpr uint32_t N = 1; | ||
| 2914 | + constexpr uint32_t H = 4; | ||
| 2915 | + constexpr uint32_t W = 4; | ||
| 2916 | + constexpr uint32_t C0 = 16; | ||
| 2917 | + constexpr uint32_t C = 32; | ||
| 2918 | + constexpr uint32_t C1 = C / C0; // 2 | ||
| 2919 | + | ||
| 2920 | + auto gmNchw = MakeFrameLayout<NCHWLayoutPtn>( | ||
| 2921 | + static_cast<int32_t>(N), static_cast<int32_t>(C), static_cast<int32_t>(H), static_cast<int32_t>(W)); | ||
| 2922 | + auto l1Nc1hwc0 = MakeFrameLayout<NC1HWC0LayoutPtn>( | ||
| 2923 | + static_cast<int32_t>(N), static_cast<int32_t>(C1), static_cast<int32_t>(H), static_cast<int32_t>(W), | ||
| 2924 | + static_cast<int32_t>(C0)); | ||
| 2925 | + auto gmA = MakeTensor(MakeMemPtr<Location::GM>(reinterpret_cast<T*>(src0Gm)), gmNchw); | ||
| 2926 | + auto l1A = MakeTensor(MakeMemPtr<Location::L1>(reinterpret_cast<T*>(l1ABuf)), l1Nc1hwc0); | ||
| 2927 | + | ||
| 2928 | + MakeCopy(CopyGM2L1{}, CopyGM2L1TraitDefault{}).Call(l1A, gmA); | ||
| 2929 | + | ||
| 2930 | + ASSERT_EQ(gGm2L1DN2NzCaptures.size(), 1); | ||
| 2931 | + ASSERT_EQ(gGm2L1NzParaCaptures.size(), 1); | ||
| 2932 | + const auto& dn2nz = gGm2L1DN2NzCaptures.back(); | ||
| 2933 | + const auto& nzPara = gGm2L1NzParaCaptures.back(); | ||
| 2934 | + | ||
| 2935 | + EXPECT_EQ(nzPara.ndNum, H); // dnNum = srcH | ||
| 2936 | + EXPECT_EQ(dn2nz.nValue, W); // nValue = srcW | ||
| 2937 | + EXPECT_EQ(dn2nz.dValue, C); // dValue = srcC | ||
| 2938 | + EXPECT_EQ(dn2nz.loop1SrcStride, H * W * sizeof(T)); // srcH*srcW*sizeof (NCHW Stride[1]=H*W) | ||
| 2939 | + EXPECT_EQ(dn2nz.loop4SrcStride, W * sizeof(T)); // srcW*sizeof (NCHW Stride[2]=W) | ||
| 2940 | + EXPECT_EQ(nzPara.loop2DstStride, 1); | ||
| 2941 | + EXPECT_EQ(nzPara.loop3DstStride, H * W); // dstH * dstW | ||
| 2942 | + EXPECT_EQ(nzPara.loop4DstStride, W); // dstW | ||
| 2943 | + EXPECT_FALSE(dn2nz.enableSmallC0); | ||
| 2944 | +} | ||
| @@ -542,3 +542,82 @@ KERNEL_TENSOR_API_L0C2GM_E2E_LAYOUT(1, 128, 64, DN, DNLayoutPtn, float, float, N | |||
| 542 | KERNEL_TENSOR_API_L0C2GM_E2E_LAYOUT(1, 128, 64, DN, DNLayoutPtn, int32_t, int8_t, REQ8, false, false) | 542 | KERNEL_TENSOR_API_L0C2GM_E2E_LAYOUT(1, 128, 64, DN, DNLayoutPtn, int32_t, int8_t, REQ8, false, false) |
| 543 | KERNEL_TENSOR_API_L0C2GM_E2E_LAYOUT(1, 16, 16, ND, NDLayoutPtn, int32_t, half, VDEQF16, true, false) | 543 | KERNEL_TENSOR_API_L0C2GM_E2E_LAYOUT(1, 16, 16, ND, NDLayoutPtn, int32_t, half, VDEQF16, true, false) |
| 544 | KERNEL_TENSOR_API_L0C2GM_E2E_LAYOUT(1, 16, 16, DN, DNLayoutPtn, int32_t, half, VDEQF16, true, false) | 544 | KERNEL_TENSOR_API_L0C2GM_E2E_LAYOUT(1, 16, 16, DN, DNLayoutPtn, int32_t, half, VDEQF16, true, false) |
| 545 | + | ||
| 546 | +// L0C(NZ) -> GM(NC1HWC0), N=1, no-quant. dst NC1HWC0 built with the NC1HWC0LayoutPtn tag so routing | ||
| 547 | +// dispatches to DataCopyL0C2GM (NC1HWC0<-NZ). src NZ (M=Ho*Wo, N=Cout=C1*C0); the stub is empty so | ||
| 548 | +// this exercises routing/param derivation. | ||
| 549 | +TEST_F(Tensor_Api_Cube_Copy_3510, CopyL0C2GMNZ2NC1HWC0) | ||
| 550 | +{ | ||
| 551 | + using namespace AscendC::Te; | ||
| 552 | + | ||
| 553 | + constexpr uint32_t C1 = 2; | ||
| 554 | + constexpr uint32_t H = 4; | ||
| 555 | + constexpr uint32_t W = 4; | ||
| 556 | + constexpr uint32_t C0 = 16; | ||
| 557 | + constexpr uint32_t m = H * W; // Ho*Wo = 16 | ||
| 558 | + constexpr uint32_t n = C1 * C0; // Cout = 32 | ||
| 559 | + __cc__ float src[m * n] = {0}; | ||
| 560 | + __gm__ float dst[m * n] = {0}; | ||
| 561 | + | ||
| 562 | + auto l0cTensor = | ||
| 563 | + MakeTensorAt<Location::L0C>(src, MakeFrameLayout<NZLayoutPtn, LayoutTraitDefault<float, _16>>(m, n)); | ||
| 564 | + auto gmTensor = MakeTensorAt<Location::GM>( | ||
| 565 | + dst, MakeFrameLayout<NC1HWC0LayoutPtn>( | ||
| 566 | + 1, static_cast<int>(C1), static_cast<int>(H), static_cast<int>(W), static_cast<int>(C0))); | ||
| 567 | + | ||
| 568 | + RunCopyCallPaths<CopyL0C2GM, CopyL0C2GMTraitDefault>(gmTensor, l0cTensor); | ||
| 569 | + RunCopyWithParamPaths<CopyL0C2GM, CopyL0C2GMTraitDefault>(gmTensor, l0cTensor, FixpipeParams{}); | ||
| 570 | + | ||
| 571 | + EXPECT_EQ(dst[0], 0); | ||
| 572 | +} | ||
| 573 | + | ||
| 574 | +// L0C(NZ) -> GM(NHWC), no quant. NZ (M=H*W, N=C) unfolded to NHWC (1, H, W, C) via the nz2nd path. | ||
| 575 | +TEST_F(Tensor_Api_Cube_Copy_3510, CopyL0C2GMNZ2NHWC) | ||
| 576 | +{ | ||
| 577 | + using namespace AscendC::Te; | ||
| 578 | + | ||
| 579 | + constexpr uint32_t H = 8; | ||
| 580 | + constexpr uint32_t W = 8; | ||
| 581 | + constexpr uint32_t C = 16; | ||
| 582 | + constexpr uint32_t m = H * W; // 64 | ||
| 583 | + constexpr uint32_t n = C; // 16 | ||
| 584 | + __cc__ float src[m * n] = {0}; | ||
| 585 | + __gm__ float dst[m * n] = {0}; | ||
| 586 | + | ||
| 587 | + auto l0cTensor = | ||
| 588 | + MakeTensorAt<Location::L0C>(src, MakeFrameLayout<NZLayoutPtn, LayoutTraitDefault<float, _16>>(m, n)); | ||
| 589 | + auto gmTensor = MakeTensorAt<Location::GM>( | ||
| 590 | + dst, MakeFrameLayout<NHWCLayoutPtn, LayoutTraitDefault<float>>( | ||
| 591 | + 1, static_cast<int>(H), static_cast<int>(W), static_cast<int>(C))); | ||
| 592 | + | ||
| 593 | + RunCopyCallPaths<CopyL0C2GM, CopyL0C2GMTraitDefault>(gmTensor, l0cTensor); | ||
| 594 | + RunCopyWithParamPaths<CopyL0C2GM, CopyL0C2GMTraitDefault>(gmTensor, l0cTensor, FixpipeParams{}); | ||
| 595 | + | ||
| 596 | + EXPECT_EQ(dst[0], 0); | ||
| 597 | +} | ||
| 598 | + | ||
| 599 | +// L0C(NZ) -> GM(NCHW), no quant. NZ (M=H*W, N=C) unfolded to NCHW (1, C, H, W) via the nz2dn path | ||
| 600 | +// (NCHW is the HW<->C transpose of NHWC). | ||
| 601 | +TEST_F(Tensor_Api_Cube_Copy_3510, CopyL0C2GMNZ2NCHW) | ||
| 602 | +{ | ||
| 603 | + using namespace AscendC::Te; | ||
| 604 | + | ||
| 605 | + constexpr uint32_t H = 8; | ||
| 606 | + constexpr uint32_t W = 8; | ||
| 607 | + constexpr uint32_t C = 16; | ||
| 608 | + constexpr uint32_t m = H * W; // 64 | ||
| 609 | + constexpr uint32_t n = C; // 16 | ||
| 610 | + __cc__ float src[m * n] = {0}; | ||
| 611 | + __gm__ float dst[m * n] = {0}; | ||
| 612 | + | ||
| 613 | + auto l0cTensor = | ||
| 614 | + MakeTensorAt<Location::L0C>(src, MakeFrameLayout<NZLayoutPtn, LayoutTraitDefault<float, _16>>(m, n)); | ||
| 615 | + auto gmTensor = MakeTensorAt<Location::GM>( | ||
| 616 | + dst, MakeFrameLayout<NCHWLayoutPtn, LayoutTraitDefault<float>>( | ||
| 617 | + 1, static_cast<int>(C), static_cast<int>(H), static_cast<int>(W))); | ||
| 618 | + | ||
| 619 | + RunCopyCallPaths<CopyL0C2GM, CopyL0C2GMTraitDefault>(gmTensor, l0cTensor); | ||
| 620 | + RunCopyWithParamPaths<CopyL0C2GM, CopyL0C2GMTraitDefault>(gmTensor, l0cTensor, FixpipeParams{}); | ||
| 621 | + | ||
| 622 | + EXPECT_EQ(dst[0], 0); | ||
| 623 | +} | ||
| @@ -324,3 +324,57 @@ TEST_TENSOR_API_LOAD_DATA(int8_t, 32, 32, ZN, NZ, L1, L0A, cbuf, ca, true); | |||
| 324 | TEST_TENSOR_API_LOAD_DATA(uint8_t, 32, 32, ZN, NZ, L1, L0A, cbuf, ca, true); | 324 | TEST_TENSOR_API_LOAD_DATA(uint8_t, 32, 32, ZN, NZ, L1, L0A, cbuf, ca, true); |
| 325 | TEST_TENSOR_API_LOAD_DATA(int16_t, 16, 16, ZN, NZ, L1, L0A, cbuf, ca, true); | 325 | TEST_TENSOR_API_LOAD_DATA(int16_t, 16, 16, ZN, NZ, L1, L0A, cbuf, ca, true); |
| 326 | TEST_TENSOR_API_LOAD_DATA(uint16_t, 16, 16, ZN, NZ, L1, L0A, cbuf, ca, true); | 326 | TEST_TENSOR_API_LOAD_DATA(uint16_t, 16, 16, ZN, NZ, L1, L0A, cbuf, ca, true); |
| 327 | + | ||
| 328 | +// ================= img2col (L1 NC1HWC0 -> L0A NZ) ================= | ||
| 329 | +// The img2col instruction has 17 params, above mockcpp's API-hook arity limit, so this drives the | ||
| 330 | +// full copy path against the empty cce stub and checks it routes/compiles without a crash. The dst | ||
| 331 | +// NZ M/K (conv-unfolded, ceil-aligned) and the src NC1HWC0 axes exercise LoadDataL12L0AImg2Col::Run. | ||
| 332 | +namespace { | ||
| 333 | +template <typename T, size_t C0> | ||
| 334 | +auto MakeNc1hwc0Layout(int n, int c1, int h, int w) | ||
| 335 | +{ | ||
| 336 | + using namespace AscendC::Te; | ||
| 337 | + return MakeFrameLayout<NC1HWC0LayoutPtn>(n, c1, h, w, static_cast<int>(C0)); | ||
| 338 | +} | ||
| 339 | +} // namespace | ||
| 340 | + | ||
| 341 | +TEST_F(Tensor_Api_Cube_Copy_3510, CopyL12L0AImg2Col) | ||
| 342 | +{ | ||
| 343 | + using namespace AscendC::Te; | ||
| 344 | + | ||
| 345 | + // src NC1HWC0: N=1, C1=2, H=5, W=5, C0=16 -> channelSize = C1*C0 = 32. | ||
| 346 | + constexpr int N = 1; | ||
| 347 | + constexpr int C1 = 2; | ||
| 348 | + constexpr int H = 5; | ||
| 349 | + constexpr int W = 5; | ||
| 350 | + constexpr int C0 = 16; | ||
| 351 | + | ||
| 352 | + // filter 3x3, stride 1, pad 1, dilation 1 -> Ho = Wo = 5, M = 25, K = 3*3*32 = 288. | ||
| 353 | + constexpr int filterH = 3; | ||
| 354 | + constexpr int filterW = 3; | ||
| 355 | + constexpr int M = 25; | ||
| 356 | + constexpr int K = filterH * filterW * C1 * C0; // 288 | ||
| 357 | + constexpr int mAlign = 32; // ceil(25, 16) * 16 | ||
| 358 | + constexpr int kAlign = 288; // already a multiple of C0 | ||
| 359 | + | ||
| 360 | + __cbuf__ int16_t src[N * C1 * H * W * C0] = {0}; | ||
| 361 | + __ca__ int16_t dst[mAlign * kAlign] = {0}; | ||
| 362 | + | ||
| 363 | + auto srcTensor = MakeTensor(MakeMemPtr<Location::L1>(src), MakeNc1hwc0Layout<int16_t, C0>(N, C1, H, W)); | ||
| 364 | + auto l0aTensor = MakeTensorAt<Location::L0A>(dst, MakeFrameLayout<NZLayoutPtn, LayoutTraitDefault<int16_t>>(M, K)); | ||
| 365 | + | ||
| 366 | + // dst NZ ceil-aligned M/K, now provided by the caller via Img2ColParams. | ||
| 367 | + Img2ColParams<int16_t> params; | ||
| 368 | + params.mExtension = static_cast<uint16_t>(mAlign); | ||
| 369 | + params.kExtension = static_cast<uint16_t>(kAlign); | ||
| 370 | + params.filterW = filterW; | ||
| 371 | + params.filterH = filterH; | ||
| 372 | + params.strideW = 1; | ||
| 373 | + params.strideH = 1; | ||
| 374 | + params.padList[0] = params.padList[1] = params.padList[2] = params.padList[3] = 1; | ||
| 375 | + | ||
| 376 | + auto atom = MakeCopy(CopyL12L0A{}, CopyL12L0ATraitDefault{}); | ||
| 377 | + Copy(atom.with(params), l0aTensor, srcTensor); | ||
| 378 | + | ||
| 379 | + EXPECT_EQ(dst[0], 0); | ||
| 380 | +} | ||


1.统一一下Cbuf和L1,如果目录叫L1,那么这里这些class应该也叫L1 2.我们的buff里面有很多1/0的标记,这种情况下还用2来表示to的话就会很容易造成歧义