Merged
修复文档问题 #7437
li-yuanjie-daAdd on 11 days ago
修复文档问题 #7437
Merged
Merged fromli-yuanjie-da/asc-devkit_3masterintocann/asc-devkitmaster
Total: 9 files changed+98-34
| @@ -58,24 +58,28 @@ __aicore__ inline void disable_dma_atomic() | |||
| 58 | #include "tensor_api/tensor.h" | 58 | #include "tensor_api/tensor.h" |
| 59 | 59 | ||
| 60 | constexpr uint32_t total_length = 64; | 60 | constexpr uint32_t total_length = 64; |
| 61 | +constexpr uint8_t buffer_lock_id = 0; | ||
| 61 | 62 | ||
| 62 | __global__ __vector__ void atomic_add_kernel(__gm__ float* output, __gm__ float* input) | 63 | __global__ __vector__ void atomic_add_kernel(__gm__ float* output, __gm__ float* input) |
| 63 | { | 64 | { |
| 64 | using namespace asc::te; | 65 | using namespace asc::te; |
| 65 | __ubuf__ float input_buffer[total_length]; | 66 | __ubuf__ float input_buffer[total_length]; |
| 66 | - __ubuf__ float output_buffer[total_length]; | ||
| 67 | 67 | ||
| 68 | auto layout = make_frame_layout<nd_ext_layout_ptn>(1, total_length); | 68 | auto layout = make_frame_layout<nd_ext_layout_ptn>(1, total_length); |
| 69 | auto input_gm = make_tensor(make_mem_ptr<location::gm>(input), layout); | 69 | auto input_gm = make_tensor(make_mem_ptr<location::gm>(input), layout); |
| 70 | auto output_gm = make_tensor(make_mem_ptr<location::gm>(output), layout); | 70 | auto output_gm = make_tensor(make_mem_ptr<location::gm>(output), layout); |
| 71 | auto input_ub = make_tensor(make_mem_ptr(input_buffer), layout); | 71 | auto input_ub = make_tensor(make_mem_ptr(input_buffer), layout); |
| 72 | - auto output_ub = make_tensor(make_mem_ptr(output_buffer), layout); | ||
| 73 | 72 | ||
| 73 | + mutex<pipe_t::PIPE_MTE2> copy_in_mutex(buffer_lock_id); | ||
| 74 | + copy_in_mutex.lock(); | ||
| 74 | copy(make_copy(copy_gm_to_ub{}), input_ub, input_gm); | 75 | copy(make_copy(copy_gm_to_ub{}), input_ub, input_gm); |
| 75 | - copy(make_copy(copy_gm_to_ub{}), output_ub, output_gm); | 76 | + copy_in_mutex.unlock(); |
| 76 | 77 | ||
| 77 | set_atomic_add<float>(); | 78 | set_atomic_add<float>(); |
| 79 | + mutex<pipe_t::PIPE_MTE3> copy_out_mutex(buffer_lock_id); | ||
| 80 | + copy_out_mutex.lock(); | ||
| 78 | copy(make_copy(copy_ub_to_gm{}), output_gm, input_ub); | 81 | copy(make_copy(copy_ub_to_gm{}), output_gm, input_ub); |
| 82 | + copy_out_mutex.unlock(); | ||
| 79 | disable_dma_atomic(); | 83 | disable_dma_atomic(); |
| 80 | } | 84 | } |
| 81 | ``` | 85 | ``` |
| @@ -59,12 +59,13 @@ __aicore__ inline void set_atomic_add() | |||
| 59 | - 开启后对后续所有目的地址为GM的搬运指令生效,可以调用[`disable_dma_atomic`](disable_dma_atomic.md)关闭。 | 59 | - 开启后对后续所有目的地址为GM的搬运指令生效,可以调用[`disable_dma_atomic`](disable_dma_atomic.md)关闭。 |
| 60 | - 本接口不会自动清零GM上已有数据。若期望原子累加前GM上的原始数据为零,需手动清零。 | 60 | - 本接口不会自动清零GM上已有数据。若期望原子累加前GM上的原始数据为零,需手动清零。 |
| 61 | - 本接口仅对后续通过MTE3、Fixpipe单元搬运且目的地址为GM的搬运指令生效,对标量写GM指令不生效。 | 61 | - 本接口仅对后续通过MTE3、Fixpipe单元搬运且目的地址为GM的搬运指令生效,对标量写GM指令不生效。 |
| 62 | +- 本接口与紧邻的后续搬运指令之间的同步由硬件保证。 | ||
| 62 | - 后续搬运指令的操作数据类型必须与`T`一致。 | 63 | - 后续搬运指令的操作数据类型必须与`T`一致。 |
| 63 | - 本接口不能保证后续搬运指令的执行顺序。 | 64 | - 本接口不能保证后续搬运指令的执行顺序。 |
| 64 | 65 | ||
| 65 | ## 调用示例 | 66 | ## 调用示例 |
| 66 | 67 | ||
| 67 | -以下示例先读取Global Memory上的初始值,再通过DMA原子累加将输入数据与初始值计算后写回Global Memory,最后关闭原子模式。 | 68 | +以下示例先通过同ID的MTE2/MTE3 mutex保证输入搬运完成后,再开启DMA原子累加,将输入数据与GM初始值计算后写回Global Memory,最后关闭原子模式。 |
| 68 | 69 | ||
| 69 | ```cpp | 70 | ```cpp |
| 70 | #include <cstdint> | 71 | #include <cstdint> |
| @@ -72,24 +73,28 @@ __aicore__ inline void set_atomic_add() | |||
| 72 | #include "tensor_api/tensor.h" | 73 | #include "tensor_api/tensor.h" |
| 73 | 74 | ||
| 74 | constexpr uint32_t total_length = 64; | 75 | constexpr uint32_t total_length = 64; |
| 76 | +constexpr uint8_t buffer_lock_id = 0; | ||
| 75 | 77 | ||
| 76 | __global__ __vector__ void atomic_add_kernel(__gm__ float* output, __gm__ float* input) | 78 | __global__ __vector__ void atomic_add_kernel(__gm__ float* output, __gm__ float* input) |
| 77 | { | 79 | { |
| 78 | using namespace asc::te; | 80 | using namespace asc::te; |
| 79 | __ubuf__ float input_buffer[total_length]; | 81 | __ubuf__ float input_buffer[total_length]; |
| 80 | - __ubuf__ float output_buffer[total_length]; | ||
| 81 | 82 | ||
| 82 | auto layout = make_frame_layout<nd_ext_layout_ptn>(1, total_length); | 83 | auto layout = make_frame_layout<nd_ext_layout_ptn>(1, total_length); |
| 83 | auto input_gm = make_tensor(make_mem_ptr<location::gm>(input), layout); | 84 | auto input_gm = make_tensor(make_mem_ptr<location::gm>(input), layout); |
| 84 | auto output_gm = make_tensor(make_mem_ptr<location::gm>(output), layout); | 85 | auto output_gm = make_tensor(make_mem_ptr<location::gm>(output), layout); |
| 85 | auto input_ub = make_tensor(make_mem_ptr(input_buffer), layout); | 86 | auto input_ub = make_tensor(make_mem_ptr(input_buffer), layout); |
| 86 | - auto output_ub = make_tensor(make_mem_ptr(output_buffer), layout); | ||
| 87 | 87 | ||
| 88 | + mutex<pipe_t::PIPE_MTE2> copy_in_mutex(buffer_lock_id); | ||
| 89 | + copy_in_mutex.lock(); | ||
| 88 | copy(make_copy(copy_gm_to_ub{}), input_ub, input_gm); | 90 | copy(make_copy(copy_gm_to_ub{}), input_ub, input_gm); |
| 89 | - copy(make_copy(copy_gm_to_ub{}), output_ub, output_gm); | 91 | + copy_in_mutex.unlock(); |
| 90 | 92 | ||
| 91 | set_atomic_add<float>(); | 93 | set_atomic_add<float>(); |
| 94 | + mutex<pipe_t::PIPE_MTE3> copy_out_mutex(buffer_lock_id); | ||
| 95 | + copy_out_mutex.lock(); | ||
| 92 | copy(make_copy(copy_ub_to_gm{}), output_gm, input_ub); | 96 | copy(make_copy(copy_ub_to_gm{}), output_gm, input_ub); |
| 97 | + copy_out_mutex.unlock(); | ||
| 93 | disable_dma_atomic(); | 98 | disable_dma_atomic(); |
| 94 | } | 99 | } |
| 95 | ``` | 100 | ``` |
| @@ -59,12 +59,13 @@ __aicore__ inline void set_atomic_max() | |||
| 59 | - 开启后对后续所有目的地址为GM的搬运指令生效,可以调用[`disable_dma_atomic`](disable_dma_atomic.md)关闭。 | 59 | - 开启后对后续所有目的地址为GM的搬运指令生效,可以调用[`disable_dma_atomic`](disable_dma_atomic.md)关闭。 |
| 60 | - 本接口执行前不会自动将GM上已有数据置零。若期望原子取大前GM上的原始数据为零,需手动清零。 | 60 | - 本接口执行前不会自动将GM上已有数据置零。若期望原子取大前GM上的原始数据为零,需手动清零。 |
| 61 | - 本接口仅对后续通过MTE3、Fixpipe单元搬运且目的地址为GM的搬运指令生效,对标量写GM指令不生效。 | 61 | - 本接口仅对后续通过MTE3、Fixpipe单元搬运且目的地址为GM的搬运指令生效,对标量写GM指令不生效。 |
| 62 | +- 本接口与紧邻的后续搬运指令之间的同步由硬件保证。 | ||
| 62 | - 后续搬运指令的操作数据类型必须与`T`一致。 | 63 | - 后续搬运指令的操作数据类型必须与`T`一致。 |
| 63 | - 本接口不能保证后续搬运指令的执行顺序。 | 64 | - 本接口不能保证后续搬运指令的执行顺序。 |
| 64 | 65 | ||
| 65 | ## 调用示例 | 66 | ## 调用示例 |
| 66 | 67 | ||
| 67 | -以下示例先读取Global Memory上的初始值,再通过DMA原子取最大值将输入数据与初始值计算后写回Global Memory,最后关闭原子模式。 | 68 | +以下示例先通过同ID的MTE2/MTE3 mutex保证输入搬运完成后,再开启DMA原子取最大值,将输入数据与GM初始值计算后写回Global Memory,最后关闭原子模式。 |
| 68 | 69 | ||
| 69 | ```cpp | 70 | ```cpp |
| 70 | #include <cstdint> | 71 | #include <cstdint> |
| @@ -72,24 +73,28 @@ __aicore__ inline void set_atomic_max() | |||
| 72 | #include "tensor_api/tensor.h" | 73 | #include "tensor_api/tensor.h" |
| 73 | 74 | ||
| 74 | constexpr uint32_t total_length = 64; | 75 | constexpr uint32_t total_length = 64; |
| 76 | +constexpr uint8_t buffer_lock_id = 0; | ||
| 75 | 77 | ||
| 76 | __global__ __vector__ void atomic_max_kernel(__gm__ float* output, __gm__ float* input) | 78 | __global__ __vector__ void atomic_max_kernel(__gm__ float* output, __gm__ float* input) |
| 77 | { | 79 | { |
| 78 | using namespace asc::te; | 80 | using namespace asc::te; |
| 79 | __ubuf__ float input_buffer[total_length]; | 81 | __ubuf__ float input_buffer[total_length]; |
| 80 | - __ubuf__ float output_buffer[total_length]; | ||
| 81 | 82 | ||
| 82 | auto layout = make_frame_layout<nd_ext_layout_ptn>(1, total_length); | 83 | auto layout = make_frame_layout<nd_ext_layout_ptn>(1, total_length); |
| 83 | auto input_gm = make_tensor(make_mem_ptr<location::gm>(input), layout); | 84 | auto input_gm = make_tensor(make_mem_ptr<location::gm>(input), layout); |
| 84 | auto output_gm = make_tensor(make_mem_ptr<location::gm>(output), layout); | 85 | auto output_gm = make_tensor(make_mem_ptr<location::gm>(output), layout); |
| 85 | auto input_ub = make_tensor(make_mem_ptr(input_buffer), layout); | 86 | auto input_ub = make_tensor(make_mem_ptr(input_buffer), layout); |
| 86 | - auto output_ub = make_tensor(make_mem_ptr(output_buffer), layout); | ||
| 87 | 87 | ||
| 88 | + mutex<pipe_t::PIPE_MTE2> copy_in_mutex(buffer_lock_id); | ||
| 89 | + copy_in_mutex.lock(); | ||
| 88 | copy(make_copy(copy_gm_to_ub{}), input_ub, input_gm); | 90 | copy(make_copy(copy_gm_to_ub{}), input_ub, input_gm); |
| 89 | - copy(make_copy(copy_gm_to_ub{}), output_ub, output_gm); | 91 | + copy_in_mutex.unlock(); |
| 90 | 92 | ||
| 91 | set_atomic_max<float>(); | 93 | set_atomic_max<float>(); |
| 94 | + mutex<pipe_t::PIPE_MTE3> copy_out_mutex(buffer_lock_id); | ||
| 95 | + copy_out_mutex.lock(); | ||
| 92 | copy(make_copy(copy_ub_to_gm{}), output_gm, input_ub); | 96 | copy(make_copy(copy_ub_to_gm{}), output_gm, input_ub); |
| 97 | + copy_out_mutex.unlock(); | ||
| 93 | disable_dma_atomic(); | 98 | disable_dma_atomic(); |
| 94 | } | 99 | } |
| 95 | ``` | 100 | ``` |
| @@ -59,12 +59,13 @@ __aicore__ inline void set_atomic_min() | |||
| 59 | - 开启后对后续所有目的地址为GM的搬运指令生效,可以调用[`disable_dma_atomic`](disable_dma_atomic.md)关闭。 | 59 | - 开启后对后续所有目的地址为GM的搬运指令生效,可以调用[`disable_dma_atomic`](disable_dma_atomic.md)关闭。 |
| 60 | - 本接口执行前不会自动将GM上已有数据置零。若期望原子取小前GM上的原始数据为零,需手动清零。 | 60 | - 本接口执行前不会自动将GM上已有数据置零。若期望原子取小前GM上的原始数据为零,需手动清零。 |
| 61 | - 本接口仅对后续通过MTE3、Fixpipe单元搬运且目的地址为GM的搬运指令生效,对标量写GM指令不生效。 | 61 | - 本接口仅对后续通过MTE3、Fixpipe单元搬运且目的地址为GM的搬运指令生效,对标量写GM指令不生效。 |
| 62 | +- 本接口与紧邻的后续搬运指令之间的同步由硬件保证。 | ||
| 62 | - 后续搬运指令的操作数据类型必须与`T`一致。 | 63 | - 后续搬运指令的操作数据类型必须与`T`一致。 |
| 63 | - 本接口不能保证后续搬运指令的执行顺序。 | 64 | - 本接口不能保证后续搬运指令的执行顺序。 |
| 64 | 65 | ||
| 65 | ## 调用示例 | 66 | ## 调用示例 |
| 66 | 67 | ||
| 67 | -以下示例先读取Global Memory上的初始值,再通过DMA原子取最小值将输入数据与初始值计算后写回Global Memory,最后关闭原子模式。 | 68 | +以下示例先通过同ID的MTE2/MTE3 mutex保证输入搬运完成后,再开启DMA原子取最小值,将输入数据与GM初始值计算后写回Global Memory,最后关闭原子模式。 |
| 68 | 69 | ||
| 69 | ```cpp | 70 | ```cpp |
| 70 | #include <cstdint> | 71 | #include <cstdint> |
| @@ -72,24 +73,28 @@ __aicore__ inline void set_atomic_min() | |||
| 72 | #include "tensor_api/tensor.h" | 73 | #include "tensor_api/tensor.h" |
| 73 | 74 | ||
| 74 | constexpr uint32_t total_length = 64; | 75 | constexpr uint32_t total_length = 64; |
| 76 | +constexpr uint8_t buffer_lock_id = 0; | ||
| 75 | 77 | ||
| 76 | __global__ __vector__ void atomic_min_kernel(__gm__ float* output, __gm__ float* input) | 78 | __global__ __vector__ void atomic_min_kernel(__gm__ float* output, __gm__ float* input) |
| 77 | { | 79 | { |
| 78 | using namespace asc::te; | 80 | using namespace asc::te; |
| 79 | __ubuf__ float input_buffer[total_length]; | 81 | __ubuf__ float input_buffer[total_length]; |
| 80 | - __ubuf__ float output_buffer[total_length]; | ||
| 81 | 82 | ||
| 82 | auto layout = make_frame_layout<nd_ext_layout_ptn>(1, total_length); | 83 | auto layout = make_frame_layout<nd_ext_layout_ptn>(1, total_length); |
| 83 | auto input_gm = make_tensor(make_mem_ptr<location::gm>(input), layout); | 84 | auto input_gm = make_tensor(make_mem_ptr<location::gm>(input), layout); |
| 84 | auto output_gm = make_tensor(make_mem_ptr<location::gm>(output), layout); | 85 | auto output_gm = make_tensor(make_mem_ptr<location::gm>(output), layout); |
| 85 | auto input_ub = make_tensor(make_mem_ptr(input_buffer), layout); | 86 | auto input_ub = make_tensor(make_mem_ptr(input_buffer), layout); |
| 86 | - auto output_ub = make_tensor(make_mem_ptr(output_buffer), layout); | ||
| 87 | 87 | ||
| 88 | + mutex<pipe_t::PIPE_MTE2> copy_in_mutex(buffer_lock_id); | ||
| 89 | + copy_in_mutex.lock(); | ||
| 88 | copy(make_copy(copy_gm_to_ub{}), input_ub, input_gm); | 90 | copy(make_copy(copy_gm_to_ub{}), input_ub, input_gm); |
| 89 | - copy(make_copy(copy_gm_to_ub{}), output_ub, output_gm); | 91 | + copy_in_mutex.unlock(); |
| 90 | 92 | ||
| 91 | set_atomic_min<float>(); | 93 | set_atomic_min<float>(); |
| 94 | + mutex<pipe_t::PIPE_MTE3> copy_out_mutex(buffer_lock_id); | ||
| 95 | + copy_out_mutex.lock(); | ||
| 92 | copy(make_copy(copy_ub_to_gm{}), output_gm, input_ub); | 96 | copy(make_copy(copy_ub_to_gm{}), output_gm, input_ub); |
| 97 | + copy_out_mutex.unlock(); | ||
| 93 | disable_dma_atomic(); | 98 | disable_dma_atomic(); |
| 94 | } | 99 | } |
| 95 | ``` | 100 | ``` |
| @@ -57,25 +57,28 @@ __aicore__ inline void set_copy_pad_val(T pad_value) | |||
| 57 | 57 | ||
| 58 | - 本接口仅在AIV上生效。 | 58 | - 本接口仅在AIV上生效。 |
| 59 | - 本接口只设置填充值,须先调用本接口,再执行对应的[GM到UB搬运](../reg_vector_compute/vector_compute_load/copy_gm_to_ub.md)。 | 59 | - 本接口只设置填充值,须先调用本接口,再执行对应的[GM到UB搬运](../reg_vector_compute/vector_compute_load/copy_gm_to_ub.md)。 |
| 60 | -- 填充值仅在[GM到UB搬运](../reg_vector_compute/vector_compute_load/copy_gm_to_ub.md)的`gm_to_ub_params`参数中`enable_constant_pad`成员设置为`true`时被取用。 | 60 | +- 填充值用于[GM到UB搬运](../reg_vector_compute/vector_compute_load/copy_gm_to_ub.md)产生的左右Padding区域及32字节对齐补齐区域。 |
| 61 | -- 填充值配置对后续所有满足条件的GM到UB对齐搬运生效,直到再次调用本接口覆盖配置。 | 61 | +- 填充值配置对后续所有满足条件的GM到UB对齐搬运生效,直到再次调用本接口覆盖配置;不同搬运任务间若需不同填充值,应在调用搬运接口前重新调用本接口设置。 |
| 62 | +- 在[GM到UB搬运](../reg_vector_compute/vector_compute_load/copy_gm_to_ub.md)的`gm_to_ub_params`参数中,当`left_padding_count`和`right_padding_count`均为0时,`enable_constant_pad`为`true`才使用本接口配置的值填充补齐区域;为`false`时,由硬件自动填充dummy假数据,本接口设置不生效。 | ||
| 63 | +- 在[GM到UB搬运](../reg_vector_compute/vector_compute_load/copy_gm_to_ub.md)的`gm_to_ub_params`参数中,当`left_padding_count`或`right_padding_count`非0时,`enable_constant_pad`不生效,必须在搬运前调用本接口配置填充值。左右Padding区域及右侧为32字节对齐继续补齐的区域均使用该值。 | ||
| 62 | 64 | ||
| 63 | ## 调用示例 | 65 | ## 调用示例 |
| 64 | 66 | ||
| 65 | -以下示例从Global Memory读取每行24个int8_t数据,在每行左侧填充3个、右侧填充5个-9后写入Global Memory。示例展示set_copy_pad_val与gm_to_ub_params的配合使用。 | 67 | +以下示例从Global Memory读取32行、每行24个int8_t数据,在每行左侧填充3个、右侧填充5个-9后写入Global Memory。示例展示set_copy_pad_val与gm_to_ub_params的配合使用,并使用mutex串行化GM到UB与UB到GM两个流水阶段。 |
| 66 | 68 | ||
| 67 | ```cpp | 69 | ```cpp |
| 68 | #include <cstdint> | 70 | #include <cstdint> |
| 69 | 71 | ||
| 70 | #include "tensor_api/tensor.h" | 72 | #include "tensor_api/tensor.h" |
| 71 | 73 | ||
| 72 | -constexpr uint32_t rows = 2; | 74 | +constexpr uint32_t rows = 32; |
| 73 | constexpr uint32_t source_columns = 24; | 75 | constexpr uint32_t source_columns = 24; |
| 74 | constexpr uint32_t padded_columns = 32; | 76 | constexpr uint32_t padded_columns = 32; |
| 75 | constexpr uint32_t padded_length = rows * padded_columns; | 77 | constexpr uint32_t padded_length = rows * padded_columns; |
| 76 | constexpr uint8_t left_padding = 3; | 78 | constexpr uint8_t left_padding = 3; |
| 77 | constexpr uint8_t right_padding = 5; | 79 | constexpr uint8_t right_padding = 5; |
| 78 | constexpr int8_t padding_value = -9; | 80 | constexpr int8_t padding_value = -9; |
| 81 | +constexpr uint8_t buffer_lock_id = 0; | ||
| 79 | 82 | ||
| 80 | __global__ __vector__ void padded_copy_kernel(__gm__ int8_t* src, __gm__ int8_t* dst) | 83 | __global__ __vector__ void padded_copy_kernel(__gm__ int8_t* src, __gm__ int8_t* dst) |
| 81 | { | 84 | { |
| @@ -90,7 +93,15 @@ __global__ __vector__ void padded_copy_kernel(__gm__ int8_t* src, __gm__ int8_t* | |||
| 90 | 93 | ||
| 91 | set_copy_pad_val<int8_t>(padding_value); | 94 | set_copy_pad_val<int8_t>(padding_value); |
| 92 | constexpr gm_to_ub_params padding_params{left_padding, right_padding, true}; | 95 | constexpr gm_to_ub_params padding_params{left_padding, right_padding, true}; |
| 96 | + | ||
| 97 | + mutex<pipe_t::PIPE_MTE2> copy_in_mutex(buffer_lock_id); | ||
| 98 | + copy_in_mutex.lock(); | ||
| 93 | copy(make_copy(copy_gm_to_ub{}).with(padding_params), dst_ub, src_gm); | 99 | copy(make_copy(copy_gm_to_ub{}).with(padding_params), dst_ub, src_gm); |
| 100 | + copy_in_mutex.unlock(); | ||
| 101 | + | ||
| 102 | + mutex<pipe_t::PIPE_MTE3> copy_out_mutex(buffer_lock_id); | ||
| 103 | + copy_out_mutex.lock(); | ||
| 94 | copy(make_copy(copy_ub_to_gm{}), dst_gm, dst_ub); | 104 | copy(make_copy(copy_ub_to_gm{}), dst_gm, dst_ub); |
| 105 | + copy_out_mutex.unlock(); | ||
| 95 | } | 106 | } |
| 96 | ``` | 107 | ``` |
| @@ -102,8 +102,8 @@ __aicore__ inline void fill(const Tensor& dst, const T& init_value) | |||
| 102 | 102 | ||
| 103 | #include "tensor_api/tensor.h" | 103 | #include "tensor_api/tensor.h" |
| 104 | 104 | ||
| 105 | -constexpr uint32_t rows = 16; | 105 | +constexpr uint32_t rows = 128; |
| 106 | -constexpr uint32_t columns = 16; | 106 | +constexpr uint32_t columns = 128; |
| 107 | constexpr uint32_t element_count = rows * columns; | 107 | constexpr uint32_t element_count = rows * columns; |
| 108 | 108 | ||
| 109 | __global__ __cube__ void fill_l1_kernel(__gm__ half* output) | 109 | __global__ __cube__ void fill_l1_kernel(__gm__ half* output) |
| @@ -114,23 +114,45 @@ __global__ __cube__ void fill_l1_kernel(__gm__ half* output) | |||
| 114 | __cb__ half l0_b_buffer[element_count]; | 114 | __cb__ half l0_b_buffer[element_count]; |
| 115 | __cc__ float l0_c_buffer[element_count]; | 115 | __cc__ float l0_c_buffer[element_count]; |
| 116 | 116 | ||
| 117 | - auto l1_layout = make_frame_layout<nz_layout_ptn, layout_trait_default<half>>(rows, columns); | 117 | + auto l1_layout = make_frame_layout<nz_layout_ptn, half>(rows, columns); |
| 118 | - auto l0_a_layout = make_frame_layout<nz_layout_ptn, layout_trait_default<half>>(rows, columns); | 118 | + auto l0_a_layout = make_frame_layout<nz_layout_ptn, half>(rows, columns); |
| 119 | - auto l0_b_layout = make_frame_layout<zn_layout_ptn, layout_trait_default<half>>(rows, columns); | 119 | + auto l0_b_layout = make_frame_layout<zn_layout_ptn, half>(rows, columns); |
| 120 | - auto l0_c_layout = make_frame_layout<nz_layout_ptn, layout_trait_default<float>>(rows, columns); | 120 | + auto l0_c_layout = make_frame_layout<nz_layout_ptn, float>(rows, columns); |
| 121 | - auto output_layout = make_frame_layout<nd_ext_layout_ptn, layout_trait_default<half>>(rows, columns); | 121 | + auto output_layout = make_frame_layout<nd_ext_layout_ptn, half>(rows, columns); |
| 122 | auto l1_tensor = make_tensor(make_mem_ptr(l1_buffer), l1_layout); | 122 | auto l1_tensor = make_tensor(make_mem_ptr(l1_buffer), l1_layout); |
| 123 | auto l0_a_tensor = make_tensor(make_mem_ptr(l0_a_buffer), l0_a_layout); | 123 | auto l0_a_tensor = make_tensor(make_mem_ptr(l0_a_buffer), l0_a_layout); |
| 124 | auto l0_b_tensor = make_tensor(make_mem_ptr(l0_b_buffer), l0_b_layout); | 124 | auto l0_b_tensor = make_tensor(make_mem_ptr(l0_b_buffer), l0_b_layout); |
| 125 | auto l0_c_tensor = make_tensor(make_mem_ptr(l0_c_buffer), l0_c_layout); | 125 | auto l0_c_tensor = make_tensor(make_mem_ptr(l0_c_buffer), l0_c_layout); |
| 126 | auto output_tensor = make_tensor(make_mem_ptr<location::gm>(output), output_layout); | 126 | auto output_tensor = make_tensor(make_mem_ptr<location::gm>(output), output_layout); |
| 127 | 127 | ||
| 128 | - fill(l1_tensor, static_cast<half>(2)); | 128 | + fill_params<half> params{}; |
| 129 | - copy(make_copy(copy_l1_to_l0a{}), l0_a_tensor, l1_tensor); | 129 | + params.repeat_times = rows; |
| 130 | - copy(make_copy(copy_l1_to_l0b{}), l0_b_tensor, l1_tensor); | 130 | + params.block_num = columns * sizeof(half) / 32; |
| 131 | + params.dst_gap = 0; | ||
| 132 | + params.init_value = static_cast<half>(2); | ||
| 133 | + | ||
| 134 | + mutex<PIPE_MTE2> fill_mutex(0); | ||
| 135 | + fill_mutex.lock(); | ||
| 136 | + fill(l1_tensor, params); | ||
| 137 | + fill_mutex.unlock(); | ||
| 138 | + | ||
| 139 | + mutex<PIPE_MTE1> l0_mutex(0); | ||
| 140 | + l0_mutex.lock(); | ||
| 141 | + copy(make_copy(copy_l1_to_l0a{}, l1_to_l0a_trait_default{}), l0_a_tensor, l1_tensor); | ||
| 142 | + copy(make_copy(copy_l1_to_l0b{}, l1_to_l0b_trait_default{}), l0_b_tensor, l1_tensor); | ||
| 143 | + l0_mutex.unlock(); | ||
| 144 | + | ||
| 145 | + mutex<PIPE_M> mmad_mutex(0); | ||
| 146 | + mmad_mutex.lock(); | ||
| 131 | mmad(make_mmad(mmad_operation{}, mmad_trait_default{}).with(mmad_params{rows, columns, columns, | 147 | mmad(make_mmad(mmad_operation{}, mmad_trait_default{}).with(mmad_params{rows, columns, columns, |
| 132 | unit_flag_mode::disable, true}), | 148 | unit_flag_mode::disable, true}), |
| 133 | l0_c_tensor, l0_a_tensor, l0_b_tensor); | 149 | l0_c_tensor, l0_a_tensor, l0_b_tensor); |
| 150 | + mmad_mutex.unlock(); | ||
| 151 | + | ||
| 152 | + mutex<PIPE_FIX> output_mutex(0); | ||
| 153 | + output_mutex.lock(); | ||
| 134 | copy(make_copy(copy_l0c_to_gm{}, l0c_to_gm_trait_default{}), output_tensor, l0_c_tensor); | 154 | copy(make_copy(copy_l0c_to_gm{}, l0c_to_gm_trait_default{}), output_tensor, l0_c_tensor); |
| 155 | + output_mutex.unlock(); | ||
| 156 | + sync_pipe(PIPE_ALL); | ||
| 135 | } | 157 | } |
| 136 | ``` | 158 | ``` |
| @@ -115,13 +115,14 @@ constexpr int64_t sync_flag_id = 0; | |||
| 115 | __global__ __mix__(1, 2) void block_sync_kernel(__gm__ float* output) | 115 | __global__ __mix__(1, 2) void block_sync_kernel(__gm__ float* output) |
| 116 | { | 116 | { |
| 117 | using namespace asc::te; | 117 | using namespace asc::te; |
| 118 | + init_soc_state(); | ||
| 118 | __ubuf__ float data_buffer[total_length]; | 119 | __ubuf__ float data_buffer[total_length]; |
| 119 | 120 | ||
| 120 | auto layout = make_layout(make_shape(total_length)); | 121 | auto layout = make_layout(make_shape(total_length)); |
| 121 | auto data_ub = make_tensor(make_mem_ptr(data_buffer), layout); | 122 | auto data_ub = make_tensor(make_mem_ptr(data_buffer), layout); |
| 122 | auto output_gm = make_tensor(make_mem_ptr<location::gm>(output), layout); | 123 | auto output_gm = make_tensor(make_mem_ptr<location::gm>(output), layout); |
| 123 | 124 | ||
| 124 | - if ASC_IS_AIV { | 125 | + if ASCEND_IS_AIV { |
| 125 | for (uint32_t i = 0; i < total_length; ++i) { | 126 | for (uint32_t i = 0; i < total_length; ++i) { |
| 126 | data_buffer[i] = 1.0f; | 127 | data_buffer[i] = 1.0f; |
| 127 | } | 128 | } |
| @@ -129,9 +130,10 @@ __global__ __mix__(1, 2) void block_sync_kernel(__gm__ float* output) | |||
| 129 | sync_block_arrive(pipe_t::PIPE_MTE3, sync_flag_id); | 130 | sync_block_arrive(pipe_t::PIPE_MTE3, sync_flag_id); |
| 130 | } | 131 | } |
| 131 | 132 | ||
| 132 | - if ASC_IS_AIC { | 133 | + if ASCEND_IS_AIC { |
| 133 | - sync_block_wait(pipe_t::PIPE_MTE1, sync_flag_id); | 134 | + sync_block_wait(pipe_t::PIPE_S, sync_flag_id); |
| 134 | output[0] = 1.0f; | 135 | output[0] = 1.0f; |
| 135 | } | 136 | } |
| 137 | + sync_pipe(pipe_t::PIPE_ALL); | ||
| 136 | } | 138 | } |
| 137 | ``` | 139 | ``` |
| @@ -50,7 +50,8 @@ __aicore__ inline void sync_data_barrier(mem_dsb_t scope) | |||
| 50 | 50 | ||
| 51 | ## 约束说明 | 51 | ## 约束说明 |
| 52 | 52 | ||
| 53 | -`DSB_ALL`只等待GM和UB访问指令,不包括其他内存位置,例如L0C Buffer、L1 Buffer。 | 53 | +- `mem_dsb_t`类型的入参必须直接传递枚举值字面量。 |
| 54 | +- 等待的内存位置取值为`DSB_ALL`时,只能等待GM和Unified Buffer(UB)的访问指令,不包括其他内存位置,例如L0C Buffer、L1 Buffer。 | ||
| 54 | 55 | ||
| 55 | ## 调用示例 | 56 | ## 调用示例 |
| 56 | 57 | ||
| @@ -55,17 +55,19 @@ PIPE_S | |||
| 55 | ## 约束说明 | 55 | ## 约束说明 |
| 56 | 56 | ||
| 57 | - Scalar流水之间的同步由硬件自动保证,本接口不支持`PIPE_S`。 | 57 | - Scalar流水之间的同步由硬件自动保证,本接口不支持`PIPE_S`。 |
| 58 | +- `PIPE_V`之间的同步由硬件自动保证。 | ||
| 58 | - `sync_pipe(pipe_t::PIPE_ALL)`会等待所有流水中此前提交的指令完成,可能影响性能。若只需等待单条流水,应传入对应流水值。 | 59 | - `sync_pipe(pipe_t::PIPE_ALL)`会等待所有流水中此前提交的指令完成,可能影响性能。若只需等待单条流水,应传入对应流水值。 |
| 59 | - `pipe_t::PIPE_MTE2`和`pipe_t::PIPE_MTE3`在搬运目的地址重叠时,开发者需要插入同步,保证多条搬运指令串行执行。 | 60 | - `pipe_t::PIPE_MTE2`和`pipe_t::PIPE_MTE3`在搬运目的地址重叠时,开发者需要插入同步,保证多条搬运指令串行执行。 |
| 60 | 61 | ||
| 61 | ## 调用示例 | 62 | ## 调用示例 |
| 62 | 63 | ||
| 63 | -以下示例完成GM到UB搬入、UB内计算和UB到GM搬出后,调用sync_pipe等待所有流水中的前序指令完成。 | 64 | +以下示例先通过同ID的MTE2/MTE3 mutex完成GM到UB搬入和UB到GM搬出,最后调用sync_pipe等待所有流水中的前序指令完成。 |
| 64 | 65 | ||
| 65 | ```cpp | 66 | ```cpp |
| 66 | #include "tensor_api/tensor.h" | 67 | #include "tensor_api/tensor.h" |
| 67 | 68 | ||
| 68 | constexpr uint32_t total_length = 64; | 69 | constexpr uint32_t total_length = 64; |
| 70 | +constexpr uint8_t buffer_lock_id = 0; | ||
| 69 | 71 | ||
| 70 | __global__ __vector__ void sync_pipe_kernel(__gm__ float* src, __gm__ float* dst) | 72 | __global__ __vector__ void sync_pipe_kernel(__gm__ float* src, __gm__ float* dst) |
| 71 | { | 73 | { |
| @@ -77,8 +79,15 @@ __global__ __vector__ void sync_pipe_kernel(__gm__ float* src, __gm__ float* dst | |||
| 77 | auto dst_gm = make_tensor(make_mem_ptr<location::gm>(dst), layout); | 79 | auto dst_gm = make_tensor(make_mem_ptr<location::gm>(dst), layout); |
| 78 | auto ub_tensor = make_tensor(make_mem_ptr(buffer), layout); | 80 | auto ub_tensor = make_tensor(make_mem_ptr(buffer), layout); |
| 79 | 81 | ||
| 82 | + mutex<pipe_t::PIPE_MTE2> copy_in_mutex(buffer_lock_id); | ||
| 83 | + copy_in_mutex.lock(); | ||
| 80 | copy(make_copy(copy_gm_to_ub{}), ub_tensor, src_gm); | 84 | copy(make_copy(copy_gm_to_ub{}), ub_tensor, src_gm); |
| 85 | + copy_in_mutex.unlock(); | ||
| 86 | + | ||
| 87 | + mutex<pipe_t::PIPE_MTE3> copy_out_mutex(buffer_lock_id); | ||
| 88 | + copy_out_mutex.lock(); | ||
| 81 | copy(make_copy(copy_ub_to_gm{}), dst_gm, ub_tensor); | 89 | copy(make_copy(copy_ub_to_gm{}), dst_gm, ub_tensor); |
| 90 | + copy_out_mutex.unlock(); | ||
| 82 | sync_pipe(pipe_t::PIPE_ALL); | 91 | sync_pipe(pipe_t::PIPE_ALL); |
| 83 | } | 92 | } |
| 84 | ``` | 93 | ``` |