bitorch_engine.layers.qlinear.binary.cpp.layer.BinaryLinearForward

class bitorch_engine.layers.qlinear.binary.cpp.layer.BinaryLinearForward(*args, **kwargs)[source]

A custom autograd function for performing forward pass of binary linear layer. This function uses a custom C++ backend for efficient computation.

Parameters:
  • ctx (torch.autograd.function.FunctionCtx) – The context for storing information for backward computation.

  • input (torch.Tensor) – The input tensor.

  • weights (torch.Tensor) – The binary weights tensor.

  • m (int) – The batch size.

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

  • k (int) – The number of input features.

Returns:

The output tensor after applying the binary linear transformation.

Return type:

torch.Tensor

Methods

forward

Define the forward of the custom autograd Function.

Attributes

static forward(ctx, input: Tensor, weights: Tensor, m: int, n: int, k: int) Tensor[source]

Define the forward of the custom autograd Function.

This function is to be overridden by all subclasses. There are two ways to define forward:

Usage 1 (Combined forward and ctx):

@staticmethod
def forward(ctx: Any, *args: Any, **kwargs: Any) -> Any:
    pass
  • It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types).

  • See combining-forward-context for more details

Usage 2 (Separate forward and ctx):

@staticmethod
def forward(*args: Any, **kwargs: Any) -> Any:
    pass

@staticmethod
def setup_context(ctx: Any, inputs: Tuple[Any, ...], output: Any) -> None:
    pass
  • The forward no longer accepts a ctx argument.

  • Instead, you must also override the torch.autograd.Function.setup_context() staticmethod to handle setting up the ctx object. output is the output of the forward, inputs are a Tuple of inputs to the forward.

  • See extending-autograd for more details

The context can be used to store arbitrary data that can be then retrieved during the backward pass. Tensors should not be stored directly on ctx (though this is not currently enforced for backward compatibility). Instead, tensors should be saved either with ctx.save_for_backward() if they are intended to be used in backward (equivalently, vjp) or ctx.save_for_forward() if they are intended to be used for in jvp.