Skip to content
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
15 changes: 12 additions & 3 deletions onnxscript/optimizer/_constant_folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,18 @@ def _get_numpy_value(
if size_limit is not None and const_value.size > size_limit:
return None
try:
# Reinterpret the array with `.view()` because some implementations of
# ir.TensorProtocol (e.g. PyTorch<=2.7) do not use ml_dtypes for bfloat16 etc.
array = const_value.numpy().view(const_value.dtype.numpy())
# Turn the constant value into a numpy array representation with the
# specifics of this conversion handled by the tensor type
array = const_value.numpy()
# Can/should not reinterpret strings via .view, resulting in
# "TypeError: Cannot change data-type for array of references."
# There is also no reason to reinterpret strings, this is only
# relevant for some arithmetic types
if const_value.dtype != ir.DataType.STRING:
# Reinterpret the array with `.view()` because some
# implementations of ir.TensorProtocol (e.g. PyTorch<=2.7) do
# not use ml_dtypes for bfloat16 etc.
array = array.view(const_value.dtype.numpy())
except FileNotFoundError:
# External data is not available.
logger.warning(
Expand Down
Loading