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

Commit

Permalink
fixed documentation and nitpicky suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
access2rohit committed Jul 13, 2018
1 parent b036085 commit cc14190
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 42 deletions.
61 changes: 54 additions & 7 deletions src/operator/tensor/matrix_op-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2218,6 +2218,17 @@ inline bool DepthToSpaceOpType(const nnvm::NodeAttrs& attrs,
inp_index += (idx - next_idx_val * dim_size) * offset_arr[X]; \
idx = next_idx_val;

/*!
* \brief This function preforms the tensor transpose (0, 1, 2, 3, 4, 5) ->
* (0, 3, 4, 1, 5, 2) by computing linear index within input tensor to be mapped
* to the ith index of output tensor
* \param i tensor index
* \param out_data output tensor
* \param in_data input tensor
* \param block size of chunks to be moved out of depth dimension
* \param size array containing the size of each dimension of input tensor
* \param offset_arr array containing the linear offset of input tensor
*/
template<int req>
struct depth_to_space_forward {
template<typename DType>
Expand All @@ -2240,6 +2251,19 @@ struct depth_to_space_forward {
}
};

/*!
* \brief This function calculates the linear offset for each dimension of
* input tensor and stores them in an array, which is later used in
* performing depth_to_space operation
* \param i global thread id
* \param offset_arr array to be populated with offset values
* \param size array to be populated with size of each dimension of input tensor
* \param block size of chunks to be moved out of depth dimension
* \param size0 size of Dim 0 of input tensor
* \param size1 size of Dim 1 of input tensor
* \param size2 size of Dim 2 of input tensor
* \param size3 size of Dim 3 of input tensor
*/
template<int req>
struct compute_offset_for_depth_to_space {
template<typename DType>
Expand Down Expand Up @@ -2277,10 +2301,10 @@ void DepthToSpaceOpForward(const nnvm::NodeAttrs& attrs,
int block = param.blockSize;

mshadow::Tensor<xpu, 1, char> workspace =
ctx.requested[0].get_space_typed<xpu, 1, char>(mshadow::Shape1(sizeof(int32_t)*10), s);
ctx.requested[0].get_space_typed<xpu, 1, char>(mshadow::Shape1(sizeof(int32_t) * 10), s);
char* workspace_curr_ptr = workspace.dptr_;
int32_t* offset_arr = reinterpret_cast<int32_t*>(workspace_curr_ptr);
int32_t* size = reinterpret_cast<int32_t*>(workspace_curr_ptr + sizeof(int32_t)*6);
int32_t* size = reinterpret_cast<int32_t*>(workspace_curr_ptr + sizeof(int32_t) * 6);

MSHADOW_TYPE_SWITCH(out_data.type_flag_, DType, {
MXNET_ASSIGN_REQ_SWITCH(req[0], req_type, {
Expand Down Expand Up @@ -2322,7 +2346,7 @@ inline bool SpaceToDepthOpShape(const nnvm::NodeAttrs& attrs,
" Dimension:3(2nd space dimension) should be a multiple of 'block' ";

expected_out[0] = in_shape[0];
expected_out[1] = in_shape[1] * (block * block);
expected_out[1] = in_shape[1] * block * block;
uint32_t i = 2;
while (i < expected_out.ndim()) {
expected_out[i] = in_shape[i] / block;
Expand All @@ -2344,12 +2368,22 @@ inline bool SpaceToDepthOpType(const nnvm::NodeAttrs& attrs,
return out_attrs->at(0) != -1;
}

/*!
* \brief This function preforms the tensor transpose (0, 1, 2, 3, 4, 5) ->
* (0, 3, 5, 1, 2, 4) by computing linear index within input tensor to be mapped
* to the ith index of output tensor
* \param i tensor index
* \param out_data output tensor
* \param in_data input tensor
* \param block size of chunks to be moved out of depth dimension
* \param size array containing the size of each dimension of input tensor
* \param offset_arr array containing the linear offset of input tensor
*/
template<int req>
struct space_to_depth_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) {
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 = size[3] / block;
UPDATE_INDEX_USING_OFFSET(4)
Expand All @@ -2371,6 +2405,19 @@ struct space_to_depth_forward {
#undef UPDATE_INDEX_USING_OFFSET
#endif

/*!
* \brief This function calculates the linear offset for each dimension of
* input tensor and stores them in an array, which is later used in
* performing space_to_depth operation
* \param i global thread id
* \param offset_arr array to be populated with offset values
* \param size array to be populated with size of each dimension of input tensor
* \param block size of chunks to be moved out of depth dimension
* \param size0 size of Dim 0 of input tensor
* \param size1 size of Dim 1 of input tensor
* \param size2 size of Dim 2 of input tensor
* \param size3 size of Dim 3 of input tensor
*/
template<int req>
struct compute_offset_for_space_to_depth {
template<typename DType>
Expand Down Expand Up @@ -2411,7 +2458,7 @@ void SpaceToDepthOpForward(const nnvm::NodeAttrs& attrs,
ctx.requested[0].get_space_typed<xpu, 1, char>(mshadow::Shape1(sizeof(int32_t) * 10), s);
char* workspace_curr_ptr = workspace.dptr_;
int32_t* offset_arr = reinterpret_cast<int32_t*>(workspace_curr_ptr);
int32_t* size = reinterpret_cast<int32_t*>(workspace_curr_ptr + sizeof(int32_t)*6);
int32_t* size = reinterpret_cast<int32_t*>(workspace_curr_ptr + sizeof(int32_t) * 6);

MSHADOW_TYPE_SWITCH(out_data.type_flag_, DType, {
MXNET_ASSIGN_REQ_SWITCH(req[0], req_type, {
Expand Down
74 changes: 39 additions & 35 deletions src/operator/tensor/matrix_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -911,27 +911,28 @@ NNVM_REGISTER_OP(_backward_squeeze)

NNVM_REGISTER_OP(depth_to_space)
.describe(R"code(This operators implements the depthToSpace function:
.. math::
f(x, block) = ~x
where :math:`x` is an input tensor of shape [N,C,H,W] and `~x` is the output tensor of shape [N, C/(block^2), H*block, W*block]
f(x, block) = \tilde{x}
where :math:`x` is an input tensor of shape [N,C,H,W] and :math:`\tilde{x}` is the output tensor of shape :math:`[N, C/(block^2), H*block, W*block]`
Example::
#(1,4,2,3) input tensor
x = [[[[0, 1, 2],
[3, 4, 5]],
[[6, 7, 8],
[9, 10, 11]],
[[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23]]]]
[3, 4, 5]],
[[6, 7, 8],
[9, 10, 11]],
[[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23]]]]
y = depth_to_space(x, 2)
# (1, 1, 4, 6) output tensor
y = [[[[0, 6, 1, 7, 2, 8],
[12, 18, 13, 19, 14, 20],
[3, 9, 4, 10, 5, 11],
[15, 21, 16, 22, 17, 23]]]]
depth_to_space(x, 2) = [[[[0, 6, 1, 7, 2, 8],
[12, 18, 13, 19, 14, 20],
[3, 9, 4, 10, 5, 11],
[15, 21, 16, 22, 17, 23]]]]
)code" ADD_FILELINE)
.set_attr_parser(ParamParser<DepthToSpaceParam>)
.set_num_inputs(1)
Expand All @@ -943,8 +944,9 @@ Example::
.set_attr<nnvm::FInferShape>("FInferShape", DepthToSpaceOpShape)
.set_attr<nnvm::FInferType>("FInferType", DepthToSpaceOpType)
.set_attr<FCompute>("FCompute<cpu>", DepthToSpaceOpForward<cpu>)
.set_attr<FResourceRequest>("FResourceRequest", [](const NodeAttrs& n) {
return std::vector<ResourceRequest>{ResourceRequest::kTempSpace};
.set_attr<FResourceRequest>("FResourceRequest",
[](const NodeAttrs& n) {
return std::vector<ResourceRequest>{ResourceRequest::kTempSpace};
})
.set_attr<nnvm::FInplaceOption>("FInplaceOption",
[](const NodeAttrs& attrs) {
Expand All @@ -956,28 +958,29 @@ Example::

NNVM_REGISTER_OP(space_to_depth)
.describe(R"code(This operators implements the spacetodepth function:
.. math::
f(x, block) = ~x
where :math:`x` is an input tensor of shape [N,C,H,W] and `~x` is the output tensor of shape [N, C*(block^2), H/block, W/block]
f(x, blockSize) = \tilde{x}
where :math:`x` is an input tensor of shape [N,C,H,W] and :math:`\tilde{x}` is the output tensor of shape :math:`[N, C*(block^2), H/block, W/block]`
Example::
#(1,4,2,3) input tensor
x = [[[[0, 6, 1, 7, 2, 8],
[12, 18, 13, 19, 14, 20],
[3, 9, 4, 10, 5, 11],
[15, 21, 16, 22, 17, 23]]]]
y = space_to_depth(x, 2)
# (1, 1, 4, 6) output tensor
y = [[[[0, 1, 2],
[3, 4, 5]],
[[6, 7, 8],
[9, 10, 11]],
[[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23]]]]
space_to_depth(x, 2) = [[[[0, 1, 2],
[3, 4, 5]],
[[6, 7, 8],
[9, 10, 11]],
[[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23]]]]
)code" ADD_FILELINE)
.set_attr_parser(ParamParser<DepthToSpaceParam>)
.set_num_inputs(1)
Expand All @@ -989,8 +992,9 @@ Example::
.set_attr<nnvm::FInferShape>("FInferShape", SpaceToDepthOpShape)
.set_attr<nnvm::FInferType>("FInferType", SpaceToDepthOpType)
.set_attr<FCompute>("FCompute<cpu>", SpaceToDepthOpForward<cpu>)
.set_attr<FResourceRequest>("FResourceRequest", [](const NodeAttrs& n) {
return std::vector<ResourceRequest>{ResourceRequest::kTempSpace};
.set_attr<FResourceRequest>("FResourceRequest",
[](const NodeAttrs& n) {
return std::vector<ResourceRequest>{ResourceRequest::kTempSpace};
})
.set_attr<nnvm::FInplaceOption>("FInplaceOption",
[](const NodeAttrs& attrs) {
Expand Down

0 comments on commit cc14190

Please sign in to comment.