(v: Union[bool, numbers.Number, ndarray, torch.Tensor])
| 153 | |
| 154 | |
| 155 | def to_nd_array(v: Union[bool, numbers.Number, ndarray, torch.Tensor]) -> np.ndarray: |
| 156 | if isinstance(v, np.ndarray): |
| 157 | return v |
| 158 | |
| 159 | if isinstance(v, torch.Tensor): |
| 160 | # If v was quantized, we compare its int representation. |
| 161 | v = v.int_repr() if v.is_quantized else v |
| 162 | return v.cpu().detach().numpy() |
| 163 | |
| 164 | if isinstance(v, (numbers.Number, bool)): |
| 165 | return np.array([v]) |
| 166 | |
| 167 | raise RuntimeError(f"Unknown type {type(v)}") |