MCPcopy Create free account
hub / github.com/cactus-compute/cactus / numpy

Method numpy

python/src/graph.py:350–378  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

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})"

Callers 15

_coerce_input_arrayMethod · 0.80
fold_bn_into_convFunction · 0.80
save_tensor_with_headerFunction · 0.80
test_pow_absMethod · 0.80
test_subtractMethod · 0.80
test_addMethod · 0.80
test_multiplyMethod · 0.80
test_divideMethod · 0.80
test_viewMethod · 0.80
test_flattenMethod · 0.80

Calls 3

reshapeMethod · 0.80
viewMethod · 0.45

Tested by 15

test_pow_absMethod · 0.64
test_subtractMethod · 0.64
test_addMethod · 0.64
test_multiplyMethod · 0.64
test_divideMethod · 0.64
test_viewMethod · 0.64
test_flattenMethod · 0.64
test_concatMethod · 0.64
test_cat_1dMethod · 0.64
test_cat_2d_axis1Method · 0.64
test_reluMethod · 0.64