Repository navigation
Expand file tree
/
Copy pathgpu_cpp.cpp
More file actions
235 lines (210 loc) · 8.41 KB
/
Copy pathgpu_cpp.cpp
File metadata and controls
235 lines (210 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "gpu.hpp"
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <cstdint>
#include <memory>
#include <span>
#include <string>
#include <utility>
#include <vector>
namespace py = pybind11;
using namespace gpu;
namespace {
Shape shape(const std::vector<py::ssize_t> &dimensions) {
if (dimensions.size() > Shape::kMaxRank)
throw std::invalid_argument("tensor rank exceeds 8");
Shape result;
result.rank = dimensions.size();
for (size_t i = 0; i < dimensions.size(); ++i) {
if (dimensions[i] < 0)
throw std::invalid_argument("tensor dimensions cannot be negative");
result.data[i] = dimensions[i];
}
return result;
}
std::vector<py::ssize_t> dimensions(const Shape &shape) {
std::vector<py::ssize_t> result(shape.rank);
for (size_t i = 0; i < shape.rank; ++i) result[i] = shape[i];
return result;
}
void requireContiguous(const py::array &array) {
if (!(array.flags() & py::array::c_style))
throw std::invalid_argument("array must be C-contiguous");
}
NumType numType(const py::array &array) {
if (array.dtype().is(py::dtype::of<float>())) return kf32;
if (array.dtype().is(py::dtype::of<int32_t>())) return ki32;
if (array.dtype().is(py::dtype("float16"))) return kf16;
throw std::invalid_argument("array dtype must be float16, float32, or int32");
}
py::dtype dtype(NumType type) {
switch (type) {
case kf16: return py::dtype("float16");
case kf32: return py::dtype::of<float>();
case ki32: return py::dtype::of<int32_t>();
}
throw std::invalid_argument("unknown numeric type");
}
template <typename T>
std::span<const T> input(const py::buffer_info &buffer) {
return {static_cast<const T *>(buffer.ptr),
static_cast<size_t>(buffer.size)};
}
struct PythonFuture {
std::shared_ptr<Context> context;
gpu::Future value;
py::object result;
};
Tensor tensor(Context &context, const py::array &array) {
requireContiguous(array);
const auto type = numType(array);
const auto buffer = array.request();
const auto tensorShape = shape(buffer.shape);
switch (type) {
case kf16:
return createTensor(context, tensorShape, type, input<uint16_t>(buffer));
case kf32:
return createTensor(context, tensorShape, type, input<float>(buffer));
case ki32:
return createTensor(context, tensorShape, type, input<int32_t>(buffer));
}
throw std::invalid_argument("unknown numeric type");
}
void upload(Context &context, const Tensor &tensor, const py::array &array) {
requireContiguous(array);
if (numType(array) != tensor.type)
throw std::invalid_argument("array and tensor dtypes differ");
const auto buffer = array.request();
switch (tensor.type) {
case kf16: return toGPU(context, tensor, input<uint16_t>(buffer));
case kf32: return toGPU(context, tensor, input<float>(buffer));
case ki32: return toGPU(context, tensor, input<int32_t>(buffer));
}
throw std::invalid_argument("unknown numeric type");
}
template <typename T>
PythonFuture download(std::shared_ptr<Context> context, const Tensor &tensor) {
auto values = std::make_shared<std::vector<T>>(size(tensor.shape));
auto owner = py::capsule(
new std::shared_ptr<std::vector<T>>(values), [](void *pointer) {
delete static_cast<std::shared_ptr<std::vector<T>> *>(pointer);
});
py::array result(dtype(tensor.type), dimensions(tensor.shape), values->data(),
owner);
auto future = toCPU(*context, tensor, values);
return {std::move(context), std::move(future), std::move(result)};
}
PythonFuture numpy(std::shared_ptr<Context> context, const Tensor &tensor) {
switch (tensor.type) {
case kf16: return download<uint16_t>(std::move(context), tensor);
case kf32: return download<float>(std::move(context), tensor);
case ki32: return download<int32_t>(std::move(context), tensor);
}
throw std::invalid_argument("unknown numeric type");
}
bool pollFuture(PythonFuture &future) {
py::gil_scoped_release release;
return poll(*future.context, future.value);
}
py::object waitFuture(PythonFuture &future) {
{
py::gil_scoped_release release;
wait(*future.context, future.value);
}
return future.result;
}
} // namespace
PYBIND11_MODULE(_gpu_cpp, module) {
module.doc() = "Native WebGPU compute with gpu.cpp";
py::enum_<NumType>(module, "NumType")
.value("f16", kf16)
.value("f32", kf32)
.value("i32", ki32)
.export_values();
py::class_<Context, std::shared_ptr<Context>>(module, "Context")
.def(py::init([](bool shaderF16, bool spirv) {
ContextOptions options{.enableSPIRV = spirv};
if (shaderF16)
options.requiredFeatures = {wgpu::FeatureName::ShaderF16};
py::gil_scoped_release release;
return std::make_shared<Context>(createContext(options));
}),
py::kw_only(), py::arg("shader_f16") = false,
py::arg("spirv") = false);
py::class_<WGSL>(module, "WGSL")
.def(py::init([](std::string code,
const std::vector<py::ssize_t> &workgroupSize,
NumType precision) {
return WGSL(std::move(code), shape(workgroupSize), precision);
}),
py::arg("code"), py::arg("workgroup_size") =
std::vector<py::ssize_t>{256, 1, 1},
py::arg("precision") = kf32)
.def_readwrite("code", &WGSL::code)
.def_readwrite("label", &WGSL::label)
.def_readwrite("entry_point", &WGSL::entryPoint)
.def_property_readonly("workgroup_size",
[](const WGSL &shader) {
return dimensions(shader.workgroupSize);
});
py::class_<SPIRV>(module, "SPIRV")
.def(py::init([](std::vector<uint32_t> words) {
return SPIRV{std::move(words)};
}),
py::arg("words"))
.def_readwrite("words", &SPIRV::code)
.def_readwrite("label", &SPIRV::label)
.def_readwrite("entry_point", &SPIRV::entryPoint);
py::class_<Tensor>(module, "Tensor")
.def_property_readonly("shape",
[](const Tensor &tensor) {
return dimensions(tensor.shape);
})
.def_readonly("dtype", &Tensor::type);
py::class_<Binding>(module, "Binding");
py::class_<Kernel>(module, "Kernel");
py::class_<PythonFuture>(module, "Future")
.def("_poll", &pollFuture)
.def("_result", [](PythonFuture &future) { return future.result; })
.def("__await__", [](py::object self) {
return py::module_::import("gpu_cpp._async")
.attr("wait")(std::move(self))
.attr("__await__")();
});
module.def("create_tensor",
[](Context &context,
const std::vector<py::ssize_t> &tensorShape, NumType type) {
return createTensor(context, shape(tensorShape), type);
},
py::arg("context"), py::arg("shape"), py::arg("dtype"));
module.def("tensor", &tensor, py::arg("context"), py::arg("array"));
module.def("read", py::overload_cast<const Tensor &>(&read),
py::arg("tensor"));
module.def("read_write", py::overload_cast<const Tensor &>(&readWrite),
py::arg("tensor"));
module.def(
"create_kernel",
[](Context &context, const Shader &shader,
const std::vector<Binding> &bindings,
const std::vector<py::ssize_t> &workgroups, py::bytes parameters) {
const std::string bytes = parameters;
const auto *data = reinterpret_cast<const std::byte *>(bytes.data());
return createKernel(context, shader, std::span<const Binding>(bindings),
shape(workgroups), {data, bytes.size()});
},
py::arg("context"), py::arg("shader"), py::arg("bindings"),
py::arg("workgroups") = std::vector<py::ssize_t>{1, 1, 1},
py::arg("parameters") = py::bytes());
module.def("dispatch_kernel",
[](std::shared_ptr<Context> context, const Kernel &kernel) {
auto future = dispatchKernel(*context, kernel);
return PythonFuture{std::move(context), std::move(future),
py::none()};
},
py::arg("context"), py::arg("kernel"));
module.def("wait", &waitFuture, py::arg("future"));
module.def("to_gpu", &upload, py::arg("context"), py::arg("tensor"),
py::arg("array"));
module.def("to_numpy", &numpy, py::arg("context"), py::arg("tensor"));
}