A wrapper for numpy.save(), ensures the array is stored 4KiB aligned.
(*args, **kwargs)
| 13 | |
| 14 | |
| 15 | def numpy_save_aligned(*args, **kwargs): |
| 16 | """A wrapper for numpy.save(), ensures the array is stored 4KiB aligned.""" |
| 17 | # https://github.com/numpy/numpy/blob/2093a6d5b933f812d15a3de0eafeeb23c61f948a/numpy/lib/format.py#L179 |
| 18 | has_array_align = hasattr(np.lib.format, "ARRAY_ALIGN") |
| 19 | if has_array_align: |
| 20 | default_alignment = np.lib.format.ARRAY_ALIGN |
| 21 | # The maximum allowed alignment by the numpy code linked above is 4K. |
| 22 | # Most filesystems work with block sizes of 4K so in practice, the file |
| 23 | # size on the disk won't be larger. |
| 24 | np.lib.format.ARRAY_ALIGN = 4096 |
| 25 | np.save(*args, **kwargs) |
| 26 | if has_array_align: |
| 27 | np.lib.format.ARRAY_ALIGN = default_alignment |
| 28 | |
| 29 | |
| 30 | def _read_torch_data(path): |