Skip to content

Tied Biases (Pre-Encoder and Post-Decoder)¤

Tied Biases (Pre-Encoder and Post-Decoder).

TiedBias ¤

Bases: Module

Tied Bias Layer.

The tied pre-encoder bias is a learned bias term that is subtracted from the input before encoding, and added back after decoding.

The bias parameter must be initialised in the parent module, and then passed to this layer.

https://transformer-circuits.pub/2023/monosemantic-features/index.html#appendix-autoencoder-bias

Source code in sparse_autoencoder/autoencoder/components/tied_bias.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
@final
class TiedBias(Module):
    """Tied Bias Layer.

    The tied pre-encoder bias is a learned bias term that is subtracted from the input before
    encoding, and added back after decoding.

    The bias parameter must be initialised in the parent module, and then passed to this layer.

    https://transformer-circuits.pub/2023/monosemantic-features/index.html#appendix-autoencoder-bias
    """

    _bias_position: TiedBiasPosition

    _bias_reference: Float[
        Parameter, Axis.names(Axis.COMPONENT_OPTIONAL, Axis.INPUT_OUTPUT_FEATURE)
    ]

    @property
    def bias(
        self,
    ) -> Float[Parameter, Axis.names(Axis.COMPONENT_OPTIONAL, Axis.INPUT_OUTPUT_FEATURE)]:
        """Bias."""
        return self._bias_reference

    def __init__(
        self,
        bias_reference: Float[
            Parameter, Axis.names(Axis.COMPONENT_OPTIONAL, Axis.INPUT_OUTPUT_FEATURE)
        ],
        position: TiedBiasPosition,
    ) -> None:
        """Initialize the bias layer.

        Args:
            bias_reference: Tied bias parameter (initialised in the parent module), used for both
                the pre-encoder and post-encoder bias. The original paper initialised this using the
                geometric median of the dataset.
            position: Whether this is the pre-encoder or post-encoder bias.
        """
        super().__init__()

        self._bias_reference = bias_reference

        # Support string literals as well as enums
        self._bias_position = position

    def forward(
        self,
        x: Float[
            Tensor, Axis.names(Axis.BATCH, Axis.COMPONENT_OPTIONAL, Axis.INPUT_OUTPUT_FEATURE)
        ],
    ) -> Float[Tensor, Axis.names(Axis.BATCH, Axis.COMPONENT_OPTIONAL, Axis.INPUT_OUTPUT_FEATURE)]:
        """Forward Pass.

        Args:
            x: Input tensor.

        Returns:
            Output of the forward pass.
        """
        # If this is the pre-encoder bias, we subtract the bias from the input.
        if self._bias_position == TiedBiasPosition.PRE_ENCODER:
            return x - self.bias

        # If it's the post-encoder bias, we add the bias to the input.
        return x + self.bias

    def extra_repr(self) -> str:
        """String extra representation of the module."""
        return f"position={self._bias_position.value}"

bias: Float[Parameter, Axis.names(Axis.COMPONENT_OPTIONAL, Axis.INPUT_OUTPUT_FEATURE)] property ¤

Bias.

__init__(bias_reference, position) ¤

Initialize the bias layer.

Parameters:

Name Type Description Default
bias_reference Float[Parameter, names(COMPONENT_OPTIONAL, INPUT_OUTPUT_FEATURE)]

Tied bias parameter (initialised in the parent module), used for both the pre-encoder and post-encoder bias. The original paper initialised this using the geometric median of the dataset.

required
position TiedBiasPosition

Whether this is the pre-encoder or post-encoder bias.

required
Source code in sparse_autoencoder/autoencoder/components/tied_bias.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def __init__(
    self,
    bias_reference: Float[
        Parameter, Axis.names(Axis.COMPONENT_OPTIONAL, Axis.INPUT_OUTPUT_FEATURE)
    ],
    position: TiedBiasPosition,
) -> None:
    """Initialize the bias layer.

    Args:
        bias_reference: Tied bias parameter (initialised in the parent module), used for both
            the pre-encoder and post-encoder bias. The original paper initialised this using the
            geometric median of the dataset.
        position: Whether this is the pre-encoder or post-encoder bias.
    """
    super().__init__()

    self._bias_reference = bias_reference

    # Support string literals as well as enums
    self._bias_position = position

extra_repr() ¤

String extra representation of the module.

Source code in sparse_autoencoder/autoencoder/components/tied_bias.py
87
88
89
def extra_repr(self) -> str:
    """String extra representation of the module."""
    return f"position={self._bias_position.value}"

forward(x) ¤

Forward Pass.

Parameters:

Name Type Description Default
x Float[Tensor, names(BATCH, COMPONENT_OPTIONAL, INPUT_OUTPUT_FEATURE)]

Input tensor.

required

Returns:

Type Description
Float[Tensor, names(BATCH, COMPONENT_OPTIONAL, INPUT_OUTPUT_FEATURE)]

Output of the forward pass.

Source code in sparse_autoencoder/autoencoder/components/tied_bias.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def forward(
    self,
    x: Float[
        Tensor, Axis.names(Axis.BATCH, Axis.COMPONENT_OPTIONAL, Axis.INPUT_OUTPUT_FEATURE)
    ],
) -> Float[Tensor, Axis.names(Axis.BATCH, Axis.COMPONENT_OPTIONAL, Axis.INPUT_OUTPUT_FEATURE)]:
    """Forward Pass.

    Args:
        x: Input tensor.

    Returns:
        Output of the forward pass.
    """
    # If this is the pre-encoder bias, we subtract the bias from the input.
    if self._bias_position == TiedBiasPosition.PRE_ENCODER:
        return x - self.bias

    # If it's the post-encoder bias, we add the bias to the input.
    return x + self.bias

TiedBiasPosition ¤

Bases: str, Enum

Tied Bias Position.

Source code in sparse_autoencoder/autoencoder/components/tied_bias.py
12
13
14
15
16
class TiedBiasPosition(str, Enum):
    """Tied Bias Position."""

    PRE_ENCODER = "pre_encoder"
    POST_DECODER = "post_decoder"