blosc2.pack_tensor#

blosc2.pack_tensor(tensor: tensorflow.Tensor | torch.Tensor | np.ndarray, chunksize: int | None = None, **kwargs: dict) bytes | int#

Pack (compress) a TensorFlow or PyTorch tensor or a NumPy array.

Parameters:
  • tensor (tensorflow.Tensor, torch.Tensor, or np.ndarray.) – The tensor or array to be packed.

  • chunksize (int, optional) – The size (in bytes) for the chunks during compression. If not provided, it is computed automatically.

  • kwargs (dict, optional) – These are the same as the kwargs in SChunk.__init__.

Returns:

out – The serialized version (cframe) of the array. If urlpath is provided, the number of bytes in file is returned instead.

Return type:

bytes | int

Notes

In case you pass a TensorFlow/PyTorch tensor, the tensor will be converted to a NumPy array before being packed. The tensor will be restored to its original form when unpacked.

Examples

>>> import numpy as np
>>> th = np.arange(1e6, dtype=np.float32)
>>> cframe = blosc2.pack_tensor(th)
>>> if not os.getenv("BTUNE_TRADEOFF"):
...     assert len(cframe) < th.size * th.itemsize
...