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

Add option to read tensors lazily #9

Merged
merged 2 commits into from
Feb 23, 2024
Merged

Add option to read tensors lazily #9

merged 2 commits into from
Feb 23, 2024

Conversation

jonatanklosko
Copy link
Member

Safetensors.read!(path, lazy: true is going to return a map, where keys are lazy containers. Calling Nx.to_tensor the container opens the file, reads the corresponding chunk and builds tensor.

lib/safetensors.ex Outdated Show resolved Hide resolved
lib/safetensors.ex Outdated Show resolved Hide resolved
assert %{"test" => %Safetensors.FileTensor{} = file_tensor} =
Safetensors.read!(path, lazy: true)

assert Nx.to_tensor(file_tensor) == Nx.tensor([[0, 0], [0, 0]], type: :s32)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat 👏

@jonatanklosko jonatanklosko merged commit 5d530c7 into main Feb 23, 2024
2 checks passed
@jonatanklosko jonatanklosko deleted the jk-lazy-read branch February 23, 2024 06:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants