Draft
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
3639ca7 to
9fc431e
Compare
danieldk
reviewed
Dec 5, 2025
Comment on lines
+496
to
+508
| let aligned_prefix_len = file_len / ALIGN * ALIGN; | ||
| let tail_len = file_len - aligned_prefix_len; | ||
|
|
||
| let aligned_capacity = file_len.checked_next_multiple_of(ALIGN).ok_or_else(|| { | ||
| SafetensorError::new_err("overflow while computing aligned file len") | ||
| })?; | ||
| let layout = Layout::from_size_align(aligned_capacity, ALIGN) | ||
| .map_err(|_| SafetensorError::new_err("invalid layout"))?; | ||
| let ptr = alloc(layout); | ||
| if ptr.is_null() { | ||
| handle_alloc_error(layout); | ||
| } | ||
| let mut vec: Vec<u8> = Vec::from_raw_parts(ptr, 0, aligned_capacity); |
Member
There was a problem hiding this comment.
I might be mistaken, but I think Vec uses the alignment of the type, so you can do this more safely using something like:
let mut vec: Vec<[u8; ALIGN]> = Vec::with_capacity((file_len + ALIGN - 1) / ALIGN);
// Use vec as u8 rather than [u8; ALIGN].
|
|
||
| let mut handles = Vec::new(); | ||
|
|
||
| for (tid, chunk) in buffer.chunks_mut(region_bytes).enumerate() { |
Member
There was a problem hiding this comment.
Question for understanding, since region_bytes = blocks_per_thread * block_size. Suppose we have 16 blocks in the file and 4 threads. Then the responsibility by thread is:
0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3
I am curious how this compares to patterns like:
0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
39ecb56 to
f7f5a1b
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Experimental changes to file loading
Warning
sorry for the messy thought dump you're about to read
Still measuring performance benefits. Probably implemented the wrong approach (full file loading in host memory), the goal with this impl is to try saturating disk read bandwidth on an nvme.
Running
pytest benches/test_pt.py -k test_pt_sf_load_directyields poor performance atm, but it's hard to compare to saytest_pt_sf_load_cpugiven os page cache is populated when writing the file to disk, which is done right before thebenchmark(load_file, ...)call.I want to continue experimenting with this loading method, perhaps instead imitating what is done in fastsafetensors'
nogds_file_readerwhere they have a bounce buffer allocated withcudaHostAllocfrom which they runcudaMemcpyonce theypreadmemory in said buffer.I'd also like to try playing around with creating a
safetensors-distributedpackage that'd be built on top of this infrastructure layer for fast loading of files in tensor parallel scenarios. We'll probably want to port over some of the code in transformers in that package.I'm not convinced we want to imitate fastsafetensors fully, where they scatter and broadcast tensors from gpu memory leveraging nvlink (iiuc). But at the same time, I'm not sure either we can slice the file like we currently do in our distributed weight loader in transformers while reading file in the bounce buffer, we want reads to be aligned when reading with
O_DIRECTand this will get messy when sending over bytes to device. Need to investigate some more 🤕References