Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Bug fix for the input of same axes of the swapaxes operator #16513

Merged
merged 3 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/operator/swapaxis-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <string>
#include <utility>
#include "./operator_common.h"
#include "./mshadow_op.h"

namespace mxnet {
namespace op {
Expand Down Expand Up @@ -63,7 +64,6 @@ template<typename xpu, typename DType>
class SwapAxisOp : public Operator {
public:
explicit SwapAxisOp(SwapAxisParam p) {
CHECK_NE(p.dim1, p.dim2) << "dim1 can not be equal dim2.";
this->param_ = p;
}

Expand Down Expand Up @@ -131,6 +131,16 @@ class SwapAxisOp : public Operator {

if (shape_in.Size() == 0U) return;

if (axis1 == axis2) {
if (out_req == kAddTo) {
mxnet_op::Kernel<mxnet_op::op_with_req<mshadow_op::identity, kAddTo>, xpu>::Launch(
s, data_out.Size(), data_out.dptr<DType>(), data_in.dptr<DType>());
} else {
mxnet_op::copy(s, data_out, data_in);
}
return;
}

Shape<5> inter_shape;

Reshape2Five(&inter_shape, shape_in, axis1, axis2);
Expand Down
17 changes: 17 additions & 0 deletions tests/python/unittest/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,23 @@ def test_swapaxes():

assert_almost_equal(out, swap_)

config = [((1, 1, 2), 0, 1),
xiezhq-hermann marked this conversation as resolved.
Show resolved Hide resolved
((1, 1, 2), -1, -2),
((4, 5, 6, 7), 1, 1),
((4, 5, 6, 7), 2, 3),
((4, 5, 6, 7), -2, 2),
((4, 5, 6, 7), -2, -3)]

for shape, axis1, axis2 in config:
data_np = np.random.uniform(size=shape)
data_mx = mx.nd.array(data_np, dtype=data_np.dtype)
ret_np = np.swapaxes(data_np, axis1=axis1, axis2=axis2)
ret_mx = mx.symbol.SwapAxis(data, dim1=axis1, dim2=axis2)
exe_c = ret_mx.bind(default_context(), args=[data_mx])
exe_c.forward(is_train=True)
out = exe_c.outputs[0]
assert_almost_equal(out, ret_np)


@with_seed()
def test_scalarop():
Expand Down