Skip to content
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

Updated tolerance levels for torch onnx export #90

Merged
merged 1 commit into from
Jan 17, 2024
Merged
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
18 changes: 12 additions & 6 deletions src/turnkeyml/build/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,18 +280,24 @@ def fire(self, state: build.State):
default_warnings = warnings.showwarning
warnings.showwarning = _warn_to_stdout

stats = fs.Stats(
state.cache_dir, state.config.build_name, state.evaluation_id
)
stats = fs.Stats(state.cache_dir, state.config.build_name, state.evaluation_id)

# Verify if the exported model matches the input torch model
try:
# Tolerance levels for the torch export are recommended by Pytorch here:
# https://pytorch.org/docs/stable/testing.html#module-torch.testing
fp32_tolerance = torch.onnx.verification.VerificationOptions(
rtol=1.3e-6, atol=1e-5
)
ramkrishna2910 marked this conversation as resolved.
Show resolved Hide resolved

# The `torch.onnx.verification.find_mismatch()` takes input arguments to the
# model as `input_args (Tuple[Any, ...])`
export_verification = torch.onnx.verification.find_mismatch(
state.model,
tuple(state.inputs.values()),
opset_version=state.config.onnx_opset)
opset_version=state.config.onnx_opset,
options=fp32_tolerance,
)

# `export_verification.has_mismatch()` returns True if a mismatch is found and
# False otherwise. If no mismatch is found,# `is_export_valid` is set to "Valid",
Expand All @@ -311,8 +317,8 @@ def fire(self, state: build.State):
is_export_valid = "unverified"

stats.save_model_eval_stat(
fs.Keys.TORCH_ONNX_EXPORT_VALIDITY,
is_export_valid,
fs.Keys.TORCH_ONNX_EXPORT_VALIDITY,
is_export_valid,
)

# Export the model to ONNX
Expand Down
Loading