Skip to content

Commit

Permalink
Introduce IdentityProjection (#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbalioglu authored Dec 20, 2024
1 parent e8cdbd6 commit 131f9f9
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/fairseq2/nn/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,25 @@ def forward(self, x: Tensor) -> Tensor:
return linear(x, self.weight, self.bias)


@final
class IdentityProjection(Projection):
"""
Used to disable a projection layer without changing the module architecture.
"""

def __init__(self, input_dim: int, output_dim: int) -> None:
if input_dim != output_dim:
raise ValueError(
f"For identity projection, `input_dim` and `output_dim` must match, but are {input_dim} and {output_dim} instead."
)

super().__init__(input_dim, output_dim)

@override
def forward(self, x: Tensor) -> Tensor:
return x


def _init_uniform(weight: Tensor, bias: Tensor | None) -> None:
nn.init.kaiming_uniform_(weight, a=math.sqrt(5))

Expand Down

0 comments on commit 131f9f9

Please sign in to comment.