native_neural_network 0.3.1

Lib no_std Rust for native neural network (.rnn)
Documentation
#include "../include/rnn_wrapper.hpp"

namespace rnn {

std::uint32_t api_version() {
  return rnn_ffi_api_version();
}

std::string error_message(int code) {
  const char* msg = rnn_ffi_error_message(code);
  return msg ? std::string(msg) : std::string("unknown error");
}

static void throw_if_error(int code) {
  if (code != RNN_FFI_OK) {
    throw FfiError(code, error_message(code));
  }
}

static RnnFfiBenchmarkRecord to_ffi_record(const BenchmarkRecord& record) {
  RnnFfiBenchmarkRecord ffi_record{};
  ffi_record.model_name_ptr = reinterpret_cast<const std::uint8_t*>(record.model_name.data());
  ffi_record.model_name_len = record.model_name.size();
  ffi_record.precision_ptr = reinterpret_cast<const std::uint8_t*>(record.precision.data());
  ffi_record.precision_len = record.precision.size();
  ffi_record.elapsed_ms = record.elapsed_ms;
  ffi_record.iterations = record.iterations;
  ffi_record.avg_loss = record.avg_loss;
  ffi_record.last_loss = record.last_loss;
  ffi_record.output_bytes = record.output_bytes;
  ffi_record.train_samples = record.train_samples;
  ffi_record.total_params = record.total_params;
  ffi_record.layer_count = record.layer_count;
  ffi_record.input_dim = record.input_dim;
  ffi_record.output_dim = record.output_dim;
  ffi_record.benchmark_flags = record.benchmark_flags;
  ffi_record.weights_bytes = record.weights_bytes;
  ffi_record.biases_bytes = record.biases_bytes;
  ffi_record.min_loss = record.min_loss;
  ffi_record.max_loss = record.max_loss;
  ffi_record.loss_stddev = record.loss_stddev;
  ffi_record.iterations_per_sec = record.iterations_per_sec;
  ffi_record.samples_per_sec = record.samples_per_sec;
  return ffi_record;
}

std::size_t benchmark_encoded_size(const BenchmarkRecord& record) {
  auto ffi_record = to_ffi_record(record);
  std::size_t out_size = 0;
  const int code = rnn_ffi_benchmark_encoded_size(&ffi_record, &out_size);
  throw_if_error(code);
  return out_size;
}

std::vector<std::uint8_t> encode_benchmark_blob(const BenchmarkRecord& record) {
  auto ffi_record = to_ffi_record(record);
  std::vector<std::uint8_t> out(benchmark_encoded_size(record), 0);
  std::size_t used = 0;
  const int code = rnn_ffi_encode_benchmark_blob(&ffi_record, out.data(), out.size(), &used);
  throw_if_error(code);
  out.resize(used);
  return out;
}

BenchmarkView decode_benchmark_blob(const std::vector<std::uint8_t>& blob) {
  RnnFfiBenchmarkView view{};
  const int code = rnn_ffi_decode_benchmark_blob(blob.empty() ? nullptr : blob.data(), blob.size(), &view);
  throw_if_error(code);

  BenchmarkView out{};
  out.model_name.assign(reinterpret_cast<const char*>(view.model_name_ptr), view.model_name_len);
  out.precision.assign(reinterpret_cast<const char*>(view.precision_ptr), view.precision_len);
  out.elapsed_ms = view.elapsed_ms;
  out.iterations = view.iterations;
  out.avg_loss = view.avg_loss;
  out.last_loss = view.last_loss;
  out.output_bytes = view.output_bytes;
  out.train_samples = view.train_samples;
  out.total_params = view.total_params;
  out.layer_count = view.layer_count;
  out.input_dim = view.input_dim;
  out.output_dim = view.output_dim;
  out.benchmark_flags = view.benchmark_flags;
  out.weights_bytes = view.weights_bytes;
  out.biases_bytes = view.biases_bytes;
  out.min_loss = view.min_loss;
  out.max_loss = view.max_loss;
  out.loss_stddev = view.loss_stddev;
  out.iterations_per_sec = view.iterations_per_sec;
  out.samples_per_sec = view.samples_per_sec;
  return out;
}

} // namespace rnn