Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate Axon.Loop.handle/4 #470

Merged
merged 1 commit into from
Feb 1, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ model =
loop =
model
|> Axon.Loop.trainer(:mean_squared_error, :sgd)
|> Axon.Loop.handle(:epoch_completed, &CustomEventHandler.my_weird_handler/1)
|> Axon.Loop.handle_event(:epoch_completed, &CustomEventHandler.my_weird_handler/1)
```

<!-- livebook:{"output":true} -->
Expand Down Expand Up @@ -190,7 +190,7 @@ The loop will immediately stop executing and return the current state at the tim
```elixir
model
|> Axon.Loop.trainer(:mean_squared_error, :sgd)
|> Axon.Loop.handle(:epoch_completed, &CustomEventHandler.always_halts/1)
|> Axon.Loop.handle_event(:epoch_completed, &CustomEventHandler.always_halts/1)
|> Axon.Loop.run(train_data, %{}, epochs: 5, iterations: 100)
```

Expand Down Expand Up @@ -283,8 +283,8 @@ If you run these handlers in conjunction, the loop will not terminate prematurel
```elixir
model
|> Axon.Loop.trainer(:mean_squared_error, :sgd)
|> Axon.Loop.handle(:iteration_completed, &CustomEventHandler.always_halts_epoch/1)
|> Axon.Loop.handle(:epoch_completed, &CustomEventHandler.always_halts_loop/1)
|> Axon.Loop.handle_event(:iteration_completed, &CustomEventHandler.always_halts_epoch/1)
|> Axon.Loop.handle_event(:epoch_completed, &CustomEventHandler.always_halts_loop/1)
|> Axon.Loop.run(train_data, %{}, epochs: 5, iterations: 100)
```

Expand Down
33 changes: 19 additions & 14 deletions lib/axon/loop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ defmodule Axon.Loop do
:completed # On loop completion
]

You can attach event handlers to events using `Axon.Loop.handle/4`:
You can attach event handlers to events using `Axon.Loop.handle_event/4`:

loop
|> Axon.Loop.handle(:iteration_completed, &log_metrics/1, every: 100)
|> Axon.Loop.handle_event(:iteration_completed, &log_metrics/1, every: 100)
|> Axon.Loop.run(data)

The above will trigger `log_metrics/1` every 100 times the `:iteration_completed` event
Expand All @@ -177,10 +177,10 @@ defmodule Axon.Loop do
to the loop. If you have two handlers on the same event, they will trigger in order:

loop
|> Axon.Loop.handle(:epoch_completed, &normalize_state/1) # Runs first
|> Axon.Loop.handle(:epoch_completed, &log_state/1) # Runs second
|> Axon.Loop.handle_event(:epoch_completed, &normalize_state/1) # Runs first
|> Axon.Loop.handle_event(:epoch_completed, &log_state/1) # Runs second

You may provide filters to filter when event handlers trigger. See `Axon.Loop.handle/4`
You may provide filters to filter when event handlers trigger. See `Axon.Loop.handle_event/4`
for more details on valid filters.

## Factories
Expand Down Expand Up @@ -907,8 +907,8 @@ defmodule Axon.Loop do
loop:

loop
|> Axon.Loop.handle(:epoch_started, &normalize_step_state/1) # executes first
|> Axon.Loop.handle(:epoch_started, &log_step_state/1) # executes second
|> Axon.Loop.handle_event(:epoch_started, &normalize_step_state/1) # executes first
|> Axon.Loop.handle_event(:epoch_started, &log_step_state/1) # executes second

Thus, if you have separate handlers which alter or depend on loop state,
you need to ensure they are ordered correctly, or combined into a single
Expand Down Expand Up @@ -944,8 +944,7 @@ defmodule Axon.Loop do
potentially excessive recompilation and result in significant additinal overhead
during loop execution.**
"""
# TODO(seanmor5): Custom events
def handle(%Loop{handlers: handle_fns} = loop, event, handler, filter \\ :always) do
def handle_event(%Loop{handlers: handle_fns} = loop, event, handler, filter \\ :always) do
filter = build_filter_fn(filter)

handle_fns =
Expand All @@ -963,6 +962,12 @@ defmodule Axon.Loop do
%Loop{loop | handlers: handle_fns}
end

@doc false
@deprecated "handle/4 is deprecated, use handle_event/4 instead"
def handle(%Loop{handlers: handle_fns} = loop, event, handler, filter \\ :always) do
handle_event(loop, event, handler, filter)
end

@doc """
Adds a handler function which logs the given message produced
by `message_fn` to the given IO device every `event` satisfying
Expand Down Expand Up @@ -1001,7 +1006,7 @@ defmodule Axon.Loop do
end
end

handle(loop, event, log_fn, filter)
handle_event(loop, event, log_fn, filter)
end

@doc """
Expand Down Expand Up @@ -1072,7 +1077,7 @@ defmodule Axon.Loop do
{:continue, %{state | metrics: metrics}}
end

handle(loop, event, validation_loop, filter)
handle_event(loop, event, validation_loop, filter)
end

@doc """
Expand Down Expand Up @@ -1120,7 +1125,7 @@ defmodule Axon.Loop do
mode = opts[:mode] || :min
patience = opts[:patience] || 3

handle(loop, event, &monitor_impl(&1, metric, fun, name, mode, patience), filter)
handle_event(loop, event, &monitor_impl(&1, metric, fun, name, mode, patience), filter)
end

defp monitor_impl(
Expand Down Expand Up @@ -1273,7 +1278,7 @@ defmodule Axon.Loop do
filter: filter
)
else
handle(loop, event, checkpoint_fun, filter)
handle_event(loop, event, checkpoint_fun, filter)
end
end

Expand Down Expand Up @@ -1437,7 +1442,7 @@ defmodule Axon.Loop do

opts = Keyword.validate!(opts, event: :iteration_completed, filter: :always)

handle(
handle_event(
loop,
opts[:event],
fn %{
Expand Down