已合并
add_example测试输入多样化 #2004
Sun创建于 3月31日
add_example测试输入多样化 #2004
已合并
共 4 个文件变更+59-26
| @@ -83,15 +83,17 @@ bash build.sh --run_example add_example eager cust --vendor_name=custom | |||
| 83 | 预期输出:打印算子`AddExample`的加法计算结果,表明算子已成功部署并正确执行。 | 83 | 预期输出:打印算子`AddExample`的加法计算结果,表明算子已成功部署并正确执行。 |
| 84 | 84 | ||
| 85 | ``` | 85 | ``` |
| 86 | -add_example first input[0] is: 1.000000, second input[0] is: 1.000000, result[0] is: 2.000000 | 86 | +Print the first 10 groups of data: |
| 87 | -add_example first input[1] is: 1.000000, second input[1] is: 1.000000, result[1] is: 2.000000 | 87 | +add_example first input[0] is: 1.000000, second input[0] is: -257.000000, result[0] is: -256.000000 |
| 88 | -add_example first input[2] is: 1.000000, second input[2] is: 1.000000, result[2] is: 2.000000 | 88 | +add_example first input[1] is: 2.000000, second input[1] is: 608.000000, result[1] is: 610.000000 |
| 89 | -add_example first input[3] is: 1.000000, second input[3] is: 1.000000, result[3] is: 2.000000 | 89 | +add_example first input[2] is: 3.000000, second input[2] is: 924.000000, result[2] is: 927.000000 |
| 90 | -add_example first input[4] is: 1.000000, second input[4] is: 1.000000, result[4] is: 2.000000 | 90 | +add_example first input[3] is: 4.000000, second input[3] is: -649.000000, result[3] is: -645.000000 |
| 91 | -add_example first input[5] is: 1.000000, second input[5] is: 1.000000, result[5] is: 2.000000 | 91 | +add_example first input[4] is: 5.000000, second input[4] is: 475.000000, result[4] is: 480.000000 |
| 92 | -add_example first input[6] is: 1.000000, second input[6] is: 1.000000, result[6] is: 2.000000 | 92 | +add_example first input[5] is: 6.000000, second input[5] is: 573.000000, result[5] is: 579.000000 |
| 93 | -add_example first input[7] is: 1.000000, second input[7] is: 1.000000, result[7] is: 2.000000 | 93 | +add_example first input[6] is: 7.000000, second input[6] is: 202.000000, result[6] is: 209.000000 |
| 94 | -... | 94 | +add_example first input[7] is: 8.000000, second input[7] is: 198.000000, result[7] is: 206.000000 |
| 95 | +add_example first input[8] is: 9.000000, second input[8] is: -705.000000, result[8] is: -696.000000 | ||
| 96 | +add_example first input[9] is: 10.000000, second input[9] is: -111.000000, result[9] is: -101.000000 | ||
| 95 | ``` | 97 | ``` |
| 96 | 98 | ||
| 97 | ## 二、算子开发 | 99 | ## 二、算子开发 |
| @@ -60,13 +60,17 @@ cannsim.log | |||
| 60 | 从仿真工具日志文件可以看到示例中的打印信息: | 60 | 从仿真工具日志文件可以看到示例中的打印信息: |
| 61 | 61 | ||
| 62 | ``` | 62 | ``` |
| 63 | -add_example first input[0] is: 1.000000, second input[0] is: 1.000000, result[0] is: 2.000000 | 63 | +Print the first 10 groups of data: |
| 64 | -add_example first input[1] is: 1.000000, second input[1] is: 1.000000, result[1] is: 2.000000 | 64 | +add_example first input[0] is: 1.000000, second input[0] is: -257.000000, result[0] is: -256.000000 |
| 65 | -add_example first input[2] is: 1.000000, second input[2] is: 1.000000, result[2] is: 2.000000 | 65 | +add_example first input[1] is: 2.000000, second input[1] is: 608.000000, result[1] is: 610.000000 |
| 66 | -add_example first input[3] is: 1.000000, second input[3] is: 1.000000, result[3] is: 2.000000 | 66 | +add_example first input[2] is: 3.000000, second input[2] is: 924.000000, result[2] is: 927.000000 |
| 67 | -add_example first input[4] is: 1.000000, second input[4] is: 1.000000, result[4] is: 2.000000 | 67 | +add_example first input[3] is: 4.000000, second input[3] is: -649.000000, result[3] is: -645.000000 |
| 68 | -add_example first input[5] is: 1.000000, second input[5] is: 1.000000, result[5] is: 2.000000 | 68 | +add_example first input[4] is: 5.000000, second input[4] is: 475.000000, result[4] is: 480.000000 |
| 69 | -add_example first input[6] is: 1.000000, second input[6] is: 1.000000, result[6] is: 2.000000 | 69 | +add_example first input[5] is: 6.000000, second input[5] is: 573.000000, result[5] is: 579.000000 |
| 70 | +add_example first input[6] is: 7.000000, second input[6] is: 202.000000, result[6] is: 209.000000 | ||
| 71 | +add_example first input[7] is: 8.000000, second input[7] is: 198.000000, result[7] is: 206.000000 | ||
| 72 | +add_example first input[8] is: 9.000000, second input[8] is: -705.000000, result[8] is: -696.000000 | ||
| 73 | +add_example first input[9] is: 10.000000, second input[9] is: -111.000000, result[9] is: -101.000000 | ||
| 70 | ``` | 74 | ``` |
| 71 | 75 | ||
| 72 | ## 查看性能流水 | 76 | ## 查看性能流水 |
| @@ -202,16 +202,25 @@ int main() | |||
| 202 | 4. 运行run.sh文件。 | 202 | 4. 运行run.sh文件。 |
| 203 | 在run.sh文件所在路径执行如下命令: | 203 | 在run.sh文件所在路径执行如下命令: |
| 204 | 204 | ||
| 205 | - ```bash | 205 | + ```bash |
| 206 | - bash run.sh | 206 | + bash run.sh |
| 207 | - ``` | 207 | + ``` |
| 208 | 208 | ||
| 209 | 默认在当前执行路径 `/build/bin`下生成可执行文件test\_aclnn\_add\_example,运行结果如下: | 209 | 默认在当前执行路径 `/build/bin`下生成可执行文件test\_aclnn\_add\_example,运行结果如下: |
| 210 | 210 | ||
| 211 | - ``` | 211 | + ``` |
| 212 | - mean result[2046] is 2.000000 | 212 | + Print the first 10 groups of data: |
| 213 | - mean result[2047] is 2.000000 | 213 | + add_example first input[0] is: 1.000000, second input[0] is: -257.000000, result[0] is: -256.000000 |
| 214 | - ``` | 214 | + add_example first input[1] is: 2.000000, second input[1] is: 608.000000, result[1] is: 610.000000 |
| 215 | + add_example first input[2] is: 3.000000, second input[2] is: 924.000000, result[2] is: 927.000000 | ||
| 216 | + add_example first input[3] is: 4.000000, second input[3] is: -649.000000, result[3] is: -645.000000 | ||
| 217 | + add_example first input[4] is: 5.000000, second input[4] is: 475.000000, result[4] is: 480.000000 | ||
| 218 | + add_example first input[5] is: 6.000000, second input[5] is: 573.000000, result[5] is: 579.000000 | ||
| 219 | + add_example first input[6] is: 7.000000, second input[6] is: 202.000000, result[6] is: 209.000000 | ||
| 220 | + add_example first input[7] is: 8.000000, second input[7] is: 198.000000, result[7] is: 206.000000 | ||
| 221 | + add_example first input[8] is: 9.000000, second input[8] is: -705.000000, result[8] is: -696.000000 | ||
| 222 | + add_example first input[9] is: 10.000000, second input[9] is: -111.000000, result[9] is: -101.000000 | ||
| 223 | + ``` | ||
| 215 | 224 | ||
| 216 | ## 图模式调用 | 225 | ## 图模式调用 |
| 217 | 226 | ||
| @@ -10,9 +10,14 @@ | |||
| 10 | 10 | ||
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | + | ||
| 14 | + | ||
| 15 | + | ||
| 13 | 16 | ||
| 14 | 17 | ||
| 15 | 18 | ||
| 19 | +constexpr uint32_t PRINT_LENGTH = 10; | ||
| 20 | + | ||
| 16 | 21 | ||
| 17 | do { \ | 22 | do { \ |
| 18 | if (!(cond)) { \ | 23 | if (!(cond)) { \ |
| @@ -43,7 +48,8 @@ void PrintOutResult(std::vector<int64_t>& shape, void** deviceAddr, | |||
| 43 | resultData.data(), resultData.size() * sizeof(resultData[0]), *deviceAddr, size * sizeof(resultData[0]), | 48 | resultData.data(), resultData.size() * sizeof(resultData[0]), *deviceAddr, size * sizeof(resultData[0]), |
| 44 | ACL_MEMCPY_DEVICE_TO_HOST); | 49 | ACL_MEMCPY_DEVICE_TO_HOST); |
| 45 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return); | 50 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return); |
| 46 | - for (int64_t i = 0; i < size; i++) { | 51 | + LOG_PRINT("Print the first %u groups of data: \n", PRINT_LENGTH); |
| 52 | + for (int64_t i = 0; i < PRINT_LENGTH; i++) { | ||
| 47 | LOG_PRINT("add_example first input[%ld] is: %f, second input[%ld] is: %f, result[%ld] is: %f\n", i, selfXHostData[i], i, selfYHostData[i], i, resultData[i]); | 53 | LOG_PRINT("add_example first input[%ld] is: %f, second input[%ld] is: %f, result[%ld] is: %f\n", i, selfXHostData[i], i, selfYHostData[i], i, resultData[i]); |
| 48 | } | 54 | } |
| 49 | } | 55 | } |
| @@ -99,14 +105,26 @@ int main() | |||
| 99 | void* selfXDeviceAddr = nullptr; | 105 | void* selfXDeviceAddr = nullptr; |
| 100 | // 当前样例算子未进行shape、dtype全泛化,其他输入场景可能存在不支持情况 | 106 | // 当前样例算子未进行shape、dtype全泛化,其他输入场景可能存在不支持情况 |
| 101 | std::vector<int64_t> selfXShape = {32, 4, 4, 4}; | 107 | std::vector<int64_t> selfXShape = {32, 4, 4, 4}; |
| 102 | - std::vector<float> selfXHostData(2048, 1); | 108 | + std::vector<float> selfXHostData(2048); |
| 109 | + // 此行代码仅为填充输入的值,从指定初始值1.0递增,步长为1的等差数列,也可自行选用其他填充方法 | ||
| 110 | + std::iota(selfXHostData.begin(), selfXHostData.end(), 1.0f); | ||
| 111 | + | ||
| 103 | ret = CreateAclTensor(selfXHostData, selfXShape, &selfXDeviceAddr, aclDataType::ACL_FLOAT, &selfX); | 112 | ret = CreateAclTensor(selfXHostData, selfXShape, &selfXDeviceAddr, aclDataType::ACL_FLOAT, &selfX); |
| 104 | CHECK_RET(ret == ACL_SUCCESS, return ret); | 113 | CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 105 | 114 | ||
| 106 | aclTensor* selfY = nullptr; | 115 | aclTensor* selfY = nullptr; |
| 107 | void* selfYDeviceAddr = nullptr; | 116 | void* selfYDeviceAddr = nullptr; |
| 108 | std::vector<int64_t> selfYShape = {32, 4, 4, 4}; | 117 | std::vector<int64_t> selfYShape = {32, 4, 4, 4}; |
| 109 | - std::vector<float> selfYHostData(2048, 1); | 118 | + std::vector<float> selfYHostData(2048); |
| 119 | + // 指定随机数种子,生成指定范围内的随机数 | ||
| 120 | + const uint32_t seed = 42; | ||
| 121 | + std::mt19937 gen(seed); | ||
| 122 | + std::uniform_int_distribution<int> dist(-1024, 1024); | ||
| 123 | + std::generate(selfYHostData.begin(), selfYHostData.end(), [&dist, &gen]() { | ||
| 124 | + return dist(gen); | ||
| 125 | + }); | ||
| 126 | + | ||
| 127 | + | ||
| 110 | ret = CreateAclTensor(selfYHostData, selfYShape, &selfYDeviceAddr, aclDataType::ACL_FLOAT, &selfY); | 128 | ret = CreateAclTensor(selfYHostData, selfYShape, &selfYDeviceAddr, aclDataType::ACL_FLOAT, &selfY); |
| 111 | CHECK_RET(ret == ACL_SUCCESS, return ret); | 129 | CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 112 | 130 | ||