-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to read tensors lazily (#9)
- Loading branch information
1 parent
2287789
commit 5d530c7
Showing
5 changed files
with
116 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule Safetensors.FileTensor do | ||
@moduledoc false | ||
|
||
defstruct [:shape, :type, :path, :byte_offset, :byte_size] | ||
end | ||
|
||
defimpl Nx.LazyContainer, for: Safetensors.FileTensor do | ||
def traverse(lazy_tensor, acc, fun) do | ||
template = Nx.template(lazy_tensor.shape, lazy_tensor.type) | ||
|
||
load = fn -> | ||
File.open!(lazy_tensor.path, [:read, :raw], fn file -> | ||
{:ok, binary} = :file.pread(file, lazy_tensor.byte_offset, lazy_tensor.byte_size) | ||
Safetensors.Shared.build_tensor(binary, lazy_tensor.shape, lazy_tensor.type) | ||
end) | ||
end | ||
|
||
fun.(template, load, acc) | ||
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,36 @@ | ||
defmodule Safetensors.Shared do | ||
@moduledoc false | ||
|
||
@doc """ | ||
Builds Nx tensor from the given safetensors binary. | ||
""" | ||
@spec build_tensor(binary(), tuple(), Nx.Type.t()) :: Nx.Tensor.t() | ||
def build_tensor(binary, shape, type) do | ||
{_, elem_size} = type | ||
|
||
binary | ||
|> new_byte_order(elem_size, :little) | ||
|> IO.iodata_to_binary() | ||
|> Nx.from_binary(type) | ||
|> Nx.reshape(shape) | ||
end | ||
|
||
@doc """ | ||
Changes endianness `binary` if `endianness` does not match system. | ||
""" | ||
@spec new_byte_order(binary(), pos_integer(), :little | :big) :: iodata() | ||
def new_byte_order(binary, size, endianness) do | ||
if System.endianness() == endianness do | ||
binary | ||
else | ||
data = | ||
for <<data::size(size)-binary <- binary>> do | ||
data | ||
|> :binary.decode_unsigned() | ||
|> :binary.encode_unsigned(endianness) | ||
end | ||
|
||
IO.iodata_to_binary(data) | ||
end | ||
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
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