use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::ops::matmul_output_shape;
use crate::runtime::cuda::kernels::{
MAX_SMALL_M, int_matmul_output_dtype, launch_gemv_kernel_bt_mr, launch_matmul_batched_kernel,
launch_matmul_batched_kernel_bt, launch_matmul_batched_smallm_bt_kernel, launch_matmul_kernel,
launch_matmul_kernel_bt, launch_matmul_smallm_bt_kernel,
};
use crate::runtime::cuda::ops::matmul_broadcast::resolve_batched_operands;
use crate::runtime::cuda::{CudaClient, CudaRuntime};
use crate::runtime::ensure_contiguous;
use crate::tensor::Tensor;
fn is_simple_transpose_2d(tensor: &Tensor<CudaRuntime>) -> bool {
let shape = tensor.shape();
let strides = tensor.strides();
if shape.len() != 2 {
return false;
}
strides[0] == 1 && strides[1] == shape[0] as isize
}
#[inline]
fn gemv_only_half(dtype: DType) -> bool {
matches!(dtype, DType::F16 | DType::BF16)
}
pub(crate) fn matmul_native(
client: &CudaClient,
a: &Tensor<CudaRuntime>,
b: &Tensor<CudaRuntime>,
dtype: DType,
m: usize,
k: usize,
n: usize,
) -> Result<Tensor<CudaRuntime>> {
let out_shape = matmul_output_shape(a.shape(), b.shape()).ok_or(Error::ShapeMismatch {
expected: a.shape().to_vec(),
got: b.shape().to_vec(),
})?;
if m <= 16 && gemv_only_half(dtype) && is_simple_transpose_2d(b) {
let a_contig = ensure_contiguous(a)?;
let out = Tensor::<CudaRuntime>::empty(&out_shape, dtype, &client.device)?;
unsafe {
launch_gemv_kernel_bt_mr(
&client.context,
&client.stream,
client.device.index,
dtype,
a_contig.ptr(),
b.ptr(), out.ptr(),
1, m,
n,
k,
1, 1, )?;
}
return Ok(out);
}
if dtype == DType::F32 && m <= MAX_SMALL_M && is_simple_transpose_2d(b) {
let a_contig = ensure_contiguous(a)?;
let out = Tensor::<CudaRuntime>::empty(&out_shape, dtype, &client.device)?;
let launched = unsafe {
launch_matmul_smallm_bt_kernel(
client,
dtype,
a_contig.ptr(),
b.ptr(),
out.ptr(),
m,
n,
k,
)?
};
if launched {
return Ok(out);
}
}
if dtype == DType::F32 && is_simple_transpose_2d(b) {
let a_contig = ensure_contiguous(a)?;
let out = Tensor::<CudaRuntime>::empty(&out_shape, dtype, &client.device)?;
let launched = unsafe {
launch_matmul_kernel_bt(
&client.context,
&client.stream,
client.device.index,
dtype,
a_contig.ptr(),
b.ptr(),
out.ptr(),
m,
n,
k,
)?
};
if launched {
return Ok(out);
}
}
let a_contig = ensure_contiguous(a)?;
let b_contig = ensure_contiguous(b)?;
let out =
Tensor::<CudaRuntime>::empty(&out_shape, int_matmul_output_dtype(dtype), &client.device)?;
unsafe {
launch_matmul_kernel(
&client.context,
&client.stream,
client.device.index,
dtype,
a_contig.ptr(),
b_contig.ptr(),
out.ptr(),
m,
n,
k,
)?;
}
Ok(out)
}
fn is_batched_transpose_last2(tensor: &Tensor<CudaRuntime>) -> bool {
let shape = tensor.shape();
let strides = tensor.strides();
if shape.len() != 3 {
return false;
}
let k = shape[1];
let n = shape[2];
strides[1] == 1 && strides[2] == k as isize && strides[0] == (n * k) as isize
}
pub(crate) fn matmul_batched_native(
client: &CudaClient,
a: &Tensor<CudaRuntime>,
b: &Tensor<CudaRuntime>,
dtype: DType,
batch: usize,
m: usize,
k: usize,
n: usize,
) -> Result<Tensor<CudaRuntime>> {
let out_shape = matmul_output_shape(a.shape(), b.shape()).ok_or(Error::ShapeMismatch {
expected: a.shape().to_vec(),
got: b.shape().to_vec(),
})?;
let operands = resolve_batched_operands(a, b, &out_shape)?;
let (a, b) = (&operands.a, &operands.b);
let (a_batch, b_batch) = (operands.a_batch, operands.b_batch);
if m <= 16 && gemv_only_half(dtype) && is_batched_transpose_last2(b) {
let a_contig = ensure_contiguous(a)?;
let out = Tensor::<CudaRuntime>::empty(&out_shape, dtype, &client.device)?;
unsafe {
launch_gemv_kernel_bt_mr(
&client.context,
&client.stream,
client.device.index,
dtype,
a_contig.ptr(),
b.ptr(),
out.ptr(),
batch,
m,
n,
k,
a_batch,
b_batch,
)?;
}
return Ok(out);
}
if dtype == DType::F32 && m <= MAX_SMALL_M && is_batched_transpose_last2(b) {
let a_contig = ensure_contiguous(a)?;
let out = Tensor::<CudaRuntime>::empty(&out_shape, dtype, &client.device)?;
let launched = unsafe {
launch_matmul_batched_smallm_bt_kernel(
client,
dtype,
a_contig.ptr(),
b.ptr(),
out.ptr(),
batch,
m,
n,
k,
a_batch,
b_batch,
)?
};
if launched {
return Ok(out);
}
}
if dtype == DType::F32 && is_batched_transpose_last2(b) {
let a_contig = ensure_contiguous(a)?;
let out = Tensor::<CudaRuntime>::empty(&out_shape, dtype, &client.device)?;
let launched = unsafe {
launch_matmul_batched_kernel_bt(
&client.context,
&client.stream,
client.device.index,
dtype,
a_contig.ptr(),
b.ptr(),
out.ptr(),
batch,
m,
n,
k,
a_batch,
b_batch,
)?
};
if launched {
return Ok(out);
}
}
let a_contig = ensure_contiguous(a)?;
let b_contig = ensure_contiguous(b)?;
let out =
Tensor::<CudaRuntime>::empty(&out_shape, int_matmul_output_dtype(dtype), &client.device)?;
unsafe {
launch_matmul_batched_kernel(
&client.context,
&client.stream,
client.device.index,
dtype,
a_contig.ptr(),
b_contig.ptr(),
out.ptr(),
batch,
m,
n,
k,
a_batch,
b_batch,
)?;
}
Ok(out)
}