rustnn C and C++ API
WebNN graph construction and execution
Loading...
Searching...
No Matches
rustnn.hpp
Go to the documentation of this file.
1#pragma once
2
10#include <rustnn/rustnn.h>
11
12#include <cstddef>
13#include <cstdint>
14#include <optional>
15#include <stdexcept>
16#include <string>
17#include <type_traits>
18#include <utility>
19#include <vector>
20
21namespace rustnn {
22
25
27class Error : public std::runtime_error {
28public:
29 explicit Error(const std::string &message) : std::runtime_error(message) {}
30};
31
33inline void check(RustnnStatus status) {
34 if (status != RustnnStatus_Success) {
35 const char *message = rustnn_last_error_message();
36 throw Error(message != nullptr ? message : "unknown rustnn error");
37 }
38}
39
53
60
70
77
84
87 bool engine_caching = true;
88 bool runtime_cache = true;
89 bool fail_on_cache_miss = false;
90 bool cuda_graphs = true;
91};
92
97
106
109 std::string label;
110};
111
114public:
115 MLOperandDescriptor(MLOperandDataType data_type, const std::vector<std::uint64_t> &shape) {
117 static_cast<RustnnDataType>(data_type), shape.data(), shape.size(), &value_));
118 }
119
123
125 : value_(std::exchange(other.value_, nullptr)) {}
126
128 if (this != &other) {
130 value_ = std::exchange(other.value_, nullptr);
131 }
132 return *this;
133 }
134
135private:
136 friend class MLGraphBuilder;
137 RustnnOperandDescriptor *value_ = nullptr;
138};
139
142public:
143 MLTensorDescriptor(MLOperandDataType data_type, const std::vector<std::uint64_t> &shape,
144 bool readable, bool writable) {
146 shape.data(), shape.size(), readable,
147 writable, &value_));
148 }
149
153
155 : value_(std::exchange(other.value_, nullptr)) {}
156
158 if (this != &other) {
160 value_ = std::exchange(other.value_, nullptr);
161 }
162 return *this;
163 }
164
165private:
166 friend class MLContext;
167 RustnnTensorDescriptor *value_ = nullptr;
168};
169
172public:
174 MLOperand(const MLOperand &) = delete;
175 MLOperand &operator=(const MLOperand &) = delete;
176
177 MLOperand(MLOperand &&other) noexcept : value_(std::exchange(other.value_, nullptr)) {}
178
179 MLOperand &operator=(MLOperand &&other) noexcept {
180 if (this != &other) {
182 value_ = std::exchange(other.value_, nullptr);
183 }
184 return *this;
185 }
186
187private:
188 friend class MLGraphBuilder;
189 explicit MLOperand(RustnnOperand *value) : value_(value) {}
190 RustnnOperand *value_ = nullptr;
191};
192
194class MLGraph {
195public:
197 MLGraph(const MLGraph &) = delete;
198 MLGraph &operator=(const MLGraph &) = delete;
199
200 MLGraph(MLGraph &&other) noexcept : value_(std::exchange(other.value_, nullptr)) {}
201
202 MLGraph &operator=(MLGraph &&other) noexcept {
203 if (this != &other) {
204 rustnn_graph_destroy(value_);
205 value_ = std::exchange(other.value_, nullptr);
206 }
207 return *this;
208 }
209
210private:
211 friend class MLContext;
212 friend class MLGraphBuilder;
213 explicit MLGraph(RustnnGraph *value) : value_(value) {}
214 RustnnGraph *value_ = nullptr;
215};
216
218class MLTensor {
219public:
221 MLTensor(const MLTensor &) = delete;
222 MLTensor &operator=(const MLTensor &) = delete;
223
224 MLTensor(MLTensor &&other) noexcept : value_(std::exchange(other.value_, nullptr)) {}
225
226 MLTensor &operator=(MLTensor &&other) noexcept {
227 if (this != &other) {
228 rustnn_tensor_destroy(value_);
229 value_ = std::exchange(other.value_, nullptr);
230 }
231 return *this;
232 }
233
234private:
235 friend class MLContext;
236 explicit MLTensor(RustnnTensor *value) : value_(value) {}
237 RustnnTensor *value_ = nullptr;
238};
239
242public:
243 explicit MLContext(const MLContextOptions &options = {}) {
244 const RustnnBackendDevice raw_device_hint{
245 options.device_hint.has_value()
246 ? static_cast<RustnnBackend>(options.device_hint->backend)
248 options.device_hint.has_value()
249 ? static_cast<RustnnDeviceType>(options.device_hint->device_type)
251 options.device_hint.has_value() ? options.device_hint->device_index : 0};
252 const TrtxOptions trtx = options.rustnn_options.has_value()
253 ? options.rustnn_options->trtx
254 : TrtxOptions{};
255 const RustnnOptions raw_rustnn_options{{
256 trtx.engine_caching,
257 trtx.runtime_cache,
259 trtx.cuda_graphs,
260 }};
261 const RustnnContextOptions raw_options{
262 static_cast<RustnnPowerPreference>(options.power_preference),
263 options.accelerated,
264 static_cast<RustnnBackend>(options.backend_hint),
265 options.device_hint.has_value(),
266 raw_device_hint,
267 options.rustnn_options.has_value(),
268 raw_rustnn_options};
269 check(rustnn_context_create(&raw_options, &value_));
270 }
271
273 MLContext(const MLContext &) = delete;
274 MLContext &operator=(const MLContext &) = delete;
275
276 MLContext(MLContext &&other) noexcept : value_(std::exchange(other.value_, nullptr)) {}
277
278 MLContext &operator=(MLContext &&other) noexcept {
279 if (this != &other) {
281 value_ = std::exchange(other.value_, nullptr);
282 }
283 return *this;
284 }
285
288 RustnnTensor *tensor = nullptr;
289 check(rustnn_context_create_tensor(value_, descriptor.value_, &tensor));
290 return MLTensor(tensor);
291 }
292
294 template <typename T>
295 void writeTensor(const MLTensor &tensor, const std::vector<T> &data) {
296 static_assert(std::is_trivially_copyable_v<T>, "tensor elements must be plain data");
297 check(rustnn_context_write_tensor(value_, tensor.value_, data.data(),
298 data.size() * sizeof(T)));
299 }
300
302 template <typename T>
303 std::vector<T> readTensor(const MLTensor &tensor, std::size_t element_count) {
304 static_assert(std::is_trivially_copyable_v<T>, "tensor elements must be plain data");
305 std::vector<T> data(element_count);
306 check(rustnn_context_read_tensor(value_, tensor.value_, data.data(),
307 data.size() * sizeof(T)));
308 return data;
309 }
310
312 void dispatch(MLGraph &graph,
313 const std::vector<std::pair<std::string, const MLTensor *>> &inputs,
314 const std::vector<std::pair<std::string, const MLTensor *>> &outputs) {
315 std::vector<RustnnNamedTensor> named_inputs;
316 named_inputs.reserve(inputs.size());
317 for (const auto &[name, tensor] : inputs) {
318 named_inputs.push_back({name.c_str(), tensor != nullptr ? tensor->value_ : nullptr});
319 }
320 std::vector<RustnnNamedTensor> named_outputs;
321 named_outputs.reserve(outputs.size());
322 for (const auto &[name, tensor] : outputs) {
323 named_outputs.push_back({name.c_str(), tensor != nullptr ? tensor->value_ : nullptr});
324 }
325 check(rustnn_context_dispatch(value_, graph.value_, named_inputs.data(),
326 named_inputs.size(), named_outputs.data(),
327 named_outputs.size()));
328 }
329
330private:
331 friend class MLGraphBuilder;
332 RustnnContext *value_ = nullptr;
333};
334
335#define RUSTNN_ML_UNARY_METHOD(method, function) \
336 MLOperand method(const MLOperand &input) { \
337 RustnnOperand *output = nullptr; \
338 check(function(value_, input.value_, &output)); \
339 return MLOperand(output); \
340 }
341
342#define RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(method, function) \
343 RUSTNN_ML_UNARY_METHOD(method, function) \
344 MLOperand method(const MLOperand &input, \
345 const MLOperatorOptions &options) { \
346 RustnnOperand *output = nullptr; \
347 const RustnnOperatorOptions raw_options{options.label.c_str()}; \
348 check(function##_with_options(value_, input.value_, &raw_options, \
349 &output)); \
350 return MLOperand(output); \
351 }
352
353#define RUSTNN_ML_BINARY_METHOD(method, function) \
354 MLOperand method(const MLOperand &lhs, const MLOperand &rhs) { \
355 RustnnOperand *output = nullptr; \
356 check(function(value_, lhs.value_, rhs.value_, &output)); \
357 return MLOperand(output); \
358 }
359
360#define RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(method, function) \
361 RUSTNN_ML_BINARY_METHOD(method, function) \
362 MLOperand method(const MLOperand &lhs, const MLOperand &rhs, \
363 const MLOperatorOptions &options) { \
364 RustnnOperand *output = nullptr; \
365 const RustnnOperatorOptions raw_options{options.label.c_str()}; \
366 check(function##_with_options(value_, lhs.value_, rhs.value_, \
367 &raw_options, &output)); \
368 return MLOperand(output); \
369 }
370
371#define RUSTNN_ML_TERNARY_METHOD(method, function) \
372 MLOperand method(const MLOperand &first, const MLOperand &second, \
373 const MLOperand &third) { \
374 RustnnOperand *output = nullptr; \
375 check(function(value_, first.value_, second.value_, third.value_, \
376 &output)); \
377 return MLOperand(output); \
378 }
379
386public:
388 explicit MLGraphBuilder(MLContext &context) {
389 check(rustnn_graph_builder_create(context.value_, &value_));
390 }
394
396 : value_(std::exchange(other.value_, nullptr)) {}
397
399 if (this != &other) {
401 value_ = std::exchange(other.value_, nullptr);
402 }
403 return *this;
404 }
405
407 MLOperand input(const std::string &name, const MLOperandDescriptor &descriptor) {
408 RustnnOperand *output = nullptr;
409 check(rustnn_graph_builder_input(value_, name.c_str(), descriptor.value_, &output));
410 return MLOperand(output);
411 }
412
414 template <typename T>
415 MLOperand constant(const MLOperandDescriptor &descriptor, const std::vector<T> &data) {
416 static_assert(std::is_trivially_copyable_v<T>, "tensor elements must be plain data");
417 RustnnOperand *output = nullptr;
418 check(rustnn_graph_builder_constant(value_, descriptor.value_, data.data(),
419 data.size() * sizeof(T), &output));
420 return MLOperand(output);
421 }
422
424 RustnnOperand *output = nullptr;
425 check(rustnn_graph_builder_unary(value_, operation, input.value_, &output));
426 return MLOperand(output);
427 }
428
430 const MLOperatorOptions &options) {
431 RustnnOperand *output = nullptr;
432 const RustnnOperatorOptions raw_options{options.label.c_str()};
434 value_, operation, input.value_, &raw_options, &output));
435 return MLOperand(output);
436 }
437
439 const MLOperand &rhs) {
440 RustnnOperand *output = nullptr;
441 check(rustnn_graph_builder_binary(value_, operation, lhs.value_, rhs.value_, &output));
442 return MLOperand(output);
443 }
444
446 const MLOperand &rhs, const MLOperatorOptions &options) {
447 RustnnOperand *output = nullptr;
448 const RustnnOperatorOptions raw_options{options.label.c_str()};
450 value_, operation, lhs.value_, rhs.value_, &raw_options, &output));
451 return MLOperand(output);
452 }
453
454 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(add, rustnn_graph_builder_add)
455 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(subtract, rustnn_graph_builder_subtract)
456 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(multiply, rustnn_graph_builder_multiply)
457 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(divide, rustnn_graph_builder_divide)
458 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(power, rustnn_graph_builder_power)
459 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(maximum, rustnn_graph_builder_maximum)
460 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(minimum, rustnn_graph_builder_minimum)
461 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(equal, rustnn_graph_builder_equal)
462 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(greater, rustnn_graph_builder_greater)
463 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(greaterOrEqual,
465 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(lesser, rustnn_graph_builder_lesser)
466 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(lesserOrEqual,
468 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(notEqual, rustnn_graph_builder_not_equal)
469 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(logicalAnd,
471 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(logicalOr,
473 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(logicalXor,
475 RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS(matmul, rustnn_graph_builder_matmul)
476
477 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(abs, rustnn_graph_builder_abs)
478 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(roundEven, rustnn_graph_builder_round_even)
479 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(ceil, rustnn_graph_builder_ceil)
480 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(cos, rustnn_graph_builder_cos)
481 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(exp, rustnn_graph_builder_exp)
482 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(floor, rustnn_graph_builder_floor)
483 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(gelu, rustnn_graph_builder_gelu)
484 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(log, rustnn_graph_builder_log)
485 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(negate, rustnn_graph_builder_negate)
486 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(relu, rustnn_graph_builder_relu)
487 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(sigmoid, rustnn_graph_builder_sigmoid)
488 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(sin, rustnn_graph_builder_sin)
489 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(sqrt, rustnn_graph_builder_sqrt)
490 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(tan, rustnn_graph_builder_tan)
491 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(tanh, rustnn_graph_builder_tanh)
492 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(erf, rustnn_graph_builder_erf)
493 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(reciprocal,
495 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(sign, rustnn_graph_builder_sign)
496 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(logicalNot,
498 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(identity, rustnn_graph_builder_identity)
499 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(softplus, rustnn_graph_builder_softplus)
500 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(softsign, rustnn_graph_builder_softsign)
501 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(isNaN, rustnn_graph_builder_is_nan)
502 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(isInfinite,
504 RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS(shape, rustnn_graph_builder_shape)
505
506 RUSTNN_ML_UNARY_METHOD(elu, rustnn_graph_builder_elu)
507 RUSTNN_ML_UNARY_METHOD(hardSigmoid, rustnn_graph_builder_hard_sigmoid)
508 RUSTNN_ML_UNARY_METHOD(hardSwish, rustnn_graph_builder_hard_swish)
509 RUSTNN_ML_UNARY_METHOD(leakyRelu, rustnn_graph_builder_leaky_relu)
510 RUSTNN_ML_UNARY_METHOD(linear, rustnn_graph_builder_linear)
511 RUSTNN_ML_UNARY_METHOD(clamp, rustnn_graph_builder_clamp)
512 RUSTNN_ML_UNARY_METHOD(instanceNormalization,
514 RUSTNN_ML_UNARY_METHOD(layerNormalization,
516 RUSTNN_ML_UNARY_METHOD(resample2d, rustnn_graph_builder_resample2d)
517 RUSTNN_ML_UNARY_METHOD(reverse, rustnn_graph_builder_reverse)
518 RUSTNN_ML_UNARY_METHOD(triangular, rustnn_graph_builder_triangular)
519 RUSTNN_ML_UNARY_METHOD(averagePool2d, rustnn_graph_builder_average_pool2d)
520 RUSTNN_ML_UNARY_METHOD(maxPool2d, rustnn_graph_builder_max_pool2d)
521 RUSTNN_ML_UNARY_METHOD(l2Pool2d, rustnn_graph_builder_l2_pool2d)
522 RUSTNN_ML_UNARY_METHOD(reduceSum, rustnn_graph_builder_reduce_sum)
523 RUSTNN_ML_UNARY_METHOD(reduceMean, rustnn_graph_builder_reduce_mean)
524 RUSTNN_ML_UNARY_METHOD(reduceMax, rustnn_graph_builder_reduce_max)
525 RUSTNN_ML_UNARY_METHOD(reduceMin, rustnn_graph_builder_reduce_min)
526 RUSTNN_ML_UNARY_METHOD(reduceProduct, rustnn_graph_builder_reduce_product)
527 RUSTNN_ML_UNARY_METHOD(reduceL1, rustnn_graph_builder_reduce_l1)
528 RUSTNN_ML_UNARY_METHOD(reduceL2, rustnn_graph_builder_reduce_l2)
529 RUSTNN_ML_UNARY_METHOD(reduceLogSum, rustnn_graph_builder_reduce_log_sum)
530 RUSTNN_ML_UNARY_METHOD(reduceLogSumExp,
532 RUSTNN_ML_UNARY_METHOD(reduceSumSquare,
534 RUSTNN_ML_UNARY_METHOD(transpose, rustnn_graph_builder_transpose)
535 RUSTNN_ML_UNARY_METHOD(squeeze, rustnn_graph_builder_squeeze)
536 RUSTNN_ML_UNARY_METHOD(unsqueeze, rustnn_graph_builder_unsqueeze)
537
538 RUSTNN_ML_BINARY_METHOD(gemm, rustnn_graph_builder_gemm)
539 RUSTNN_ML_BINARY_METHOD(conv2d, rustnn_graph_builder_conv2d)
540 RUSTNN_ML_BINARY_METHOD(convTranspose2d,
542 RUSTNN_ML_BINARY_METHOD(gather, rustnn_graph_builder_gather)
543 RUSTNN_ML_BINARY_METHOD(gatherElements,
545 RUSTNN_ML_BINARY_METHOD(gatherND, rustnn_graph_builder_gather_nd)
546 RUSTNN_ML_BINARY_METHOD(prelu, rustnn_graph_builder_prelu)
547
548 RUSTNN_ML_TERNARY_METHOD(batchNormalization,
550 RUSTNN_ML_TERNARY_METHOD(where, rustnn_graph_builder_where)
551 RUSTNN_ML_TERNARY_METHOD(scatterElements,
553 RUSTNN_ML_TERNARY_METHOD(scatterND, rustnn_graph_builder_scatter_nd)
554
555 MLOperand argMin(const MLOperand &input, std::uint32_t axis) {
556 RustnnOperand *output = nullptr;
557 check(rustnn_graph_builder_arg_min(value_, input.value_, axis, &output));
558 return MLOperand(output);
559 }
560
561 MLOperand argMax(const MLOperand &input, std::uint32_t axis) {
562 RustnnOperand *output = nullptr;
563 check(rustnn_graph_builder_arg_max(value_, input.value_, axis, &output));
564 return MLOperand(output);
565 }
566
567 MLOperand cast(const MLOperand &input, MLOperandDataType data_type) {
568 RustnnOperand *output = nullptr;
569 check(rustnn_graph_builder_cast(value_, input.value_,
570 static_cast<RustnnDataType>(data_type), &output));
571 return MLOperand(output);
572 }
573
574 MLOperand cumulativeSum(const MLOperand &input, std::uint32_t axis) {
575 RustnnOperand *output = nullptr;
576 check(rustnn_graph_builder_cumulative_sum(value_, input.value_, axis, &output));
577 return MLOperand(output);
578 }
579
580 MLOperand softmax(const MLOperand &input, std::uint32_t axis) {
581 RustnnOperand *output = nullptr;
582 check(rustnn_graph_builder_softmax(value_, input.value_, axis, &output));
583 return MLOperand(output);
584 }
585
587 const std::vector<std::uint32_t> &new_shape) {
588 RustnnOperand *output = nullptr;
589 check(rustnn_graph_builder_expand(value_, input.value_, new_shape.data(),
590 new_shape.size(), &output));
591 return MLOperand(output);
592 }
593
595 const std::vector<std::uint32_t> &new_shape) {
596 RustnnOperand *output = nullptr;
597 check(rustnn_graph_builder_reshape(value_, input.value_, new_shape.data(),
598 new_shape.size(), &output));
599 return MLOperand(output);
600 }
601
603 const std::vector<std::uint32_t> &repetitions) {
604 RustnnOperand *output = nullptr;
605 check(rustnn_graph_builder_tile(value_, input.value_, repetitions.data(),
606 repetitions.size(), &output));
607 return MLOperand(output);
608 }
609
610 MLOperand pad(const MLOperand &input,
611 const std::vector<std::uint32_t> &beginning_padding,
612 const std::vector<std::uint32_t> &ending_padding) {
613 RustnnOperand *output = nullptr;
615 value_, input.value_, beginning_padding.data(), beginning_padding.size(),
616 ending_padding.data(), ending_padding.size(), &output));
617 return MLOperand(output);
618 }
619
621 const std::vector<std::uint32_t> &starts,
622 const std::vector<std::uint32_t> &sizes) {
623 RustnnOperand *output = nullptr;
624 check(rustnn_graph_builder_slice(value_, input.value_, starts.data(),
625 starts.size(), sizes.data(), sizes.size(),
626 &output));
627 return MLOperand(output);
628 }
629
630 MLOperand concat(const std::vector<const MLOperand *> &inputs,
631 std::uint32_t axis) {
632 std::vector<const RustnnOperand *> raw_inputs;
633 raw_inputs.reserve(inputs.size());
634 for (const MLOperand *input : inputs) {
635 raw_inputs.push_back(input != nullptr ? input->value_ : nullptr);
636 }
637 RustnnOperand *output = nullptr;
638 check(rustnn_graph_builder_concat(value_, raw_inputs.data(), raw_inputs.size(),
639 axis, &output));
640 return MLOperand(output);
641 }
642
643 MLOperand quantizeLinear(const MLOperand &input, const MLOperand &scale,
644 const MLOperand *zero_point = nullptr) {
645 RustnnOperand *output = nullptr;
647 value_, input.value_, scale.value_,
648 zero_point != nullptr ? zero_point->value_ : nullptr, &output));
649 return MLOperand(output);
650 }
651
652 MLOperand dequantizeLinear(const MLOperand &input, const MLOperand &scale,
653 const MLOperand *zero_point = nullptr) {
654 RustnnOperand *output = nullptr;
656 value_, input.value_, scale.value_,
657 zero_point != nullptr ? zero_point->value_ : nullptr, &output));
658 return MLOperand(output);
659 }
660
661 std::vector<MLOperand> split(const MLOperand &input,
662 const std::vector<std::uint32_t> &splits) {
663 std::vector<RustnnOperand *> raw_outputs(splits.size());
664 std::size_t outputs_len = 0;
665 check(rustnn_graph_builder_split(value_, input.value_, splits.data(), splits.size(),
666 raw_outputs.data(), raw_outputs.size(),
667 &outputs_len));
668 return wrapOperands(raw_outputs, outputs_len);
669 }
670
671 std::vector<MLOperand> split(const MLOperand &input,
672 std::uint32_t num_splits) {
673 std::vector<RustnnOperand *> raw_outputs(num_splits);
674 std::size_t outputs_len = 0;
676 value_, input.value_, num_splits, raw_outputs.data(), raw_outputs.size(),
677 &outputs_len));
678 return wrapOperands(raw_outputs, outputs_len);
679 }
680
681 std::vector<MLOperand> gru(const MLOperand &input, const MLOperand &weight,
682 const MLOperand &recurrent_weight,
683 std::uint32_t steps, std::uint32_t hidden_size) {
684 std::vector<RustnnOperand *> raw_outputs(1);
685 std::size_t outputs_len = 0;
687 value_, input.value_, weight.value_, recurrent_weight.value_, steps,
688 hidden_size, raw_outputs.data(), raw_outputs.size(), &outputs_len));
689 return wrapOperands(raw_outputs, outputs_len);
690 }
691
692 MLOperand gruCell(const MLOperand &input, const MLOperand &weight,
693 const MLOperand &recurrent_weight,
694 const MLOperand &hidden_state, std::uint32_t hidden_size) {
695 RustnnOperand *output = nullptr;
697 value_, input.value_, weight.value_, recurrent_weight.value_,
698 hidden_state.value_, hidden_size, &output));
699 return MLOperand(output);
700 }
701
702 std::vector<MLOperand> lstm(const MLOperand &input, const MLOperand &weight,
703 const MLOperand &recurrent_weight,
704 std::uint32_t steps, std::uint32_t hidden_size) {
705 std::vector<RustnnOperand *> raw_outputs(2);
706 std::size_t outputs_len = 0;
708 value_, input.value_, weight.value_, recurrent_weight.value_, steps,
709 hidden_size, raw_outputs.data(), raw_outputs.size(), &outputs_len));
710 return wrapOperands(raw_outputs, outputs_len);
711 }
712
713 std::vector<MLOperand>
714 lstmCell(const MLOperand &input, const MLOperand &weight,
715 const MLOperand &recurrent_weight, const MLOperand &hidden_state,
716 const MLOperand &cell_state, std::uint32_t hidden_size) {
717 std::vector<RustnnOperand *> raw_outputs(2);
718 std::size_t outputs_len = 0;
720 value_, input.value_, weight.value_, recurrent_weight.value_,
721 hidden_state.value_, cell_state.value_, hidden_size, raw_outputs.data(),
722 raw_outputs.size(), &outputs_len));
723 return wrapOperands(raw_outputs, outputs_len);
724 }
725
727 std::vector<std::uint64_t> operandShape(const MLOperand &operand) {
728 std::size_t rank = 0;
729 check(rustnn_graph_builder_operand_shape(value_, operand.value_, nullptr, 0, &rank));
730 std::vector<std::uint64_t> dimensions(rank);
731 check(rustnn_graph_builder_operand_shape(value_, operand.value_, dimensions.data(),
732 dimensions.size(), &rank));
733 return dimensions;
734 }
735
738 check(rustnn_graph_builder_operand_data_type(value_, operand.value_, &data_type));
739 return static_cast<MLOperandDataType>(data_type);
740 }
741
743 std::string webnnText(
744 const std::vector<std::pair<std::string, const MLOperand *>> &outputs) const {
745 std::vector<RustnnNamedOperand> named;
746 named.reserve(outputs.size());
747 for (const auto &[name, operand] : outputs) {
748 named.push_back({name.c_str(), operand != nullptr ? operand->value_ : nullptr});
749 }
750 char *text = nullptr;
751 check(rustnn_graph_builder_webnn_text(value_, named.data(), named.size(), &text));
752 std::string result(text);
754 return result;
755 }
756
758 MLGraph build(const std::vector<std::pair<std::string, const MLOperand *>> &outputs) {
759 std::vector<RustnnNamedOperand> named;
760 named.reserve(outputs.size());
761 for (const auto &[name, operand] : outputs) {
762 named.push_back({name.c_str(), operand != nullptr ? operand->value_ : nullptr});
763 }
764 RustnnGraph *graph = nullptr;
765 RustnnStatus status =
766 rustnn_graph_builder_build(&value_, named.data(), named.size(), &graph);
767 check(status);
768 return MLGraph(graph);
769 }
770
771private:
772 static std::vector<MLOperand>
773 wrapOperands(const std::vector<RustnnOperand *> &raw_operands,
774 std::size_t length) {
775 std::vector<MLOperand> operands;
776 operands.reserve(length);
777 for (std::size_t index = 0; index < length; ++index) {
778 operands.push_back(MLOperand(raw_operands[index]));
779 }
780 return operands;
781 }
782
783 RustnnGraphBuilder *value_ = nullptr;
784};
785
786#undef RUSTNN_ML_UNARY_METHOD
787#undef RUSTNN_ML_UNARY_METHOD_WITH_OPTIONS
788#undef RUSTNN_ML_BINARY_METHOD
789#undef RUSTNN_ML_BINARY_METHOD_WITH_OPTIONS
790#undef RUSTNN_ML_TERNARY_METHOD
791
792} // namespace rustnn
Exception containing the C API's failure message.
Definition rustnn.hpp:27
Error(const std::string &message)
Definition rustnn.hpp:29
Owns a backend context for graph compilation, tensors and dispatch.
Definition rustnn.hpp:241
MLContext & operator=(const MLContext &)=delete
MLContext & operator=(MLContext &&other) noexcept
Definition rustnn.hpp:278
MLContext(MLContext &&other) noexcept
Definition rustnn.hpp:276
std::vector< T > readTensor(const MLTensor &tensor, std::size_t element_count)
Copy a readable tensor into element_count values; T must match its storage.
Definition rustnn.hpp:303
void writeTensor(const MLTensor &tensor, const std::vector< T > &data)
Copy raw bytes from data into a writable tensor; T must match its storage.
Definition rustnn.hpp:295
MLTensor createTensor(const MLTensorDescriptor &descriptor)
Allocate a backend tensor with the descriptor's host access flags.
Definition rustnn.hpp:287
void dispatch(MLGraph &graph, const std::vector< std::pair< std::string, const MLTensor * > > &inputs, const std::vector< std::pair< std::string, const MLTensor * > > &outputs)
Execute a compiled graph with tensors bound by input and output name.
Definition rustnn.hpp:312
MLContext(const MLContext &)=delete
MLContext(const MLContextOptions &options={})
Definition rustnn.hpp:243
Records and validates graph operations.
Definition rustnn.hpp:385
MLOperand gruCell(const MLOperand &input, const MLOperand &weight, const MLOperand &recurrent_weight, const MLOperand &hidden_state, std::uint32_t hidden_size)
Definition rustnn.hpp:692
std::vector< MLOperand > split(const MLOperand &input, const std::vector< std::uint32_t > &splits)
Definition rustnn.hpp:661
MLOperand binary(RustnnBinaryOperation operation, const MLOperand &lhs, const MLOperand &rhs, const MLOperatorOptions &options)
Definition rustnn.hpp:445
std::vector< MLOperand > lstmCell(const MLOperand &input, const MLOperand &weight, const MLOperand &recurrent_weight, const MLOperand &hidden_state, const MLOperand &cell_state, std::uint32_t hidden_size)
Definition rustnn.hpp:714
MLOperand reshape(const MLOperand &input, const std::vector< std::uint32_t > &new_shape)
Definition rustnn.hpp:594
MLGraphBuilder & operator=(MLGraphBuilder &&other) noexcept
Definition rustnn.hpp:398
MLOperand unary(RustnnUnaryOperation operation, const MLOperand &input)
Definition rustnn.hpp:423
MLOperand argMax(const MLOperand &input, std::uint32_t axis)
Definition rustnn.hpp:561
MLOperandDataType dataType(const MLOperand &operand)
Definition rustnn.hpp:736
std::string webnnText(const std::vector< std::pair< std::string, const MLOperand * > > &outputs) const
Serialize the named outputs as rustnn WebNN graph text.
Definition rustnn.hpp:743
MLGraphBuilder(MLGraphBuilder &&other) noexcept
Definition rustnn.hpp:395
MLOperand dequantizeLinear(const MLOperand &input, const MLOperand &scale, const MLOperand *zero_point=nullptr)
Definition rustnn.hpp:652
MLOperand cast(const MLOperand &input, MLOperandDataType data_type)
Definition rustnn.hpp:567
MLOperand input(const std::string &name, const MLOperandDescriptor &descriptor)
Declare a named graph input with the supplied type and shape.
Definition rustnn.hpp:407
MLOperand pad(const MLOperand &input, const std::vector< std::uint32_t > &beginning_padding, const std::vector< std::uint32_t > &ending_padding)
Definition rustnn.hpp:610
MLOperand concat(const std::vector< const MLOperand * > &inputs, std::uint32_t axis)
Definition rustnn.hpp:630
std::vector< MLOperand > gru(const MLOperand &input, const MLOperand &weight, const MLOperand &recurrent_weight, std::uint32_t steps, std::uint32_t hidden_size)
Definition rustnn.hpp:681
MLGraphBuilder(const MLGraphBuilder &)=delete
MLOperand cumulativeSum(const MLOperand &input, std::uint32_t axis)
Definition rustnn.hpp:574
MLOperand slice(const MLOperand &input, const std::vector< std::uint32_t > &starts, const std::vector< std::uint32_t > &sizes)
Definition rustnn.hpp:620
MLOperand quantizeLinear(const MLOperand &input, const MLOperand &scale, const MLOperand *zero_point=nullptr)
Definition rustnn.hpp:643
MLOperand unary(RustnnUnaryOperation operation, const MLOperand &input, const MLOperatorOptions &options)
Definition rustnn.hpp:429
MLGraphBuilder & operator=(const MLGraphBuilder &)=delete
MLOperand softmax(const MLOperand &input, std::uint32_t axis)
Definition rustnn.hpp:580
MLOperand constant(const MLOperandDescriptor &descriptor, const std::vector< T > &data)
Copy constant data into the graph; T must match the descriptor's storage.
Definition rustnn.hpp:415
MLOperand tile(const MLOperand &input, const std::vector< std::uint32_t > &repetitions)
Definition rustnn.hpp:602
std::vector< std::uint64_t > operandShape(const MLOperand &operand)
Return an operand's inferred dimensions.
Definition rustnn.hpp:727
MLOperand binary(RustnnBinaryOperation operation, const MLOperand &lhs, const MLOperand &rhs)
Definition rustnn.hpp:438
MLGraphBuilder(MLContext &context)
Definition rustnn.hpp:388
std::vector< MLOperand > split(const MLOperand &input, std::uint32_t num_splits)
Definition rustnn.hpp:671
MLGraph build(const std::vector< std::pair< std::string, const MLOperand * > > &outputs)
Compile named outputs and consume this builder; the context must outlive the graph.
Definition rustnn.hpp:758
MLOperand expand(const MLOperand &input, const std::vector< std::uint32_t > &new_shape)
Definition rustnn.hpp:586
std::vector< MLOperand > lstm(const MLOperand &input, const MLOperand &weight, const MLOperand &recurrent_weight, std::uint32_t steps, std::uint32_t hidden_size)
Definition rustnn.hpp:702
Owns a compiled graph. Its originating context must outlive it.
Definition rustnn.hpp:194
MLGraph(const MLGraph &)=delete
MLGraph & operator=(const MLGraph &)=delete
MLGraph & operator=(MLGraph &&other) noexcept
Definition rustnn.hpp:202
MLGraph(MLGraph &&other) noexcept
Definition rustnn.hpp:200
Owns an operand's element type and shape descriptor.
Definition rustnn.hpp:113
MLOperandDescriptor(const MLOperandDescriptor &)=delete
MLOperandDescriptor & operator=(MLOperandDescriptor &&other) noexcept
Definition rustnn.hpp:127
MLOperandDescriptor(MLOperandDescriptor &&other) noexcept
Definition rustnn.hpp:124
MLOperandDescriptor & operator=(const MLOperandDescriptor &)=delete
MLOperandDescriptor(MLOperandDataType data_type, const std::vector< std::uint64_t > &shape)
Definition rustnn.hpp:115
Owns an operand handle belonging to a graph builder.
Definition rustnn.hpp:171
MLOperand & operator=(const MLOperand &)=delete
MLOperand & operator=(MLOperand &&other) noexcept
Definition rustnn.hpp:179
MLOperand(MLOperand &&other) noexcept
Definition rustnn.hpp:177
MLOperand(const MLOperand &)=delete
Owns a tensor descriptor with host read and write permissions.
Definition rustnn.hpp:141
MLTensorDescriptor(const MLTensorDescriptor &)=delete
MLTensorDescriptor(MLOperandDataType data_type, const std::vector< std::uint64_t > &shape, bool readable, bool writable)
Definition rustnn.hpp:143
MLTensorDescriptor & operator=(const MLTensorDescriptor &)=delete
MLTensorDescriptor & operator=(MLTensorDescriptor &&other) noexcept
Definition rustnn.hpp:157
MLTensorDescriptor(MLTensorDescriptor &&other) noexcept
Definition rustnn.hpp:154
Owns a backend tensor. Its originating context must outlive it.
Definition rustnn.hpp:218
MLTensor(MLTensor &&other) noexcept
Definition rustnn.hpp:224
MLTensor(const MLTensor &)=delete
MLTensor & operator=(const MLTensor &)=delete
MLTensor & operator=(MLTensor &&other) noexcept
Definition rustnn.hpp:226
void check(RustnnStatus status)
Throw Error if a C API call returned a failing status.
Definition rustnn.hpp:33
MLOperandDataType
Element type, including packed signed and unsigned 4-bit integers.
Definition rustnn.hpp:41
void initializeLogger()
Initialize rustnn's logger using the environment configuration.
Definition rustnn.hpp:24
MLPowerPreference
Power preference used for automatic backend selection.
Definition rustnn.hpp:55
Backend
Backend hint; Automatic delegates selection to rustnn.
Definition rustnn.hpp:62
DeviceType
Kind of device used to execute a graph.
Definition rustnn.hpp:72
enum RustnnStatus rustnn_graph_builder_erf(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the erf operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_gelu(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the gelu operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_elu(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the elu operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_round_even(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the round even operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_lesser_or_equal(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the lesser or equal operation using [MLGraphBuilder].
enum RustnnStatus rustnn_context_dispatch(struct RustnnContext *context, struct RustnnGraph *graph, const struct RustnnNamedTensor *inputs, uintptr_t inputs_len, const struct RustnnNamedTensor *outputs, uintptr_t outputs_len)
Run an [MLGraph] through [MLContext] with named input and output tensors.
void rustnn_operand_destroy(struct RustnnOperand *operand)
Release an [MLOperand] handle.
enum RustnnStatus rustnn_graph_builder_constant(struct RustnnGraphBuilder *builder, const struct RustnnOperandDescriptor *descriptor, const void *data, uintptr_t data_len, struct RustnnOperand **output)
Create a constant [MLOperand] from descriptor and bytes on [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_is_infinite(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the is infinite operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_concat(struct RustnnGraphBuilder *builder, const struct RustnnOperand *const *inputs, uintptr_t inputs_len, uint32_t axis, struct RustnnOperand **output)
Apply the concat operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_add(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the add operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_sqrt(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the sqrt operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_max_pool2d(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the max pool2d operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_operand_data_type(struct RustnnGraphBuilder *builder, const struct RustnnOperand *operand, enum RustnnDataType *data_type)
Read an [MLOperand] data type from [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_reduce_l1(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the reduce l1 operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_softsign(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the softsign operation using [MLGraphBuilder].
const char * rustnn_last_error_message(void)
Return the last C API error message for this thread.
enum RustnnStatus rustnn_graph_builder_matmul(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the matmul operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_binary(struct RustnnGraphBuilder *builder, enum RustnnBinaryOperation operation, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply a selected binary operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_log(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the log operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_sin(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the sin operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_layer_normalization(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the layer normalization operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_negate(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the negate operation using [MLGraphBuilder].
void rustnn_graph_destroy(struct RustnnGraph *graph)
Release an [MLGraph] handle.
void rustnn_operand_descriptor_destroy(struct RustnnOperandDescriptor *descriptor)
Release an [MLOperandDescriptor] handle.
enum RustnnStatus rustnn_graph_builder_floor(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the floor operation using [MLGraphBuilder].
struct RustnnOperandDescriptor RustnnOperandDescriptor
Opaque handle wrapping [MLOperandDescriptor].
Definition rustnn.h:256
enum RustnnStatus rustnn_graph_builder_gru_cell(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, const struct RustnnOperand *weight, const struct RustnnOperand *recurrent_weight, const struct RustnnOperand *hidden_state, uint32_t hidden_size, struct RustnnOperand **output)
Apply the gru cell operation using [MLGraphBuilder].
RustnnBinaryOperation
Implementation detail of the C++ API.
Definition rustnn.h:186
RustnnDataType
C representation of [MLOperandDataType].
Definition rustnn.h:43
@ RustnnDataType_Uint4
Definition rustnn.h:53
@ RustnnDataType_Uint32
Definition rustnn.h:47
@ RustnnDataType_Float32
Definition rustnn.h:44
@ RustnnDataType_Int4
Definition rustnn.h:52
@ RustnnDataType_Int8
Definition rustnn.h:50
@ RustnnDataType_Uint8
Definition rustnn.h:51
@ RustnnDataType_Uint64
Definition rustnn.h:49
@ RustnnDataType_Int64
Definition rustnn.h:48
@ RustnnDataType_Float16
Definition rustnn.h:45
@ RustnnDataType_Int32
Definition rustnn.h:46
enum RustnnStatus rustnn_graph_builder_batch_normalization(struct RustnnGraphBuilder *builder, const struct RustnnOperand *first, const struct RustnnOperand *second, const struct RustnnOperand *third, struct RustnnOperand **output)
Apply the batch normalization operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_shape(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the shape operation using [MLGraphBuilder].
struct RustnnOperand RustnnOperand
Opaque handle wrapping [MLOperand].
Definition rustnn.h:249
struct RustnnGraphBuilder RustnnGraphBuilder
Opaque handle wrapping [MLGraphBuilder].
Definition rustnn.h:242
enum RustnnStatus rustnn_graph_builder_unary(struct RustnnGraphBuilder *builder, enum RustnnUnaryOperation operation, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply a selected unary operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_is_nan(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the is nan operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_power(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the power operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_minimum(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the minimum operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_sign(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the sign operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_webnn_text(const struct RustnnGraphBuilder *builder, const struct RustnnNamedOperand *outputs, uintptr_t outputs_len, char **text)
Serialize [MLGraphBuilder] output operands as rustnn WebNN text.
RustnnBackend
Backend hint for [MLContextOptions], wrapping [Backend].
Definition rustnn.h:80
@ RustnnBackend_Automatic
Select a backend automatically.
Definition rustnn.h:84
@ RustnnBackend_Litert
Use LiteRT.
Definition rustnn.h:100
@ RustnnBackend_Cann
Use CANN.
Definition rustnn.h:104
@ RustnnBackend_Onnx
Use ONNX Runtime.
Definition rustnn.h:88
@ RustnnBackend_Coreml
Use CoreML.
Definition rustnn.h:96
@ RustnnBackend_Trtx
Use TensorRT-RTX.
Definition rustnn.h:92
enum RustnnStatus rustnn_graph_builder_ceil(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the ceil operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_cos(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the cos operation using [MLGraphBuilder].
struct RustnnTensor RustnnTensor
Opaque handle wrapping [MLTensor].
Definition rustnn.h:263
enum RustnnStatus rustnn_graph_builder_squeeze(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the squeeze operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_conv_transpose2d(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the conv transpose2d operation using [MLGraphBuilder].
enum RustnnStatus rustnn_operand_descriptor_create(enum RustnnDataType data_type, const uint64_t *shape, uintptr_t shape_len, struct RustnnOperandDescriptor **output)
Allocate an [MLOperandDescriptor] from a data type and shape.
enum RustnnStatus rustnn_graph_builder_softmax(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, uint32_t axis, struct RustnnOperand **output)
Apply the softmax operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_build(struct RustnnGraphBuilder **builder, const struct RustnnNamedOperand *outputs, uintptr_t outputs_len, struct RustnnGraph **graph)
Compile a runtime-backed [MLGraphBuilder] into an [MLGraph] and consume it, setting builder to null.
enum RustnnStatus rustnn_graph_builder_clamp(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the clamp operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_split_equal(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, uint32_t num_splits, struct RustnnOperand **outputs, uintptr_t outputs_capacity, uintptr_t *outputs_len)
Apply the split equal operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_multiply(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the multiply operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_hard_sigmoid(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the hard sigmoid operation using [MLGraphBuilder].
struct RustnnContext RustnnContext
Opaque handle wrapping [MLContext].
Definition rustnn.h:228
enum RustnnStatus rustnn_graph_builder_subtract(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the subtract operation using [MLGraphBuilder].
void rustnn_tensor_destroy(struct RustnnTensor *tensor)
Release an [MLTensor] handle.
enum RustnnStatus rustnn_graph_builder_expand(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, const uint32_t *new_shape, uintptr_t new_shape_len, struct RustnnOperand **output)
Apply the expand operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_conv2d(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the conv2d operation using [MLGraphBuilder].
RustnnDeviceType
C representation of [DeviceType] for a [BackendDevice] hint.
Definition rustnn.h:110
@ RustnnDeviceType_Npu
NPU device.
Definition rustnn.h:122
@ RustnnDeviceType_Gpu
GPU device.
Definition rustnn.h:118
@ RustnnDeviceType_Cpu
CPU device.
Definition rustnn.h:114
enum RustnnStatus rustnn_graph_builder_sigmoid(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the sigmoid operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_transpose(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the transpose operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_softplus(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the softplus operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_reduce_mean(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the reduce mean operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_gather_elements(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the gather elements operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_quantize_linear(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, const struct RustnnOperand *scale, const struct RustnnOperand *zero_point, struct RustnnOperand **output)
Apply the quantize linear operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_leaky_relu(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the leaky relu operation using [MLGraphBuilder].
enum RustnnStatus rustnn_context_create(const struct RustnnContextOptions *options, struct RustnnContext **output)
Create an [MLContext].
enum RustnnStatus rustnn_graph_builder_reduce_max(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the reduce max operation using [MLGraphBuilder].
struct RustnnTensorDescriptor RustnnTensorDescriptor
Opaque handle wrapping [MLTensorDescriptor].
Definition rustnn.h:270
enum RustnnStatus rustnn_graph_builder_reshape(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, const uint32_t *new_shape, uintptr_t new_shape_len, struct RustnnOperand **output)
Apply the reshape operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_reduce_min(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the reduce min operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_slice(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, const uint32_t *starts, uintptr_t starts_len, const uint32_t *sizes, uintptr_t sizes_len, struct RustnnOperand **output)
Apply the slice operation using [MLGraphBuilder].
void rustnn_tensor_descriptor_destroy(struct RustnnTensorDescriptor *descriptor)
Release an [MLTensorDescriptor] handle.
enum RustnnStatus rustnn_graph_builder_scatter_elements(struct RustnnGraphBuilder *builder, const struct RustnnOperand *first, const struct RustnnOperand *second, const struct RustnnOperand *third, struct RustnnOperand **output)
Apply the scatter elements operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_unary_with_options(struct RustnnGraphBuilder *builder, enum RustnnUnaryOperation operation, const struct RustnnOperand *input, const struct RustnnOperatorOptions *options, struct RustnnOperand **output)
Implementation detail of the C++ API.
enum RustnnStatus rustnn_graph_builder_reduce_log_sum(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the reduce log sum operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_divide(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the divide operation using [MLGraphBuilder].
enum RustnnStatus rustnn_tensor_descriptor_create(enum RustnnDataType data_type, const uint64_t *shape, uintptr_t shape_len, bool readable, bool writable, struct RustnnTensorDescriptor **output)
Allocate an [MLTensorDescriptor].
enum RustnnStatus rustnn_graph_builder_input(struct RustnnGraphBuilder *builder, const char *name, const struct RustnnOperandDescriptor *descriptor, struct RustnnOperand **output)
Declare a named input [MLOperand] on [MLGraphBuilder].
RustnnUnaryOperation
Implementation detail of the C++ API.
Definition rustnn.h:128
enum RustnnStatus rustnn_graph_builder_reciprocal(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the reciprocal operation using [MLGraphBuilder].
void rustnn_string_destroy(char *value)
Release a string returned by this C API.
enum RustnnStatus rustnn_graph_builder_relu(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the relu operation using [MLGraphBuilder].
enum RustnnStatus rustnn_context_create_tensor(struct RustnnContext *context, const struct RustnnTensorDescriptor *descriptor, struct RustnnTensor **output)
Allocate an [MLTensor] from an [MLTensorDescriptor].
struct RustnnGraph RustnnGraph
Opaque handle wrapping [MLGraph].
Definition rustnn.h:235
enum RustnnStatus rustnn_graph_builder_reduce_sum(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the reduce sum operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_create_uncompiled(struct RustnnGraphBuilder **output)
Create an uncompiled [MLGraphBuilder] for graph construction and serialization.
void rustnn_init_logger(void)
Install rustnn's pretty_env_logger global logger if no logger is installed yet.
enum RustnnStatus rustnn_graph_builder_identity(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the identity operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_tan(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the tan operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_arg_max(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, uint32_t axis, struct RustnnOperand **output)
Apply the arg max operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_logical_or(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the logical or operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_binary_with_options(struct RustnnGraphBuilder *builder, enum RustnnBinaryOperation operation, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, const struct RustnnOperatorOptions *options, struct RustnnOperand **output)
Implementation detail of the C++ API.
RustnnPowerPreference
C representation of [MLPowerPreference].
Definition rustnn.h:61
@ RustnnPowerPreference_HighPerformance
Prefer high performance.
Definition rustnn.h:69
@ RustnnPowerPreference_LowPower
Prefer low power.
Definition rustnn.h:73
@ RustnnPowerPreference_Default
Use the default power preference.
Definition rustnn.h:65
enum RustnnStatus rustnn_graph_builder_logical_not(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the logical not operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_gather_nd(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the gather nd operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_triangular(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the triangular operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_reduce_l2(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the reduce l2 operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_l2_pool2d(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the l2 pool2d operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_resample2d(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the resample2d operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_prelu(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the prelu operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_pad(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, const uint32_t *beginning_padding, uintptr_t beginning_padding_len, const uint32_t *ending_padding, uintptr_t ending_padding_len, struct RustnnOperand **output)
Apply the pad operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_gru(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, const struct RustnnOperand *weight, const struct RustnnOperand *recurrent_weight, uint32_t steps, uint32_t hidden_size, struct RustnnOperand **outputs, uintptr_t outputs_capacity, uintptr_t *outputs_len)
Apply the gru operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_hard_swish(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the hard swish operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_split(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, const uint32_t *splits, uintptr_t splits_len, struct RustnnOperand **outputs, uintptr_t outputs_capacity, uintptr_t *outputs_len)
Apply the split operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_reverse(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the reverse operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_lstm(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, const struct RustnnOperand *weight, const struct RustnnOperand *recurrent_weight, uint32_t steps, uint32_t hidden_size, struct RustnnOperand **outputs, uintptr_t outputs_capacity, uintptr_t *outputs_len)
Apply the lstm operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_not_equal(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the not equal operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_gemm(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the gemm operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_logical_and(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the logical and operation using [MLGraphBuilder].
RustnnStatus
Result of a C API call.
Definition rustnn.h:11
@ RustnnStatus_Success
The call completed successfully.
Definition rustnn.h:15
enum RustnnStatus rustnn_graph_builder_tile(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, const uint32_t *repetitions, uintptr_t repetitions_len, struct RustnnOperand **output)
Apply the tile operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_arg_min(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, uint32_t axis, struct RustnnOperand **output)
Apply the arg min operation using [MLGraphBuilder].
enum RustnnStatus rustnn_context_read_tensor(struct RustnnContext *context, const struct RustnnTensor *tensor, void *data, uintptr_t data_len)
Copy bytes from an [MLTensor] through [MLContext].
enum RustnnStatus rustnn_graph_builder_abs(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the abs operation using [MLGraphBuilder].
void rustnn_graph_builder_destroy(struct RustnnGraphBuilder *builder)
Release an [MLGraphBuilder] handle.
enum RustnnStatus rustnn_graph_builder_instance_normalization(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the instance normalization operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_scatter_nd(struct RustnnGraphBuilder *builder, const struct RustnnOperand *first, const struct RustnnOperand *second, const struct RustnnOperand *third, struct RustnnOperand **output)
Apply the scatter nd operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_cumulative_sum(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, uint32_t axis, struct RustnnOperand **output)
Apply the cumulative sum operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_greater_or_equal(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the greater or equal operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_maximum(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the maximum operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_greater(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the greater operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_unsqueeze(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the unsqueeze operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_create(struct RustnnContext *context, struct RustnnGraphBuilder **output)
Create a runtime-backed [MLGraphBuilder] that exclusively borrows context.
enum RustnnStatus rustnn_context_write_tensor(struct RustnnContext *context, const struct RustnnTensor *tensor, const void *data, uintptr_t data_len)
Copy bytes into an [MLTensor] through [MLContext].
enum RustnnStatus rustnn_graph_builder_lstm_cell(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, const struct RustnnOperand *weight, const struct RustnnOperand *recurrent_weight, const struct RustnnOperand *hidden_state, const struct RustnnOperand *cell_state, uint32_t hidden_size, struct RustnnOperand **outputs, uintptr_t outputs_capacity, uintptr_t *outputs_len)
Apply the lstm cell operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_cast(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, enum RustnnDataType data_type, struct RustnnOperand **output)
Apply the cast operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_logical_xor(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the logical xor operation using [MLGraphBuilder].
void rustnn_context_destroy(struct RustnnContext *context)
Release an [MLContext] handle.
enum RustnnStatus rustnn_graph_builder_reduce_product(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the reduce product operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_equal(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the equal operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_reduce_sum_square(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the reduce sum square operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_operand_shape(struct RustnnGraphBuilder *builder, const struct RustnnOperand *operand, uint64_t *dimensions, uintptr_t dimensions_capacity, uintptr_t *dimensions_len)
Copy an [MLOperand] shape from [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_dequantize_linear(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, const struct RustnnOperand *scale, const struct RustnnOperand *zero_point, struct RustnnOperand **output)
Apply the dequantize linear operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_where(struct RustnnGraphBuilder *builder, const struct RustnnOperand *first, const struct RustnnOperand *second, const struct RustnnOperand *third, struct RustnnOperand **output)
Apply the where operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_tanh(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the tanh operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_gather(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the gather operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_linear(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the linear operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_lesser(struct RustnnGraphBuilder *builder, const struct RustnnOperand *lhs, const struct RustnnOperand *rhs, struct RustnnOperand **output)
Apply the lesser operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_reduce_log_sum_exp(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the reduce log sum exp operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_exp(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the exp operation using [MLGraphBuilder].
enum RustnnStatus rustnn_graph_builder_average_pool2d(struct RustnnGraphBuilder *builder, const struct RustnnOperand *input, struct RustnnOperand **output)
Apply the average pool2d operation using [MLGraphBuilder].
C representation of [BackendDevice].
Definition rustnn.h:277
C representation of [MLContextOptions].
Definition rustnn.h:330
bool accelerated
Whether to prefer an accelerated device.
Definition rustnn.h:338
C representation of [MLOperatorOptions] shared by the operations exposed by this ABI.
Definition rustnn.h:367
C representation of [RustNNOptions].
Definition rustnn.h:318
Explicit backend and device hint for context creation.
Definition rustnn.hpp:79
DeviceType device_type
Definition rustnn.hpp:81
std::size_t device_index
Definition rustnn.hpp:82
Backend selection and runtime configuration for MLContext.
Definition rustnn.hpp:99
std::optional< RustNNOptions > rustnn_options
Definition rustnn.hpp:104
std::optional< BackendDevice > device_hint
Definition rustnn.hpp:103
MLPowerPreference power_preference
Definition rustnn.hpp:100
Optional operator label used in diagnostics.
Definition rustnn.hpp:108
rustnn-specific backend configuration.
Definition rustnn.hpp:94
TrtxOptions trtx
Definition rustnn.hpp:95
TensorRT-RTX engine caching and execution options.
Definition rustnn.hpp:86