haojin2 commented on a change in pull request #11587: [MXNET-378] Adding
depth_to_space and space_to_depth operator(Updated)
URL: https://github.com/apache/incubator-mxnet/pull/11587#discussion_r200792447
##########
File path: src/operator/tensor/matrix_op-inl.h
##########
@@ -2158,6 +2158,285 @@ inline bool SqueezeShape(const nnvm::NodeAttrs& attrs,
return true;
}
+
+struct DepthToSpaceParam : public dmlc::Parameter<DepthToSpaceParam> {
+ int blockSize;
+ DMLC_DECLARE_PARAMETER(DepthToSpaceParam) {
+ DMLC_DECLARE_FIELD(blockSize)
+ .describe("The size of chunks that need to be taken from depth and
spread across to the "
+ "shape dimension of the tensor");
+ }
+};
+
+inline bool DepthToSpaceOpShape(const nnvm::NodeAttrs& attrs,
+ std::vector<TShape>* in_attrs,
+ std::vector<TShape>* out_attrs) {
+ const DepthToSpaceParam& param = nnvm::get<DepthToSpaceParam>(attrs.parsed);
+ CHECK_EQ(in_attrs->size(), 1U);
+ CHECK_EQ(out_attrs->size(), 1U);
+ CHECK_EQ(in_attrs->at(0).ndim(), 4) << "Operation Depth To Space requires
exactly 4D tensor";
+
+ TShape expected_out(in_attrs->at(0).ndim());
+
+ TShape& in_shape = in_attrs->at(0);
+ int block = param.blockSize;
+ CHECK_NE(in_shape[1], 0)
+ << "Depth dimension:1 cannot be 0";
+ CHECK_EQ(in_shape[1]%(block*block), 0)
+ << "Cannot perform Depth To Space operation on the specified tensor. "
+ "Dimension:1(depth dimension) should be a multiple of 'block^2' ";
+ CHECK_NE(in_shape[0], 0)
+ << "Operation requires a 4D tensor. Size of dimension:0 cannot be 0";
+ CHECK_NE(in_shape[2], 0)
+ << "Operation requires a 4D tensor. Size of dimension:2 cannot be 0";
+ CHECK_NE(in_shape[3], 0)
+ << "Operation requires a 4D tensor. Size of dimension:3 cannot be 0";
+
+ expected_out[0] = in_shape[0];
+ expected_out[1] = in_shape[1]/(block*block);
+ uint16_t i = 2;
+ while (i < expected_out.ndim()) {
+ expected_out[i] = in_shape[i] * block;
+ ++i;
+ }
+
+ SHAPE_ASSIGN_CHECK(*out_attrs, 0, expected_out);
+ return true;
+}
+
+inline bool DepthToSpaceOpType(const nnvm::NodeAttrs& attrs,
+ std::vector<int>* in_attrs,
+ std::vector<int>* out_attrs) {
+ CHECK_EQ(in_attrs->size(), 1U);
+ CHECK_EQ(out_attrs->size(), 1U);
+
+ TYPE_ASSIGN_CHECK(*out_attrs, 0, in_attrs->at(0));
+ TYPE_ASSIGN_CHECK(*in_attrs, 0, out_attrs->at(0));
+ return out_attrs->at(0) != -1;
+}
+
+#define UPDATE_INDEX_USING_OFFSET(X) \
+ next_idx_val = idx/dim_size; \
+ inp_index += (idx - (next_idx_val) * dim_size) * offset_arr[X]; \
+ idx = next_idx_val;
+
+template<int req>
+struct depth_to_space_forward {
+ template<typename DType>
+ MSHADOW_XINLINE static void Map(int i, DType* out_data, const DType* in_data,
+ const int block, const int* size, const int* offset_arr) {
+ int inp_index = 0, idx = i, next_idx_val, dim_size;
+ dim_size = block;
+ UPDATE_INDEX_USING_OFFSET(2)
+ dim_size = size[3];
+ UPDATE_INDEX_USING_OFFSET(5)
+ dim_size = block;
+ UPDATE_INDEX_USING_OFFSET(1)
+ dim_size = size[2];
+ UPDATE_INDEX_USING_OFFSET(4)
+ dim_size = size[1]/block/block;
+ UPDATE_INDEX_USING_OFFSET(3)
+ dim_size = size[0];
+ UPDATE_INDEX_USING_OFFSET(0)
+ KERNEL_ASSIGN(out_data[i], req, in_data[inp_index]);
+ }
+};
+
+template<int req>
+struct compute_offset_for_depth_to_space {
+ template<typename DType>
+ MSHADOW_XINLINE static void Map(int i, DType* offset_arr, DType* size, const
int block,
+ const int32_t size0, const int32_t size1, const int32_t size2, const
int32_t size3) {
Review comment:
Alignment
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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