MCPcopy Create free account
hub / github.com/apple/axlearn / test_serialize

Method test_serialize

axlearn/common/array_serialization_test.py:237–292  ·  view source on GitHub ↗
(
        self, arrays: list[list[int]], max_concurrent_gb: int, expect_max_concurrent_gb: int
    )

Source from the content-addressed store, hash-verified

235 ),
236 )
237 def test_serialize(
238 self, arrays: list[list[int]], max_concurrent_gb: int, expect_max_concurrent_gb: int
239 ):
240 arrays = [
241 mock.Mock(
242 addressable_shards=[
243 mock.Mock(replica_id=0, **{"data.nbytes": int(shard * 10**9), "data.shape": ()})
244 for shard in array
245 ],
246 nbytes=int(sum(array) * 10**9),
247 dtype=jax.numpy.bfloat16,
248 )
249 for array in arrays
250 ]
251 tensorstore_specs = [{} for _ in range(len(arrays))]
252 expect_max_concurrent_bytes = int(expect_max_concurrent_gb * 10**9)
253
254 concurrent_bytes = 0
255
256 class FakeTs:
257 def __getitem__(self, *_):
258 return self
259
260 async def write(self, data: jax.Array, **_):
261 await asyncio.sleep(0.1)
262 nonlocal concurrent_bytes
263 concurrent_bytes -= data.nbytes
264
265 async def open_patch(*_, **__):
266 return FakeTs()
267
268 async def _copy_to_host_patch(shard_infos: list[_ShardInfo]):
269 nonlocal concurrent_bytes
270 for info in shard_infos:
271 concurrent_bytes += info.data.nbytes
272 # In-flight bytes should be lower than the expected max bytes
273 self.assertLessEqual(concurrent_bytes, expect_max_concurrent_bytes)
274
275 manager = BoundedDataShardedAsyncCheckpointManager(max_concurrent_gb=max_concurrent_gb)
276 with (
277 mock.patch(
278 f"{array_serialization.__name__}._num_replicas_per_shard",
279 lambda *args: defaultdict(lambda: 1),
280 ),
281 mock.patch(f"{array_serialization.__name__}._slices_to_tuple", lambda *_: 1),
282 mock.patch(
283 f"{array_serialization.__name__}._slice_shard_and_copy_to_host", _copy_to_host_patch
284 ),
285 mock.patch(
286 f"{array_serialization.__name__}.serialization._get_metadata", lambda *_: {}
287 ),
288 mock.patch(f"{array_serialization.__name__}.ts.open", open_patch),
289 mock.patch(f"{array_serialization.__name__}.ts.Spec", mock.MagicMock()),
290 ):
291 manager.serialize(arrays, tensorstore_specs, on_commit_callback=lambda: None)
292 manager.wait_until_finished()
293
294 @parameterized.product(

Callers

nothing calls this directly

Calls 4

serializeMethod · 0.95
patchMethod · 0.45
wait_until_finishedMethod · 0.45

Tested by

no test coverage detected