矢量编程时使能Vector Core
针对
本节将重点介绍如何使能
- 完成算子kernel侧开发时,需要通过宏KERNEL_TASK_TYPE_DEFAULT使能Vector Core,算子执行时会同时启动AI Core和Vector Core。如下的代码样例展示了使能Vector Core的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
extern "C" __global__ __aicore__ void add_custom(__gm__ uint8_t *x, __gm__ uint8_t *y, __gm__ uint8_t *z, __gm__ uint8_t *workspace, __gm__ uint8_t *tiling) { GET_TILING_DATA(tilingData, tiling); if (workspace == nullptr) { return; } GM_ADDR usr = AscendC::GetUserWorkspace(workspace); KernelAdd op; op.Init(x, y, z, tilingData.blockDim, tilingData.totalLength, tilingData.tileNum); KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_MIX_VECTOR_CORE); // 使能VectorCore if (TILING_KEY_IS(1)) { op.Process1(); } else if (TILING_KEY_IS(2)) { op.Process2(); } // ... }
- 完成host侧tiling开发时,设置的blockDim代表的是AI Core和Vector Core的总数,比如用户在host侧设置blockDim为10,则会启动总数为10的AI Core和Vector Core;为保证启动Vector Core,设置数值应大于AI Core的核数。您可以通过GetCoreNumAic接口获取AI Core的核数,GetCoreNumVector接口获取Vector Core的核数。 如下代码片段,分别为使用kernel直调工程和自定义算子工程时的设置样例,此处设置为AI Core和Vector Core的总和,表示所有AI Core和Vector Core都启动。
- kernel直调工程
1 2 3 4 5 6 7 8
auto ascendcPlatform = platform_ascendc::PlatformAscendCManager::GetInstance(); auto totalCoreNum = ascendcPlatform.GetCoreNumAic(); // ASCENDXXX请替换为实际的版本型号 if (ascendcPlatform.GetSocVersion() == platform_ascendc::SocVersion::ASCENDXXX) { totalCoreNum = totalCoreNum + ascendcPlatform.GetCoreNumVector(); } ... kernel_name<<<totalCoreNum , l2ctrl, stream>>>(argument list);
- 自定义算子工程
1 2 3 4 5 6 7 8 9 10 11 12
// 配套的host侧tiling函数示例: ge::graphStatus TilingFunc(gert::TilingContext* context) { // 使能VectorCore,将blockDim置为AI Core中vector核数 + Vector Core中的vector核数 auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfo); auto totalCoreNum = ascendcPlatform.GetCoreNumAic(); // ASCENDXXX请替换为实际的版本型号 if (ascendcPlatform.GetSocVersion() == platform_ascendc::SocVersion::ASCENDXXX) { totalCoreNum = totalCoreNum + ascendcPlatform.GetCoreNumVector(); } context->SetBlockDim(totalCoreNum); }
- kernel直调工程
父主题: 附录