bitorch_engine.layers.qlinear.binary.cpp.layer.BinaryLinearCPP

class bitorch_engine.layers.qlinear.binary.cpp.layer.BinaryLinearCPP(input_features: int, out_features: int, device: device | None = None)[source]

A class representing the binary linear layer implemented in C++ for CPU runtime mode. Inherits from BinaryLinearBase and mixes in BinaryLinearImplementationMixin for common functionality.

This class supports creating a clone of itself from a given LayerRecipe, allowing for easy replication and modification of layer parameters.

Methods

__init__

Initializes the BinaryLinearCPP layer.

create_clone_from

Creates a clone of this layer based on the provided LayerRecipe.

forward

Defines the forward pass of the binary linear layer.

generate_quantized_weight

Generates the quantized weight matrix for this layer and optionally clears the original weight.

prepare_params

Prepares and initializes the model parameters for training.

Attributes

training

__init__(input_features: int, out_features: int, device: device | None = None) None[source]

Initializes the BinaryLinearCPP layer.

Parameters:
  • input_features (int) – The number of input features (divided by 8 for binary).

  • out_features (int) – The number of output features.

  • device (torch.device, optional) – The device on which to perform computations.

classmethod create_clone_from(recipe: LayerRecipe) Any[source]

Creates a clone of this layer based on the provided LayerRecipe.

Parameters:

recipe (LayerRecipe) – The recipe containing the parameters for the clone.

Returns:

A new instance of this class with parameters derived from the recipe.

Return type:

Any

forward(x: Tensor) Tensor[source]

Defines the forward pass of the binary linear layer.

Parameters:

x (torch.Tensor) – The input tensor.

Returns:

The output tensor after applying the binary linear transformation.

Return type:

torch.Tensor

generate_quantized_weight(qweight_only: bool = False) None[source]

Generates the quantized weight matrix for this layer and optionally clears the original weight.

Parameters:

qweight_only (bool, optional) – If True, the original weight matrix is cleared to save memory.

prepare_params() None[source]

Prepares and initializes the model parameters for training. One can use “prepare_bie_layers” method from project_root.utils.model_helper to call this function.