<!--
SPDX-FileCopyrightText: 2026 James Harton

SPDX-License-Identifier: Apache-2.0
-->

<img src="https://github.com/beam-bots/bb/blob/main/logos/beam_bots_logo.png?raw=true" alt="Beam Bots Logo" width="250" />

# bb_inference

[![CI](https://github.com/beam-bots/bb_inference/actions/workflows/ci.yml/badge.svg)](https://github.com/beam-bots/bb_inference/actions/workflows/ci.yml)
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache--2.0-green.svg)](https://opensource.org/licenses/Apache-2.0)
[![Hex version badge](https://img.shields.io/hexpm/v/bb_inference.svg)](https://hex.pm/packages/bb_inference)
[![Hexdocs badge](https://img.shields.io/badge/docs-hexdocs-purple)](https://hexdocs.pm/bb_inference)
[![REUSE status](https://api.reuse.software/badge/github.com/beam-bots/bb_inference)](https://api.reuse.software/info/github.com/beam-bots/bb_inference)
[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/beam-bots/bb_inference)

Declare machine-learning models on a [Beam Bots](https://github.com/beam-bots/bb)
robot, fetch their artefacts at build time, and run them through a pluggable
runtime.

```elixir
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

  topology do
    link :head do
      sensor :camera, {BB.Camera.V4L, device: "/dev/video0"} do
        estimator :objects, {BB.Estimator.ObjectDetection, model: :detector}
      end
    end
  end
end
```

This package has no camera or vision dependencies. It provides:

- **The `inference` DSL section.** A `model` is one artefact, the runtime
  that executes it, and the codec that speaks its conventions.
- **`BB.Inference.Runtime`**, the behaviour a runtime package implements:
  `load`, `describe`, `run`, `can?`, and optionally `unload`.
  [`bb_inference_opencv`](https://github.com/beam-bots/bb_inference_opencv)
  runs ONNX through OpenCV;
  [`bb_inference_tflite`](https://github.com/beam-bots/bb_inference_tflite)
  runs TensorFlow Lite.
- **`BB.Inference.ModelCodec`**, the behaviour for converting a task's input
  into a model's tensors and its output back. The codec states its task;
  [`bb_estimator_object_detection`](https://github.com/beam-bots/bb_estimator_object_detection)
  defines `:object_detection`.
- **A controller per model.** Each `model` becomes a robot-level controller
  that loads the artefact after the robot has started and serves it to
  components.
- **A Mix compiler** that fetches every artefact into `priv` when the
  project builds, through a download cache outside the project, so models
  are part of every release.
- **A compile-time check** that each component naming a model names one
  that exists and performs the task it needs.

## Installation

```bash
mix igniter.install bb_inference
```

This adds the extension to your robot, the `:bb_inference` compiler to your
project and `priv/bb_inference/` to `.gitignore`. By hand:

```elixir
# mix.exs
def project do
  [
    compilers: Mix.compilers() ++ [:bb_inference],
    # ...
  ]
end

def deps do
  [
    {:bb_inference, "~> 0.1.0"}
  ]
end
```

## Artefacts

`source` is an `http`/`https` URL or a path relative to the project. A URL
must declare a `sha256`. An archive (`.zip`, `.tar`, `.tar.gz`, `.tgz`)
names the file inside it with `archive_member`, and `sha256` is the hash of
that file:

```elixir
model :detector do
  runtime BB.Inference.TFLite
  source "https://storage.googleapis.com/download.tensorflow.org/models/tflite/coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip"
  archive_member "detect.tflite"
  sha256 "e4b118e5e4531945de2e659742c7c590f7536f8d0ed26d135abcfe83b4779d13"
  codec {BB.Estimator.ObjectDetection.FinalDetections, mean: 127.5, std: 127.5}
end
```

Downloads are cached in `BB_INFERENCE_CACHE_DIR`, or the user cache
directory (`~/.cache/bb_inference` on Linux). A download that fails is a
warning, and is tried again on every build; an artefact whose hash doesn't
match is an error, and the bad download is deleted from the cache.

## When a model fails to load

The model's controller reports the error through `BB.Safety.report_error/3`,
logs one line saying why, and stops. Its supervisor restarts it straight
away, and once the restart budget is spent the robot latches in `:error`.
`BB.Safety.force_disarm/1` acknowledges the fault and tries again. There is
no back-off between attempts.

## Simulation

A model's controller is omitted in simulation unless the model is declared
with `simulation :start`. A component that needs an omitted model gets a
`BB.Error.State.Inference.ModelNotRunning` saying so.

## Documentation

- [DSL reference](documentation/dsls/DSL-BB.Inference.md)
- [Hexdocs](https://hexdocs.pm/bb_inference)
