Packages

QNN (Qualcomm AI Engine Direct) NIF bindings for the Hexagon NPU (QCS6490/HTP)

Current section

Files

Jump to
hexagon_tpu lib hexagon_tpu.ex
Raw

lib/hexagon_tpu.ex

defmodule HexagonTpu do
@moduledoc """
Elixir bindings for the Qualcomm Hexagon NPU (HTP) via the QNN
(Qualcomm AI Engine Direct) runtime.
Models are pre-compiled QNN context binaries (see `docs/models.md` for
the ONNX/TFLite conversion pipeline). Typical usage:
{:ok, model} = HexagonTpu.load_model("/data/mobilenet_v2.bin")
{:ok, [scores]} = HexagonTpu.predict(model, [input_tensor])
Or with explicit control:
{:ok, rt} = HexagonTpu.Runtime.htp()
{:ok, ctx} = HexagonTpu.Context.from_file(rt, "/data/mobilenet_v2.bin")
{:ok, graph} = HexagonTpu.Graph.retrieve(ctx)
{:ok, outputs} = HexagonTpu.Graph.execute(graph, %{"image" => input_tensor})
"""
alias HexagonTpu.{Context, Graph, Runtime}
@doc """
Loads a context binary from `path` on the shared HTP runtime and
retrieves its (sole) graph.
"""
def load_model(path, opts \\ []) do
with {:ok, rt} <- Runtime.htp(opts),
{:ok, ctx} <- Context.from_file(rt, path),
{:ok, graph} <- Graph.retrieve(ctx, opts[:graph]) do
{:ok, graph}
end
end
@doc "Runs inference on a graph returned by `load_model/2`. See `HexagonTpu.Graph.execute/3`."
defdelegate predict(graph, inputs, opts \\ []), to: Graph, as: :execute
@doc """
Whether the NIF is loaded and the HTP backend library can be opened on
this system.
"""
def available? do
case Runtime.htp() do
{:ok, _} -> true
_ -> false
end
rescue
ErlangError -> false
end
end