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

fix missing device in fake operator #2203

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions torchrec/ir/tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,23 @@ def test_dynamic_shape_ebc(self) -> None:
self.assertEqual(eager_out[i].shape, tensor.shape)
assert torch.allclose(eager_out[i], tensor)

def test_ir_custom_op_device(self) -> None:
model = self.generate_model()
model.fpebc1 = copy.deepcopy(model.ebc1)
model.fpebc2 = copy.deepcopy(model.ebc1)
feature1 = KeyedJaggedTensor.from_offsets_sync(
keys=["f1", "f2", "f3"],
values=torch.tensor([0, 1, 2, 3, 2, 3]),
offsets=torch.tensor([0, 2, 2, 3, 4, 5, 6]),
)

model, sparse_fqns = encapsulate_ir_modules(model, JsonSerializer)
for device in ["cpu", "cuda", "meta"]:
device = torch.device(device)
outputs = model.to(device)(feature1.to(device))
for output in outputs:
self.assertEqual(output.device.type, device.type)

def test_deserialized_device(self) -> None:
model = self.generate_model()
id_list_features = KeyedJaggedTensor.from_offsets_sync(
Expand Down
13 changes: 9 additions & 4 deletions torchrec/ir/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import torch

from torch import nn
from torch.export import Dim, ExportedProgram, ShapesCollection
from torch.export import Dim, ShapesCollection
from torch.export.dynamic_shapes import _Dim as DIM
from torchrec.ir.types import SerializerInterface
from torchrec.sparse.jagged_tensor import KeyedJaggedTensor
Expand All @@ -37,16 +37,21 @@ def ir_custom_op_impl(
if t is not None:
device = t.device
break
logger.info(f"torch.ops.torchrec.ir_custom_op -> ({batch_size}, {dim})")
logger.info(f"torch.ops.torchrec.ir_custom_op -> ({batch_size}, {dim}) {device}")
return torch.empty(batch_size, dim, device=device)


@torch.library.register_fake("torchrec::ir_custom_op")
def ir_custom_op_fake(
tensors: List[Optional[torch.Tensor]], batch_size: int, dim: int
) -> torch.Tensor:
logger.info(f"ir_custom_op_fake -> ({batch_size}, {dim})")
return torch.empty(batch_size, dim)
device = None
for t in tensors:
if t is not None:
device = t.device
break
logger.info(f"ir_custom_op_fake -> ({batch_size}, {dim}) {device}")
return torch.empty(batch_size, dim, device=device)


def encapsulate_ir_modules(
Expand Down
Loading