-
Notifications
You must be signed in to change notification settings - Fork 125
Annotate activation functions with Nx blocks #636
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
Open
seanmor5
wants to merge
2
commits into
main
Choose a base branch
from
sm-nx-block
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+142
−22
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| defmodule Axon.Block do | ||
| @moduledoc """ | ||
| Defines reusable `Nx.block/4` layers via the `defblock/2` macro. | ||
|
|
||
| `defblock prefix, name(args...) do body end` expands to two things: | ||
|
|
||
| * a struct module named `<prefix>.<CamelName>`, defined as `defstruct []` | ||
| * a `defn` named `name` whose body is wrapped in `Nx.block/4` tagged with | ||
| that struct | ||
|
|
||
| The struct serves as the dispatch tag for custom kernel implementations. | ||
|
|
||
| The module that calls `defblock` must `import Nx.Defn` so the generated | ||
| `defn` is in scope. | ||
|
|
||
| ## Examples | ||
|
|
||
| defmodule MyLayers do | ||
| import Nx.Defn | ||
| import Axon.Block | ||
|
|
||
| defblock MyBlock, dense(x, w, b) do | ||
| x |> Nx.dot(w) |> Nx.add(b) | ||
| end | ||
| end | ||
|
|
||
| This defines `MyLayers.dense/3` and the struct `%MyBlock.Dense{}`. External | ||
| libraries can override the default `defn` implementation with a custom kernel: | ||
|
|
||
| defimpl EXLA.CustomCall, for: MyBlock.Dense do | ||
| ... | ||
| end | ||
|
|
||
| defimpl EMLX.Fast, for: MyBlock.Dense do | ||
| ... | ||
| end | ||
|
|
||
| Trailing `opts \\\\ default` parameters are supported and forwarded to the | ||
| block lambda as the last argument, so the body can call `keyword!/2` | ||
| normally: | ||
|
|
||
| defblock MyBlock, leaky_relu(x, opts \\\\ []) do | ||
| opts = keyword!(opts, alpha: 1.0e-2) | ||
| Nx.select(Nx.greater(x, 0), x, x * opts[:alpha]) | ||
| end | ||
|
|
||
| All layers and activations are implemented as `defblock`, namespaced under this | ||
| module. Library users can use this to override default Axon implementations with | ||
| their own custom kernels. | ||
| """ | ||
|
|
||
| @doc """ | ||
| Defines a block under the given module `prefix`. | ||
| """ | ||
| defmacro defblock(prefix, call, do: body) do | ||
| build(__CALLER__, prefix, call, body) | ||
| end | ||
|
|
||
| defp build(env, prefix_ast, call, body) do | ||
| {name, args} = parse_call(call, env) | ||
| vars = Enum.map(args, &arg_var/1) | ||
| prefix = expand_prefix(prefix_ast, env) | ||
|
|
||
| camelized = name |> Atom.to_string() |> Macro.camelize() | ||
| struct_module = Module.concat(prefix, camelized) | ||
|
|
||
| quote do | ||
| defmodule unquote(struct_module) do | ||
| @moduledoc false | ||
| defstruct [] | ||
| end | ||
|
|
||
| defn unquote(name)(unquote_splicing(args)) do | ||
| Nx.block( | ||
| %unquote(struct_module){}, | ||
| [unquote_splicing(vars)], | ||
| nil, | ||
| fn _struct, unquote_splicing(vars) -> | ||
| unquote(body) | ||
| end | ||
| ) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| defp parse_call({name, _meta, args}, _env) when is_atom(name) and is_list(args) do | ||
| {name, args} | ||
| end | ||
|
|
||
| defp parse_call({name, _meta, nil}, _env) when is_atom(name) do | ||
| {name, []} | ||
| end | ||
|
|
||
| defp parse_call(other, env) do | ||
| raise CompileError, | ||
| description: | ||
| "defblock expects a function head like `name(args...)`, got: " <> | ||
| Macro.to_string(other), | ||
| file: env.file, | ||
| line: env.line | ||
| end | ||
|
|
||
| defp arg_var({:\\, _meta, [var, _default]}), do: var | ||
| defp arg_var(var), do: var | ||
|
|
||
| defp expand_prefix(prefix_ast, env) do | ||
| case Macro.expand(prefix_ast, env) do | ||
| mod when is_atom(mod) -> | ||
| mod | ||
|
|
||
| other -> | ||
| raise CompileError, | ||
| description: | ||
| "defblock expects a module prefix, got: " <> Macro.to_string(other), | ||
| file: env.file, | ||
| line: env.line | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@josevalim @seanmor5 wdyt about having a single Axon.Block struct with module, function and arity as the name?
or something like that