-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add serde for embedding nx iree tensors (#8)
- Loading branch information
1 parent
fe2f093
commit 0392d0e
Showing
7 changed files
with
139 additions
and
5 deletions.
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
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,37 @@ | ||
defmodule NxIREE.NativeTest do | ||
use ExUnit.Case, async: true | ||
|
||
test "serializes and deserializes a tensor" do | ||
tensor = Nx.tensor([[[1, 2], [3, 4], [5, 6]]], type: :s32, backend: NxIREE.Tensor) | ||
|
||
{:ok, serialized} = NxIREE.Native.serialize_tensor(tensor.data.ref) | ||
|
||
assert << | ||
type::unsigned-integer-native-size(32), | ||
num_bytes::unsigned-integer-native-size(64), | ||
data::binary-size(num_bytes), | ||
num_dims::unsigned-integer-native-size(64), | ||
dims_bin::bitstring | ||
>> = serialized | ||
|
||
dims = | ||
for <<x::signed-integer-native-size(64) <- dims_bin>> do | ||
x | ||
end | ||
|
||
# the type assertion is really an internal type to iree, | ||
# but we assert on it as a sanity check. | ||
# This can be skipped if needed in the future. | ||
assert Bitwise.band(type, 0xFF) == 32 | ||
assert Bitwise.band(Bitwise.bsr(type, 24), 0xFF) == 0x10 | ||
|
||
assert num_bytes == Nx.byte_size(tensor) | ||
assert data == Nx.to_binary(tensor) | ||
assert num_dims == 3 | ||
assert dims == [1, 3, 2] | ||
|
||
{:ok, deserialized_ref} = NxIREE.Native.deserialize_tensor(serialized) | ||
|
||
assert Nx.to_binary(tensor) == Nx.to_binary(put_in(tensor.data.ref, deserialized_ref)) | ||
end | ||
end |