-
Notifications
You must be signed in to change notification settings - Fork 58
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
[torchlib] Implement type promotion #2010
base: main
Are you sure you want to change the base?
Changes from all commits
ecbb6d0
b507bf5
1033ac5
eeeae08
c6e300c
fd915c1
ff934c3
5c45f70
db16b8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"""Type promotion functions for op implementations.""" | ||
Check warning Code scanning / lintrunner RUFF/format Warning
Run lintrunner -a to apply this patch.
Check warning Code scanning / lintrunner RUFF-FORMAT/format Warning
Run lintrunner -a to apply this patch.
|
||
|
||
from typing import Sequence | ||
Check warning Code scanning / lintrunner RUFF/I001 Warning
Import block is un-sorted or un-formatted.
See https://docs.astral.sh/ruff/rules/unsorted-imports |
||
from onnxscript import ir | ||
|
||
def _get_higher_dtype(a: ir.DataType, b: ir.DataType) -> ir.DataType: | ||
"""Get the higher dtype of two dtypes.""" | ||
# Reference: https://github.com/pytorch/pytorch/blob/bdd942efd76e74baa5dd0a262f7c843ddfe2e11b/torch/_prims_common/__init__.py#L1160 | ||
if a == b: | ||
return a | ||
|
||
if a is None: | ||
return b | ||
|
||
if b is None: | ||
return a | ||
|
||
ordered_datatypes = ( | ||
(ir.DataType.BOOL,), | ||
(ir.DataType.UINT8, ir.DataType.INT8), | ||
(ir.DataType.INT16,), | ||
(ir.DataType.INT32,), | ||
(ir.DataType.INT64,), | ||
(ir.DataType.FLOAT16, ir.DataType.BFLOAT16), | ||
(ir.DataType.FLOAT,), | ||
(ir.DataType.DOUBLE,), | ||
(ir.DataType.COMPLEX64,), | ||
(ir.DataType.COMPLEX128,), | ||
) | ||
|
||
for idx, dtypes in enumerate(ordered_datatypes): | ||
if a in dtypes and b in dtypes: | ||
return ordered_datatypes[idx + 1][0] | ||
if a in dtypes: | ||
return b | ||
if b in dtypes: | ||
return a | ||
|
||
raise ValueError(f"Unexpected data types: {a}, {b}") | ||
|
||
|
||
def promote_types(op, values: Sequence[ir.Value]) -> Sequence[ir.Value]: | ||
"""Promote the types of the given values.""" | ||
if not values: | ||
return () | ||
|
||
for value in values: | ||
if value.dtype is None: | ||
raise ValueError(f"Value {value} does not have dtype information and cannot be promoted.") | ||
|
||
promoted = values[0].dtype | ||
assert promoted is not None | ||
for value in values[1:]: | ||
dtype = value.dtype | ||
assert dtype is not None | ||
promoted = _get_higher_dtype(promoted, dtype) | ||
|
||
results = [] | ||
for value in values: | ||
if value.dtype != promoted: | ||
new_val = op.Cast(value, to=promoted) | ||
new_val.dtype = promoted | ||
new_val.shape = value.shape | ||
results.append(new_val) | ||
else: | ||
results.append(value) | ||
|
||
return results |
Check warning
Code scanning / lintrunner
RUFF/CPY001 Warning