-
Notifications
You must be signed in to change notification settings - Fork 205
Add NIF for loading custom plugins #1519
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
3
commits into
main
Choose a base branch
from
sm-plugins
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.
Open
Changes from all commits
Commits
Show all changes
3 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
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
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
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,56 @@ | ||
defmodule EXLA.Plugin do | ||
@moduledoc """ | ||
Plugin system for registering custom calls. | ||
""" | ||
use GenServer | ||
|
||
# TODO: Register and lookup per client | ||
|
||
def start_link(_opts) do | ||
GenServer.start_link(__MODULE__, %{}, name: __MODULE__) | ||
end | ||
|
||
def register(key, library_path) do | ||
GenServer.cast(__MODULE__, {:register, key, library_path}) | ||
end | ||
|
||
def lookup(key) do | ||
GenServer.call(__MODULE__, {:lookup, key}) | ||
end | ||
|
||
def register_symbol(key, symbol, dimensions) do | ||
if ref = lookup(key) do | ||
EXLA.NIF.register_custom_call_symbol(ref, symbol, dimensions) | ||
end | ||
end | ||
|
||
@impl true | ||
def init(_opts) do | ||
{:ok, %{}} | ||
end | ||
|
||
@impl true | ||
def handle_cast({:register, key, library_path}, state) do | ||
case state do | ||
%{^key => _ref} -> | ||
{:noreply, state} | ||
|
||
%{} -> | ||
ref = | ||
library_path | ||
|> EXLA.NIF.load_custom_call_plugin_library() | ||
|> unwrap!() | ||
|
||
{:noreply, Map.put(state, key, ref)} | ||
end | ||
end | ||
|
||
@impl true | ||
def handle_call({:lookup, key}, _from, state) do | ||
value = Map.get(state, key) | ||
{:reply, value, state} | ||
end | ||
|
||
defp unwrap!({:ok, ref}), do: ref | ||
defp unwrap!({:error, reason}), do: raise("#{reason}") | ||
end |
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,9 @@ | ||
defmodule EXLA.PluginTest do | ||
use ExUnit.Case | ||
|
||
describe "register/1" do | ||
test "registers a plugin" do | ||
assert :ok = EXLA.Plugin.register(:custom_plugin, "test/support/c/libcustom_plugin.so") | ||
end | ||
end | ||
end |
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,22 @@ | ||
#include <cstdint> | ||
#include <stddef.h> | ||
|
||
typedef void (*ExlaCustomCallFunction)(void *out[], const void *in[], int **dims); | ||
|
||
typedef struct { | ||
const char* name; | ||
ExlaCustomCallFunction func; | ||
} ExlaPluginCustomCall; | ||
|
||
extern "C" void custom_increment(void *out[], const void *in[], int **dims) { | ||
int64_t *operand = (int64_t *)in[0]; | ||
int64_t *dim_sizes = (int64_t *)dims[0]; | ||
|
||
int64_t *out_buffer = (int64_t *)out[0]; | ||
|
||
int64_t n = dim_sizes[0]; | ||
|
||
for (int64_t i = 0; i < n; i++) { | ||
out_buffer[i] = operand[i] + 1; | ||
} | ||
} |
Binary file not shown.
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.
Should we use a process instead such that, if someone does
Application.stop(:exla)
the process is shutdown as well as all plugins? 🤔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.
Regarding how to store things, I would rather do an ETS than a process for storing the custom call registry if persistent term is not what we want, to keep close to the same read concurrency.
Another possible alternative would be a GenServer that manages the persistent term on terminate, it would clean up the persitent term state.
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.
In this manager alternative, we'd have non-process functions that read first and write if needed to the persistent term, and the only purpose for the GenServer would be to ensure cleanup upon termination.
PS: Upon writing this I went reading and found https://hexdocs.pm/elixir/1.12/Application.html#c:prep_stop/1 which would serve this purpose nicely.
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.
prep_stop also works but you would need to iterate all persistent term to find the keys relevant to us. ETS would be better.
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.
If there's something like EXLA.CustomCalls.cleanup/0 we could call, then that function will already know about all of the keys that should be cleaned up.
I see no issue with using ETS however, as the speed difference here only matters at defn compile time and not runtime