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

Broadcast zero point vector when converting batched matmul to non-batched #572

Merged
merged 1 commit into from
Feb 3, 2025
Merged
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
26 changes: 24 additions & 2 deletions src/ops/matmul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ where
// nb. We assume `a` is likely already contiguous, so this will be cheap.
let a_contig = a.to_contiguous_in(pool).auto_return(pool);
let a_matrix = a_contig.reshaped([num_a_matrices * a_rows, a_cols].as_slice());

// Broadcast zero point to match new row count.
let a_quant: Option<Vec<LhsT>> = a_quant.map(|a_quant| {
a_quant
.zero_point
.iter()
.copied()
.cycle()
.take(a_matrix.size(0))
.collect()
});

let mut output = matmul_impl(
pool,
a_matrix.view(),
Expand All @@ -230,7 +242,9 @@ where
strategy,
bias,
alpha,
a_quant,
a_quant.as_ref().map(|zero_point| QuantParams {
zero_point: zero_point.as_slice(),
}),
b_quant,
)?;
output.reshape(out_shape);
Expand Down Expand Up @@ -1065,7 +1079,15 @@ mod tests {
b_zero_point: Some(Tensor::from([3, 4])),
expected_err: None,
},
// A input which is a row vector
// LHS batch input with vector zero point
Case {
a: Tensor::zeros(&[3, 2, 2]),
b: Tensor::from([[5, 6], [7, 8]]),
a_zero_point: Some(Tensor::from([1, 2])),
b_zero_point: Some(Tensor::from([3, 4])),
expected_err: None,
},
// An input which is a row vector
Case {
a: Tensor::from([[1, 2, 3, 4]]),
b: Tensor::from([[5, 6], [7, 8], [9, 10], [11, 12]]),
Expand Down