|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +# pyre-strict |
| 9 | + |
| 10 | +import unittest |
| 11 | +from unittest.mock import MagicMock |
| 12 | + |
| 13 | +from torchrec.distributed.train_pipeline.pipeline_context import ( |
| 14 | + PrefetchTrainPipelineContext, |
| 15 | +) |
| 16 | +from torchrec.distributed.train_pipeline.runtime_forwards import ( |
| 17 | + PrefetchEmbeddingPipelinedForward, |
| 18 | + PrefetchPipelinedForward, |
| 19 | +) |
| 20 | +from torchrec.distributed.train_pipeline.types import CallArgs |
| 21 | + |
| 22 | + |
| 23 | +class TestPrefetchEmbeddingPipelinedForward(unittest.TestCase): |
| 24 | + """Test PrefetchEmbeddingPipelinedForward key functionality""" |
| 25 | + |
| 26 | + def setUp(self) -> None: |
| 27 | + """Set up test fixtures.""" |
| 28 | + self.mock_module = MagicMock() |
| 29 | + self.prefetch_context = PrefetchTrainPipelineContext() |
| 30 | + self.mock_args = CallArgs(args=[], kwargs={}) |
| 31 | + |
| 32 | + def test_prefetch_returns_true(self) -> None: |
| 33 | + """Test that prefetch() returns True.""" |
| 34 | + forward = PrefetchEmbeddingPipelinedForward( |
| 35 | + name="test_prefetch", |
| 36 | + args=self.mock_args, |
| 37 | + module=self.mock_module, |
| 38 | + context=self.prefetch_context, |
| 39 | + ) |
| 40 | + |
| 41 | + # Test that prefetch returns True |
| 42 | + self.assertIsInstance(forward, PrefetchPipelinedForward) |
| 43 | + |
| 44 | + def test_call_fails_without_compute_and_output_dist(self) -> None: |
| 45 | + """Test that __call__ fails if compute_and_output_dist is not called first.""" |
| 46 | + forward = PrefetchEmbeddingPipelinedForward( |
| 47 | + name="test_call_error", |
| 48 | + args=self.mock_args, |
| 49 | + module=self.mock_module, |
| 50 | + context=self.prefetch_context, |
| 51 | + ) |
| 52 | + |
| 53 | + # Should raise exception when called without compute_and_output_dist |
| 54 | + with self.assertRaises(Exception) as context: |
| 55 | + forward() |
| 56 | + |
| 57 | + self.assertIn( |
| 58 | + "compute_and_output_dist must be called before __call__", |
| 59 | + str(context.exception), |
| 60 | + ) |
| 61 | + |
| 62 | + def test_call_succeeds_after_compute_and_output_dist(self) -> None: |
| 63 | + """Test that __call__ succeeds when compute_and_output_dist is called first.""" |
| 64 | + forward = PrefetchEmbeddingPipelinedForward( |
| 65 | + name="test_call_success", |
| 66 | + args=self.mock_args, |
| 67 | + module=self.mock_module, |
| 68 | + context=self.prefetch_context, |
| 69 | + ) |
| 70 | + |
| 71 | + # Set up mock data in context |
| 72 | + test_data = MagicMock() |
| 73 | + test_ctx = MagicMock() |
| 74 | + self.prefetch_context.module_input_post_prefetch = { |
| 75 | + "test_call_success": test_data |
| 76 | + } |
| 77 | + self.prefetch_context.module_contexts_post_prefetch = { |
| 78 | + "test_call_success": test_ctx |
| 79 | + } |
| 80 | + |
| 81 | + # Mock the module's compute_and_output_dist method |
| 82 | + mock_awaitable = MagicMock() |
| 83 | + self.mock_module.compute_and_output_dist.return_value = mock_awaitable |
| 84 | + |
| 85 | + # Call compute_and_output_dist first |
| 86 | + forward.compute_and_output_dist() |
| 87 | + |
| 88 | + # Now __call__ should succeed and return the awaitable |
| 89 | + result = forward() |
| 90 | + self.assertEqual(result, mock_awaitable) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + unittest.main() |
0 commit comments