(tensor: torch.Tensor)
| 29 | |
| 30 | |
| 31 | def _tensor_to_numpy(tensor: torch.Tensor) -> np.ndarray: |
| 32 | tensor = tensor.detach().cpu().contiguous() |
| 33 | if tensor.dtype in (torch.bfloat16, torch.float8_e4m3fn, torch.float8_e5m2): |
| 34 | try: |
| 35 | import ml_dtypes # type: ignore[import-not-found] |
| 36 | except ImportError as e: |
| 37 | raise RuntimeError( |
| 38 | f"ml_dtypes is required to serialize {tensor.dtype} tensors for TOSA. " |
| 39 | "Have you run setup.sh?" |
| 40 | ) from e |
| 41 | |
| 42 | ml_dtype_map = { |
| 43 | torch.bfloat16: (torch.uint16, ml_dtypes.bfloat16), |
| 44 | torch.float8_e4m3fn: (torch.uint8, ml_dtypes.float8_e4m3fn), |
| 45 | torch.float8_e5m2: (torch.uint8, ml_dtypes.float8_e5m2), |
| 46 | } |
| 47 | storage_dtype, ml_dtype = ml_dtype_map[tensor.dtype] |
| 48 | return tensor.view(storage_dtype).numpy().view(ml_dtype) |
| 49 | else: |
| 50 | return tensor.numpy() |
| 51 | |
| 52 | |
| 53 | def _prepare_const_values_for_tosa_dtype( |
no outgoing calls
no test coverage detected