-
Notifications
You must be signed in to change notification settings - Fork 220
Introduce buffer donation construct #1755
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
Draft
seanmor5
wants to merge
1
commit into
main
Choose a base branch
from
sm-buffer-donation
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.
Draft
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| defmodule EXLA.Defn.DonationTest do | ||
| use EXLA.Case, async: true | ||
|
|
||
| alias EXLA.DeviceBuffer | ||
|
|
||
| defp on_device(value) do | ||
| Nx.backend_transfer(Nx.tensor(value), {EXLA.Backend, client: :host}) | ||
| end | ||
|
|
||
| describe "donate_argnums" do | ||
| test "donates a single positional argument and consumes its buffer" do | ||
| x = on_device([1, 2, 3, 4]) | ||
| %EXLA.Backend{buffer: %DeviceBuffer{} = orig} = x.data | ||
|
|
||
| fun = EXLA.jit(&Nx.add(&1, 1), donate_argnums: [0]) | ||
| result = fun.(x) | ||
|
|
||
| assert Nx.to_flat_list(result) == [2, 3, 4, 5] | ||
|
|
||
| assert_raise RuntimeError, ~r"called on deleted or donated buffer", fn -> | ||
| DeviceBuffer.read(orig) | ||
| end | ||
| end | ||
|
|
||
| test "donates both args of a two-arg function" do | ||
| x = on_device([1, 2, 3]) | ||
| y = on_device([10, 20, 30]) | ||
| %EXLA.Backend{buffer: %DeviceBuffer{} = xb} = x.data | ||
| %EXLA.Backend{buffer: %DeviceBuffer{} = yb} = y.data | ||
|
|
||
| fun = EXLA.jit(&{Nx.add(&1, &2), Nx.subtract(&1, &2)}, donate_argnums: [0, 1]) | ||
| {sum, diff} = fun.(x, y) | ||
|
|
||
| assert Nx.to_flat_list(sum) == [11, 22, 33] | ||
| assert Nx.to_flat_list(diff) == [-9, -18, -27] | ||
|
|
||
| assert_raise RuntimeError, ~r"called on deleted or donated buffer", fn -> | ||
| DeviceBuffer.read(xb) | ||
| end | ||
|
|
||
| assert_raise RuntimeError, ~r"called on deleted or donated buffer", fn -> | ||
| DeviceBuffer.read(yb) | ||
| end | ||
| end | ||
|
|
||
| test "donating a composite argument consumes every leaf within it" do | ||
| a = on_device([1, 2]) | ||
| b = on_device([3, 4]) | ||
| %EXLA.Backend{buffer: %DeviceBuffer{} = ab} = a.data | ||
| %EXLA.Backend{buffer: %DeviceBuffer{} = bb} = b.data | ||
|
|
||
| fun = EXLA.jit(fn {l, r} -> {Nx.add(l, 1), Nx.multiply(r, 2)} end, donate_argnums: [0]) | ||
| {l, r} = fun.({a, b}) | ||
|
|
||
| assert Nx.to_flat_list(l) == [2, 3] | ||
| assert Nx.to_flat_list(r) == [6, 8] | ||
|
|
||
| assert_raise RuntimeError, ~r"called on deleted or donated buffer", fn -> | ||
| DeviceBuffer.read(ab) | ||
| end | ||
|
|
||
| assert_raise RuntimeError, ~r"called on deleted or donated buffer", fn -> | ||
| DeviceBuffer.read(bb) | ||
| end | ||
| end | ||
|
|
||
| test "donation does not consume non-donated args" do | ||
| x = on_device([1, 2, 3]) | ||
| y = on_device([10, 20, 30]) | ||
| %EXLA.Backend{buffer: %DeviceBuffer{} = yb} = y.data | ||
|
|
||
| fun = EXLA.jit(&Nx.add(&1, &2), donate_argnums: [0]) | ||
| result = fun.(x, y) | ||
|
|
||
| assert Nx.to_flat_list(result) == [11, 22, 33] | ||
| # `y` was not donated; reading should still succeed. | ||
| assert byte_size(DeviceBuffer.read(yb)) > 0 | ||
| end | ||
|
|
||
| test "raises when no output has a matching shape/dtype" do | ||
| assert_raise ArgumentError, ~r"no output with matching shape", fn -> | ||
| EXLA.jit(&Nx.sum/1, donate_argnums: [0]).(on_device([1, 2, 3, 4])) | ||
| end | ||
| end | ||
|
|
||
| test "raises when an argnum is out of range" do | ||
| assert_raise ArgumentError, ~r":donate_argnums entries must be in the range", fn -> | ||
| EXLA.jit(&Nx.add(&1, 1), donate_argnums: [5]).(on_device([1, 2, 3])) | ||
| end | ||
| end | ||
|
|
||
| test "raises when :donate_argnums is malformed" do | ||
| assert_raise ArgumentError, ~r":donate_argnums must be a list", fn -> | ||
| EXLA.jit(&Nx.add(&1, 1), donate_argnums: [-1]).(on_device([1, 2, 3])) | ||
| end | ||
|
|
||
| assert_raise ArgumentError, ~r":donate_argnums must be a list", fn -> | ||
| EXLA.jit(&Nx.add(&1, 1), donate_argnums: :foo).(on_device([1, 2, 3])) | ||
| end | ||
| end | ||
|
|
||
| test "duplicates in :donate_argnums are deduped silently" do | ||
| x = on_device([1, 2, 3]) | ||
| %EXLA.Backend{buffer: %DeviceBuffer{} = xb} = x.data | ||
|
|
||
| fun = EXLA.jit(&Nx.add(&1, 1), donate_argnums: [0, 0]) | ||
| assert Nx.to_flat_list(fun.(x)) == [2, 3, 4] | ||
|
|
||
| assert_raise RuntimeError, ~r"called on deleted or donated buffer", fn -> | ||
| DeviceBuffer.read(xb) | ||
| end | ||
| end | ||
|
|
||
| test "different donate sets produce distinct cached executables" do | ||
| # Same shape/typespec, but distinct option values must not share the executable. | ||
| x = on_device([1, 2, 3]) | ||
|
|
||
| _ = EXLA.jit(&Nx.add(&1, 1)).(x) | ||
| # If this cached the non-donating executable, the buffer wouldn't be consumed below. | ||
| x2 = on_device([1, 2, 3]) | ||
| %EXLA.Backend{buffer: %DeviceBuffer{} = xb2} = x2.data | ||
|
|
||
| _ = EXLA.jit(&Nx.add(&1, 1), donate_argnums: [0]).(x2) | ||
|
|
||
| assert_raise RuntimeError, ~r"called on deleted or donated buffer", fn -> | ||
| DeviceBuffer.read(xb2) | ||
| end | ||
| 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.
I think the only think that is a bit confusing for users here is that we're exposing the flat arg structure as the argument, while users operate on containers.
Maybe we should push this to Nx for 2 reasons:
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.
Other than this, the PR looks great