Commit d48d457
Add MultiProcessMock for cross-process testing
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:**
1. Created `MultiProcessMock` class in `/data/sandcastle/boxes/fbsource/fbcode/torchrec/distributed/test_utils/multi_process.py`:
- Added `mocks` list to maintain mock configurations
- Added `add_mock(target, return_value, side_effect, **kwargs)` to register new mocks
- Added `apply_mocks()` to apply all registered mocks in child processes
- Added `clear_mocks()` to clear all registered mocks
2. Integrated `MultiProcessMock` with `MultiProcessTestBase`:
- Added `_mock_manager` instance variable initialized in `__init__`
- Added public `add_mock(...)` method for users to register mocks
- Created `_callable_wrapper_with_mocks()` static method to apply mocks before calling test functions
- Modified `_run_multi_process_test()` to pass mock manager to child processes and use the wrapper
- Modified `_run_multi_process_test_per_rank()` to pass mock manager to child processes and use the wrapper
3. Added comprehensive test suite in `/data/sandcastle/boxes/fbsource/fbcode/torchrec/distributed/tests/test_multi_process_mock.py`:
- 6 unit tests for `MultiProcessMock` class functionality
- 6 integration tests including baseline tests to ensure backward compatibility
- Added BUCK target for the new test file
**TEST:**
All 12 tests pass successfully:
- Unit tests verify mock storage, multiple mocks, side effects, clearing, and application
- Integration tests verify cross-process mocking with return values, multiple mocks, and side effects
- Baseline tests confirm backward compatibility - existing tests without mocks continue to work exactly as before
- Fixed pickling issue by using module-level functions for side effects (required for multiprocessing)
Ran: `buck2 test torchrec/distributed/tests:test_multi_process_mock`
Result: Tests finished: Pass 12. Fail 0. Fatal 0. Skip 0.
Differential Revision: D886960581 parent 7b3effd commit d48d457
File tree
2 files changed
+443
-2
lines changed- torchrec/distributed
- test_utils
- tests
2 files changed
+443
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
28 | 88 | | |
29 | 89 | | |
30 | 90 | | |
| |||
111 | 171 | | |
112 | 172 | | |
113 | 173 | | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
114 | 200 | | |
115 | 201 | | |
116 | 202 | | |
| |||
149 | 235 | | |
150 | 236 | | |
151 | 237 | | |
| 238 | + | |
152 | 239 | | |
153 | | - | |
| 240 | + | |
| 241 | + | |
154 | 242 | | |
155 | 243 | | |
156 | 244 | | |
| |||
176 | 264 | | |
177 | 265 | | |
178 | 266 | | |
| 267 | + | |
179 | 268 | | |
180 | 269 | | |
181 | | - | |
| 270 | + | |
| 271 | + | |
182 | 272 | | |
183 | 273 | | |
184 | 274 | | |
| |||
188 | 278 | | |
189 | 279 | | |
190 | 280 | | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
191 | 311 | | |
192 | 312 | | |
193 | 313 | | |
| |||
0 commit comments