当前位置:   article > 正文

昇腾AI原生创新算子挑战赛(S1赛季)复盘-Cross算子_华为升腾算子大赛

华为升腾算子大赛
  • 正文前感谢昇腾各位工作人员,没有你们的辛勤就没有我们的进步
  • 本文立意交流大赛Cross算子编译过程
  • 这道题难点在于如何实现算法公式,以及其中广播逻辑
  • 本代码源于0xcccccccc仓库代码,做了部分修改,再次感谢0xcccccccc分享

cke_754.png

  • 注意有以下要求
  • 返回沿着维度 dim 上,两个张量 input 和 other 的向量积 (叉积), input 和 other 必须有相同的形状,且指定的 dim 维上 size 必须为 3. 
  • 如果不指定 dim, 则默认为第一个尺度为 3 的维.

cke_1832.png

  • 看公式很简单,只需要想着如何去实现即可
  • 这里沿用了GreaterEqual中的index查找思路,将广播数据用原非广播数据替代
  1. __aicore__ inline int64_t get_index(int64_t i, int64_t j) {
  2. int64_t index = 0, step = 1, offset = 1;
  3. for (int64_t k = numshapes - 1; k >= 0; --k) {
  4. const int64_t idx = i / step % outshape[k];
  5. step *= outshape[k];
  6. index += idx % shape[j][k] * offset;
  7. offset *= shape[j][k];
  8. }
  9. return index;
  10. }

  1. __aicore__ inline void Process() {
  2. using F = typename map<T>::type;
  3. for (int64_t i = 0; i < maxbatchSize; ++i) {
  4. for (int64_t j = 0; j < maxstepSize; ++j) {
  5. auto index1 = i * 3 * maxstepSize + 0 * maxstepSize + j;
  6. auto index2 = i * 3 * maxstepSize + 1 * maxstepSize + j;
  7. auto index3 = i * 3 * maxstepSize + 2 * maxstepSize + j;
  8. F a1 = Gm_x1.GetValue(get_index(index1, 0));
  9. F a2 = Gm_x1.GetValue(get_index(index2, 0));
  10. F a3 = Gm_x1.GetValue(get_index(index3, 0));
  11. F b1 = Gm_x2.GetValue(get_index(index1, 1));
  12. F b2 = Gm_x2.GetValue(get_index(index2, 1));
  13. F b3 = Gm_x2.GetValue(get_index(index3, 1));
  14. auto result1 = a2 * b3 - a3 * b2;
  15. auto result2 = a3 * b1 - a1 * b3;
  16. auto result3 = a1 * b2 - a2 * b1;
  17. Gm_y(index1) = (T)result1;
  18. Gm_y(index2) = (T)result2;
  19. Gm_y(index3) = (T)result3;
  20. }
  21. }
  22. }

  • 使用如下数据进行验证
  1. x1_tensor = np.random.uniform(-1e9, 1e9, [4, 3, 6, 5, 7, 8, 3]).astype(np.int8)
  2. x2_tensor = np.random.uniform(-1e9, 1e9, [1, 3, 1, 1, 1, 1, 1]).astype(np.int8)

cke_48672.png

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号