115 nb::set_leak_warnings(
false);
117 nb::class_<Type>(m,
"Type")
118 .def(nb::init<const Type::ID &>(), nb::arg(
"id"))
121 .def(
"__repr__", [](
Type &t) {
return "<" + t.
getID() +
">"; });
122 nb::class_<ChannelType, Type>(m,
"ChannelType")
123 .def(nb::init<const Type::ID &, const Type *>(), nb::arg(
"id"),
126 nb::enum_<BundleType::Direction>(m,
"Direction")
130 nb::class_<BundleType, Type>(m,
"BundleType")
131 .def(nb::init<const Type::ID &, const BundleType::ChannelVector &>(),
132 nb::arg(
"id"), nb::arg(
"channels"))
134 nb::rv_policy::reference);
135 nb::class_<VoidType, Type>(m,
"VoidType")
136 .def(nb::init<const Type::ID &>(), nb::arg(
"id"));
137 nb::class_<AnyType, Type>(m,
"AnyType")
138 .def(nb::init<const Type::ID &>(), nb::arg(
"id"));
139 nb::class_<TypeAliasType, Type>(m,
"TypeAliasType")
140 .def(nb::init<const Type::ID &, std::string, const Type *>(),
141 nb::arg(
"id"), nb::arg(
"name"), nb::arg(
"inner_type"))
144 nb::rv_policy::reference);
145 nb::class_<BitVectorType, Type>(m,
"BitVectorType")
146 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
149 nb::class_<BitsType, BitVectorType>(m,
"BitsType")
150 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
152 nb::class_<IntegerType, BitVectorType>(m,
"IntegerType")
153 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
155 nb::class_<SIntType, IntegerType>(m,
"SIntType")
156 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
158 nb::class_<UIntType, IntegerType>(m,
"UIntType")
159 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
161 nb::class_<StructType, Type>(m,
"StructType")
162 .def(nb::init<const Type::ID &, const StructType::FieldVector &, bool>(),
163 nb::arg(
"id"), nb::arg(
"fields"), nb::arg(
"reverse") =
true)
166 nb::class_<ArrayType, Type>(m,
"ArrayType")
167 .def(nb::init<const Type::ID &, const Type *, uint64_t>(), nb::arg(
"id"),
168 nb::arg(
"element_type"), nb::arg(
"size"))
170 nb::rv_policy::reference)
172 nb::class_<WindowType::Field>(m,
"WindowField")
174 .def(nb::init<std::string, uint64_t, uint64_t>(), nb::arg(
"name"),
175 nb::arg(
"num_items") = 0, nb::arg(
"bulk_count_width") = 0)
179 nb::class_<WindowType::Frame>(m,
"WindowFrame")
181 .def(nb::init<std::string,
const std::vector<WindowType::Field> &>(),
182 nb::arg(
"name"), nb::arg(
"fields"))
185 nb::class_<WindowType, Type>(m,
"WindowType")
186 .def(nb::init<
const Type::ID &,
const std::string &,
const Type *,
187 const Type *,
const std::vector<WindowType::Frame> &>(),
188 nb::arg(
"id"), nb::arg(
"name"), nb::arg(
"into_type"),
189 nb::arg(
"lowered_type"), nb::arg(
"frames"))
193 nb::rv_policy::reference)
195 nb::class_<ListType, Type>(m,
"ListType")
196 .def(nb::init<const Type::ID &, const Type *>(), nb::arg(
"id"),
197 nb::arg(
"element_type"))
199 nb::rv_policy::reference);
200 nb::class_<UnionType, Type>(m,
"UnionType")
201 .def(nb::init<const Type::ID &, const UnionType::FieldVector &>(),
202 nb::arg(
"id"), nb::arg(
"fields"))
205 nb::class_<Constant>(m,
"Constant")
209 nb::class_<AppID>(m,
"AppID")
210 .def(nb::init<std::string, std::optional<uint32_t>>(), nb::arg(
"name"),
211 nb::arg(
"idx") = std::nullopt)
212 .def_prop_ro(
"name", [](
AppID &
id) {
return id.name; })
214 [](
AppID &
id) -> nb::object {
216 return nb::cast(
id.idx);
221 std::string ret =
"<" +
id.name;
223 ret = ret +
"[" + std::to_string(*
id.idx) +
"]";
227 .def(
"__eq__", [](
AppID &a,
AppID &b) {
return a == b; })
228 .def(
"__hash__", [](
AppID &
id) {
230 std::hash<uint32_t>{}(
id.idx.value_or(-1)));
234 nb::class_<ModuleInfo>(m,
"ModuleInfo")
235 .def_prop_ro(
"name", [](
ModuleInfo &info) {
return info.name; })
236 .def_prop_ro(
"summary", [](
ModuleInfo &info) {
return info.summary; })
237 .def_prop_ro(
"version", [](
ModuleInfo &info) {
return info.version; })
238 .def_prop_ro(
"repo", [](
ModuleInfo &info) {
return info.repo; })
239 .def_prop_ro(
"commit_hash",
240 [](
ModuleInfo &info) {
return info.commitHash; })
241 .def_prop_ro(
"constants", [](
ModuleInfo &info) {
return info.constants; })
245 std::stringstream os(ret);
250 nb::enum_<Logger::Level>(m,
"LogLevel")
256 nb::class_<Logger>(m,
"Logger");
258 nb::class_<services::Service>(m,
"Service")
261 nb::class_<SysInfo, services::Service>(m,
"SysInfo")
265 "Get the current cycle count of the accelerator system")
267 "Get the core clock frequency of the accelerator system in Hz");
269 nb::class_<MMIO::RegionDescriptor>(m,
"MMIORegionDescriptor")
272 nb::class_<services::MMIO, services::Service>(m,
"MMIO")
275 nb::call_guard<nb::gil_scoped_release>())
277 nb::call_guard<nb::gil_scoped_release>())
279 nb::rv_policy::reference);
281 nb::class_<services::HostMem::HostMemRegion>(m,
"HostMemRegion")
284 return reinterpret_cast<uintptr_t
>(mem.
getPtr());
288 nb::class_<services::HostMem::Options>(m,
"HostMemOptions")
290 .def_rw(
"writeable", &services::HostMem::Options::writeable)
291 .def_rw(
"use_large_pages", &services::HostMem::Options::useLargePages)
293 std::string ret =
"HostMemOptions(";
297 ret +=
"use_large_pages";
302 nb::class_<services::HostMem, services::Service>(m,
"HostMem")
304 "Enable the host memory service (e.g. start servicing the "
305 "accelerator's read and write requests). Required before the "
306 "accelerator accesses host memory on some backends.")
309 nb::rv_policy::take_ownership)
313 return self.
mapMemory(
reinterpret_cast<void *
>(ptr), size, opts);
315 nb::arg(
"ptr"), nb::arg(
"size"),
319 [](
HostMem &self, uintptr_t ptr) {
320 return self.
unmapMemory(
reinterpret_cast<void *
>(ptr));
323 nb::class_<services::TelemetryService, services::Service>(m,
326 nb::class_<std::future<MessageData>>(m,
"MessageDataFuture")
327 .def(
"valid", [](std::future<MessageData> &f) {
return f.valid(); })
329 [](std::future<MessageData> &f) {
332 nb::gil_scoped_release release{};
335 .def(
"get", [](std::future<MessageData> &f) {
336 std::optional<MessageData> data;
340 nb::gil_scoped_release release{};
341 data.emplace(f.get());
343 return nb::bytearray((
const char *)data->getBytes(), data->getSize());
346 nb::class_<ChannelPort::ConnectOptions>(m,
"ConnectOptions")
349 nb::arg(
"buffer_size").none())
350 .def_rw(
"translate_message",
353 nb::class_<ChannelPort>(m,
"ChannelPort")
355 "Connect with specified options")
359 nb::rv_policy::reference);
361 nb::class_<WriteChannelPort, ChannelPort>(m,
"WriteChannelPort")
364 std::vector<uint8_t> dataVec((
const uint8_t *)data.c_str(),
365 (
const uint8_t *)data.c_str() +
369 nb::gil_scoped_release release{};
373 std::vector<uint8_t> dataVec((
const uint8_t *)data.c_str(),
374 (
const uint8_t *)data.c_str() +
378 nb::class_<ReadChannelPort, ChannelPort>(m,
"ReadChannelPort")
384 return nb::bytearray((
const char *)data.getBytes(), data.getSize());
386 "Read data from the channel. Blocking.")
389 nb::class_<BundlePort>(m,
"BundlePort")
392 nb::rv_policy::reference)
396 nb::class_<ServicePort, BundlePort>(m,
"ServicePort");
398 nb::class_<MMIO::MMIORegion, ServicePort>(m,
"MMIORegion")
402 nb::call_guard<nb::gil_scoped_release>())
404 nb::call_guard<nb::gil_scoped_release>());
406 nb::class_<FuncService::Function, ServicePort>(m,
"Function")
409 nb::bytearray msg) -> std::future<MessageData> {
410 std::vector<uint8_t> dataVec((
const uint8_t *)msg.c_str(),
411 (
const uint8_t *)msg.c_str() +
415 nb::gil_scoped_release release{};
416 return self.call(data);
420 nb::class_<CallService::Callback, ServicePort>(m,
"Callback")
422 std::function<nb::object(nb::object)> pyCallback) {
427 nb::gil_scoped_acquire acquire{};
428 std::vector<uint8_t> arg(req.
getBytes(),
430 nb::bytes argObj((
const char *)arg.data(), arg.size());
431 auto ret = pyCallback(argObj);
434 nb::bytearray retBytes = nb::cast<nb::bytearray>(ret);
435 std::vector<uint8_t> dataVec((
const uint8_t *)retBytes.c_str(),
436 (
const uint8_t *)retBytes.c_str() +
442 nb::class_<TelemetryService::Metric, ServicePort>(m,
"Metric")
447 nb::class_<ChannelService::ToHost, ServicePort>(m,
"ToHostChannel")
451 nb::class_<ChannelService::FromHost, ServicePort>(m,
"FromHostChannel")
456 std::vector<uint8_t> dataVec((
const uint8_t *)data.c_str(),
457 (
const uint8_t *)data.c_str() +
460 nb::gil_scoped_release release{};
468 nb::class_<HWModule>(m,
"HWModule")
472 nb::rv_policy::reference);
475 nb::class_<Instance, HWModule>(m,
"Instance")
478 nb::class_<Accelerator, HWModule>(m,
"Accelerator");
483 nb::rv_policy::reference);
485 auto accConn = nb::class_<AcceleratorConnection>(m,
"AcceleratorConnection");
489 "An ESI context owns everything -- types, accelerator connections, and "
490 "the accelerator facade (aka Accelerator) itself. It MUST NOT be garbage "
491 "collected while the accelerator is still in use. When it is destroyed, "
492 "all accelerator connections are disconnected.")
493 .def(nb::init<>(),
"Create a context with a default logger.")
496 ctxt.
setLogger(std::make_unique<StreamLogger>(level));
505 nb::rv_policy::reference)
511 nb::rv_policy::reference)
513 "get_service_hostmem",
517 nb::rv_policy::reference)
519 nb::rv_policy::reference);
521 nb::class_<Manifest>(m,
"Manifest")
522 .def(nb::init<Context &, std::string>())
527 auto *acc = m.buildAccelerator(conn);
528 conn.getServiceThread()->addPoll(*acc);
531 nb::rv_policy::reference)
532 .def_prop_ro(
"type_table",
534 std::vector<nb::object> ret;
535 std::ranges::transform(m.getTypeTable(),