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

Slice loading errors with integer indices #439

Closed
2 tasks done
hct-synth opened this issue Feb 14, 2024 · 3 comments · Fixed by #440
Closed
2 tasks done

Slice loading errors with integer indices #439

hct-synth opened this issue Feb 14, 2024 · 3 comments · Fixed by #440

Comments

@hct-synth
Copy link

System Info

safetensors==0.4.2

Information

  • The official example scripts
  • My own modified scripts

Reproduction

from safetensors import safe_open
from safetensors.torch import save_file

import torch

save_file({"test":torch.zeros([10,10])}, "test.st")

with safe_open("test.st", framework="pt", device="cpu") as f:
    print(f.get_slice("test")[0, :])

Expected behavior

I expect this to return a slice of the tensor "test", instead I get the error:

argument 'slices': failed to extract enum Slice ('Slice | Slices')

As far as I can see there's no notes in the documentation or examples about integer slices not working?
https://huggingface.co/docs/safetensors/en/index

@hct-synth
Copy link
Author

NB: For anyone running into this, the workaround is to use a slice width of one, which resolves the issue:
print(f.get_slice("test")[0:1, :]) # WORKS

@Narsil
Copy link
Collaborator

Narsil commented Feb 14, 2024

Thanks a lot for reporting this, I've prepared a fix !

And yes slicing with real slices work, I forgot to add support for real index indexing.
I added support for negative indexing too.

@hct-synth
Copy link
Author

@Narsil - thanks for looking into this! Cool to see a PR so soon.

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 a pull request may close this issue.

2 participants