Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions caffe2/ideep/operators/operator_fallback_ideep.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include <caffe2/operators/stop_gradient.h>
#include <caffe2/operators/tanh_op.h>
#include <caffe2/operators/tensor_protos_db_input.h>
#include <caffe2/operators/transpose_op.h>
#include <caffe2/operators/utility_ops.h>
#include <caffe2/queue/queue_ops.h>
#include <caffe2/sgd/iter_op.h>
Expand Down Expand Up @@ -80,7 +79,6 @@ REGISTER_IDEEP_OPERATOR(
IDEEPFallbackOp<AveragedLoss<float, CPUContext>, SkipIndices<0>>);
REGISTER_IDEEP_OPERATOR(Flatten, IDEEPFallbackOp<FlattenOp<CPUContext>>);
REGISTER_IDEEP_OPERATOR(ResizeLike, IDEEPFallbackOp<ResizeLikeOp<CPUContext>>);
REGISTER_IDEEP_OPERATOR(Transpose, IDEEPFallbackOp<TransposeOp<CPUContext>>);
REGISTER_IDEEP_OPERATOR(Slice, IDEEPFallbackOp<SliceOp<CPUContext>>);
REGISTER_IDEEP_OPERATOR(Clip, IDEEPFallbackOp<ClipOp<float, CPUContext>>);
REGISTER_IDEEP_OPERATOR(
Expand Down
35 changes: 35 additions & 0 deletions caffe2/ideep/operators/transpose_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <caffe2/ideep/ideep_utils.h>

using namespace caffe2;

namespace {

class IDEEPTransposeOp final : public IDEEPOperator {
public:
USE_IDEEP_DEF_ALIASES();
USE_IDEEP_OPERATOR_FUNCTIONS();

IDEEPTransposeOp(const OperatorDef& operator_def, Workspace* ws)
: IDEEPOperator(operator_def, ws),
axes_(this->template GetRepeatedArgument<int>("axes")){ }
~IDEEPTransposeOp() override {}

bool RunOnDevice() override {
const auto& X = Input(INPUT);
auto* Y = Output(OUTPUT);

Y->transpose_from(X, axes_);

return true;
}

private:
std::vector<int> axes_;

INPUT_TAGS(INPUT);
OUTPUT_TAGS(OUTPUT);
};

REGISTER_IDEEP_OPERATOR(Transpose, IDEEPTransposeOp);

} // namespace
43 changes: 43 additions & 0 deletions caffe2/python/ideep/transpose_op_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import unittest
import hypothesis.strategies as st
from hypothesis import given
import numpy as np
from caffe2.proto import caffe2_pb2
from caffe2.python import core, workspace
import caffe2.python.hypothesis_test_util as hu
import caffe2.python.ideep_test_util as mu

@unittest.skipIf(not workspace.C.use_mkldnn, "No MKLDNN support.")
class TransposeTest(hu.HypothesisTestCase):
@given(
X=hu.tensor(min_dim=1, max_dim=5, dtype=np.float32), use_axes=st.booleans(), **mu.gcs)
def test_transpose(self, X, use_axes, gc, dc):
ndim = len(X.shape)
axes = np.arange(ndim)
np.random.shuffle(axes)

if use_axes:
op = core.CreateOperator(
"Transpose", ["X"], ["Y"], axes=axes, device_option=gc)
else:
op = core.CreateOperator(
"Transpose", ["X"], ["Y"], device_option=gc)

def transpose_ref(X):
if use_axes:
return [np.transpose(X, axes=axes)]
else:
return [np.transpose(X)]

self.assertReferenceChecks(gc, op, [X], transpose_ref)
self.assertDeviceChecks(dc, op, [X], [0])
self.assertGradientChecks(gc, op, [X], 0, [0])


if __name__ == "__main__":
unittest.main()