Convert to corresponding PyTorch activation module.
Gated activations (SwiGLU, GeGLU) require input_dimension and
hidden_dimension constructor args. Standard activations are zero-arg.
Source code in src/versatil/models/layers/activation.py
| def to_torch_activation(self) -> type[nn.Module]:
"""Convert to corresponding PyTorch activation module.
Gated activations (SwiGLU, GeGLU) require ``input_dimension`` and
``hidden_dimension`` constructor args. Standard activations are zero-arg.
"""
mapping = {
ActivationFunction.RELU.value: nn.ReLU,
ActivationFunction.GELU.value: nn.GELU,
ActivationFunction.SILU.value: nn.SiLU,
ActivationFunction.SWIGLU.value: SwiGLU,
ActivationFunction.GEGLU.value: GeGLU,
ActivationFunction.SIGMOID.value: nn.Sigmoid,
ActivationFunction.TANH.value: nn.Tanh,
ActivationFunction.LEAKY_RELU.value: nn.LeakyReLU,
ActivationFunction.LINEAR.value: nn.Identity,
ActivationFunction.MISH.value: nn.Mish,
}
if self.value not in mapping:
raise ValueError(
f"Unsupported activation: {self.value}. "
f"Choose from {[e.value for e in ActivationFunction]}"
)
return mapping[self.value]
|