SqueezeGrad Bugfix (#7412)

* squeezegrad bugfix

* fix ut

Co-authored-by: Vincent Wang <weicwang@microsoft.com>
This commit is contained in:
Vincent Wang 2021-04-26 09:12:03 +08:00 committed by GitHub
parent ca9b3f18e9
commit 368e4a324f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 22 deletions

View file

@ -788,36 +788,30 @@ IMPLEMENT_GRADIENT_BUILDER(GetReluGradient) {
}
IMPLEMENT_GRADIENT_BUILDER(GetSqueezeGradient) {
std::vector<NodeDef> result;
size_t numInputs = GetSrcNodeInputSize();
if (SrcNodeOpsetVersion() < 13) { //axes attribute
if (SrcNodeOpsetVersion() < 13) { // Axes attribute exists.
auto attributes = SrcNodeAttributes();
std::vector<int64_t> axes_values;
if (attributes.find("axes") != attributes.end()) {
axes_values = RetrieveValues<int64_t>(attributes.at("axes"));
result.push_back(
NodeDef("Unsqueeze",
{GO(0)},
{GI(0)},
{MakeAttribute("axes", axes_values)}));
return std::vector<NodeDef>{NodeDef("Unsqueeze",
{GO(0)},
{GI(0)},
{MakeAttribute("axes", axes_values)})};
}
} else if (numInputs == 2) { //optional input 'axes' is provided
result.push_back(
NodeDef(OpDef{"Unsqueeze", kOnnxDomain, 13},
{GO(0), I(1)},
{GI(0)}));
} else { // if axes attribute/input not provided for squeeze
result.push_back(
NodeDef("Shape",
{I(0)},
{IA("I0_shape")}));
result.push_back(
NodeDef("Reshape",
{GO(0), IA("I0_shape")},
{GI(0)}));
} else if (numInputs == 2) { // Optional input 'axes' is provided
return std::vector<NodeDef>{NodeDef(OpDef{"Unsqueeze", kOnnxDomain, 13},
{GO(0), I(1)},
{GI(0)})};
}
return result;
// If axes attribute/input is not provided for squeeze, no matter which OpSet version.
return std::vector<NodeDef>{NodeDef("Shape",
{I(0)},
{IA("I0_shape")}),
NodeDef("Reshape",
{GO(0), IA("I0_shape")},
{GI(0)})};
}
IMPLEMENT_GRADIENT_BUILDER(GetAddSubGradient) {

View file

@ -1308,6 +1308,14 @@ static void RunSqueezeUnsqueezeTests(const OpDef& op_def,
x_datas.push_back(random.Gaussian<float>(x_shapes[i], 0.f, 5.f));
std::vector<TensorInfo> input = {x_shape};
std::vector<ONNX_NAMESPACE::AttributeProto> attributes = {};
// Test case w/o axes attribute/input, only valid for Squeeze Op.
if (op_def.type == "Squeeze") {
gradient_checker.ComputeGradientError(op_def, input, {y_shape}, &max_error, x_datas, attributes);
EXPECT_IS_TINIER_THAN(max_error, error_tolerance);
}
// test case w/ axes attribute/input.
if (axes_input) {
std::vector<float> axes_float;
std::transform(begin(axes), end(axes), std::back_inserter(axes_float), [](int64_t i) { return static_cast<float>(i); });