Skip to content

onnx

stable

Neural network activation functions, tensor utilities, and a simulated ONNX model session API for running and post-processing inference results.

use plugin onnx::{softmax, argmax, topk, …}
18 functions AI & ML
/ filter jk navigate Esc clear
Functions (18)
  1. softmax Normalize a vector to a probability distribution
  2. argmax Index of the largest value in a table
  3. topk Top-k values and indices from a table
  4. sigmoid Sigmoid activation for a scalar
  5. relu ReLU activation for a scalar
  6. create_tensor_info Describe a tensor shape and total element count
  7. batch_softmax Apply softmax to each row of a 2D table
  8. normalize_vector L2-normalize a vector
  9. load_model Load a model descriptor from a file path
  10. create_session Create an inference session from a model
  11. get_input_info Query input tensor descriptors for a session
  12. get_output_info Query output tensor descriptors for a session
  13. run Run inference on a session with input tensors
  14. get_metadata Read model metadata from a model table
  15. leaky_relu Leaky ReLU activation for a scalar
  16. tanh Hyperbolic tangent activation for a scalar
  17. flatten Recursively flatten nested tables to a flat array
  18. reshape Reinterpret flat data with a new shape

Overview

onnx bundles two layers that together cover the post-processing and orchestration around a neural network. The first is a set of pure math primitives — activation functions (sigmoid, relu, leaky_relu, tanh) and tensor utilities (softmax, argmax, topk, normalize_vector, flatten, reshape, create_tensor_info) — that operate on plain tables of numbers and return tables or scalars. The second is a simulated model-session API (load_model, create_session, get_input_info, get_output_info, run, get_metadata) that mirrors the shape of a real ONNX runtime so you can prototype an inference pipeline end to end.

Nothing here is an opaque handle: a "model" and a "session" are ordinary descriptor tables you can inspect, and every tensor is just an indexed table of numbers (1-based). The session functions are simulated — run applies softmax to the flattened inputs rather than executing a graph — so use this plugin to learn the pipeline, post-process logits from an external runtime, or build classification logic without a heavyweight dependency.

Common patterns

Turn raw logits into a ranked prediction with softmax, argmax, and topk:

use plugin onnx::{softmax, argmax, topk}

let logits = #{1: 1.5, 2: 3.2, 3: 0.1, 4: 2.8}
let probs = softmax(logits)
print("predicted class: {argmax(probs)}")

let top2 = topk(probs, 2)
let best = top2[1]
print("best index={best["index"]} prob={best["value"]}")

Drive the full simulated session pipeline from a file path to a class label:

use plugin onnx::{load_model, create_session, get_input_info, run, argmax}

let model = load_model("models/classifier.onnx")
let session = create_session(model)
let inputs = get_input_info(session, 1)
print("expects input: {inputs[1]["name"]}")

let outputs = run(session, #{1: #{1: 0.5, 2: 1.2, 3: -0.3}})
print("class: {argmax(outputs["output_0"])}")

Prepare an embedding by flattening, reshaping, and normalizing it:

use plugin onnx::{flatten, reshape, normalize_vector}

let nested = #{1: #{1: 3.0, 2: 4.0}, 2: #{1: 0.0, 2: 0.0}}
let flat = flatten(nested)
let tensor = reshape(flat, #{1: 2, 2: 2})
let unit = normalize_vector(tensor["data"])
print("normalized first element: {unit[1]}")

Normalize a vector to a probability distribution

Converts a table of raw logit scores into a probability distribution that sums to 1.0 using the numerically stable softmax (subtracts the max before exponentiating). Use after the final layer of a classification model.

use plugin onnx::{softmax, argmax}

let logits = #{1: 2.1, 2: 0.5, 3: -1.2}
let probs = softmax(logits)
let predicted = argmax(probs)
print("class {predicted}")

Index of the largest value in a table

Returns the zero-based index of the largest value in the table. Use to convert a softmax probability table into a predicted class label.

use plugin onnx::{argmax}

let probs = #{1: 0.05, 2: 0.82, 3: 0.13}
print("predicted: {argmax(probs)}")

Top-k values and indices from a table

Returns the top-k entries sorted by value descending. Each entry is {index, value}. Use for beam search, label suggestions, or displaying confidence rankings.

use plugin onnx::{softmax, topk}

let probs = softmax(#{1: 1.5, 2: 3.2, 3: 0.1, 4: 2.8})
let top3 = topk(probs, 3)
let best = top3[1]
print("top: index={best["index"]} prob={best["value"]}")

Iterate the ranking to print a small leaderboard of candidate labels:

use plugin onnx::{softmax, topk}

let probs = softmax(#{1: 0.2, 2: 4.0, 3: 1.1, 4: 3.5})
let ranked = topk(probs, 3)
for entry in ranked {
  print("{entry["index"]} -> {entry["value"]}")
}

Sigmoid activation for a scalar

Computes 1 / (1 + exp(-x)), squashing any scalar into (0, 1). Use for binary classification output layers or gating mechanisms.

use plugin onnx::{sigmoid}

print(sigmoid(0.0))
print(sigmoid(2.5))

ReLU activation for a scalar

Returns max(0, x). The standard rectified linear unit activation. Apply element-wise to a table using a loop, or use on individual neuron outputs.

use plugin onnx::{relu}

print(relu(-3.0))
print(relu(2.5))

Apply it across a hidden layer by mapping over each table entry:

use plugin onnx::{relu}

let pre = #{1: -1.0, 2: 0.0, 3: 2.0, 4: -0.5}
let activated = #{}
for i, x in pre {
  activated[i] = relu(x)
}
print("activated[3] = {activated[3]}")

Describe a tensor shape and total element count

Creates a descriptor for a tensor with the given shape (a table of dimension sizes). Returns {dims, total_elements}. Useful for validating that input data has the right number of elements before inference.

use plugin onnx::{create_tensor_info}

let info = create_tensor_info(#{1: 1, 2: 3, 3: 224, 4: 224})
print("total elements: {info["total_elements"]}")

Apply softmax to each row of a 2D table

Applies softmax independently to each row of a 2D table (table of tables). Returns the same structure with each row normalized. Use for processing a batch of logit vectors at once.

use plugin onnx::{batch_softmax}

let batch = #{
  1: #{1: 1.0, 2: 2.0, 3: 0.5},
  2: #{1: -1.0, 2: 3.0, 3: 0.0}
}
let result = batch_softmax(batch)

L2-normalize a vector

Divides each element by the L2 norm of the vector, producing a unit vector. Use for cosine similarity comparisons or when a model expects normalized embeddings as input.

use plugin onnx::{normalize_vector}

let embedding = #{1: 3.0, 2: 4.0}
let norm = normalize_vector(embedding)

Load a model descriptor from a file path

Creates a model descriptor table from a file path and optional format string (defaults to "onnx"). The returned table holds path and format metadata used by create_session and get_metadata.

use plugin onnx::{load_model, create_session}

let model = load_model("models/resnet50.onnx")
let session = create_session(model)

Create an inference session from a model

Creates an inference session descriptor from a model table. Returns {session_id, model_path, device, active}. Pass this session to run, get_input_info, and get_output_info.

use plugin onnx::{load_model, create_session}

let model = load_model("models/classifier.onnx")
let session = create_session(model)
print("session {session["session_id"]} on {session["device"]}")

Query input tensor descriptors for a session

Returns descriptors for the session's input tensors. Each entry has {name, dtype, shape}. num_inputs is optional and defaults to 1. Use to verify your input dimensions before calling run.

use plugin onnx::{load_model, create_session, get_input_info}

let session = create_session(load_model("model.onnx"))
let inputs = get_input_info(session, 1)
print("input name: {inputs[1]["name"]}")

Query output tensor descriptors for a session

Returns descriptors for the session's output tensors. Each entry has {name, dtype, shape}. Defaults to 1 output if not specified.

use plugin onnx::{load_model, create_session, get_output_info}

let session = create_session(load_model("model.onnx"))
let outputs = get_output_info(session)
print("output shape entries: {outputs[1]["shape"]}")

Run inference on a session with input tensors

Runs inference with the given input tensors and returns output tensors. The simulated backend applies softmax to the flattened inputs and returns the result as output_0. In a real integration, this drives the ONNX runtime.

use plugin onnx::{load_model, create_session, run, argmax}

let session = create_session(load_model("classifier.onnx"))
let outputs = run(session, #{1: #{1: 0.5, 2: 1.2, 3: -0.3}})
let predicted = argmax(outputs["output_0"])
print("class: {predicted}")

Because the simulated output is already a probability distribution, you can feed it straight into topk to rank candidates:

use plugin onnx::{load_model, create_session, run, topk}

let session = create_session(load_model("model.onnx"))
let outputs = run(session, #{1: #{1: 2.0, 2: 1.0, 3: 3.0}})
let ranked = topk(outputs["output_0"], 2)
print("most likely index: {ranked[1]["index"]}")

Read model metadata from a model table

Reads metadata from a model descriptor table. Returns {model_path, format, producer, domain, ir_version, opset_version}.

use plugin onnx::{load_model, get_metadata}

let model = load_model("model.onnx")
let meta = get_metadata(model)
print("opset: {meta["opset_version"]}")

Leaky ReLU activation for a scalar

Leaky ReLU: returns x when positive, alpha * x when negative. alpha defaults to 0.01. Prevents dying neurons compared to standard ReLU.

use plugin onnx::{leaky_relu}

print(leaky_relu(-2.0, 0.1))
print(leaky_relu(3.0))

Hyperbolic tangent activation for a scalar

Computes the hyperbolic tangent of x, returning values in (-1, 1). Used as an activation function in recurrent and older feed-forward networks.

use plugin onnx::{tanh}

print(tanh(0.0))
print(tanh(1.5))

Recursively flatten nested tables to a flat array

Recursively flattens nested tables of numbers into a single flat indexed table. Use to convert a multi-dimensional tensor representation into a 1D array before passing to an operation that expects flat input.

use plugin onnx::{flatten}

let nested = #{1: #{1: 1.0, 2: 2.0}, 2: #{1: 3.0, 2: 4.0}}
let flat = flatten(nested)
print("elements: {flat[1]}, {flat[2]}, {flat[3]}, {flat[4]}")

Reinterpret flat data with a new shape

Reinterprets a flat table of values with a new shape. Returns {data, shape}. The product of shape dimensions must equal the number of values, otherwise an error is raised.

use plugin onnx::{reshape}

let flat = #{1: 1.0, 2: 2.0, 3: 3.0, 4: 4.0}
let tensor = reshape(flat, #{1: 2, 2: 2})
print("shape: {tensor["shape"][1]}x{tensor["shape"][2]}")

A mismatched shape product raises an error, so reshape doubles as a quick element-count guard before further processing:

use plugin onnx::{create_tensor_info, reshape}

let data = #{1: 1.0, 2: 2.0, 3: 3.0, 4: 4.0, 5: 5.0, 6: 6.0}
let info = create_tensor_info(#{1: 2, 2: 3})
print("expecting {info["total_elements"]} elements")
let tensor = reshape(data, #{1: 2, 2: 3})
print("ok: {tensor["shape"][1]} rows")
enespt-br