haojin2 commented on a change in pull request #15987: Numpy add numpy op rot90
URL: https://github.com/apache/incubator-mxnet/pull/15987#discussion_r318874434
 
 

 ##########
 File path: src/operator/numpy/np_matrix_op-inl.h
 ##########
 @@ -60,6 +60,246 @@ void NumpyTranspose(const nnvm::NodeAttrs& attrs,
   }
 }
 
+struct NumpyRot90Param : public dmlc::Parameter<NumpyRot90Param> {
+  int k;
+  dmlc::optional<mxnet::TShape> axes;
+  DMLC_DECLARE_PARAMETER(NumpyRot90Param) {
+    DMLC_DECLARE_FIELD(k)
+    .set_default(1)
+    .describe("Number of times the array is rotated by 90 degrees.");
+    DMLC_DECLARE_FIELD(axes)
+    .set_default(dmlc::optional<mxnet::TShape>())
+    .describe(" The array is rotated in the plane defined by the axes. Axes 
must be different.");
+  }
+};
+
+struct rot90reverse {
+  MSHADOW_XINLINE static index_t ReverseIndex(index_t idx,
+                                              index_t nreversedim,
+                                              const index_t * stride_,
+                                              const index_t * trailing_) {
+    index_t outputIndex = idx;
+    for (index_t i = 0; i < nreversedim; ++i) {
+      const index_t low = outputIndex % trailing_[i];
+      index_t high = outputIndex / trailing_[i];
+      const index_t x = high % stride_[i];
+      high /= stride_[i];
+      outputIndex = (high * stride_[i] + stride_[i] - 1 - x) * trailing_[i] + 
low;
+    }
+    return outputIndex;
+  }
+  template<typename DType>
+  MSHADOW_XINLINE  static void Map(index_t index, index_t nreversedim, const 
DType *src, DType *dst,
+                                   const index_t * stride_,
+                                   const index_t * trailing_) {
+    index_t new_idx = ReverseIndex(index, nreversedim, stride_, trailing_);
+    dst[new_idx] = src[index];
+  }
+};
+
+template<typename xpu>
+void NumpyRot90ComputeFlipIml(const OpContext& ctx,
+                              const std::vector<TBlob>& inputs,
+                              const std::vector<OpReqType>& req,
+                              const std::vector<TBlob>& outputs,
+                              const index_t axis0, const index_t axis1) {
+  using namespace mshadow;
+  using namespace mxnet_op;
+
+  const mxnet::TShape& ishape = inputs[0].shape_;
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+
+  std::vector<index_t> stride_(2);
+  std::vector<index_t>  trailing_(2);
+  index_t reverse_index = 0;
+  std::vector<index_t> temp{axis0, axis1};
+  for (int axis : temp) {
+    stride_[reverse_index] = ishape[axis];
+    trailing_[reverse_index] = 1;
+    for (int i2 = axis + 1; i2 < ishape.ndim(); ++i2) {
+      trailing_[reverse_index] *= ishape[i2];
+    }
+    reverse_index++;
+  }
+
+  index_t workspace_size = 2 * sizeof(index_t);
+  Tensor<xpu, 1, char> workspace =
+      ctx.requested[0].get_space_typed<xpu, 1, char>(Shape1(2 * 
workspace_size), s);
+  Tensor<cpu, 1, index_t> stride_cpu_tensor(stride_.data(), 
Shape1(stride_.size()));
+  Tensor<xpu, 1, index_t> stride_xpu_tensor(
+      reinterpret_cast<index_t*>(workspace.dptr_), Shape1(stride_.size()));
+  Tensor<cpu, 1, index_t> trailing_cpu_tensor(trailing_.data(), 
Shape1(trailing_.size()));
+  Tensor<xpu, 1, index_t> trailing_xpu_tensor(
+      reinterpret_cast<index_t*>(workspace.dptr_ + workspace_size), 
Shape1(trailing_.size()));
+
+  mshadow::Copy(stride_xpu_tensor, stride_cpu_tensor, s);
+  mshadow::Copy(trailing_xpu_tensor, trailing_cpu_tensor, s);
+  MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, DType, {
+                      Kernel<rot90reverse, xpu>::Launch(s, inputs[0].Size(), 
reverse_index,
 
 Review comment:
   Pay attention to the indentation.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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