Skip to content
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

Efficiently read safetensors from disk #4

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 48 additions & 18 deletions lib/safetensors.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@ defmodule Safetensors do
]
end

@doc """
Reads a safe tensor from file.

Tensors are loaded into Nx one by one,
without loading the whole file into disk.
"""
@spec read!(path :: iodata()) :: %{String.t() => Nx.Tensor.t()}
josevalim marked this conversation as resolved.
Show resolved Hide resolved
def read!(path) do
File.open!(path, [:read, :raw], fn file ->
{:ok, <<header_size::unsigned-64-integer-little>>} = :file.read(file, 8)
{:ok, header_json} = :file.read(file, header_size)

header = decode_header!(header_json)

for {tensor_name, tensor_info} <- header, into: %{} do
%{"data_offsets" => [offset_start, offset_end]} = tensor_info

{:ok, binary} =
:file.pread(file, header_size + 8 + offset_start, offset_end - offset_start)

{tensor_name, build_tensor(binary, tensor_info)}
end
end)
end

@doc """
Loads a serialized map of tensors.

Expand All @@ -100,34 +125,39 @@ defmodule Safetensors do
buffer::binary
>> = data

{_metadata, header} =
header_json
|> Jason.decode!()
|> Map.pop(@header_metadata_key)
header = decode_header!(header_json)

for {tensor_name, tensor_info} <- header, into: %{} do
%{
"data_offsets" => [offset_start, offset_end],
"dtype" => dtype,
"shape" => shape
} = tensor_info

{_, elem_size} = type = dtype_to_type(dtype)
%{"data_offsets" => [offset_start, offset_end]} = tensor_info

binary =
tensor =
buffer
|> binary_slice(offset_start, offset_end - offset_start)
|> new_byte_order(elem_size, :little)

tensor =
binary
|> Nx.from_binary(type)
|> Nx.reshape(List.to_tuple(shape))
|> build_tensor(tensor_info)

{tensor_name, tensor}
end
end

defp decode_header!(header_json) do
{_metadata, header} =
header_json
|> Jason.decode!()
|> Map.pop(@header_metadata_key)

header
end

defp build_tensor(binary, tensor_info) do
%{"dtype" => dtype, "shape" => shape} = tensor_info
{_, elem_size} = type = dtype_to_type(dtype)

binary
|> new_byte_order(elem_size, :little)
|> Nx.from_binary(type)
|> Nx.reshape(List.to_tuple(shape))
end

defp type_to_dtype(type) do
@type_to_dtype[type] || raise "unrecognized type #{inspect(type)}"
end
Expand Down
14 changes: 14 additions & 0 deletions test/safetensors_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ defmodule SafetensorsTest do
~s(<\x00\x00\x00\x00\x00\x00\x00{"test":{"dtype":"I32","shape":[2,2],"data_offsets":[0,16]}}\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00)
end

@tag :tmp_dir
test "read", %{tmp_dir: tmp_dir} do
path = Path.join(tmp_dir, "safetensor")

# source:
# https://github.com/huggingface/safetensors/blob/1a65a3fdebcf280ef0ca32934901d3e2ad3b2c65/bindings/python/tests/test_simple.py#L35-L40
File.write!(
path,
~s(<\x00\x00\x00\x00\x00\x00\x00{"test":{"dtype":"I32","shape":[2,2],"data_offsets":[0,16]}}\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00)
)

assert Safetensors.read!(path) == %{"test" => Nx.tensor([[0, 0], [0, 0]], type: :s32)}
end

test "load" do
# source:
# https://github.com/huggingface/safetensors/blob/1a65a3fdebcf280ef0ca32934901d3e2ad3b2c65/bindings/python/tests/test_simple.py#L35-L40
Expand Down