BB.Inference (bb_inference v0.1.0)

Copy Markdown View Source

Declares machine-learning models on a robot and runs them.

defmodule MyRobot do
  use BB, extensions: [BB.Inference]

  inference do
    model :detector do
      runtime BB.Inference.OpenCV
      source "https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_nano.onnx"
      sha256 "c789161ed43c8269fcd4e67c67eeeb4e80c622da2eb296a20bc6007bd18a0b7d"
      codec BB.Estimator.Yolo.YoloX
      labels MyRobot.Coco.labels()
    end
  end
end

A model is one artefact, the BB.Inference.Runtime that executes it, and the BB.Inference.ModelCodec that converts to and from the artefact's own conventions. The codec also says what task the model performs.

Models are controllers

Each model is lowered into a robot-level controller of the same name, running BB.Inference.ModelController. It shares the namespace every controller, sensor, actuator, link and joint shares, and its restarts count against the topology supervisor's budget like any other controller's. It defaults to simulation: :omit.

Referring to a model

A component names a model through an ordinary option typed with model_ref/1:

use BB.Estimator,
  options_schema: [
    model: [type: BB.Inference.model_ref(:object_detection), required: true]
  ]

Compiling the robot fails if that option names a model the robot doesn't declare, or one whose codec performs a different task.

Artefacts

Artefacts are fetched at build time by the :bb_inference compiler, which places them under the application's priv. See Mix.Tasks.Compile.BbInference.

Summary

Functions

Describe a loaded model's inputs and outputs.

Encode input with the model's codec, run the model and decode its outputs.

Fetch a loaded model: its description, its codec fitted to that description, and its labels.

An option type for a component option that names a model performing task.

The models declared on robot.

Run a model on named input tensors, returning named output tensors, both in the model's own types.

Functions

describe(robot, model)

@spec describe(module(), atom()) ::
  {:ok, BB.Inference.Description.t()} | {:error, Exception.t()}

Describe a loaded model's inputs and outputs.

infer(robot, loaded, input, timeout \\ 5000)

@spec infer(module(), BB.Inference.LoadedModel.t(), term(), timeout()) ::
  {:ok, term()} | {:error, term()}

Encode input with the model's codec, run the model and decode its outputs.

What input and the result are depends on the codec's task. Encoding and decoding run in the calling process; only the model itself runs in the model's process.

Each stage is a :telemetry span: [:bb, :inference, :encode], [:bb, :inference, :run] and [:bb, :inference, :decode], with %{robot: robot, model: name} metadata.

inference(body)

(macro)

loaded_model(robot, model)

@spec loaded_model(module(), atom()) ::
  {:ok, BB.Inference.LoadedModel.t()} | {:error, Exception.t()}

Fetch a loaded model: its description, its codec fitted to that description, and its labels.

Waits for the model to finish loading. Returns {:error, %BB.Error.State.Inference.ModelNotRunning{}} when no process is running the model, which in simulation is the default, and {:error, %BB.Error.State.Inference.ModelUnavailable{}} when the model stops while loading.

model_ref(task)

@spec model_ref(atom()) :: {:custom, module(), atom(), [atom()]}

An option type for a component option that names a model performing task.

options_schema: [model: [type: BB.Inference.model_ref(:object_detection), required: true]]

models(robot)

@spec models(module()) :: [BB.Inference.Dsl.Model.t()]

The models declared on robot.

run(robot, model, inputs, timeout \\ 5000)

@spec run(module(), atom(), %{required(String.t()) => Nx.Tensor.t()}, timeout()) ::
  {:ok, %{required(String.t()) => Nx.Tensor.t()}} | {:error, term()}

Run a model on named input tensors, returning named output tensors, both in the model's own types.