]> khamidou — GitHub mirrors - safetensors.git/commit
[Major Change] Enforcing tensor alignment (#148)
authorNicolas Patry <patry.nicolas@protonmail.com>
Tue, 21 Feb 2023 14:23:56 +0000 (15:23 +0100)
committerGitHub <noreply@github.com>
Tue, 21 Feb 2023 14:23:56 +0000 (15:23 +0100)
commit0c5b3a6b9c9be653b91ce8355225b8cad074f8b0
tree4ea554c9d19f3f89f35d5072545a10e2c4c4356a
parent443152565fe803138d0575b77c8e8ad651e985db
[Major Change] Enforcing tensor alignment (#148)

* [Major Change] Enforcing tensor alignment

- Now the header will automatically align itself to 8 bytes (f64) with
  appending extra spaces as necessary.
- This will allow extra fast memory mapping by reinterpreting bytes as
  f32/f64 etc.. Unaligned bytes do not allow for this. https://www.reddit.com/r/rust/comments/tanaxm/mutating_a_buffer_of_u8s_as_f32s_in_place/
- This does not change contiguousness of tensors
- This does not change the actual spec (we're just putting extra valid bytes
  in the header and using a different serialization ordering)
- Readers should still be able to read old files, they would just need
  to be copied before being cast as their final destination when using
  mmap
- This has no effect for GPU since copy is already necessary (*I think*,
  depends on the cuda API actually if it allows filling f32 addresses
  from raw unaligned bytes).

This change will only be interesting if things like https://github.com/Narsil/fast_gpt2
actually pick up. And even with the copy, load times are still vastly
superior to `pytorch`.

We need to be able to read old files.

* Fixup.

* Clippy fix.

* Cargo fmt (clippy --fix broke it ? :( )
bindings/python/src/lib.rs
bindings/python/tests/test_pt_comparison.py
bindings/python/tests/test_simple.py
safetensors/benches/benchmark.rs
safetensors/src/slice.rs
safetensors/src/tensor.rs