MCPcopy Create free account
hub / github.com/OpenNMT/CTranslate2 / register_storage_view

Function register_storage_view

python/cpp/storage_view.cc:99–239  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

97 }
98
99 void register_storage_view(py::module& m) {
100 py::enum_<DataType>(m, "DataType")
101 .value("float32", DataType::FLOAT32)
102 .value("float16", DataType::FLOAT16)
103 .value("bfloat16", DataType::BFLOAT16)
104 .value("int8", DataType::INT8)
105 .value("int16", DataType::INT16)
106 .value("int32", DataType::INT32)
107 ;
108
109 py::enum_<Device>(m, "Device")
110 .value("cpu", Device::CPU)
111 .value("cuda", Device::CUDA)
112 ;
113
114 py::class_<StorageView>(
115 m, "StorageView",
116 R"pbdoc(
117 An allocated buffer with shape information.
118
119 The object implements the
120 `Array Interface <https://numpy.org/doc/stable/reference/arrays.interface.html>`_
121 and the
122 `CUDA Array Interface <https://numba.readthedocs.io/en/stable/cuda/cuda_array_interface.html>`_
123 so that it can be passed to Numpy or PyTorch without copy.
124
125 Example:
126
127 >>> x = np.ones((2, 4), dtype=np.int32)
128 >>> y = ctranslate2.StorageView.from_array(x)
129 >>> print(y)
130 1 1 1 ... 1 1 1
131 [cpu:0 int32 storage viewed as 2x4]
132 >>> z = np.array(y)
133 ...
134 >>> x = torch.ones((2, 4), dtype=torch.int32, device="cuda")
135 >>> y = ctranslate2.StorageView.from_array(x)
136 >>> print(y)
137 1 1 1 ... 1 1 1
138 [cuda:0 int32 storage viewed as 2x4]
139 >>> z = torch.as_tensor(y, device="cuda")
140
141 )pbdoc")
142
143 .def_static("from_array", &create_view_from_array, py::arg("array"),
144 py::keep_alive<0, 1>(),
145 R"pbdoc(
146 Creates a ``StorageView`` from an object implementing the array interface.
147
148 Arguments:
149 array: An object implementing the array interface (e.g. a Numpy array
150 or a PyTorch Tensor).
151
152 Returns:
153 A new ``StorageView`` instance sharing the same data as the input array.
154
155 Raises:
156 ValueError: if the object does not implement the array interface or

Callers 1

PYBIND11_MODULEFunction · 0.85

Calls 7

device_to_strFunction · 0.85
get_array_interfaceFunction · 0.85
synchronize_streamFunction · 0.85
valueMethod · 0.45
deviceMethod · 0.45
device_indexMethod · 0.45
toMethod · 0.45

Tested by

no test coverage detected