Forward pass: dequantizes weight if needed, computes matmul in weight dtype. The cached weight is already transposed and contiguous for optimal matmul.
(&self, x: &candle_core::Tensor)
| 230 | /// Forward pass: dequantizes weight if needed, computes matmul in weight dtype. |
| 231 | /// The cached weight is already transposed and contiguous for optimal matmul. |
| 232 | pub fn forward(&self, x: &candle_core::Tensor) -> candle_core::Result<candle_core::Tensor> { |
| 233 | let in_dtype = x.dtype(); |
| 234 | let wt = self.get_weight()?; // pre-transposed |
| 235 | let compute = wt.dtype(); |
| 236 | let x = if x.dtype() == compute { x.clone() } else { x.to_dtype(compute)? }; |
| 237 | let y = x.broadcast_matmul(&wt)?; |
| 238 | let y = match &self.bias { |
| 239 | Some(b) => y.broadcast_add(&b.to_dtype(compute)?)?, |
| 240 | None => y, |
| 241 | }; |
| 242 | if in_dtype == compute { Ok(y) } else { y.to_dtype(in_dtype) } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /// Create an Fp8Linear from a VarBuilder (no bias). |