samskalicky 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_r215381506
 
 

 ##########
 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:
   Could this be simplified to something like:
   
   int idx = 0;
   for(int i=0; i<ndim; i++) {
   ___if(i != axis1 && i != axis2)
   ______oshape[idx++] = ishape[i]
   }
   oshape[ndim-2] = s;
   
   Also, the final dimension assignment on line 104 looks wrong. Shouldnt it be 
oshape[n_dim - 2] = s;? Since the output tensor will be 1 dimension smaller 
than the input tensor (and off-by-1 for 0-based versus ndim being the count).

----------------------------------------------------------------
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