Skip to content

Add dynamo configs to TorchrecPT2TrainPipeline #2130

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

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
14 changes: 12 additions & 2 deletions torchrec/distributed/train_pipeline/train_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
)
from torchrec.distributed.types import Awaitable
from torchrec.pt2.checks import is_torchdynamo_compiling
from torchrec.pt2.utils import default_pipeline_input_transformer
from torchrec.sparse.jagged_tensor import KeyedJaggedTensor
from torchrec.streamable import Multistreamable


logger: logging.Logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -190,7 +190,10 @@ def __init__(
)
self._pre_compile_fn = pre_compile_fn
self._post_compile_fn = post_compile_fn
self._input_transformer = input_transformer
# pyre-ignore
self._input_transformer = (
input_transformer or default_pipeline_input_transformer
)
self._iter = 0
self._cur_batch: Optional[In] = None

Expand All @@ -215,6 +218,13 @@ def progress(self, dataloader_iter: Iterator[In]) -> Out:
logger.info("Compiling model...")
if self._pre_compile_fn:
self._pre_compile_fn(self._model)

# Mandatory dynamo configuration for Torchrec PT2 compilation
torch._dynamo.config.capture_scalar_outputs = True
torch._dynamo.config.capture_dynamic_output_shape_ops = True
torch._dynamo.config.force_unspec_int_unbacked_size_like_on_torchrec_kjt = (
True
)
self._model.compile(
fullgraph=cc.fullgraph, dynamic=cc.dynamic, backend=cc.backend
)
Expand Down
11 changes: 11 additions & 0 deletions torchrec/pt2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# pyre-strict


import torch
from torchrec.sparse.jagged_tensor import KeyedJaggedTensor

Expand Down Expand Up @@ -81,3 +82,13 @@ def kjt_for_pt2_tracing(
stride_per_key_per_rank=kjt.stride_per_key_per_rank() if is_vb else None,
inverse_indices=inverse_indices,
)


# pyre-ignore
def default_pipeline_input_transformer(inp):
for attr_name in ["id_list_features", "id_score_list_features"]:
if hasattr(inp, attr_name):
attr = getattr(inp, attr_name)
if isinstance(attr, KeyedJaggedTensor):
setattr(inp, attr_name, kjt_for_pt2_tracing(attr))
return inp
Loading