-
Notifications
You must be signed in to change notification settings - Fork 284
feat: add direct io backend #681
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
base: main
Are you sure you want to change the base?
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
| 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); |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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
39ecb56 to
f7f5a1b
Compare
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