Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/ortex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ defmodule Ortex do
"""

@doc """
`intra_threads` optionally bounds CPU intra-operator parallelism: 0 retains
runtime defaults; 1 executes on the calling thread without an intra-op worker pool.
Values must be integers in 0..2_147_483_647. This is not a process-wide thread cap.

Load an `Ortex.Model` from disk. Optionally pass the execution providers as a list
of descending priority and graph optimization level 1-3. Any graph optimization level
beyond the range of 1-3 will disable graph optimization.
Expand All @@ -25,7 +29,7 @@ defmodule Ortex do
iex> Ortex.load("./models/tinymodel.onnx", [:cpu], 0)

"""
defdelegate load(path, eps \\ [:cpu], opt \\ 3), to: Ortex.Model
defdelegate load(path, eps \\ [:cpu], opt \\ 3, intra_threads \\ 0), to: Ortex.Model

@doc """
Run a forward pass through a model.
Expand Down
4 changes: 2 additions & 2 deletions lib/ortex/model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ defmodule Ortex.Model do
defstruct [:reference]

@doc false
def load(path, eps \\ [:cpu], opt \\ 3) do
case Ortex.Native.init(path, eps, opt) do
def load(path, eps \\ [:cpu], opt \\ 3, intra_threads \\ 0) do
case Ortex.Native.init(path, eps, opt, intra_threads) do
{:error, msg} ->
raise msg

Expand Down
5 changes: 4 additions & 1 deletion lib/ortex/native.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ defmodule Ortex.Native do

# When loading a NIF module, dummy clauses for all NIF function are required.
# NIF dummies usually just error out when called when the NIF is not loaded, as that should never normally happen.
def init(_model_path, _execution_providers, _optimization_level),
def init(model_path, execution_providers, optimization_level),
do: init(model_path, execution_providers, optimization_level, 0)

def init(_model_path, _execution_providers, _optimization_level, _intra_threads),
do: :erlang.nif_error(:nif_not_loaded)

def run(_model, _inputs), do: :erlang.nif_error(:nif_not_loaded)
Expand Down
6 changes: 5 additions & 1 deletion native/ortex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ fn init(
model_path: String,
eps: Vec<Atom>,
opt: i32,
intra_threads: i32,
) -> NifResult<ResourceArc<model::OrtexModel>> {
if intra_threads < 0 {
return Err(rustler::Error::BadArg);
}
let eps = utils::map_eps(env, eps);
let model = model::init(model_path, eps, opt)
let model = model::init(model_path, eps, opt, intra_threads as usize)
.map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
Ok(ResourceArc::new(model))
}
Expand Down
13 changes: 10 additions & 3 deletions native/ortex/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,21 @@ pub fn init(
model_path: String,
eps: Vec<ExecutionProviderDispatch>,
opt: i32,
intra_threads: usize,
) -> Result<OrtexModel, Error> {
// TODO: send tracing logs to erlang/elixir _somehow_
// tracing_subscriber::fmt::init();

let session = Session::builder()?
let builder = Session::builder()?
.with_optimization_level(map_opt_level(opt))?
.with_execution_providers(eps)?
.commit_from_file(model_path)?;
.with_execution_providers(eps)?;
// Preserve the upstream default when the caller does not select a limit.
let builder = if intra_threads == 0 {
builder
} else {
builder.with_intra_threads(intra_threads)?
};
let session = builder.commit_from_file(model_path)?;

let state = OrtexModel { session };
Ok(state)
Expand Down
23 changes: 23 additions & 0 deletions test/ortex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@ defmodule OrtexTest do
use ExUnit.Case
doctest Ortex

test "an explicit intra-op thread count preserves model outputs" do
path = "./models/tinymodel.onnx"
inputs = {Nx.broadcast(Nx.tensor(0, type: :s32), {1, 100}), Nx.broadcast(0.0, {1, 100})}

output_bytes = fn model ->
model |> Ortex.run(inputs) |> Tuple.to_list() |> Enum.map(&Nx.to_binary/1)
end

expected = output_bytes.(Ortex.load(path, [:cpu], 3))

for threads <- [0, 1, 2] do
assert output_bytes.(Ortex.load(path, [:cpu], 3, threads)) == expected
end
end

test "invalid intra-op thread counts are rejected at load time" do
for threads <- [-1, 0.5, "1", 2_147_483_648] do
assert_raise ArgumentError, fn ->
Ortex.load("./models/tinymodel.onnx", [:cpu], 3, threads)
end
end
end

@tag :resnet50
test "resnet50" do
model = Ortex.load("./models/resnet50.onnx")
Expand Down