115 nb::set_leak_warnings(
false);
117 nb::class_<Type>(m,
"Type")
118 .def(nb::init<const Type::ID &>(), nb::arg(
"id"))
119 .def_prop_ro(
"id", &Type::getID)
120 .def(
"__repr__", [](
Type &t) {
return "<" + t.
getID() +
">"; });
121 nb::class_<ChannelType, Type>(m,
"ChannelType")
122 .def(nb::init<const Type::ID &, const Type *>(), nb::arg(
"id"),
124 .def_prop_ro(
"inner", &ChannelType::getInner, nb::rv_policy::reference);
125 nb::enum_<BundleType::Direction>(m,
"Direction")
126 .value(
"To", BundleType::Direction::To)
127 .value(
"From", BundleType::Direction::From)
129 nb::class_<BundleType, Type>(m,
"BundleType")
130 .def(nb::init<const Type::ID &, const BundleType::ChannelVector &>(),
131 nb::arg(
"id"), nb::arg(
"channels"))
132 .def_prop_ro(
"channels", &BundleType::getChannels,
133 nb::rv_policy::reference);
134 nb::class_<VoidType, Type>(m,
"VoidType")
135 .def(nb::init<const Type::ID &>(), nb::arg(
"id"));
136 nb::class_<AnyType, Type>(m,
"AnyType")
137 .def(nb::init<const Type::ID &>(), nb::arg(
"id"));
138 nb::class_<TypeAliasType, Type>(m,
"TypeAliasType")
139 .def(nb::init<const Type::ID &, std::string, const Type *>(),
140 nb::arg(
"id"), nb::arg(
"name"), nb::arg(
"inner_type"))
143 nb::rv_policy::reference);
144 nb::class_<BitVectorType, Type>(m,
"BitVectorType")
145 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
148 nb::class_<BitsType, BitVectorType>(m,
"BitsType")
149 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
151 nb::class_<IntegerType, BitVectorType>(m,
"IntegerType")
152 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
154 nb::class_<SIntType, IntegerType>(m,
"SIntType")
155 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
157 nb::class_<UIntType, IntegerType>(m,
"UIntType")
158 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
160 nb::class_<StructType, Type>(m,
"StructType")
161 .def(nb::init<const Type::ID &, const StructType::FieldVector &, bool>(),
162 nb::arg(
"id"), nb::arg(
"fields"), nb::arg(
"reverse") =
true)
163 .def_prop_ro(
"fields", &StructType::getFields, nb::rv_policy::reference)
164 .def_prop_ro(
"reverse", &StructType::isReverse);
165 nb::class_<ArrayType, Type>(m,
"ArrayType")
166 .def(nb::init<const Type::ID &, const Type *, uint64_t>(), nb::arg(
"id"),
167 nb::arg(
"element_type"), nb::arg(
"size"))
168 .def_prop_ro(
"element", &ArrayType::getElementType,
169 nb::rv_policy::reference)
170 .def_prop_ro(
"size", &ArrayType::getSize);
172 nb::class_<Constant>(m,
"Constant")
176 nb::class_<AppID>(m,
"AppID")
177 .def(nb::init<std::string, std::optional<uint32_t>>(), nb::arg(
"name"),
178 nb::arg(
"idx") = std::nullopt)
179 .def_prop_ro(
"name", [](
AppID &
id) {
return id.name; })
181 [](
AppID &
id) -> nb::object {
183 return nb::cast(
id.idx);
188 std::string ret =
"<" +
id.name;
190 ret = ret +
"[" + std::to_string(*
id.idx) +
"]";
194 .def(
"__eq__", [](
AppID &a,
AppID &b) {
return a == b; })
195 .def(
"__hash__", [](
AppID &
id) {
197 std::hash<uint32_t>{}(
id.idx.value_or(-1)));
201 nb::class_<ModuleInfo>(m,
"ModuleInfo")
202 .def_prop_ro(
"name", [](
ModuleInfo &info) {
return info.name; })
203 .def_prop_ro(
"summary", [](
ModuleInfo &info) {
return info.summary; })
204 .def_prop_ro(
"version", [](
ModuleInfo &info) {
return info.version; })
205 .def_prop_ro(
"repo", [](
ModuleInfo &info) {
return info.repo; })
206 .def_prop_ro(
"commit_hash",
207 [](
ModuleInfo &info) {
return info.commitHash; })
208 .def_prop_ro(
"constants", [](
ModuleInfo &info) {
return info.constants; })
212 std::stringstream os(ret);
217 nb::enum_<Logger::Level>(m,
"LogLevel")
218 .value(
"Debug", Logger::Level::Debug)
219 .value(
"Info", Logger::Level::Info)
220 .value(
"Warning", Logger::Level::Warning)
221 .value(
"Error", Logger::Level::Error)
223 nb::class_<Logger>(m,
"Logger");
225 nb::class_<services::Service>(m,
"Service")
228 nb::class_<SysInfo, services::Service>(m,
"SysInfo")
232 "Get the current cycle count of the accelerator system")
234 "Get the core clock frequency of the accelerator system in Hz");
236 nb::class_<MMIO::RegionDescriptor>(m,
"MMIORegionDescriptor")
239 nb::class_<services::MMIO, services::Service>(m,
"MMIO")
243 nb::rv_policy::reference);
245 nb::class_<services::HostMem::HostMemRegion>(m,
"HostMemRegion")
248 return reinterpret_cast<uintptr_t
>(mem.
getPtr());
252 nb::class_<services::HostMem::Options>(m,
"HostMemOptions")
257 std::string ret =
"HostMemOptions(";
261 ret +=
"use_large_pages";
266 nb::class_<services::HostMem, services::Service>(m,
"HostMem")
269 nb::rv_policy::take_ownership)
273 return self.
mapMemory(
reinterpret_cast<void *
>(ptr), size, opts);
275 nb::arg(
"ptr"), nb::arg(
"size"),
279 [](
HostMem &self, uintptr_t ptr) {
280 return self.
unmapMemory(
reinterpret_cast<void *
>(ptr));
283 nb::class_<services::TelemetryService, services::Service>(m,
286 nb::class_<std::future<MessageData>>(m,
"MessageDataFuture")
287 .def(
"valid", [](std::future<MessageData> &f) {
return f.valid(); })
289 [](std::future<MessageData> &f) {
292 nb::gil_scoped_release release{};
295 .def(
"get", [](std::future<MessageData> &f) {
296 std::optional<MessageData> data;
300 nb::gil_scoped_release release{};
301 data.emplace(f.get());
303 return nb::bytearray((
const char *)data->getBytes(), data->getSize());
306 nb::class_<ChannelPort::ConnectOptions>(m,
"ConnectOptions")
309 nb::arg(
"buffer_size").none())
310 .def_rw(
"translate_message",
313 nb::class_<ChannelPort>(m,
"ChannelPort")
315 "Connect with specified options")
319 nb::class_<WriteChannelPort, ChannelPort>(m,
"WriteChannelPort")
322 std::vector<uint8_t> dataVec((
const uint8_t *)data.c_str(),
323 (
const uint8_t *)data.c_str() +
328 std::vector<uint8_t> dataVec((
const uint8_t *)data.c_str(),
329 (
const uint8_t *)data.c_str() +
333 nb::class_<ReadChannelPort, ChannelPort>(m,
"ReadChannelPort")
339 return nb::bytearray((
const char *)data.getBytes(), data.getSize());
341 "Read data from the channel. Blocking.")
344 nb::class_<BundlePort>(m,
"BundlePort")
345 .def_prop_ro(
"id", &BundlePort::getID)
346 .def_prop_ro(
"channels", &BundlePort::getChannels,
347 nb::rv_policy::reference)
348 .def(
"getWrite", &BundlePort::getRawWrite, nb::rv_policy::reference)
349 .def(
"getRead", &BundlePort::getRawRead, nb::rv_policy::reference);
351 nb::class_<ServicePort, BundlePort>(m,
"ServicePort");
353 nb::class_<MMIO::MMIORegion, ServicePort>(m,
"MMIORegion")
358 nb::class_<FuncService::Function, ServicePort>(m,
"Function")
361 nb::bytearray msg) -> std::future<MessageData> {
362 std::vector<uint8_t> dataVec((
const uint8_t *)msg.c_str(),
363 (
const uint8_t *)msg.c_str() +
366 return self.call(data);
370 nb::class_<CallService::Callback, ServicePort>(m,
"Callback")
372 std::function<nb::object(nb::object)> pyCallback) {
377 nb::gil_scoped_acquire acquire{};
378 std::vector<uint8_t> arg(req.
getBytes(),
380 nb::bytes argObj((
const char *)arg.data(), arg.size());
381 auto ret = pyCallback(argObj);
384 nb::bytearray retBytes = nb::cast<nb::bytearray>(ret);
385 std::vector<uint8_t> dataVec((
const uint8_t *)retBytes.c_str(),
386 (
const uint8_t *)retBytes.c_str() +
392 nb::class_<TelemetryService::Metric, ServicePort>(m,
"Metric")
400 nb::class_<HWModule>(m,
"HWModule")
404 nb::rv_policy::reference);
407 nb::class_<Instance, HWModule>(m,
"Instance")
410 nb::class_<Accelerator, HWModule>(m,
"Accelerator");
415 nb::rv_policy::reference);
417 auto accConn = nb::class_<AcceleratorConnection>(m,
"AcceleratorConnection");
421 "An ESI context owns everything -- types, accelerator connections, and "
422 "the accelerator facade (aka Accelerator) itself. It MUST NOT be garbage "
423 "collected while the accelerator is still in use. When it is destroyed, "
424 "all accelerator connections are disconnected.")
425 .def(nb::init<>(),
"Create a context with a default logger.")
426 .def(
"connect", &Context::connect, nb::rv_policy::reference)
428 ctxt.
setLogger(std::make_unique<StreamLogger>(level));
437 nb::rv_policy::reference)
443 nb::rv_policy::reference)
445 "get_service_hostmem",
449 nb::rv_policy::reference)
451 nb::rv_policy::reference);
453 nb::class_<Manifest>(m,
"Manifest")
454 .def(nb::init<Context &, std::string>())
455 .def_prop_ro(
"api_version", &Manifest::getApiVersion)
459 auto *acc = m.buildAccelerator(conn);
463 nb::rv_policy::reference)
464 .def_prop_ro(
"type_table",
466 std::vector<nb::object> ret;
467 std::ranges::transform(m.getTypeTable(),
471 .def_prop_ro(
"module_infos", &Manifest::getModuleInfos);