Add MultiProcessMock for cross-process testing #3603
+443
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary:
WHY:
Distributed testing in TorchRec requires the ability to mock functions and methods across multiple processes. Without this capability, tests that spawn multiple processes cannot easily mock expensive operations, external dependencies, or specific behaviors, making it difficult to write comprehensive unit tests for distributed PyTorch code. This leads to either incomplete test coverage or tests that require actual expensive resources.
This change is also for T245908194
WHAT:
Created
MultiProcessMockclass in/data/sandcastle/boxes/fbsource/fbcode/torchrec/distributed/test_utils/multi_process.py:mockslist to maintain mock configurationsadd_mock(target, return_value, side_effect, **kwargs)to register new mocksapply_mocks()to apply all registered mocks in child processesclear_mocks()to clear all registered mocksIntegrated
MultiProcessMockwithMultiProcessTestBase:_mock_managerinstance variable initialized in__init__add_mock(...)method for users to register mocks_callable_wrapper_with_mocks()static method to apply mocks before calling test functions_run_multi_process_test()to pass mock manager to child processes and use the wrapper_run_multi_process_test_per_rank()to pass mock manager to child processes and use the wrapperAdded comprehensive test suite in
/data/sandcastle/boxes/fbsource/fbcode/torchrec/distributed/tests/test_multi_process_mock.py:MultiProcessMockclass functionalityTEST:
All 12 tests pass successfully:
Ran:
buck2 test torchrec/distributed/tests:test_multi_process_mockResult: Tests finished: Pass 12. Fail 0. Fatal 0. Skip 0.
Differential Revision: D88696058