Fix log2 and log10 converters to handle integer input#2735
Open
joaopedroassad wants to merge 1 commit into
Open
Fix log2 and log10 converters to handle integer input#2735joaopedroassad wants to merge 1 commit into
joaopedroassad wants to merge 1 commit into
Conversation
Collaborator
|
The change looks good. CI: https://gitlab.com/coremltools1/coremltools/-/pipelines/2592574391 |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The PyTorch
log2andlog10converters pass their input straight into MIL'smb.log, which only accepts floats (fp16/fp32). The issue is thattorch.log2andtorch.log10take integer tensors and return float, so converting a model that calls either of them on an int tensor currently fails MIL type inference.The neighboring
logandlog1pconverters already handle this by casting int input to float first (that cast was added tologin #2017). This change giveslog2andlog10the same two-line guard:Float inputs behave exactly as before. Integer inputs now lower correctly instead of erroring out.
Both changes are in
coremltools/converters/mil/frontend/torch/ops.py, right next to thelog/log1pguards they mirror.Testing
I added
test_log10_dtypeandtest_log2_dtypetoTestLog10/TestLog2incoremltools/converters/mil/frontend/torch/test/test_torch_ops.py, parametrized overnp.int32andnp.float32, following the existingtest_log_dtypethat already coverstorch.logon integer input.I ran them against coremltools 9.0 both ways. Without the fix, the
int32cases fail (thelog2/log10conversion errors on integer input) whilefloat32passes. With the fix, all 8 selected cases pass (int32andfloat32across the mlprogram and neuralnetwork backends, on CPU, TorchScript frontend).