| 272 | return Tensor(self, int(node_id), meta["shape"], meta["precision"]) |
| 273 | |
| 274 | def _coerce_input_array(self, data, precision): |
| 275 | if isinstance(data, Tensor): |
| 276 | arr = data.numpy() |
| 277 | else: |
| 278 | arr = np.asarray(data) |
| 279 | if precision == self.INT8: |
| 280 | arr = np.ascontiguousarray(arr, dtype=np.int8) |
| 281 | elif precision == self.FP16: |
| 282 | arr = np.ascontiguousarray(arr, dtype=np.float16) |
| 283 | elif precision == self.FP32: |
| 284 | arr = np.ascontiguousarray(arr, dtype=np.float32) |
| 285 | elif precision == self.INT4: |
| 286 | arr = np.ascontiguousarray(arr, dtype=np.uint8) |
| 287 | else: |
| 288 | raise RuntimeError("unsupported precision") |
| 289 | return arr |
| 290 | |
| 291 | |
| 292 | class Tensor: |