Return a contiguous uint8 view of a tensor/array.
(data: Tensor)
| 74 | |
| 75 | |
| 76 | def _contiguous_tensor_bytes(data: Tensor) -> memoryview: |
| 77 | """Return a contiguous uint8 view of a tensor/array.""" |
| 78 | data = standardize_tensor(data) |
| 79 | # From joblib.hashing |
| 80 | if data.shape == (): |
| 81 | # 0d arrays need to be flattened because viewing them as bytes |
| 82 | # raises a ValueError exception. |
| 83 | data_c_contiguous = data.flatten() |
| 84 | elif data.flags.c_contiguous: |
| 85 | data_c_contiguous = data |
| 86 | elif data.flags.f_contiguous: |
| 87 | data_c_contiguous = data.T |
| 88 | else: |
| 89 | # Cater for non-single-segment arrays, this creates a copy, and thus |
| 90 | # alleviates this issue. Note: There might be a more efficient way of |
| 91 | # doing this, check for joblib updates. |
| 92 | data_c_contiguous = data.flatten() |
| 93 | return memoryview(data_c_contiguous.view("uint8")) |
| 94 | |
| 95 | |
| 96 | def data_to_buffer(data: Tensor) -> bytes: |
no test coverage detected
searching dependent graphs…