Skip to content
4 changes: 4 additions & 0 deletions sdm/tensor/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
to_arrow,
to_cudf,
)
from sdm.tensor.mixin import _record_compiled_stream

if TYPE_CHECKING:
import cudf
Expand Down Expand Up @@ -463,6 +464,9 @@ def __torch_dispatch__( # type: ignore
if (handler := cls.HANDLED_FUNCTIONS.get(func)) is not None:
return handler(*args, **(kwargs or {}))

if str(func) == "streams.record_stream.default":
return _record_compiled_stream(*args, **(kwargs or {}))

# Operate on vanilla tensors for all non-handled functions:
args = pytree.tree_map_only(CategoricalTensor, lambda x: x._code, args)
kwargs = pytree.tree_map_only(
Expand Down
5 changes: 4 additions & 1 deletion sdm/tensor/columnar.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from sdm.tensor import NullableTensor, StringTensor, VarLenTensor
from sdm.tensor.io import arrow_as_tensor, to_arrow, to_cudf
from sdm.tensor.mixin import _resolve_device
from sdm.tensor.mixin import _record_compiled_stream, _resolve_device

if TYPE_CHECKING:
import cudf
Expand Down Expand Up @@ -360,6 +360,9 @@ def __torch_dispatch__( # type: ignore
if (handler := cls.HANDLED_FUNCTIONS.get(func)) is not None:
return handler(*args, **(kwargs or {}))

if str(func) == "streams.record_stream.default":
return _record_compiled_stream(*args, **(kwargs or {}))

raise NotImplementedError(
f"'{func}' is not supported for {cls.__name__!r}"
)
Expand Down
10 changes: 9 additions & 1 deletion sdm/tensor/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import abc
from collections.abc import Callable, Iterator
from typing import Self
from typing import Any, Self

import torch
from torch import Tensor
Expand Down Expand Up @@ -122,3 +122,11 @@ def _resolve_device(device: torch.device | str | None) -> torch.device | None:
if device.type == "mps" and device.index is None:
return torch.device("mps", 0)
return device


def _record_compiled_stream(tensor: Any, stream_index: int) -> None:
"""Keep each stored tensor's memory alive while the stream uses it."""
tensor_names, _ = tensor.__tensor_flatten__()
for tensor_name in tensor_names:
stored_tensor = getattr(tensor, tensor_name)
torch.ops.streams.record_stream.default(stored_tensor, stream_index)
Comment thread
aw471 marked this conversation as resolved.
4 changes: 4 additions & 0 deletions sdm/tensor/nullable.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
to_arrow,
to_cudf,
)
from sdm.tensor.mixin import _record_compiled_stream

if TYPE_CHECKING:
import cudf
Expand Down Expand Up @@ -385,6 +386,9 @@ def __torch_dispatch__( # type: ignore
if (handler := cls.HANDLED_FUNCTIONS.get(func)) is not None:
return handler(*args, **(kwargs or {}))

if str(func) == "streams.record_stream.default":
return _record_compiled_stream(*args, **(kwargs or {}))

raise NotImplementedError(
f"'{func}' is not supported for {cls.__name__!r}"
)
Expand Down
5 changes: 4 additions & 1 deletion sdm/tensor/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from sdm import NaT, Stype, StypeLike
from sdm.tensor import CategoricalTensor, ColumnarTensor, StringTensor
from sdm.tensor.io import arrow_as_tensor, to_arrow, to_cudf
from sdm.tensor.mixin import _resolve_device
from sdm.tensor.mixin import _record_compiled_stream, _resolve_device

if TYPE_CHECKING:
import cudf
Expand Down Expand Up @@ -1201,6 +1201,9 @@ def __torch_dispatch__( # type: ignore
if func in cls.HANDLED_FUNCTIONS:
return cls.HANDLED_FUNCTIONS[func](*args, **(kwargs or {}))

if str(func) == "streams.record_stream.default":
return _record_compiled_stream(*args, **(kwargs or {}))

raise NotImplementedError(
f"'{func}' is not supported for {cls.__name__!r}"
)
Expand Down
4 changes: 4 additions & 0 deletions sdm/tensor/var_len.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
combine_arrow_chunks,
to_arrow,
)
from sdm.tensor.mixin import _record_compiled_stream

aten = torch.ops.aten

Expand Down Expand Up @@ -632,6 +633,9 @@ def __torch_dispatch__( # type: ignore
with enable_reentrant_dispatch(): # Record autograd in `_data`.
return handler(*args, **(kwargs or {}))

if str(func) == "streams.record_stream.default":
return _record_compiled_stream(*args, **(kwargs or {}))

raise NotImplementedError(
f"'{func}' is not supported for {cls.__name__!r}"
)
Expand Down
Loading