Skip to content

Commit 5514fd5

Browse files
committed
Add exclude to cutlass
1 parent e8209f3 commit 5514fd5

5 files changed

Lines changed: 83 additions & 6 deletions

File tree

candle-core/src/quantized/k_quants.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,52 @@ pub fn matmul<T: GgmlType>(
23132313
Ok(())
23142314
}
23152315

2316+
pub fn matmul_f16<T: GgmlType>(
2317+
mkn: (usize, usize, usize),
2318+
lhs: &[f16],
2319+
rhs_t: &[T],
2320+
dst: &mut [f16],
2321+
) -> Result<()> {
2322+
let (m, k, n) = mkn;
2323+
if m * k != lhs.len() {
2324+
crate::bail!("unexpected lhs length {} {mkn:?}", lhs.len());
2325+
}
2326+
2327+
let k_in_lhs_blocks = k.div_ceil(T::BLCK_SIZE);
2328+
let k_in_rhs_blocks = k.div_ceil(T::VecDotType::BLCK_SIZE);
2329+
// TODO: Do not make this copy if the DotType is f32.
2330+
// TODO: Pre-allocate this.
2331+
let mut lhs_b = vec![T::VecDotType::zeros(); m * k_in_lhs_blocks];
2332+
for row_idx in 0..m {
2333+
let lhs_b = &mut lhs_b[row_idx * k_in_lhs_blocks..(row_idx + 1) * k_in_lhs_blocks];
2334+
let lhs = &lhs[row_idx * k..(row_idx + 1) * k];
2335+
T::VecDotType::from_float(
2336+
&lhs.into_iter().map(|&x| x.to_f32()).collect::<Vec<_>>(),
2337+
lhs_b,
2338+
)?
2339+
}
2340+
let lhs_b = lhs_b.as_slice();
2341+
2342+
for row_idx in 0..m {
2343+
let lhs_row = &lhs_b[row_idx * k_in_lhs_blocks..(row_idx + 1) * k_in_lhs_blocks];
2344+
let dst_row = &mut dst[row_idx * n..(row_idx + 1) * n];
2345+
2346+
let result: Result<Vec<_>> = dst_row
2347+
.into_par_iter()
2348+
.enumerate()
2349+
.with_min_len(128)
2350+
.with_max_len(512)
2351+
.map(|(col_idx, dst)| {
2352+
let rhs_col = &rhs_t[col_idx * k_in_rhs_blocks..(col_idx + 1) * k_in_rhs_blocks];
2353+
T::vec_dot(k, rhs_col, lhs_row).map(|value| *dst = f16::from_f32(value))
2354+
})
2355+
.collect();
2356+
2357+
result?;
2358+
}
2359+
Ok(())
2360+
}
2361+
23162362
impl GgmlType for f32 {
23172363
const DTYPE: GgmlDType = GgmlDType::F32;
23182364
const BLCK_SIZE: usize = 1;

candle-core/src/quantized/mod.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{CpuStorage, DType, Device, Result, Shape, Storage, Tensor, D};
1+
use crate::{
2+
backend::BackendStorage, CpuStorage, DType, Device, Result, Shape, Storage, Tensor, D,
3+
};
24
use k_quants::*;
35
use std::borrow::Cow;
46

@@ -386,6 +388,7 @@ impl GgmlDType {
386388
pub trait QuantizedType: Send + Sync {
387389
fn dtype(&self) -> GgmlDType;
388390
fn matmul_t(&self, mkn: (usize, usize, usize), lhs: &[f32], dst: &mut [f32]) -> Result<()>;
391+
fn matmul_t_f16(&self, mkn: (usize, usize, usize), lhs: &[f16], dst: &mut [f16]) -> Result<()>;
389392
fn dequantize(&self, elem_count: usize) -> Result<CpuStorage>;
390393
fn storage_size_in_bytes(&self) -> usize;
391394
fn as_ptr(&self) -> *const u8;
@@ -406,6 +409,9 @@ impl<T: k_quants::GgmlType + Send + Sync> QuantizedType for Vec<T> {
406409
fn matmul_t(&self, mkn: (usize, usize, usize), lhs: &[f32], dst: &mut [f32]) -> Result<()> {
407410
k_quants::matmul(mkn, lhs, self.as_slice(), dst)
408411
}
412+
fn matmul_t_f16(&self, mkn: (usize, usize, usize), lhs: &[f16], dst: &mut [f16]) -> Result<()> {
413+
k_quants::matmul_f16(mkn, lhs, self.as_slice(), dst)
414+
}
409415

410416
fn size(&self) -> usize {
411417
self.len() * core::mem::size_of::<T>()
@@ -750,11 +756,33 @@ impl crate::CustomOp1 for QTensor {
750756
QStorage::Cpu(storage) => storage,
751757
QStorage::Metal(_) | QStorage::Cuda(_) => crate::bail!("Invalid storage"),
752758
};
753-
let slice = storage.as_slice::<f32>()?;
754-
let slice = &slice[layout.start_offset()..layout.start_offset() + src_shape.elem_count()];
755-
let mut dst_storage = vec![0f32; dst_shape.elem_count()];
756-
self_storage.matmul_t((dst_shape.elem_count() / n, k, n), slice, &mut dst_storage)?;
757-
Ok((crate::CpuStorage::F32(dst_storage), dst_shape))
759+
match storage.dtype() {
760+
DType::F32 => {
761+
let slice = storage.as_slice::<f32>()?;
762+
let slice =
763+
&slice[layout.start_offset()..layout.start_offset() + src_shape.elem_count()];
764+
let mut dst_storage = vec![0f32; dst_shape.elem_count()];
765+
self_storage.matmul_t(
766+
(dst_shape.elem_count() / n, k, n),
767+
slice,
768+
&mut dst_storage,
769+
)?;
770+
Ok((crate::CpuStorage::F32(dst_storage), dst_shape))
771+
}
772+
DType::F16 => {
773+
let slice = storage.as_slice::<f16>()?;
774+
let slice =
775+
&slice[layout.start_offset()..layout.start_offset() + src_shape.elem_count()];
776+
let mut dst_storage = vec![f16::ZERO; dst_shape.elem_count()];
777+
self_storage.matmul_t_f16(
778+
(dst_shape.elem_count() / n, k, n),
779+
slice,
780+
&mut dst_storage,
781+
)?;
782+
Ok((crate::CpuStorage::F16(dst_storage), dst_shape))
783+
}
784+
_ => crate::bail!("Expected f32/f16"),
785+
}
758786
}
759787

760788
fn metal_fwd(

candle-flash-attn-v3/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ license = "MIT OR Apache-2.0"
1010
readme = "README.md"
1111
authors = ["Michael Feil"]
1212
repository = "https://github.com/michaelfeil/candle-flash-attn-v3"
13+
exclude = ["cutlass/docs/**", "cutlass/test/**", "cutlass/examples/**", "cutlass/tools/**", "cutlass/media/**"]
1314

1415
[dependencies]
1516
candle = { path = "../candle-core", features = ["cuda"], package = "candle-core", version = "0.8.0" }

candle-flash-attn/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ keywords = ["blas", "tensor", "machine-learning"]
99
categories = ["science"]
1010
license = "MIT OR Apache-2.0"
1111
readme = "README.md"
12+
exclude = ["cutlass/docs/**", "cutlass/test/**", "cutlass/examples/**", "cutlass/tools/**", "cutlass/media/**"]
1213

1314
[dependencies]
1415
candle = { path = "../candle-core", features = ["cuda"], package = "candle-core", version = "0.8.0" }

candle-flash-mla/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ keywords = ["blas", "tensor", "machine-learning"]
88
categories = ["science"]
99
license = "MIT OR Apache-2.0"
1010
readme = "README.md"
11+
exclude = ["cutlass/docs/**", "cutlass/test/**", "cutlass/examples/**", "cutlass/tools/**", "cutlass/media/**"]
1112

1213
[dependencies]
1314
candle = { path = "../candle-core", features = ["cuda"], package = "candle-core", version = "0.8.0" }

0 commit comments

Comments
 (0)