-
Notifications
You must be signed in to change notification settings - Fork 6.8k
[MXNET-382] Shape and Size Operator #10889
Changes from 2 commits
4635957
38c15d9
6540678
01d1b95
b513dbe
125c3cd
ac96ef1
93ffddc
3d578d3
ef43d2f
1b7ba47
f8cc278
eb74750
08346da
d2f7999
93fc294
3f84b1a
3d8f560
2074b46
4b1164e
ee97196
10cb562
cbec1e5
039e6d4
ee110fd
460c315
67cbbfe
804dfb4
f17f9c8
c188404
de64b97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -388,6 +388,26 @@ void CastCompute(const nnvm::NodeAttrs& attrs, | |
}); | ||
} | ||
|
||
template<typename xpu> | ||
void ShapeCompute(const nnvm::NodeAttrs& attrs, | ||
const OpContext& ctx, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alignment. |
||
const std::vector<TBlob>& inputs, | ||
const std::vector<OpReqType>& req, | ||
const std::vector<TBlob>& outputs) { | ||
CHECK_EQ(inputs.size(), 1U); | ||
CHECK_EQ(outputs.size(), 1U); | ||
CHECK_EQ(req.size(), 1U); | ||
const TBlob& in_data = inputs[0]; | ||
const TBlob& out_data = outputs[0]; | ||
mshadow::Stream<xpu> *s = ctx.get_stream<xpu>(); | ||
TShape in_shape = in_data.shape_; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const TShape&. |
||
MSHADOW_TYPE_SWITCH(out_data.type_flag_, DType, { | ||
mxnet_op::Kernel<mshadow_op::identity_with_cast, xpu>::Launch( | ||
s, in_data.ndim(), out_data.dptr<DType>(), in_shape.data()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, i will make this change. |
||
}); | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please get rid of one blank line here, c++ use only 1 blank line between functions |
||
struct HardSigmoidParam : public dmlc::Parameter<HardSigmoidParam> { | ||
real_t alpha; | ||
real_t beta; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -399,6 +399,37 @@ NNVM_REGISTER_OP(reshape_like) | |
.add_argument("lhs", "NDArray-or-Symbol", "First input.") | ||
.add_argument("rhs", "NDArray-or-Symbol", "Second input."); | ||
|
||
NNVM_REGISTER_OP(shape) | ||
.describe("Returns a 1D int64 array containing the shape of data.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be clearer if you add an example here? |
||
.set_num_inputs(1) | ||
.set_num_outputs(1) | ||
.set_attr<nnvm::FInplaceIdentity>("FInplaceIdentity", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why add this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will remove it. |
||
[](const NodeAttrs& attrs){ return std::vector<bool>{true}; }) | ||
.set_attr<nnvm::FIgnoreInputs>("FIgnoreInputs", | ||
[](const NodeAttrs& attrs) { return std::vector<uint32_t>(1, 1); }) | ||
.set_attr<FCompute>("FCompute<cpu>", ShapeCompute<cpu>) | ||
.set_attr<nnvm::FInferShape>("FInferShape", | ||
[](const nnvm::NodeAttrs& attrs, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use spaces instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, the CI failed due to cpplint. I will fix it. |
||
std::vector<TShape> *in_attrs, | ||
std::vector<TShape> *out_attrs) { | ||
CHECK_EQ(in_attrs->size(), 1U); | ||
CHECK_EQ(out_attrs->size(), 1U); | ||
TShape target_shape(1); | ||
target_shape[0] = in_attrs->at(0).ndim(); | ||
SHAPE_ASSIGN_CHECK(*out_attrs, 0, target_shape); | ||
return out_attrs->at(0).ndim() != 0U && out_attrs->at(0).Size() != 0U; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you suggest that I define a shape_is_none function and use it here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's already available. |
||
}) | ||
.set_attr<nnvm::FInferType>("FInferType", | ||
[](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, 0U); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use type enum variable name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay, i will use mshadow::kInt64 |
||
return out_attrs->at(0) != -1; | ||
}) | ||
.set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't there be no appropriate gradient definition for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, shape and size operator will not have gradients right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I'm asking the same thing. Why did you define FGradient attribute for the op? |
||
.add_argument("data", "NDArray-or-Symbol", "Input Array."); | ||
|
||
DMLC_REGISTER_PARAMETER(CastParam); | ||
NNVM_REGISTER_OP(Cast) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,9 @@ NNVM_REGISTER_OP(_identity_with_attr_like_rhs) | |
NNVM_REGISTER_OP(reshape_like) | ||
.set_attr<FCompute>("FCompute<gpu>", UnaryOp::IdentityCompute<gpu>); | ||
|
||
NNVM_REGISTER_OP(shape) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does the name come from? This looks confusing and conflicts with the property name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do you suggest the name of the operator should be? This is more or less the name used in a couple of other frameworks too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make sure the doc page can be rendered correctly at least. |
||
.set_attr<FCompute>("FCompute<gpu>", ShapeCompute<gpu>); | ||
|
||
NNVM_REGISTER_OP(Cast) | ||
.set_attr<FCompute>("FCompute<gpu>", CastCompute<gpu>); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to make templates for shape and size functions based on the type of device. CPU and GPU FCompute functions are defined respectively in .cc and .cu and don't share anything.