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_r215823798
 
 

 ##########
 File path: src/operator/tensor/diag_op-inl.h
 ##########
 @@ -104,26 +138,26 @@ inline bool DiagOpType(const nnvm::NodeAttrs& attrs,
   return (*out_attrs)[0] != -1;
 }
 
-template<int req>
+template<int ndim, int req, bool back>
 struct diag {
   template<typename DType>
   MSHADOW_XINLINE static void Map(int i, DType* out, const DType* a,
-                                  mshadow::Shape<2> ishape, int k) {
+                                  mshadow::Shape<ndim> oshape,
+                                  mshadow::Shape<ndim> ishape,
+                                  int stride, int offset,
+                                  int base) {
     using namespace mxnet_op;
-    int j = 0;
-    if (k > 0) {
-      j = ravel(mshadow::Shape2(i, i + k), ishape);
-    } else if (k < 0) {
-      j = ravel(mshadow::Shape2(i - k, i), ishape);
+    int idx = i / base;
+    int j = ravel(unravel(idx, oshape), ishape) + offset + stride * (i - idx * 
base);
 
 Review comment:
   @samskalicky The base is equal to the size of the diagonal. The output is 
reshaped to `oshape + (base,)`, and the input is reshaped to `ishape`. `idx` is 
therefore the part corresponding to `oshape` and `ishape`. And it corresponds 
to the mth element in the diagonal, where `m = i % base = I - idx * base`. We 
need to multiply it by `stride`, which is basically the sum of the strides of 
`axis1` and `axis2` in order compute the index of the element we want. However, 
because the diagonal we want is not necessarily the main diagonal (only when `k 
= 0`), we need to add to the result an offset. 

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