Skip to content

feat: torchtrtc using atol and rtol for tolerance test #1038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions cpp/bin/torchtrtc/accuracy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ bool check_rtol(const at::Tensor& diff, const std::vector<at::Tensor> inputs, fl
return diff.abs().max().item<float>() <= threshold * maxValue;
}

bool almost_equal(const at::Tensor& a, const at::Tensor& b, float threshold) {
return check_rtol(a - b, {a, b}, threshold);
bool almost_equal(const at::Tensor& a, const at::Tensor& b, float threshold, float atol, float rtol) {
auto a_float = a.toType(at::kFloat);
auto b_float = b.toType(at::kFloat);

auto diff = a_float - b_float;
auto result = diff.abs().max().item<float>() - (atol + rtol * b.abs().max().item<float>());

std::cout << "Max Difference: " << result << std::endl;
std::cout << "Acceptable Threshold: " << threshold << std::endl;

return result <= threshold;
}

} // namespace accuracy
} // namespace torchtrtc
} // namespace torchtrtc
4 changes: 2 additions & 2 deletions cpp/bin/torchtrtc/accuracy.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace torchtrtc {
namespace accuracy {

bool check_rtol(const at::Tensor& diff, const std::vector<at::Tensor> inputs, float threshold);
bool almost_equal(const at::Tensor& a, const at::Tensor& b, float threshold);
bool almost_equal(const at::Tensor& a, const at::Tensor& b, float threshold, float atol = 1e-8, float rtol = 1e-5);

} // namespace accuracy
} // namespace torchtrtc
} // namespace torchtrtc