pub struct BitLinear { /* private fields */ }Expand description
BitLinear layer with ternary weights and INT8 activations.
This is a drop-in replacement for candle_nn::Linear that uses:
- Ternary weights {-1, 0, +1} with per-group scales
- INT8 activation quantization with per-token scales
§Example
ⓘ
use bitnet_rs::{BitLinear, BitNetConfig};
use candle_core::{Device, Tensor};
let device = Device::Cpu;
let config = BitNetConfig::default();
// Create from existing weights
let weight = Tensor::randn(0.0f32, 1.0, (512, 256), &device)?;
let layer = BitLinear::from_weight(&weight, None, &config)?;
// Forward pass
let input = Tensor::randn(0.0f32, 1.0, (4, 256), &device)?;
let output = layer.forward(&input)?;Implementations§
Source§impl BitLinear
impl BitLinear
Sourcepub fn from_weight(
weight: &Tensor,
bias: Option<&Tensor>,
config: &BitNetConfig,
) -> Result<Self>
pub fn from_weight( weight: &Tensor, bias: Option<&Tensor>, config: &BitNetConfig, ) -> Result<Self>
Sourcepub fn from_quantized(
weight: TernaryWeight,
bias: Option<Tensor>,
config: BitNetConfig,
device: Device,
) -> Self
pub fn from_quantized( weight: TernaryWeight, bias: Option<Tensor>, config: BitNetConfig, device: Device, ) -> Self
Create a new BitLinear layer from pre-quantized weights.
§Arguments
weight- Pre-quantized ternary weightbias- Optional bias tensorconfig- BitNet configurationdevice- Device for operations
Sourcepub fn in_features(&self) -> usize
pub fn in_features(&self) -> usize
Get the input features dimension.
Sourcepub fn out_features(&self) -> usize
pub fn out_features(&self) -> usize
Get the output features dimension.
Sourcepub const fn quantized_weight(&self) -> &TernaryWeight
pub const fn quantized_weight(&self) -> &TernaryWeight
Get reference to the quantized weights.
Sourcepub const fn config(&self) -> &BitNetConfig
pub const fn config(&self) -> &BitNetConfig
Get reference to the configuration.
Sourcepub fn compression_ratio(&self) -> f32
pub fn compression_ratio(&self) -> f32
Get the compression ratio.
Sourcepub fn forward_quantized(&self, input: &Tensor) -> Result<Tensor>
pub fn forward_quantized(&self, input: &Tensor) -> Result<Tensor>
Forward pass with explicit activation quantization.
This method:
- Quantizes input activations to INT8
- Dequantizes weights for matmul (or uses optimized kernel)
- Performs the linear transformation
- Adds bias if present
§Arguments
input- Input tensor [batch, …, in_features]
§Errors
Returns error if forward pass fails.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for BitLinear
impl !RefUnwindSafe for BitLinear
impl Send for BitLinear
impl Sync for BitLinear
impl Unpin for BitLinear
impl !UnwindSafe for BitLinear
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more