| 348 | return self.g.softmax(self, axis) |
| 349 | |
| 350 | def numpy(self): |
| 351 | info = cactus_tensor_info_t() |
| 352 | rc = _lib.cactus_graph_get_output_info(self.g.h, cactus_node_t(self.id), ctypes.byref(info)) |
| 353 | if rc != 0: |
| 354 | raise RuntimeError("graph_get_output_info failed") |
| 355 | |
| 356 | out_ptr = ctypes.c_void_p() |
| 357 | rc = _lib.cactus_graph_get_output_ptr(self.g.h, cactus_node_t(self.id), ctypes.byref(out_ptr)) |
| 358 | if rc != 0 or not out_ptr.value: |
| 359 | raise RuntimeError("graph_get_output_ptr failed") |
| 360 | |
| 361 | rank = int(info.rank) |
| 362 | shape = tuple(int(info.shape[i]) for i in range(rank)) |
| 363 | num_elements = int(info.num_elements) |
| 364 | precision = int(info.precision) |
| 365 | |
| 366 | if precision == Graph.FP16: |
| 367 | arr = np.ctypeslib.as_array((ctypes.c_uint16 * num_elements).from_address(out_ptr.value)).view(np.float16) |
| 368 | elif precision == Graph.FP32: |
| 369 | arr = np.ctypeslib.as_array((ctypes.c_float * num_elements).from_address(out_ptr.value)) |
| 370 | elif precision == Graph.INT8: |
| 371 | arr = np.ctypeslib.as_array((ctypes.c_int8 * num_elements).from_address(out_ptr.value)) |
| 372 | elif precision == Graph.INT4: |
| 373 | arr = np.ctypeslib.as_array((ctypes.c_uint8 * int(info.byte_size)).from_address(out_ptr.value)) |
| 374 | return arr.copy() |
| 375 | else: |
| 376 | raise RuntimeError("unsupported precision") |
| 377 | |
| 378 | return arr.reshape(shape).copy() |
| 379 | |
| 380 | def __repr__(self): |
| 381 | return f"Tensor(id={self.id}, shape={self.shape}, dtype={self.dtype})" |