1 #include <caffe2/ideep/ideep_utils.h> 8 USE_IDEEP_DEF_ALIASES();
9 USE_IDEEP_OPERATOR_FUNCTIONS();
13 new_shape_(OperatorBase::GetRepeatedArgument<int>(
"shape")) {}
15 bool RunOnDevice()
override {
16 ideep::tensor::dims actual_new_shape = new_shape_;
17 if (InputSize() == 2) {
20 "New shape is specified by the input blob, do not pass in " 21 "the argument `shape`.");
24 auto& shape = OperatorBase::Input<TensorCPU>(1, CPU);
25 CAFFE_ENFORCE(shape.ndim() == 1,
"Shape should be 1-D");
26 actual_new_shape.reserve(shape.size());
27 if (shape.template IsType<int>()) {
28 const int* shape_data = shape.template data<int>();
29 actual_new_shape.assign(shape_data, shape_data + shape.size());
30 }
else if (shape.template IsType<int64_t>()) {
31 const int64_t* shape_data = shape.template data<int64_t>();
32 for (
int i = 0; i < shape.size(); ++i) {
33 actual_new_shape.push_back(static_cast<int>(shape_data[i]));
37 "IDEEP reshape only supports shape data in int32_t or int64_t");
44 auto& input = Input(0);
46 for (
int i = 0; i < actual_new_shape.size() && i < input.ndims(); ++i) {
47 if (actual_new_shape[i] == 0) {
48 actual_new_shape[i] = input.get_dim(i);
55 auto total_size = input.get_nelems();
58 for (
int i = 0; i < actual_new_shape.size(); ++i) {
59 const auto dim = actual_new_shape[i];
63 "Argument `shape` has more than one missing dimension.");
69 if (size == 0 && total_size != 0) {
71 "Can not reshape a non-zero size (",
73 ") tensor to zero size.");
76 if (unknown_idx != -1) {
82 " can not be inferred since new size is zero.");
84 total_size % size == 0,
85 "Argument `shape` does not agree with the input data.",
91 actual_new_shape[unknown_idx] = total_size / size;
96 "Argument `shape` does not agree with the input data.",
106 TensorCPU* old_shape = OperatorBase::Output<TensorCPU>(1, CPU);
107 old_shape->Resize(input.ndims());
108 int* old_shape_data = old_shape->template mutable_data<int>();
109 for (
int i = 0; i < input.ndims(); ++i) {
110 old_shape_data[i] = input.get_dim(i);
113 auto* output = Output(0);
114 if (output != &input) {
116 output->reinit_like(input);
117 ideep::direct_copy::compute(input, *output);
120 output->reshape(actual_new_shape);
125 ideep::tensor::dims new_shape_;
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
bool HasArgument(const string &name) const
Checks if the operator has an argument of the given name.