-
Notifications
You must be signed in to change notification settings - Fork 229
Add runtime raise_if to Nx.Defn.Kernel #1822
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -246,28 +246,56 @@ defmodule EXLA.Defn do | |
| end) | ||
|
|
||
| infeeds = Map.new(infeeds) | ||
| error_ref = make_ref() | ||
|
|
||
| {:ok, outfeed_pid} = | ||
| Outfeed.start_child(executable, outfeed, Process.group_leader(), infeeds) | ||
| Outfeed.start_child( | ||
| executable, | ||
| outfeed, | ||
| Process.group_leader(), | ||
| infeeds, | ||
| {self(), error_ref} | ||
| ) | ||
|
|
||
| ref = Process.monitor(outfeed_pid) | ||
|
|
||
| run_options = Keyword.put(run_options, :callback_server_pid, outfeed_pid) | ||
|
|
||
| {:ok, runner} = | ||
| EXLA.Defn.Runner.start_link(lock, fn -> | ||
| EXLA.Executable.run(executable, [Enum.reverse(buffers)], run_options) | ||
| try do | ||
| EXLA.Executable.run(executable, [Enum.reverse(buffers)], run_options) | ||
| after | ||
| send(outfeed_pid, :stop) | ||
| end | ||
| end) | ||
|
|
||
| _ = EXLA.Defn.Lock.transfer(lock, fn -> send(runner, lock) end, outfeed_pid) | ||
|
|
||
| receive do | ||
| {:DOWN, ^ref, _, _, _} -> | ||
| results = EXLA.Defn.Runner.read(runner) | ||
| runner_result = EXLA.Defn.Runner.read(runner) | ||
|
|
||
| callback_error = | ||
| receive do | ||
| {:exla_callback_error, ^error_ref, kind, reason, stacktrace} -> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to not have to deal with additional messages. One option is to wrap the raised exception in |
||
| {kind, reason, stacktrace} | ||
| after | ||
| 0 -> nil | ||
| end | ||
|
|
||
| Enum.map(results, fn result -> | ||
| EXLA.Defn.Buffers.to_nx!(result, outputs, executable.mesh) | ||
| end) | ||
| case {callback_error, runner_result} do | ||
| {{kind, reason, stacktrace}, _runner_result} -> | ||
| :erlang.raise(kind, reason, stacktrace) | ||
|
|
||
| {nil, {:error, kind, reason, stacktrace}} -> | ||
| :erlang.raise(kind, reason, stacktrace) | ||
|
|
||
| {nil, {:ok, results}} -> | ||
| Enum.map(results, fn result -> | ||
| EXLA.Defn.Buffers.to_nx!(result, outputs, executable.mesh) | ||
| end) | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we're missing a change in the c++ code to not raise when unwrapping the result. This way if the callback returns
{:error, ...}we can just surface that tuple without raising