[Core] Rework dtype resolution (#18751)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
This commit is contained in:
+49
-2
@@ -37,8 +37,8 @@ from argparse import (Action, ArgumentDefaultsHelpFormatter, ArgumentParser,
|
||||
_ArgumentGroup)
|
||||
from asyncio import FIRST_COMPLETED, AbstractEventLoop, Task
|
||||
from collections import UserDict, defaultdict
|
||||
from collections.abc import (AsyncGenerator, Awaitable, Generator, Hashable,
|
||||
Iterable, Iterator, KeysView, Mapping)
|
||||
from collections.abc import (AsyncGenerator, Awaitable, Collection, Generator,
|
||||
Hashable, Iterable, Iterator, KeysView, Mapping)
|
||||
from concurrent.futures.process import ProcessPoolExecutor
|
||||
from dataclasses import dataclass, field
|
||||
from functools import cache, lru_cache, partial, wraps
|
||||
@@ -979,6 +979,53 @@ def get_dtype_size(dtype: torch.dtype) -> int:
|
||||
return torch.tensor([], dtype=dtype).element_size()
|
||||
|
||||
|
||||
# bool = 0, int = 1, float = 2, complex = 3
|
||||
def _get_precision_level(dtype: torch.dtype) -> int:
|
||||
# NOTE: Complex dtypes return `is_floating_point=False`
|
||||
return ((dtype != torch.bool) + dtype.is_floating_point +
|
||||
dtype.is_complex * 2)
|
||||
|
||||
|
||||
def is_lossless_cast(src_dtype: torch.dtype, tgt_dtype: torch.dtype):
|
||||
"""
|
||||
Test whether it is lossless to cast a tensor from
|
||||
`src_dtype` to `tgt_dtype`.
|
||||
"""
|
||||
if src_dtype == tgt_dtype:
|
||||
return True
|
||||
|
||||
src_level = _get_precision_level(src_dtype)
|
||||
tgt_level = _get_precision_level(tgt_dtype)
|
||||
|
||||
if src_level < tgt_level:
|
||||
return True
|
||||
if src_level > tgt_level:
|
||||
return False
|
||||
|
||||
# Compare integral types
|
||||
if not src_dtype.is_floating_point and not src_dtype.is_complex:
|
||||
src_info = torch.iinfo(src_dtype)
|
||||
tgt_info = torch.iinfo(tgt_dtype)
|
||||
return src_info.min >= tgt_info.min and src_info.max <= tgt_info.max
|
||||
|
||||
# Compare floating-point types
|
||||
src_info = torch.finfo(src_dtype)
|
||||
tgt_info = torch.finfo(tgt_dtype)
|
||||
return (src_info.min >= tgt_info.min and src_info.max <= tgt_info.max
|
||||
and src_info.resolution >= tgt_info.resolution)
|
||||
|
||||
|
||||
def common_broadcastable_dtype(dtypes: Collection[torch.dtype]):
|
||||
"""
|
||||
Get the common `dtype` where all of the other `dtypes` can be
|
||||
cast to it without losing any information.
|
||||
"""
|
||||
return max(
|
||||
dtypes,
|
||||
key=lambda dtype: sum(is_lossless_cast(dt, dtype) for dt in dtypes),
|
||||
)
|
||||
|
||||
|
||||
# `collections` helpers
|
||||
def is_list_of(
|
||||
value: object,
|
||||
|
||||
Reference in New Issue
Block a user