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

Module weight quantization #2000

Merged
merged 13 commits into from
Jul 15, 2024
Prev Previous commit
Next Next commit
Add QTensorOps require_grad methods to avoid dequantizing
  • Loading branch information
laggui committed Jul 12, 2024
commit feea531751750ca098a78b9a2319026301d5a7f6
18 changes: 12 additions & 6 deletions crates/burn-tensor/src/tensor/api/float.rs
Original file line number Diff line number Diff line change
@@ -271,9 +271,9 @@ where
match &self.primitive {
TensorPrimitive::Float(tensor) => B::float_is_require_grad(tensor),
TensorPrimitive::QFloat {
tensor: _,
tensor,
strategy: _,
} => B::float_is_require_grad(&self.primitive.clone().tensor()),
} => B::q_is_require_grad(tensor),
}
}

@@ -282,10 +282,16 @@ where
///
/// This function does nothing when autodiff is not enabled.
pub fn set_require_grad(self, require_grad: bool) -> Self {
Self::new(TensorPrimitive::Float(B::float_set_require_grad(
self.primitive.tensor(),
require_grad,
)))
let primitive = match self.primitive {
TensorPrimitive::Float(tensor) => {
TensorPrimitive::Float(B::float_set_require_grad(tensor, require_grad))
}
TensorPrimitive::QFloat { tensor, strategy } => TensorPrimitive::QFloat {
tensor: B::q_set_require_grad(tensor, require_grad),
strategy,
},
};
Self::new(primitive)
}

/// Applies the relu function to the tensor.
15 changes: 15 additions & 0 deletions crates/burn-tensor/src/tensor/ops/qtensor.rs
Original file line number Diff line number Diff line change
@@ -81,4 +81,19 @@ pub trait QTensorOps<B: Backend> {
tensor: QuantizedTensor<B, D>,
strategy: QuantizationStrategy,
) -> impl Future<Output = TensorData> + Send;

/// Sets the `require_grad` flag of a tensor.
fn q_set_require_grad<const D: usize>(
tensor: QuantizedTensor<B, D>,
_require_grad: bool,
) -> QuantizedTensor<B, D> {
// Should only be overridden by autodiff backends.
tensor
}

/// Returns the `require_grad` flag of a tensor.
fn q_is_require_grad<const D: usize>(_tensor: &QuantizedTensor<B, D>) -> bool {
// Should only be overridden by autodiff backends.
false
}
}