-
Notifications
You must be signed in to change notification settings - Fork 19
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
Add initial IFRT Julia bindings #738
Open
mofeing
wants to merge
4
commits into
main
Choose a base branch
from
ss/ifrt-julia-bindings
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
4 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 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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
mutable struct Client | ||
client::Ptr{Cvoid} | ||
global_ordinals::Vector{Cint} | ||
holded::Ptr{Cvoid} | ||
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. same comment here above having a separation ideally we have
|
||
|
||
function Client(client::Ptr{Cvoid}) | ||
@assert client != C_NULL | ||
global_ordinals = Cint[] | ||
|
||
client = new(client, global_ordinals) | ||
client = new(client, global_ordinals, C_NULL) | ||
|
||
# https://github.com/pytorch/xla/blob/8b2414094578e829b99a8383877c86d357eeb682/torch_xla/csrc/runtime/pjrt_computation_client.cc#L127 | ||
devices = [ | ||
|
@@ -29,7 +30,20 @@ end | |
Base.:(==)(a::Client, b::Client) = a.client == b.client | ||
|
||
@inline function free_client(client::Client) | ||
@ccall MLIR.API.mlir_c.FreeClient(client.client::Ptr{Cvoid})::Cvoid | ||
if client.holded == C_NULL | ||
@ccall MLIR.API.mlir_c.FreeClient(client.client::Ptr{Cvoid})::Cvoid | ||
else | ||
@ccall MLIR.API.mlir_c.reactant_release_pjrtclient(client.holded::Ptr{Cvoid})::Cvoid | ||
end | ||
end | ||
|
||
function hold!(client::Client) | ||
if client.holded == C_NULL | ||
client.holded = @ccall MLIR.API.mlir_c.reactant_hold_pjrtclient( | ||
client.client::Ptr{Cvoid} | ||
)::Ptr{Cvoid} | ||
end | ||
return client | ||
end | ||
|
||
function ClientNumDevices(client::Client) | ||
|
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,38 @@ | ||
@cenum ArrayCopySemantics::UInt32 begin | ||
AlwaysCopy = 0 | ||
ReuseInput = 1 | ||
DonateInput = 2 | ||
end | ||
|
||
# currently, only supports IFRT-PjRt | ||
mutable struct Array | ||
ptr::Ptr{Cvoid} | ||
|
||
function Array(ptr::Ptr{Cvoid}) | ||
@assert ptr != C_NULL | ||
return finalizer(free_array, new(ptr)) | ||
end | ||
end | ||
|
||
function free_array(array) | ||
@ccall MLIR.API.mlir_c.reactant_release_ifrt_array(array.ptr::Ptr{Cvoid})::Cvoid | ||
end | ||
|
||
function Array(client::Client, buffer::XLA.Buffer) | ||
hold!(buffer) | ||
GC.@preserve client buffer begin | ||
return Array( | ||
@ccall MLIR.API.mlir_c.ifrt_pjrt_ArrayFromHostBuffer( | ||
client.ptr::Ptr{Cvoid}, buffer.holded::Ptr{Cvoid} | ||
)::Ptr{Cvoid} | ||
) | ||
end | ||
end | ||
|
||
function CopyArrayToHostBuffer(array::Array, data) | ||
GC.@preserve array data begin | ||
@ccall MLIR.API.mlir_c.ifrt_CopyArrayToHostBuffer( | ||
array.ptr::Ptr{Cvoid}, data::Ptr{Cvoid}, AlwaysCopy::Cuint | ||
)::Cvoid | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# currently, only supports IFRT-PjRt | ||
mutable struct Client | ||
ptr::Ptr{Cvoid} | ||
|
||
function Client(ptr::Ptr{Cvoid}) | ||
@assert ptr != C_NULL | ||
return finalizer(free_client, new(ptr)) | ||
end | ||
end | ||
|
||
function Client(pjrt_client::XLA.Client) | ||
# it needs a `std::shared_ptr<xla::PjRtClient>` | ||
hold!(pjrt_client) | ||
return Client( | ||
@ccall MLIR.API.mlir_c.ifrt_pjrt_MakeClient( | ||
pjrt_client.holded::Ptr{Cvoid} | ||
)::Ptr{Cvoid} | ||
) | ||
end | ||
|
||
function free_client(client) | ||
@ccall MLIR.API.mlir_c.ifrt_FreeClient(client.ptr::Ptr{Cvoid})::Cvoid | ||
end | ||
|
||
function compile(client::Client, code::MLIR.IR.Module) | ||
return LoadedExecutable( | ||
@ccall MLIR.API.mlir_c.ifrt_ClientCompile( | ||
client.ptr::Ptr{Cvoid}, code.module_::MLIR.API.MlirModule | ||
)::Ptr{Cvoid} | ||
) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module IFRT | ||
|
||
using CEnum | ||
|
||
import ..XLA | ||
import .XLA: hold! | ||
import ..MLIR | ||
|
||
include("LoadedExecutable.jl") | ||
include("Client.jl") | ||
include("Array.jl") | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# currently, only supports IFRT-PjRt | ||
mutable struct LoadedExecutable | ||
ptr::Ptr{Cvoid} | ||
|
||
function LoadedExecutable(ptr::Ptr{Cvoid}) | ||
@assert ptr != C_NULL | ||
return finalizer(free_exec, new(ptr)) | ||
end | ||
end | ||
|
||
@inline function free_exec(exec) | ||
@ccall MLIR.API.mlir_c.ifrt_pjrt_FreeLoadedExecutable(exec.ptr::Ptr{Cvoid})::Cvoid | ||
end | ||
|
||
function execute( | ||
exec::LoadedExecutable, | ||
args::NTuple{N,Ptr{Cvoid}}, | ||
donated_mask::NTuple{N,UInt8}, | ||
::Val{n_results}, | ||
) where {N,n_results} | ||
results = Ref{NTuple{n_results,Ptr{Cvoid}}}() | ||
has_future = Ref{UInt8}() | ||
status = Ref{NTuple{1,Ptr{Cvoid}}}() # unused right now | ||
|
||
args = Base.RefValue(args) | ||
donated_mask = Base.RefValue(donated_mask) | ||
|
||
GC.@preserve exec args donated_mask results has_future status begin | ||
@ccall MLIR.API.mlir_c.ifrt_Execute( | ||
exec.ptr::Ptr{Cvoid}, | ||
N::Cint, | ||
args::Ptr{Cvoid}, | ||
donated_mask::Ptr{Cvoid}, | ||
n_results::Cint, | ||
Base.unsafe_convert(Ptr{Cvoid}, results)::Ptr{Cvoid}, | ||
has_future::Ptr{Cvoid}, | ||
status::Ptr{Cvoid}, | ||
)::Cvoid | ||
end | ||
|
||
@assert has_future[] == true | ||
|
||
results = results[] | ||
|
||
return ntuple(Val(n_results)) do i | ||
return Array(results[i]) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,4 +96,6 @@ function __init__() | |
return nothing | ||
end | ||
|
||
include("IFRT/IFRT.jl") | ||
|
||
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.
do we need this?
In essence I feel like we should consider buffer and heldbuffer as two totally different API's. Of course under the hood technically one can be converted to the other, but we shouldn't merge them unless needed. This will make it easier to transition the rest of the code from PJRT -> IFRT (and also the inner buffers having a union will make them slow)
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.
sure, I agree with your view that it would be cleaner and more performant. but I'm worried about destruction of the objects and having "use after free" bugs. In this transition period, we will have both
Buffer
andHeldBuffer
.if
Buffer
takes ownership of it, then the underlyingxla::PjRtBuffer
will be freed whenBuffer
is GCed. if we have aHeldBuffer
around, it will be broken because the ptr to which theshared_ptr
tries to point will be already freed.consider the opposite:
HeldBuffer
takes ownership of it. the same problem applies:HeldBuffer
can be GCed beforeBuffer
andBuffer
will be broken because the pointer will already be freed if you try to use it again.here is the dependency graph: both
Buffer
andHeldBuffer
have references to the samexla::PjRtBuffer
object.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.
IMO in order to avoid these issues, the first implementation was best:
HeldBuffer
andBuffer
are separate types which makes incremental transition easierThere 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.
we should never have a heldbuffer which points to the same data as a regular buffer. Each should be considered an exclusive owner of its underyling data.
And analagously we should avoid making a held buffer out of an existing pjrt buffer
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.
that way there would never be any use after free issues
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.
that means that we would be duplicating data, because
PjRtBuffer
holds raw data, which will be costlymmmm or we will need to replicate stuff for
HeldBuffer
. lemme try this weekend