114 nb::set_leak_warnings(
false);
116 nb::class_<Type>(m,
"Type")
117 .def(nb::init<const Type::ID &>(), nb::arg(
"id"))
118 .def_prop_ro(
"id", &Type::getID)
119 .def(
"__repr__", [](
Type &t) {
return "<" + t.
getID() +
">"; });
120 nb::class_<ChannelType, Type>(m,
"ChannelType")
121 .def(nb::init<const Type::ID &, const Type *>(), nb::arg(
"id"),
123 .def_prop_ro(
"inner", &ChannelType::getInner, nb::rv_policy::reference);
124 nb::enum_<BundleType::Direction>(m,
"Direction")
125 .value(
"To", BundleType::Direction::To)
126 .value(
"From", BundleType::Direction::From)
128 nb::class_<BundleType, Type>(m,
"BundleType")
129 .def(nb::init<const Type::ID &, const BundleType::ChannelVector &>(),
130 nb::arg(
"id"), nb::arg(
"channels"))
131 .def_prop_ro(
"channels", &BundleType::getChannels,
132 nb::rv_policy::reference);
133 nb::class_<VoidType, Type>(m,
"VoidType")
134 .def(nb::init<const Type::ID &>(), nb::arg(
"id"));
135 nb::class_<AnyType, Type>(m,
"AnyType")
136 .def(nb::init<const Type::ID &>(), nb::arg(
"id"));
137 nb::class_<BitVectorType, Type>(m,
"BitVectorType")
138 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
141 nb::class_<BitsType, BitVectorType>(m,
"BitsType")
142 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
144 nb::class_<IntegerType, BitVectorType>(m,
"IntegerType")
145 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
147 nb::class_<SIntType, IntegerType>(m,
"SIntType")
148 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
150 nb::class_<UIntType, IntegerType>(m,
"UIntType")
151 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
153 nb::class_<StructType, Type>(m,
"StructType")
154 .def(nb::init<const Type::ID &, const StructType::FieldVector &, bool>(),
155 nb::arg(
"id"), nb::arg(
"fields"), nb::arg(
"reverse") =
true)
156 .def_prop_ro(
"fields", &StructType::getFields, nb::rv_policy::reference)
157 .def_prop_ro(
"reverse", &StructType::isReverse);
158 nb::class_<ArrayType, Type>(m,
"ArrayType")
159 .def(nb::init<const Type::ID &, const Type *, uint64_t>(), nb::arg(
"id"),
160 nb::arg(
"element_type"), nb::arg(
"size"))
161 .def_prop_ro(
"element", &ArrayType::getElementType,
162 nb::rv_policy::reference)
163 .def_prop_ro(
"size", &ArrayType::getSize);
165 nb::class_<Constant>(m,
"Constant")
169 nb::class_<AppID>(m,
"AppID")
170 .def(nb::init<std::string, std::optional<uint32_t>>(), nb::arg(
"name"),
171 nb::arg(
"idx") = std::nullopt)
172 .def_prop_ro(
"name", [](
AppID &
id) {
return id.name; })
174 [](
AppID &
id) -> nb::object {
176 return nb::cast(
id.idx);
181 std::string ret =
"<" +
id.name;
183 ret = ret +
"[" + std::to_string(*
id.idx) +
"]";
187 .def(
"__eq__", [](
AppID &a,
AppID &b) {
return a == b; })
188 .def(
"__hash__", [](
AppID &
id) {
190 std::hash<uint32_t>{}(
id.idx.value_or(-1)));
194 nb::class_<ModuleInfo>(m,
"ModuleInfo")
195 .def_prop_ro(
"name", [](
ModuleInfo &info) {
return info.name; })
196 .def_prop_ro(
"summary", [](
ModuleInfo &info) {
return info.summary; })
197 .def_prop_ro(
"version", [](
ModuleInfo &info) {
return info.version; })
198 .def_prop_ro(
"repo", [](
ModuleInfo &info) {
return info.repo; })
199 .def_prop_ro(
"commit_hash",
200 [](
ModuleInfo &info) {
return info.commitHash; })
201 .def_prop_ro(
"constants", [](
ModuleInfo &info) {
return info.constants; })
205 std::stringstream os(ret);
210 nb::enum_<Logger::Level>(m,
"LogLevel")
211 .value(
"Debug", Logger::Level::Debug)
212 .value(
"Info", Logger::Level::Info)
213 .value(
"Warning", Logger::Level::Warning)
214 .value(
"Error", Logger::Level::Error)
216 nb::class_<Logger>(m,
"Logger");
218 nb::class_<services::Service>(m,
"Service")
221 nb::class_<SysInfo, services::Service>(m,
"SysInfo")
225 nb::class_<MMIO::RegionDescriptor>(m,
"MMIORegionDescriptor")
228 nb::class_<services::MMIO, services::Service>(m,
"MMIO")
232 nb::rv_policy::reference);
234 nb::class_<services::HostMem::HostMemRegion>(m,
"HostMemRegion")
237 return reinterpret_cast<uintptr_t
>(mem.
getPtr());
241 nb::class_<services::HostMem::Options>(m,
"HostMemOptions")
246 std::string ret =
"HostMemOptions(";
250 ret +=
"use_large_pages";
255 nb::class_<services::HostMem, services::Service>(m,
"HostMem")
258 nb::rv_policy::take_ownership)
262 return self.
mapMemory(
reinterpret_cast<void *
>(ptr), size, opts);
264 nb::arg(
"ptr"), nb::arg(
"size"),
268 [](
HostMem &self, uintptr_t ptr) {
269 return self.
unmapMemory(
reinterpret_cast<void *
>(ptr));
272 nb::class_<services::TelemetryService, services::Service>(m,
275 nb::class_<std::future<MessageData>>(m,
"MessageDataFuture")
276 .def(
"valid", [](std::future<MessageData> &f) {
return f.valid(); })
278 [](std::future<MessageData> &f) {
281 nb::gil_scoped_release release{};
284 .def(
"get", [](std::future<MessageData> &f) {
285 std::optional<MessageData> data;
289 nb::gil_scoped_release release{};
290 data.emplace(f.get());
292 return nb::bytearray((
const char *)data->getBytes(), data->getSize());
295 nb::class_<ChannelPort::ConnectOptions>(m,
"ConnectOptions")
298 nb::arg(
"buffer_size").none())
299 .def_rw(
"translate_message",
302 nb::class_<ChannelPort>(m,
"ChannelPort")
304 "Connect with specified options")
308 nb::class_<WriteChannelPort, ChannelPort>(m,
"WriteChannelPort")
311 std::vector<uint8_t> dataVec((
const uint8_t *)data.c_str(),
312 (
const uint8_t *)data.c_str() +
317 std::vector<uint8_t> dataVec((
const uint8_t *)data.c_str(),
318 (
const uint8_t *)data.c_str() +
322 nb::class_<ReadChannelPort, ChannelPort>(m,
"ReadChannelPort")
328 return nb::bytearray((
const char *)data.getBytes(), data.getSize());
330 "Read data from the channel. Blocking.")
333 nb::class_<BundlePort>(m,
"BundlePort")
334 .def_prop_ro(
"id", &BundlePort::getID)
335 .def_prop_ro(
"channels", &BundlePort::getChannels,
336 nb::rv_policy::reference)
337 .def(
"getWrite", &BundlePort::getRawWrite, nb::rv_policy::reference)
338 .def(
"getRead", &BundlePort::getRawRead, nb::rv_policy::reference);
340 nb::class_<ServicePort, BundlePort>(m,
"ServicePort");
342 nb::class_<MMIO::MMIORegion, ServicePort>(m,
"MMIORegion")
347 nb::class_<FuncService::Function, ServicePort>(m,
"Function")
350 nb::bytearray msg) -> std::future<MessageData> {
351 std::vector<uint8_t> dataVec((
const uint8_t *)msg.c_str(),
352 (
const uint8_t *)msg.c_str() +
355 return self.call(data);
359 nb::class_<CallService::Callback, ServicePort>(m,
"Callback")
361 std::function<nb::object(nb::object)> pyCallback) {
366 nb::gil_scoped_acquire acquire{};
367 std::vector<uint8_t> arg(req.
getBytes(),
369 nb::bytes argObj((
const char *)arg.data(), arg.size());
370 auto ret = pyCallback(argObj);
373 nb::bytearray retBytes = nb::cast<nb::bytearray>(ret);
374 std::vector<uint8_t> dataVec((
const uint8_t *)retBytes.c_str(),
375 (
const uint8_t *)retBytes.c_str() +
381 nb::class_<TelemetryService::Metric, ServicePort>(m,
"Metric")
389 nb::class_<HWModule>(m,
"HWModule")
393 nb::rv_policy::reference);
396 nb::class_<Instance, HWModule>(m,
"Instance")
399 nb::class_<Accelerator, HWModule>(m,
"Accelerator");
404 nb::rv_policy::reference);
406 auto accConn = nb::class_<AcceleratorConnection>(m,
"AcceleratorConnection");
410 "An ESI context owns everything -- types, accelerator connections, and "
411 "the accelerator facade (aka Accelerator) itself. It MUST NOT be garbage "
412 "collected while the accelerator is still in use. When it is destroyed, "
413 "all accelerator connections are disconnected.")
414 .def(nb::init<>(),
"Create a context with a default logger.")
415 .def(
"connect", &Context::connect, nb::rv_policy::reference)
417 ctxt.
setLogger(std::make_unique<StreamLogger>(level));
426 nb::rv_policy::reference)
432 nb::rv_policy::reference)
434 "get_service_hostmem",
438 nb::rv_policy::reference)
440 nb::rv_policy::reference);
442 nb::class_<Manifest>(m,
"Manifest")
443 .def(nb::init<Context &, std::string>())
444 .def_prop_ro(
"api_version", &Manifest::getApiVersion)
448 auto *acc = m.buildAccelerator(conn);
452 nb::rv_policy::reference)
453 .def_prop_ro(
"type_table",
455 std::vector<nb::object> ret;
456 std::ranges::transform(m.getTypeTable(),
460 .def_prop_ro(
"module_infos", &Manifest::getModuleInfos);