Skip to content

Conversation

@McPatate
Copy link
Member

@McPatate McPatate commented Dec 3, 2025

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_direct yields poor performance atm, but it's hard to compare to say test_pt_sf_load_cpu given os page cache is populated when writing the file to disk, which is done right before the benchmark(load_file, ...) call.

I want to continue experimenting with this loading method, perhaps instead imitating what is done in fastsafetensors' nogds_file_reader where they have a bounce buffer allocated with cudaHostAlloc from which they run cudaMemcpy once they pread memory in said buffer.

I'd also like to try playing around with creating a safetensors-distributed package 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_DIRECT and this will get messy when sending over bytes to device. Need to investigate some more 🤕

References

@McPatate McPatate requested a review from danieldk December 3, 2025 16:56
@HuggingFaceDocBuilderDev

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.

@McPatate McPatate force-pushed the feat/direct_io_backend branch from 3639ca7 to 9fc431e Compare December 3, 2025 18:05
Comment on lines +496 to +513
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);
Copy link
Member

Choose a reason for hiding this comment

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

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() {
Copy link
Member

Choose a reason for hiding this comment

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

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 

@McPatate McPatate force-pushed the feat/direct_io_backend branch from 39ecb56 to f7f5a1b Compare December 8, 2025 14:46
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.

4 participants