Preserve tensor dtype when the left operand is an R scalar#1471
Draft
TroyHernandez wants to merge 2 commits into
Draft
Preserve tensor dtype when the left operand is an R scalar#1471TroyHernandez wants to merge 2 commits into
TroyHernandez wants to merge 2 commits into
Conversation
`1 - t` converted the scalar with torch_tensor(), producing a dimensioned float32 tensor that dictates type promotion: bfloat16 and float16 tensors silently upcast to float32, and `1 - long_tensor` returns float. Scalars on the rhs already hit the Tensor-Scalar overloads with weak promotion, so `t - 1` kept the dtype while `1 - t` did not. Convert length-1 numeric/logical lhs operands with torch_scalar_tensor() instead: 0-dim tensors promote weakly, matching Python's scalar semantics.
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.
Problem
Arithmetic with an R scalar on the left silently promotes reduced-precision and integer tensors:
In Python,
1 - tkeepst's dtype because Python scalars participate weakly in type promotion.This surfaced as a hard failure in a bfloat16 attention stack: an additive mask built with
1 - attention_maskcame out float32, and CUDA's fusedscaled_dot_product_attentionrejects a bias whose dtype doesn't match the query ("invalid dtype for bias - should match query's dtype").Cause
The operator methods wrap a non-tensor lhs with
torch_tensor(e1, ...), which yields a dimensioned (length-1, 1-d) tensor of the default dtype. libtorch lets dimensioned tensors dictate promotion, so float32 wins over bfloat16/int64. The rhs case never hits this path:torch_sub(tensor, 1)takes the Tensor-Scalar overload, which promotes weakly.Fix
Wrap length-1 numeric/logical lhs operands with
torch_scalar_tensor()instead. 0-dim tensors promote weakly, exactly like Python scalars, so:1 - bf16stays BFloat16,2 * halfstays Half1L + longstays Long,5L %% longstays Long1L / longstill promotes to the default float dtype (division semantics unchanged)c(1, 2, 3) - bf16still promotes to Float (dimensioned vectors keep their old behavior)NA,NaN, logical, and integer scalars behave as before (verified againsttorch_tensor()conversions)Applied to the seven arithmetic ops (
+,-,*,/,^,%%,%/%). Comparison operators share the wrapping pattern but return bool, so they're left untouched.The regression test fails 4 assertions before the patch and passes after; the rest of
test-operators.Ris unchanged (149 passing before and after, CPU and CUDA checked).