Skip to content

Commit

Permalink
Improve activation quant function
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseCarlosGarcia95 committed Dec 31, 2024
1 parent f2a3809 commit c00f3c8
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions candle-nn/src/bit_linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,10 @@ fn weight_quant(x: &Tensor) -> Result<Tensor> {
}

fn activation_quant(x: &Tensor) -> Result<Tensor> {
let scale = (127.0
/ x.abs()?
.max(D::Minus1)?
.max(D::Minus1)?
.clamp(1e-5, f32::INFINITY)?)?
.to_dtype(x.dtype())?;
let scale = x.abs()?.max_keepdim(D::Minus1)?.clamp(1e-5, f32::INFINITY)?;
let scale = (127.0 / scale)?;

let y = x
.broadcast_mul(&scale.unsqueeze(D::Minus1)?.unsqueeze(D::Minus1)?)?
.clamp(-128.0, 127.0)?
.broadcast_div(&scale)?;
let y = (x.broadcast_mul(&scale))?.round()?.clamp(-128., 127.)?.broadcast_div(&scale)?;

Ok(y)
}
Expand Down

0 comments on commit c00f3c8

Please sign in to comment.