This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
[MXNET-100] Support float16 in Correlation operator #10125
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,7 @@ struct CorrelationParam : public dmlc::Parameter<CorrelationParam> { | |
.describe("operation type is either multiplication or subduction"); | ||
} | ||
}; | ||
template<typename xpu> | ||
template<typename xpu, typename DType> | ||
class CorrelationOp : public Operator { | ||
public: | ||
explicit CorrelationOp(CorrelationParam param) { | ||
|
@@ -79,14 +79,14 @@ class CorrelationOp : public Operator { | |
CHECK_EQ(in_data.size(), 2U); | ||
CHECK_EQ(out_data.size(), 3U); | ||
Stream<xpu> *s = ctx.get_stream<xpu>(); | ||
Tensor<xpu, 4> data1 = in_data[Correlation::kData1].get<xpu, 4, real_t>(s); | ||
Tensor<xpu, 4> data2 = in_data[Correlation::kData2].get<xpu, 4, real_t>(s); | ||
Tensor<xpu, 4> out = out_data[Correlation::kOut].get<xpu, 4, real_t>(s); | ||
Tensor<xpu, 4> tmp1 = out_data[Correlation::kTemp1].get<xpu, 4, real_t>(s); | ||
Tensor<xpu, 4> tmp2 = out_data[Correlation::kTemp2].get<xpu, 4, real_t>(s); | ||
tmp1 = 0.0f; | ||
tmp2 = 0.0f; | ||
out = 0.0f; | ||
Tensor<xpu, 4, DType> data1 = in_data[Correlation::kData1].get<xpu, 4, DType>(s); | ||
Tensor<xpu, 4, DType> data2 = in_data[Correlation::kData2].get<xpu, 4, DType>(s); | ||
Tensor<xpu, 4, DType> out = out_data[Correlation::kOut].get<xpu, 4, DType>(s); | ||
Tensor<xpu, 4, DType> tmp1 = out_data[Correlation::kTemp1].get<xpu, 4, DType>(s); | ||
Tensor<xpu, 4, DType> tmp2 = out_data[Correlation::kTemp2].get<xpu, 4, DType>(s); | ||
tmp1 = DType(0.0f); | ||
tmp2 = DType(0.0f); | ||
out = DType(0.0f); | ||
CHECK_EQ(data1.CheckContiguous(), true); | ||
CHECK_EQ(data2.CheckContiguous(), true); | ||
CHECK_EQ(out.CheckContiguous(), true); | ||
|
@@ -124,13 +124,13 @@ class CorrelationOp : public Operator { | |
const std::vector<TBlob> &aux_args) { | ||
using namespace mshadow; | ||
Stream<xpu> *s = ctx.get_stream<xpu>(); | ||
Tensor<xpu, 4> grad_data1 = in_grad[Correlation::kData1].get<xpu, 4, real_t>(s); | ||
Tensor<xpu, 4> grad_data2 = in_grad[Correlation::kData2].get<xpu, 4, real_t>(s); | ||
Tensor<xpu, 4> out_g = out_grad[Correlation::kOut].get<xpu, 4, real_t>(s); | ||
Tensor<xpu, 4> tmp1 = out_data[Correlation::kTemp1].get<xpu, 4, real_t>(s); | ||
Tensor<xpu, 4> tmp2 = out_data[Correlation::kTemp2].get<xpu, 4, real_t>(s); | ||
if (req[0] != kAddTo) grad_data1 = 0.0f; | ||
if (req[1] != kAddTo) grad_data2 = 0.0f; | ||
Tensor<xpu, 4, DType> grad_data1 = in_grad[Correlation::kData1].get<xpu, 4, DType>(s); | ||
Tensor<xpu, 4, DType> grad_data2 = in_grad[Correlation::kData2].get<xpu, 4, DType>(s); | ||
Tensor<xpu, 4, DType> out_g = out_grad[Correlation::kOut].get<xpu, 4, DType>(s); | ||
Tensor<xpu, 4, DType> tmp1 = out_data[Correlation::kTemp1].get<xpu, 4, DType>(s); | ||
Tensor<xpu, 4, DType> tmp2 = out_data[Correlation::kTemp2].get<xpu, 4, DType>(s); | ||
if (req[0] != kAddTo) grad_data1 = DType(0.0f); | ||
if (req[1] != kAddTo) grad_data2 = DType(0.0f); | ||
CHECK_EQ(grad_data1.CheckContiguous(), true); | ||
CHECK_EQ(grad_data2.CheckContiguous(), true); | ||
CHECK_EQ(out_g.CheckContiguous(), true); | ||
|
@@ -163,7 +163,7 @@ class CorrelationOp : public Operator { | |
}; // class CorrelationOp | ||
// Decalre Factory function | ||
template<typename xpu> | ||
Operator* CreateOp(CorrelationParam param); | ||
Operator* CreateOp(CorrelationParam param, int dtype); | ||
#if DMLC_USE_CXX11 | ||
class CorrelationProp : public OperatorProperty { | ||
public: | ||
|
@@ -228,6 +228,22 @@ void Init(const std::vector<std::pair<std::string, std::string> >& kwargs) overr | |
out_shape->push_back(Shape4(dshape1[0], paddedbottomheight, paddedbottomwidth, dshape1[1])); | ||
return true; | ||
} | ||
bool InferType(std::vector<int> *in_type, | ||
std::vector<int> *out_type, | ||
std::vector<int> *aux_type) const override { | ||
int dtype = (*in_type)[0]; | ||
type_assign(&(*in_type)[1], dtype); | ||
type_assign(&(*out_type)[0], dtype); | ||
type_assign(&(*out_type)[1], dtype); | ||
type_assign(&(*out_type)[2], dtype); | ||
|
||
TYPE_ASSIGN_CHECK(*in_type, 0, dtype); | ||
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 do you need both type_assign and TYPE_ASSIGN_CHECK ? 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 was advice from Jun, he suggested that we can do a mutual inference by reducing all datatypes to one and then assign the reduced type back to everything. 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. If the reduced datatype is not -1 then all inputs and outputs will then have the same datatype at the end of this function. |
||
TYPE_ASSIGN_CHECK(*in_type, 1, dtype); | ||
TYPE_ASSIGN_CHECK(*out_type, 0, dtype); | ||
TYPE_ASSIGN_CHECK(*out_type, 1, dtype); | ||
TYPE_ASSIGN_CHECK(*out_type, 2, dtype); | ||
return dtype != -1; | ||
} | ||
OperatorProperty* Copy() const override { | ||
CorrelationProp* Correlation_sym = new CorrelationProp(); | ||
Correlation_sym->param_ = this->param_; | ||
|
@@ -244,7 +260,13 @@ void Init(const std::vector<std::pair<std::string, std::string> >& kwargs) overr | |
return {out_grad[Correlation::kOut], | ||
out_data[Correlation::kTemp1], out_data[Correlation::kTemp2]}; | ||
} | ||
Operator* CreateOperator(Context ctx) const override; | ||
Operator* CreateOperator(Context ctx) const override { | ||
LOG(FATAL) << "Not Implemented."; | ||
return NULL; | ||
} | ||
|
||
Operator* CreateOperatorEx(Context ctx, std::vector<TShape> *in_shape, | ||
std::vector<int> *in_type) const override; | ||
|
||
private: | ||
CorrelationParam param_; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Preferably, Use static_cast.
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.
Sure, will address this issue shortly
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.
Seems like static_cast is causing some compilation errors, taking a look at it now.