Skip to content
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

Implement Tensor.type #273

Merged
merged 17 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
86 changes: 86 additions & 0 deletions thunder/tests/opinfos.py
Original file line number Diff line number Diff line change
Expand Up @@ -2778,6 +2778,92 @@ def tril_sample_generator(op, device, dtype, requires_grad, **kwargs):
# data_movement_ops.append(convert_element_type_opinfo)


def type_sample_generator_tensor(op, device, dtype, requires_grad, **kwargs):
k223kim marked this conversation as resolved.
Show resolved Hide resolved
# dtype is not None
# expected to return tensor

_torch_dtype_to_old_torch_typestring_map = {
torch.float32: "FloatTensor",
torch.float64: "DoubleTensor",
torch.float16: "HalfTensor",
torch.bfloat16: "BFloat16Tensor",
torch.uint8: "ByteTensor",
torch.int8: "CharTensor",
torch.int16: "ShortTensor",
torch.int32: "IntTensor",
torch.long: "LongTensor",
torch.bool: "BoolTensor",
}

make = partial(make_tensor, device=device, dtype=dtype, requires_grad=requires_grad)

to_dtype = torch.complex128 if dtype.is_complex else torch.float64

yield SampleInput(make(4, 4, device=device), dtype)
yield SampleInput(make(4, 4, device=device), dtype=to_dtype)
# below can be deleted if we don't support strings
yield SampleInput(make(4, 4, device=device), f"torch.{_torch_dtype_to_old_torch_typestring_map[to_dtype]}")

# Explictly pass device
if torch.device(device).type == "cuda":
yield SampleInput(make(4, 4, device=device), f"torch.cuda.{_torch_dtype_to_old_torch_typestring_map[to_dtype]}")


# kind of redundant?
def type_error_generator_tensor(op, device, dtype=torch.float32, **kwargs):
make = partial(make_tensor, device=device, dtype=dtype, requires_grad=False)

err_msg = r"type\(\): `non_blocking==True` is currently not supported."
yield SampleInput(make(3, 3), dtype, non_blocking=True), RuntimeError, err_msg


type_opinfo_tensor = OpInfo(
ltorch.type,
sample_input_generator=type_sample_generator_tensor,
error_input_generator=type_error_generator_tensor,
torch_reference=torch.Tensor.type,
)

data_movement_ops.append(type_opinfo_tensor)


def type_sample_generator_str(op, device, dtype, requires_grad, **kwargs):
# dtype is None
# expected to return string
make = partial(make_tensor, device=device, dtype=dtype, requires_grad=requires_grad)

yield SampleInput(make(4, 4))


# kind of redundant?
def type_error_generator_str(op, device, dtype=torch.float32, **kwargs):
make = partial(make_tensor, device=device, dtype=dtype, requires_grad=False)

err_msg = r"type\(\): `non_blocking==True` is currently not supported."
yield SampleInput(make(3, 3), non_blocking=True), RuntimeError, err_msg


# comparing strings (NOTE: assert_close does not support string comparison)
def string_compare(actual, expected, **kwargs):
assert actual == expected


type_opinfo_str = OpInfo(
ltorch.type,
sample_input_generator=type_sample_generator_str,
error_input_generator=type_error_generator_str,
torch_reference=torch.Tensor.type,
test_directives=(
DecorateInfo(
custom_comparator(string_compare),
k223kim marked this conversation as resolved.
Show resolved Hide resolved
"test_core_vs_torch_consistency",
),
),
)

data_movement_ops.append(type_opinfo_str)


def to_sample_generator(op, device, dtype, requires_grad, **kwargs):
make = partial(make_tensor, device=device, dtype=dtype, requires_grad=requires_grad)

Expand Down
88 changes: 87 additions & 1 deletion thunder/torch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import thunder.clang as clang
import thunder.core.devices as devices
from thunder.core.devices import to_device
from thunder.core.devices import to_device, device_from_string
import thunder.core.dtypes as dtypes
from thunder.core.dtypes import to_torch_dtype, to_dtype, _thunder_to_torch_dtype_map, _torch_to_thunder_dtype_map
import thunder.core.prims as prims
Expand Down Expand Up @@ -194,6 +194,92 @@ def is_cuda(a: TensorLike, /) -> bool:

register_method("size", size)

_torch_dtype_to_old_torch_typestring_map = {
torch.float32: "FloatTensor",
torch.float64: "DoubleTensor",
torch.float16: "HalfTensor",
torch.bfloat16: "BFloat16Tensor",
torch.uint8: "ByteTensor",
torch.int8: "CharTensor",
torch.int16: "ShortTensor",
torch.int32: "IntTensor",
torch.long: "LongTensor",
torch.bool: "BoolTensor",
}

_old_torch_typestring_to_torch_dtype_map = {v: k for k, v in _torch_dtype_to_old_torch_typestring_map.items()}


def _device_and_dtype_to_old_torch_typestring(device: DeviceLike, dtype: dtypeLike) -> str:
torch_dtype = to_torch_dtype(dtype)
dtype_str = _torch_dtype_to_old_torch_typestring_map.get(torch_dtype)
devicetype_str: str = ""
if device.devicetype is not devices.DeviceType.CPU:
devicetype_str = f"{devices.devicetype_string(device.devicetype)}."
return f"torch.{devicetype_str}{dtype_str}"


def _old_torch_typestring_to_devicetype_and_dtype(typestring: str) -> tuple[DeviceLike, dtypeLike]:

# Two cases:
# - torch.DtypeTensor
# - torch.device.DtypeTensor

_, *dev_and_dtype = typestring.split(".")
devicetype_str = "cpu"
dtype_str = ""

if len(dev_and_dtype) == 1:
# when devicetype_str is omitted, device type is CPU
(dtype_str,) = dev_and_dtype
dtype_str = _old_torch_typestring_to_torch_dtype_map[dtype_str]

if len(dev_and_dtype) == 2:
devicetype_str, dtype_str = dev_and_dtype
dtype_str = _old_torch_typestring_to_torch_dtype_map[dtype_str]

# Value error
# expected the string to split into one or two elements
# and devicetype_str should be either "cpu" or "cuda"
utils.check(
devicetype_str in ("cpu", "cuda") and 1 <= len(dev_and_dtype) <= 2,
lambda: f"type(): unrecognized torch typestring {typestring}",
exception_type=ValueError,
)

return devicetype_str, dtype_str


@torchsymbol(torch.Tensor.type, is_method=True)
def type(a: TensorLike, /, dtype: None | str | dtypeLike = None, non_blocking: bool = False) -> str | TensorLike:
utils.check(
k223kim marked this conversation as resolved.
Show resolved Hide resolved
not non_blocking,
lambda: f"type(): `non_blocking==True` is currently not supported.",
exception_type=NotImplementedError,
)

if dtype is None:
k223kim marked this conversation as resolved.
Show resolved Hide resolved
# returns the type of the input tensor in string
return _device_and_dtype_to_old_torch_typestring(a.device, a.dtype)

if isinstance(dtype, str):
k223kim marked this conversation as resolved.
Show resolved Hide resolved
devtype, dtype = _old_torch_typestring_to_devicetype_and_dtype(dtype)

if devtype == a.device.type:
# This handles two cases:
# 1. When a tensor is already on a CUDA device, and the device type string is CUDA. In this case the tensor remains on its current device.
# 2. When a tensor is on a CPU device and the device type string is omitted, the tensor remains on the CPU device.
dev = a.device
else:
dev = device_from_string(devtype)
else:
k223kim marked this conversation as resolved.
Show resolved Hide resolved
# dtype is assumed to be torch.dtype (e.g. torch.int32)
dev = a.device

return to(a, dev, dtype)


register_method("type", type)

#
# Data movement and transformation operations
Expand Down
Loading