Skip to content

Commit 9af45df

Browse files
committed
Doc fixes and examples for Ortex.Serving
1 parent 6690bb2 commit 9af45df

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

lib/ortex/model.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ defmodule Ortex.Model do
2121
@enforce_keys [:reference]
2222
defstruct [:reference]
2323

24+
@doc false
2425
def load(path, eps \\ [:cpu], opt \\ 3) do
2526
case Ortex.Native.init(path, eps, opt) do
2627
{:error, msg} ->
@@ -31,10 +32,12 @@ defmodule Ortex.Model do
3132
end
3233
end
3334

35+
@doc false
3436
def run(%Ortex.Model{} = model, tensor) when not is_tuple(tensor) do
3537
run(model, {tensor})
3638
end
3739

40+
@doc false
3841
def run(%Ortex.Model{reference: model}, tensors) do
3942
# Move tensors into Ortex backend and pass the reference to the Ortex NIF
4043
output =

lib/ortex/native.ex

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
defmodule Ortex.Native do
2-
@moduledoc """
3-
Documentation for `Ortex.Native`.
4-
5-
Stubs for `Rustler` NIFs. These should never be called directly.
6-
"""
2+
@moduledoc false
73

84
# We have to compile the crate before `use Rustler` compiles the crate since
95
# cargo downloads the onnxruntime shared libraries and they are not available

lib/ortex/serving.ex

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,58 @@ defmodule Ortex.Serving do
22
@moduledoc """
33
`Ortex.Serving` Documentation
44
5-
This is a light wrapper for using `Nx.Serving` behaviour with `Ortex`. Using `jit` and
5+
This is a lightweight wrapper for using `Nx.Serving` behaviour with `Ortex`. Using `jit` and
66
`defn` functions in this are not supported, it is strictly for serving batches to
77
an `Ortex.Model` for inference.
8+
9+
## Examples
10+
11+
### Inline/serverless workflow
12+
13+
To quickly create an `Ortex.Serving` and run it
14+
15+
```elixir
16+
iex> model = Ortex.load("./models/resnet50.onnx")
17+
iex> serving = Nx.Serving.new(Ortex.Serving, model)
18+
iex> batch = Nx.Batch.stack([{Nx.broadcast(0.0, {3, 224, 224})}])
19+
iex> {result} = Nx.Serving.run(serving, batch)
20+
iex> result |> Nx.backend_transfer |> Nx.argmax(axis: 1)
21+
#Nx.Tensor<
22+
s64[1]
23+
[499]
24+
>
25+
```
26+
27+
### Stateful/process workflow
28+
29+
An `Ortex.Serving` can also be started in your Application's supervision tree
30+
```elixir
31+
model = Ortex.load("./models/resnet50.onnx")
32+
children = [
33+
{Nx.Serving,
34+
serving: Nx.Serving.new(Ortex.Serving, model),
35+
name: MyServing,
36+
batch_size: 10,
37+
batch_timeout: 100}
38+
]
39+
opts = [strategy: :one_for_one, name: OrtexServing.Supervisor]
40+
Supervisor.start_link(children, opts)
41+
```
42+
43+
With the application started, batches can now be sent to the `Ortex.Serving` process
44+
45+
```elixir
46+
iex> Nx.Serving.batched_run(MyServing, Nx.Batch.stack([{Nx.broadcast(0.0, {3, 224, 224})}]))
47+
...> {#Nx.Tensor<
48+
f32[1][1000]
49+
Ortex.Backend
50+
[
51+
[...]
52+
]
53+
>}
54+
55+
```
56+
857
"""
958

1059
@behaviour Nx.Serving

lib/ortex/util.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule Ortex.Util do
2+
@moduledoc false
23
def copy_ort_libs() do
34
build_root = Path.absname(:code.priv_dir(:ortex)) |> Path.dirname()
45

0 commit comments

Comments
 (0)