MCPcopy Create free account
hub / github.com/pytorch/pytorch / _load

Function _load

torch/serialization.py:1333–1444  ·  view source on GitHub ↗
(zip_file, map_location, pickle_module, pickle_file='data.pkl', overall_storage=None, **pickle_load_args)

Source from the content-addressed store, hash-verified

1331
1332
1333def _load(zip_file, map_location, pickle_module, pickle_file='data.pkl', overall_storage=None, **pickle_load_args):
1334 restore_location = _get_restore_location(map_location)
1335
1336 loaded_storages = {}
1337
1338 # check if byteswapping is needed
1339 byteordername = 'byteorder'
1340 byteorderdata = None
1341 if zip_file.has_record(byteordername):
1342 byteorderdata = zip_file.get_record(byteordername)
1343 if byteorderdata not in [b'little', b'big']:
1344 raise ValueError('Unknown endianness type: ' + byteorderdata.decode())
1345 elif get_default_load_endianness() == LoadEndianness.LITTLE or \
1346 get_default_load_endianness() is None:
1347 byteorderdata = b'little'
1348 elif get_default_load_endianness() == LoadEndianness.BIG:
1349 byteorderdata = b'big'
1350 elif get_default_load_endianness() == LoadEndianness.NATIVE:
1351 pass
1352 else:
1353 raise ValueError('Invalid load endianness type')
1354
1355 if not zip_file.has_record(byteordername) and \
1356 get_default_load_endianness() is None and \
1357 sys.byteorder == 'big':
1358 # Default behaviour was changed
1359 # See https://github.com/pytorch/pytorch/issues/101688
1360 warnings.warn("The default load endianness for checkpoints without a byteorder mark "
1361 "on big endian machines was changed from 'native' to 'little' endian, "
1362 "to avoid this behavior please use "
1363 "torch.serialization.set_default_load_endianness to set "
1364 "the desired default load endianness",
1365 UserWarning)
1366
1367 def load_tensor(dtype, numel, key, location):
1368 name = f'data/{key}'
1369 if overall_storage is not None:
1370 storage_offset = zip_file.get_record_offset(name)
1371 storage = overall_storage[storage_offset:storage_offset + numel]
1372 else:
1373 storage = zip_file.get_storage_from_record(name, numel, torch.UntypedStorage)._typed_storage()._untyped_storage
1374 # swap here if byteswapping is needed
1375 if byteorderdata is not None:
1376 if byteorderdata.decode() != sys.byteorder:
1377 storage.byteswap(dtype)
1378
1379 # TODO: Once we decide to break serialization FC, we can
1380 # stop wrapping with TypedStorage
1381 typed_storage = torch.storage.TypedStorage(
1382 wrap_storage=restore_location(storage, location),
1383 dtype=dtype,
1384 _internal=True)
1385
1386 if typed_storage._data_ptr() != 0:
1387 loaded_storages[key] = typed_storage
1388
1389 return typed_storage
1390

Callers 1

loadFunction · 0.85

Calls 9

_get_restore_locationFunction · 0.85
UnpicklerWrapperClass · 0.85
has_recordMethod · 0.80
warnMethod · 0.80
serialization_idMethod · 0.80
get_recordMethod · 0.45
decodeMethod · 0.45
loadMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…