constrain inputs for min/max grad UT (#8632)

* fix inputs for min/max grad UT

* use random inputs (truncated)
This commit is contained in:
mindest 2021-08-07 18:29:06 +08:00 committed by GitHub
parent 1ce4328846
commit a56e325eb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2494,27 +2494,27 @@ void GradientCheckerMinMaxGradHelper(const std::string op) {
float max_error;
GradientChecker<float, float, float> gradient_checker;
OpDef op_def{op, kOnnxDomain, 11};
// Ensure the gap between x1 and x2 is greater than 1e-3f, otherwise the result of NumericJacobian
// will be incorrect. This also excludes equal inputs case, where Min/Max is not smooth.
std::function<float(float)> x1_transformer = [](float x) { return (int)(x * 100) / 100.f; };
std::function<float(float)> x2_transformer = [](float x) { return (int)(x * 100) / 100.f + 0.002f; };
TensorInfo x1_info({2, 3}, true, &x1_transformer);
TensorInfo y_info({2, 3}, true);
// Exclude equal inputs, since Min/Max is not smooth in such case
{
TensorInfo x_info({2, 3}, true);
TensorInfo y_info({2, 3}, true);
gradient_checker.ComputeGradientError(op_def, {x_info}, {y_info}, &max_error);
EXPECT_IS_TINY(max_error);
}
{
TensorInfo x1_info({2, 3}, true);
TensorInfo x2_info({2, 3}, true);
TensorInfo y_info({2, 3}, true);
TensorInfo x2_info({2, 3}, true, &x2_transformer);
gradient_checker.ComputeGradientError(op_def, {x1_info, x2_info}, {y_info}, &max_error);
EXPECT_IS_TINY(max_error);
}
{
TensorInfo x1_info({2, 3}, true);
TensorInfo x2_info({3}, true);
TensorInfo y_info({2, 3}, true);
TensorInfo x2_info({3}, true, &x2_transformer);
gradient_checker.ComputeGradientError(op_def, {x1_info, x2_info}, {y_info}, &max_error);
EXPECT_IS_TINY(max_error);
}