jasonyu1996 commented on a change in pull request #12430: [MXNET-882] Support 
for N-d arrays added to diag op.
URL: https://github.com/apache/incubator-mxnet/pull/12430#discussion_r215472878
 
 

 ##########
 File path: src/operator/tensor/diag_op-inl.h
 ##########
 @@ -69,7 +82,28 @@ inline TShape DiagShapeImpl(const TShape& ishape, const 
nnvm::dim_t k) {
     s = 0;
   }
 
-  return TShape({s});
+  auto x1 = CheckAxis(axis1, ishape.ndim());
+  auto x2 = CheckAxis(axis2, ishape.ndim());
+
+  CHECK_NE(x1, x2) << "axis1 and axis2 cannot refer the the same axis " << x1;
+
+  if (x1 > x2) {
+    std::swap(x1, x2);
+  }
+
+  int n_dim = static_cast<int>(ishape.ndim()) - 1;
+  TShape oshape(n_dim);
+
+  // remove axis1 and axis2 and append the new axis to the end
+  for (int i = 0; i < x1; i ++)
+    oshape[i] = ishape[i];
+  for (int i = x1 + 1; i < x2; i ++)
+    oshape[i - 1] = ishape[i];
+  for (int i = x2 + 1; i <= n_dim; i ++)
+    oshape[i - 2] = ishape[i];
+  oshape[n_dim - 1] = s;
 
 Review comment:
   Yes, the code could be simplified that way, but that does not necessarily 
make improvements in my opinion. The original implementation is equally easy to 
read and fairly efficient. As for the final assignment, note that `n_dim` is 
already the dimension of the input tensor minus 1, so I think it is fine.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to