-
Notifications
You must be signed in to change notification settings - Fork 199
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
feat: add to/from pointer #1473
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c8ae869
feat: proof of concept with cuda pointers
polvalente c870751
feat: support host
polvalente 195f471
feat: support non-cuda enabled compilation
polvalente 6bbed1d
feat: add user-facing functions for pointer manip
polvalente 2c2fb28
chore: remove example script
polvalente d4535b9
fix: makefile
polvalente 3538b4b
chore: update nx
polvalente cad3f66
fix: missing flag
polvalente d56c0af
chore: rename exla_cuda
polvalente 71217fd
fix: extension
polvalente 681f436
fix: missing include
polvalente d2d5a65
chore: changes due to code review
polvalente 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 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 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 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 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,55 @@ | ||
#include "exla_cuda.h" | ||
|
||
#ifdef CUDA_ENABLED | ||
#include <cuda_runtime.h> | ||
|
||
#include <cstring> | ||
#include <iostream> | ||
|
||
std::pair<std::vector<unsigned char>, int> get_cuda_ipc_handle(std::uintptr_t ptr) { | ||
cudaIpcMemHandle_t ipc_handle; | ||
cudaError_t status = cudaIpcGetMemHandle(&ipc_handle, reinterpret_cast<void*>(ptr)); | ||
|
||
// Assuming sizeof(cudaIpcMemHandle_t) is constant | ||
const size_t size = sizeof(cudaIpcMemHandle_t); | ||
|
||
// Copy the memory handle to a byte array | ||
std::vector<unsigned char> result(size); | ||
memcpy(result.data(), &ipc_handle, size); | ||
|
||
return std::make_pair(result, status != cudaSuccess); | ||
} | ||
|
||
std::pair<void*, int> get_pointer_for_ipc_handle(std::vector<int64_t> handle_list) { | ||
unsigned char ipc_handle_data[sizeof(cudaIpcMemHandle_t)]; | ||
for (int i = 0; i < sizeof(cudaIpcMemHandle_t); i++) { | ||
ipc_handle_data[i] = (uint8_t)handle_list[i]; | ||
} | ||
|
||
cudaIpcMemHandle_t ipc_handle; | ||
memcpy(&ipc_handle, ipc_handle_data, sizeof(cudaIpcMemHandle_t)); | ||
|
||
int* ptr; | ||
cudaError_t cuda_status = cudaSetDevice(0); // Assuming device 0, change as needed | ||
if (cuda_status != cudaSuccess) { | ||
printf("Error setting CUDA device: %s\n", cudaGetErrorString(cuda_status)); | ||
return std::make_pair(nullptr, 1); // Return with error status | ||
} | ||
|
||
cuda_status = cudaIpcOpenMemHandle((void**)&ptr, ipc_handle, cudaIpcMemLazyEnablePeerAccess); | ||
if (cuda_status != cudaSuccess) { | ||
printf("Error opening CUDA IPC memory handle: %s\n", cudaGetErrorString(cuda_status)); | ||
return std::make_pair(nullptr, 1); // Return with error status | ||
} | ||
|
||
return std::make_pair(ptr, cuda_status != cudaSuccess); | ||
} | ||
#else | ||
std::pair<std::vector<unsigned char>, int> get_cuda_ipc_handle(std::uintptr_t ptr) { | ||
return std::make_pair(std::vector<unsigned char>(0), 1); | ||
} | ||
|
||
std::pair<void*, int> get_pointer_for_ipc_handle(std::vector<int64_t> handle_list) { | ||
return std::make_pair(nullptr, 1); | ||
} | ||
#endif |
This file contains 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,7 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <vector> | ||
|
||
std::pair<std::vector<unsigned char>, int> get_cuda_ipc_handle(std::uintptr_t); | ||
std::pair<void*, int> get_pointer_for_ipc_handle(std::vector<int64_t>); |
This file contains 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 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 |
---|---|---|
|
@@ -82,6 +82,70 @@ defmodule EXLA.Backend do | |
EXLA.DeviceBuffer.deallocate(buffer) | ||
end | ||
|
||
@impl true | ||
def to_pointer(%T{data: %B{buffer: buffer}}, opts \\ []) do | ||
opts = Keyword.validate!(opts, mode: :local) | ||
|
||
mode = | ||
case opts[:mode] do | ||
mode when mode in [:local, :cuda_ipc] -> | ||
mode | ||
|
||
mode -> | ||
raise ArgumentError, "expected one of :local, :cuda_ipc, got: #{inspect(mode)}" | ||
end | ||
|
||
case buffer do | ||
%EXLA.DeviceBuffer{} -> | ||
:ok | ||
|
||
_ -> | ||
raise ArgumentError, "tensor must be allocated via a #{DeviceBuffer}" | ||
end | ||
|
||
client = EXLA.Client.fetch!(buffer.client_name) | ||
|
||
case EXLA.NIF.get_buffer_device_pointer(client.ref, buffer.ref, mode) do | ||
{:ok, {pointer, _size}} -> | ||
{:ok, pointer} | ||
|
||
error -> | ||
error | ||
end | ||
end | ||
|
||
@impl true | ||
def from_pointer(pointer, type, dims, opts) do | ||
template = Nx.template(dims, type, names: opts[:names]) | ||
|
||
opts = Keyword.validate!(opts[:backend_opts] || [], [:client_name, :device_id, mode: :local]) | ||
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. As commented below, this is mixing backend options (such as client name) with pointer options. We probably should treat them separately. |
||
|
||
client_name = opts[:client_name] || EXLA.Client.default_name() | ||
client = EXLA.Client.fetch!(client_name) | ||
|
||
device_id = opts[:device_id] || client.default_device_id | ||
|
||
shape = EXLA.Shape.make_shape(type, dims) | ||
|
||
result = | ||
EXLA.NIF.create_buffer_from_device_pointer( | ||
client.ref, | ||
pointer, | ||
opts[:mode], | ||
shape.ref, | ||
device_id | ||
) | ||
|
||
case result do | ||
{:ok, ref} -> | ||
buffer = EXLA.DeviceBuffer.from_ref(ref, client, device_id, shape) | ||
{:ok, %{template | data: %EXLA.Backend{buffer: buffer}}} | ||
|
||
error -> | ||
error | ||
end | ||
end | ||
|
||
@impl true | ||
def to_batched(out, tensor, opts) do | ||
leftover = opts[:leftover] | ||
|
This file contains 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 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 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,21 @@ | ||
defmodule EXLA.DeviceMemorySharingTest do | ||
use EXLA.Case, async: false | ||
|
||
@moduletag :cuda_required | ||
|
||
test "buffer sharing works as expected" do | ||
t1 = Nx.tensor([1, 2, 3], backend: {EXLA.Backend, client: :cuda}) | ||
|
||
assert inspect(t1) =~ "1, 2, 3" | ||
|
||
assert {:ok, pointer} = Nx.to_pointer(t1, mode: :local) | ||
|
||
assert {:ok, t2} = | ||
Nx.from_pointer(EXLA.Backend, pointer, t1.type, t1.shape, | ||
backend_opts: [client_name: :cuda] | ||
) | ||
|
||
assert t1.data.buffer.ref != t2.data.buffer.ref | ||
assert Nx.to_binary(t1) == Nx.to_binary(t2) | ||
end | ||
end |
This file contains 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
Oops, something went wrong.
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.