add_int16_cast adds uint16 cast causing underflow of int16-compatible int32 value
#2626
+85
−39
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.
The current
add_int16_castimplementation has a side-effect in theshould_cast_parametermethod. It updates thetarget_dtypemember when it is being called on different parameters. Thistarget_dtypevalue is then used in thetransform_opmethod, to add casts. The issue is that, in certain cases, thetarget_dtypedoes not get correctly reset between parameters, and unintended uint16 casts can occur!The added test case will fail with:
An easy fix would have been to reset the
target_dtypestate in every call toshould_cast_parameter.However, I believe this design is quite confusing, and likely to cause further issues, so I've gone ahead and refactored the code to make
should_cast_parameterreturn an optional with the parameter's target dtype instead of having a side effect.The
transform_function_signaturesmethod iterates the function inputs, and usesself.target_dtypeto modify the inputs. This function did not useshould_cast_parameterbefore as well, so depending on if it is called before/after the parameter conversion code, it may use a differentself.target_dtypebased on parameter ordering. In the new version, it will always use the default target_dtype. I am not sure what the correct behavior is here.