bitorch_engine.layers.qconv.binary.layer.BinaryConv2dBase

class bitorch_engine.layers.qconv.binary.layer.BinaryConv2dBase(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1, device=None, dtype: dtype = torch.float32, symmetric: bool = True)[source]

Methods

__init__

Initializes a base class for binary convolution layers.

generate_quantized_weight

Generates quantized weights from the current weights.

prepare_params

Prepares and initializes the model parameters for training.

reset_parameters

Resets the layer's parameters.

set_bits_binary_word

Sets the number of bits for binary quantization.

set_quantized_weight_data

Sets the quantized weight data for the convolution layer.

set_weight_data

Sets the weight data for the convolution layer.

Attributes

opt_weight

Returns the weight for the layer.

training

__init__(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1, device=None, dtype: dtype = torch.float32, symmetric: bool = True) None[source]

Initializes a base class for binary convolution layers.

This class is designed to serve as a base for binary convolution operations, where the convolution weights are quantized to binary values. It initializes the layer’s parameters and sets up the necessary configurations for binary convolution.

Parameters:
  • in_channels (int) – The number of channels in the input image.

  • out_channels (int) – The number of channels produced by the convolution.

  • kernel_size (int) – The size of the convolving kernel.

  • stride (int) – The stride of the convolution. Defaults to 1.

  • padding (int) – Zero-padding added to both sides of the input. Defaults to 0.

  • dilation (int) – The spacing between kernel elements. Defaults to 1.

  • device – The device on which to allocate tensors. Defaults to None.

  • dtype (torch.dtype) – The desired data type of the parameters. Defaults to torch.float.

  • symmetric (bool) – Indicates whether to use symmetric quantization. Defaults to True.

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

Generates quantized weights from the current weights.

This method should implement the logic to convert the layer’s weights to their quantized form. This is an abstract method and must be implemented by subclasses.

Parameters:

qweight_only (bool) – If True, the original weight tensor is discarded to save memory.

property opt_weight

Returns the weight for the layer.

This property checks if the layer is in training mode and returns the appropriate weight tensor (original or quantized) for use in computations.

Returns:

The optimized weight tensor.

Return type:

torch.nn.Parameter

prepare_params() None[source]

Prepares and initializes the model parameters for training.

Note

This method MUST be called after model initialization and before training starts to ensure the weights are properly prepared for efficient computation.

reset_parameters() None[source]

Resets the layer’s parameters.

This method reinitializes the weight parameter of the convolution layer, typically with a new random value.

set_bits_binary_word(num_bit: int) None[source]

Sets the number of bits for binary quantization.

Parameters:

num_bit (int) – The number of bits to use for binary quantization.

set_quantized_weight_data(x: Tensor) None[source]

Sets the quantized weight data for the convolution layer.

Parameters:

x (torch.Tensor) – A tensor containing the quantized weights for the convolution layer.

set_weight_data(x: Tensor) None[source]

Sets the weight data for the convolution layer.

Parameters:

x (torch.Tensor) – A tensor containing the weights for the convolution layer.