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

Cuda quantization padding fix. #2504

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions candle-core/src/quantized/cuda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ fn pad(p: usize, q: usize) -> usize {
ceil_div(p, q) * q
}

fn pad_for_alloc(p: usize) -> usize {
// Overallocate by q rather than just padding by q as this should pad the last row
// and we don't have enough information here to know how many elements to add :(
p + MATRIX_ROW_PADDING
}

fn quantize_q8_1(
src: &CudaView<f32>,
dst: &mut CudaSlice<u8>,
Expand Down Expand Up @@ -444,8 +450,11 @@ impl QCudaStorage {
let mut qcpu_storage = crate::Device::Cpu.qzeros(src_len, self.dtype)?;
qcpu_storage.quantize(&src)?;
let data = qcpu_storage.data()?;
let data = self.device.htod_sync_copy(data.as_ref()).w()?;
self.data = data;
let mut dst = self.device.alloc_zeros::<u8>(pad_for_alloc(src_len)).w()?;
self.device
.htod_sync_copy_into(data.as_ref(), &mut dst.slice_mut(..src_len))
.w()?;
self.data = dst;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion candle-core/src/quantized/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(super) fn group_for_quantization<'a, 'b, T: super::k_quants::GgmlType>(
let actual_blocks = ys.len();

// Validate that the input is the right size
if expected_blocks != actual_blocks {
if expected_blocks > actual_blocks {
crate::bail!("quantize {dtype:?}: expected {expected_blocks} blocks but only {actual_blocks} were provided!")
}

Expand Down
Loading