112 nb::class_<Type>(m,
"Type")
113 .def(nb::init<const Type::ID &>(), nb::arg(
"id"))
114 .def_prop_ro(
"id", &Type::getID)
115 .def(
"__repr__", [](
Type &t) {
return "<" + t.
getID() +
">"; });
116 nb::class_<ChannelType, Type>(m,
"ChannelType")
117 .def(nb::init<const Type::ID &, const Type *>(), nb::arg(
"id"),
119 .def_prop_ro(
"inner", &ChannelType::getInner, nb::rv_policy::reference);
120 nb::enum_<BundleType::Direction>(m,
"Direction")
121 .value(
"To", BundleType::Direction::To)
122 .value(
"From", BundleType::Direction::From)
124 nb::class_<BundleType, Type>(m,
"BundleType")
125 .def(nb::init<const Type::ID &, const BundleType::ChannelVector &>(),
126 nb::arg(
"id"), nb::arg(
"channels"))
127 .def_prop_ro(
"channels", &BundleType::getChannels,
128 nb::rv_policy::reference);
129 nb::class_<VoidType, Type>(m,
"VoidType")
130 .def(nb::init<const Type::ID &>(), nb::arg(
"id"));
131 nb::class_<AnyType, Type>(m,
"AnyType")
132 .def(nb::init<const Type::ID &>(), nb::arg(
"id"));
133 nb::class_<BitVectorType, Type>(m,
"BitVectorType")
134 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
137 nb::class_<BitsType, BitVectorType>(m,
"BitsType")
138 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
140 nb::class_<IntegerType, BitVectorType>(m,
"IntegerType")
141 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
143 nb::class_<SIntType, IntegerType>(m,
"SIntType")
144 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
146 nb::class_<UIntType, IntegerType>(m,
"UIntType")
147 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg(
"id"),
149 nb::class_<StructType, Type>(m,
"StructType")
150 .def(nb::init<const Type::ID &, const StructType::FieldVector &, bool>(),
151 nb::arg(
"id"), nb::arg(
"fields"), nb::arg(
"reverse") =
true)
152 .def_prop_ro(
"fields", &StructType::getFields, nb::rv_policy::reference)
153 .def_prop_ro(
"reverse", &StructType::isReverse);
154 nb::class_<ArrayType, Type>(m,
"ArrayType")
155 .def(nb::init<const Type::ID &, const Type *, uint64_t>(), nb::arg(
"id"),
156 nb::arg(
"element_type"), nb::arg(
"size"))
157 .def_prop_ro(
"element", &ArrayType::getElementType,
158 nb::rv_policy::reference)
159 .def_prop_ro(
"size", &ArrayType::getSize);
161 nb::class_<Constant>(m,
"Constant")
165 nb::class_<AppID>(m,
"AppID")
166 .def(nb::init<std::string, std::optional<uint32_t>>(), nb::arg(
"name"),
167 nb::arg(
"idx") = std::nullopt)
168 .def_prop_ro(
"name", [](
AppID &
id) {
return id.name; })
170 [](
AppID &
id) -> nb::object {
172 return nb::cast(
id.idx);
177 std::string ret =
"<" +
id.name;
179 ret = ret +
"[" + std::to_string(*
id.idx) +
"]";
183 .def(
"__eq__", [](
AppID &a,
AppID &b) {
return a == b; })
184 .def(
"__hash__", [](
AppID &
id) {
186 std::hash<uint32_t>{}(
id.idx.value_or(-1)));
190 nb::class_<ModuleInfo>(m,
"ModuleInfo")
191 .def_prop_ro(
"name", [](
ModuleInfo &info) {
return info.name; })
192 .def_prop_ro(
"summary", [](
ModuleInfo &info) {
return info.summary; })
193 .def_prop_ro(
"version", [](
ModuleInfo &info) {
return info.version; })
194 .def_prop_ro(
"repo", [](
ModuleInfo &info) {
return info.repo; })
195 .def_prop_ro(
"commit_hash",
196 [](
ModuleInfo &info) {
return info.commitHash; })
197 .def_prop_ro(
"constants", [](
ModuleInfo &info) {
return info.constants; })
201 std::stringstream os(ret);
206 nb::enum_<Logger::Level>(m,
"LogLevel")
207 .value(
"Debug", Logger::Level::Debug)
208 .value(
"Info", Logger::Level::Info)
209 .value(
"Warning", Logger::Level::Warning)
210 .value(
"Error", Logger::Level::Error)
212 nb::class_<Logger>(m,
"Logger");
214 nb::class_<services::Service>(m,
"Service")
217 nb::class_<SysInfo, services::Service>(m,
"SysInfo")
221 nb::class_<MMIO::RegionDescriptor>(m,
"MMIORegionDescriptor")
224 nb::class_<services::MMIO, services::Service>(m,
"MMIO")
228 nb::rv_policy::reference);
230 nb::class_<services::HostMem::HostMemRegion>(m,
"HostMemRegion")
233 return reinterpret_cast<uintptr_t
>(mem.
getPtr());
237 nb::class_<services::HostMem::Options>(m,
"HostMemOptions")
242 std::string ret =
"HostMemOptions(";
246 ret +=
"use_large_pages";
251 nb::class_<services::HostMem, services::Service>(m,
"HostMem")
254 nb::rv_policy::take_ownership)
258 return self.
mapMemory(
reinterpret_cast<void *
>(ptr), size, opts);
260 nb::arg(
"ptr"), nb::arg(
"size"),
264 [](
HostMem &self, uintptr_t ptr) {
265 return self.
unmapMemory(
reinterpret_cast<void *
>(ptr));
268 nb::class_<services::TelemetryService, services::Service>(m,
271 nb::class_<std::future<MessageData>>(m,
"MessageDataFuture")
272 .def(
"valid", [](std::future<MessageData> &f) {
return f.valid(); })
274 [](std::future<MessageData> &f) {
277 nb::gil_scoped_release release{};
280 .def(
"get", [](std::future<MessageData> &f) {
281 std::optional<MessageData> data;
285 nb::gil_scoped_release release{};
286 data.emplace(f.get());
288 return nb::bytearray((
const char *)data->getBytes(), data->getSize());
291 nb::class_<ChannelPort::ConnectOptions>(m,
"ConnectOptions")
294 nb::arg(
"buffer_size").none())
295 .def_rw(
"translate_message",
298 nb::class_<ChannelPort>(m,
"ChannelPort")
300 "Connect with specified options")
304 nb::class_<WriteChannelPort, ChannelPort>(m,
"WriteChannelPort")
307 std::vector<uint8_t> dataVec((
const uint8_t *)data.c_str(),
308 (
const uint8_t *)data.c_str() +
313 std::vector<uint8_t> dataVec((
const uint8_t *)data.c_str(),
314 (
const uint8_t *)data.c_str() +
318 nb::class_<ReadChannelPort, ChannelPort>(m,
"ReadChannelPort")
324 return nb::bytearray((
const char *)data.getBytes(), data.getSize());
326 "Read data from the channel. Blocking.")
329 nb::class_<BundlePort>(m,
"BundlePort")
330 .def_prop_ro(
"id", &BundlePort::getID)
331 .def_prop_ro(
"channels", &BundlePort::getChannels,
332 nb::rv_policy::reference)
333 .def(
"getWrite", &BundlePort::getRawWrite, nb::rv_policy::reference)
334 .def(
"getRead", &BundlePort::getRawRead, nb::rv_policy::reference);
336 nb::class_<ServicePort, BundlePort>(m,
"ServicePort");
338 nb::class_<MMIO::MMIORegion, ServicePort>(m,
"MMIORegion")
343 nb::class_<FuncService::Function, ServicePort>(m,
"Function")
346 nb::bytearray msg) -> std::future<MessageData> {
347 std::vector<uint8_t> dataVec((
const uint8_t *)msg.c_str(),
348 (
const uint8_t *)msg.c_str() +
351 return self.call(data);
355 nb::class_<CallService::Callback, ServicePort>(m,
"Callback")
357 std::function<nb::object(nb::object)> pyCallback) {
362 nb::gil_scoped_acquire acquire{};
363 std::vector<uint8_t> arg(req.
getBytes(),
365 nb::bytes argObj((
const char *)arg.data(), arg.size());
366 auto ret = pyCallback(argObj);
369 nb::bytes retBytes = nb::cast<nb::bytes>(ret);
370 std::vector<uint8_t> dataVec((
const uint8_t *)retBytes.c_str(),
371 (
const uint8_t *)retBytes.c_str() +
377 nb::class_<TelemetryService::Metric, ServicePort>(m,
"Metric")
385 nb::class_<HWModule>(m,
"HWModule")
389 nb::rv_policy::reference);
392 nb::class_<Instance, HWModule>(m,
"Instance")
395 nb::class_<Accelerator, HWModule>(m,
"Accelerator");
400 nb::rv_policy::reference);
402 auto accConn = nb::class_<AcceleratorConnection>(m,
"AcceleratorConnection");
406 "An ESI context owns everything -- types, accelerator connections, and "
407 "the accelerator facade (aka Accelerator) itself. It MUST NOT be garbage "
408 "collected while the accelerator is still in use. When it is destroyed, "
409 "all accelerator connections are disconnected.")
410 .def(nb::init<>(),
"Create a context with a default logger.")
411 .def(
"connect", &Context::connect, nb::rv_policy::reference)
413 ctxt.
setLogger(std::make_unique<StreamLogger>(level));
422 nb::rv_policy::reference)
428 nb::rv_policy::reference)
430 "get_service_hostmem",
434 nb::rv_policy::reference)
436 nb::rv_policy::reference);
438 nb::class_<Manifest>(m,
"Manifest")
439 .def(nb::init<Context &, std::string>())
440 .def_prop_ro(
"api_version", &Manifest::getApiVersion)
444 auto *acc = m.buildAccelerator(conn);
448 nb::rv_policy::reference)
449 .def_prop_ro(
"type_table",
451 std::vector<nb::object> ret;
452 std::ranges::transform(m.getTypeTable(),
456 .def_prop_ro(
"module_infos", &Manifest::getModuleInfos);