sxjscience commented on a change in pull request #16979: [Bugfix] [Numpy] Add
`kAddTo` and kNullOp to Transpose
URL: https://github.com/apache/incubator-mxnet/pull/16979#discussion_r358950297
##########
File path: src/operator/tensor/matrix_op-inl.h
##########
@@ -323,62 +336,79 @@ void TransposeImpl(RunContext ctx,
// Example: (0, 2, 3, 1) or (0, 3, 1, 2), but not (0, 2, 1, 3).
if (isPseudo2DTranspose(axes)) {
MSHADOW_TYPE_SWITCH(ret.type_flag_, DType, {
- transpose_pseudo2D<DType>(ret, src, axes, s);
+ transpose_pseudo2D<DType, is_addto>(ret, src, axes, s);
});
return;
}
#endif
+ // Special handle the identity case
+ if (IsIdentityTranspose(axes)) {
+ MSHADOW_TYPE_SWITCH(ret.type_flag_, DType, {
+ Tensor<xpu, 1, DType> in = src.get_with_shape<xpu, 1,
DType>(mshadow::Shape1(src.Size()), s);
+ Tensor<xpu, 1, DType> out = ret.get_with_shape<xpu, 1,
DType>(mshadow::Shape1(ret.Size()), s);
+ if (!is_addto) {
+ // Use memcpy to accelerate the speed
+ Copy(out, in, s);
+ } else {
+ mxnet_op::Kernel<mxnet_op::op_with_req<mshadow_op::identity, kAddTo>,
xpu>::Launch(
+ s, ret.Size(), out.dptr_, in.dptr_);
+ }
+ });
+ return;
+ }
+ // Handle the general transpose case
MSHADOW_TYPE_SWITCH(ret.type_flag_, DType, {
switch (axes.ndim()) {
- case 0: {
- Tensor<xpu, 1, DType> in = src.get_with_shape<xpu, 1,
DType>(mshadow::Shape1(1), s);
- Tensor<xpu, 1, DType> out = ret.get_with_shape<xpu, 1,
DType>(mshadow::Shape1(1), s);
- Copy(out, in, s);
- break;
- }
- case 1: {
- Tensor<xpu, 1, DType> in = src.get<xpu, 1, DType>(s);
- Tensor<xpu, 1, DType> out = ret.get<xpu, 1, DType>(s);
- Copy(out, in, s);
- break;
- }
case 2: {
- mshadow::Tensor<xpu, 2, DType> in = src.FlatTo2D<xpu, DType>(s);
- mshadow::Tensor<xpu, 2, DType> out = ret.FlatTo2D<xpu, DType>(s);
-
- if (axes[0] == 1 && axes[1] == 0) {
- if (ctx.get_ctx().dev_mask() == cpu::kDevMask) {
- Transpose2D<DType>(in.dptr_, out.dptr_, in.shape_[0], in.shape_[1]);
- } else {
- out = in.T();
Review comment:
@ChaiBapchya We have already had `transpose_pseudo2D`, which covers the case
of 2D Transpose in GPU.
----------------------------------------------------------------
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