# `TFLiteElixir.InterpreterBuilder`
[🔗](https://github.com/cocoa-xu/tflite_elixir/blob/main/lib/tflite_elixir/interpreter_builder.ex#L1)

Build an interpreter capable of interpreting model.

# `nif_error`

```elixir
@type nif_error() :: {:error, String.t()}
```

# `nif_resource_ok`

```elixir
@type nif_resource_ok() :: {:ok, reference()}
```

# `add_delegate`

```elixir
@spec add_delegate(reference(), reference()) :: :ok | nif_error()
```

Attach a delegate to the builder, with the default decline policy.

Equivalent to `add_delegate(builder, delegate, [])`.

# `add_delegate`

```elixir
@spec add_delegate(reference(), reference(), Keyword.t() | map()) :: :ok | nif_error()
```

Attach a delegate to every interpreter this builder goes on to build.

The delegate is applied in the order delegates were added, and it has to
outlive every interpreter built from this builder -- which is why there is no
way to detach or delete one. Holding the reference is not required: the builder
and each interpreter keep the delegate alive for as long as they need it.

Attaching any delegate also suppresses the XNNPACK one that `build/2` would
otherwise add for you.

##### Options
- `:on_decline`. What to do when a delegate reports that it cannot take the
  graph, but leaves the graph runnable -- a static-shape delegate meeting a
  dynamic tensor, say. TfLite discards the whole interpreter in that case.
  - `:error` (the default) -- the decline surfaces as `{:error, reason}` from
    `build/2`.
  - `:fallback` -- `build/2` builds again without the delegates that were added
    with this policy, and answers `{:ok, :delegate_declined}`. Only a decline
    is retried; every other failure still fails.

Note that an interpreter, and any delegate attached to it, belongs to one
process at a time. Nothing here is serialised for you.

# `add_delegate!`

Raising version of `add_delegate/2`.

# `add_delegate!`

Raising version of `add_delegate/3`.

# `build`

```elixir
@spec build(reference(), reference()) :: :ok | {:ok, :delegate_declined} | nif_error()
```

Build the interpreter with the InterpreterBuilder.

Note: all Interpreters should be built with the InterpreterBuilder,
which allocates memory for the Interpreter and does various set up
tasks so that the Interpreter can read the provided model.

# `build!`

Raising version of `build/2`.

# `new`

```elixir
@spec new(
  %TFLiteElixir.FlatBufferModel{
    initialized: term(),
    minimum_runtime: term(),
    model: term()
  },
  reference()
) :: nif_resource_ok() | nif_error()
```

New InterpreterBuilder

# `new!`

Raising version of `new/2`.

# `set_num_threads`

```elixir
@spec set_num_threads(reference(), integer()) :: :ok | nif_error()
```

Sets the number of CPU threads to use for the interpreter.
Returns `:ok` on success, `{:error, reason}` on error.

`num_threads` follows TfLite: `-1` asks the runtime to choose, `0` means the
same as `1`, and anything below `-1` is answered with `{:error, reason}`.

# `set_num_threads!`

Raising version of `set_num_threads/2`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
