Module audiocraft.modules.activations

Functions

def get_activation_fn(activation: str | Callable[[torch.Tensor], torch.Tensor]) ‑> str | Callable[[torch.Tensor], torch.Tensor]
Expand source code
def get_activation_fn(
    activation: Union[str, Callable[[Tensor], Tensor]]
) -> Union[str, Callable[[Tensor], Tensor]]:
    """Helper function to map an activation string to the activation class.
    If the supplied activation is not a string that is recognized, the activation is passed back.

    Args:
        activation (str, or Callable[[Tensor], Tensor]): Activation to check
    """
    if isinstance(activation, str):
        if activation == "reglu":
            return ReGLU()
        elif activation == "geglu":
            return GeGLU()
        elif activation == "swiglu":
            return SwiGLU()
    return activation

Helper function to map an activation string to the activation class. If the supplied activation is not a string that is recognized, the activation is passed back.

Args

activation : str, or Callable[[Tensor], Tensor]
Activation to check

Classes

class CustomGLU (activation: torch.nn.modules.module.Module, dim: int = -1)
Expand source code
class CustomGLU(nn.Module):
    """Custom Gated Linear Unit activation.
    Applies a modified gated linear unit :math:`a * f(b)` where :math:`a` is the first half
    of the input matrices, :math:`b` is the second half, and :math:`f` is a provided activation
    function (i.e. sigmoid, swish, etc.).

    Args:
        activation (nn.Module): The custom activation to apply in the Gated Linear Unit
        dim (int): the dimension on which to split the input. Default: -1

    Shape:
        - Input: :math:`(\ast_1, N, \ast_2)` where `*` means, any number of additional
          dimensions
        - Output: :math:`(\ast_1, M, \ast_2)` where :math:`M=N/2`

    Examples::
        >>> m = CustomGLU(nn.Sigmoid())
        >>> input = torch.randn(4, 2)
        >>> output = m(input)
    """
    def __init__(self, activation: nn.Module, dim: int = -1):
        super(CustomGLU, self).__init__()
        self.dim = dim
        self.activation = activation

    def forward(self, x: Tensor):
        assert x.shape[self.dim] % 2 == 0  # M = N / 2
        a, b = torch.chunk(x, 2, dim=self.dim)
        return a * self.activation(b)

Custom Gated Linear Unit activation. Applies a modified gated linear unit :math:a * f(b) where :math:a is the first half of the input matrices, :math:b is the second half, and :math:f is a provided activation function (i.e. sigmoid, swish, etc.).

Args

activation : nn.Module
The custom activation to apply in the Gated Linear Unit
dim : int
the dimension on which to split the input. Default: -1

Shape

  • Input: :math:(st_1, N, st_2) where * means, any number of additional dimensions
  • Output: :math:(st_1, M, st_2) where :math:M=N/2

Examples:: >>> m = CustomGLU(nn.Sigmoid()) >>> input = torch.randn(4, 2) >>> output = m(input)

Initializes internal Module state, shared by both nn.Module and ScriptModule.

Ancestors

  • torch.nn.modules.module.Module

Subclasses

Class variables

var call_super_init : bool
var dump_patches : bool
var training : bool

Methods

def forward(self, x: torch.Tensor) ‑> Callable[..., Any]
Expand source code
def forward(self, x: Tensor):
    assert x.shape[self.dim] % 2 == 0  # M = N / 2
    a, b = torch.chunk(x, 2, dim=self.dim)
    return a * self.activation(b)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the :class:Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class GeGLU (dim: int = -1)
Expand source code
class GeGLU(CustomGLU):
    """GeLU Gated Linear Unit activation.
    Applies GeLU Gated Linear Unit :math:`a * GELU(b)` where :math:`a` is
    the first half of the input matrices, :math:`b` is the second half.

    Args:
        dim (int): the dimension on which to split the input. Default: -1
    """
    def __init__(self, dim: int = -1):
        super(GeGLU, self).__init__(nn.GELU(), dim)

GeLU Gated Linear Unit activation. Applies GeLU Gated Linear Unit :math:a * GELU(b) where :math:a is the first half of the input matrices, :math:b is the second half.

Args

dim : int
the dimension on which to split the input. Default: -1

Initializes internal Module state, shared by both nn.Module and ScriptModule.

Ancestors

Class variables

var call_super_init : bool
var dump_patches : bool
var training : bool

Inherited members

class ReGLU (dim: int = -1)
Expand source code
class ReGLU(CustomGLU):
    """ReLU Gated Linear Unit activation.
    Applies ReLU Gated Linear Unit :math:`a * ReLU(b)` where :math:`a` is
    the first half of the input matrices, :math:`b` is the second half.

    Args:
        dim (int): the dimension on which to split the input. Default: -1
    """
    def __init__(self, dim: int = -1):
        super(ReGLU, self).__init__(nn.ReLU(), dim)

ReLU Gated Linear Unit activation. Applies ReLU Gated Linear Unit :math:a * ReLU(b) where :math:a is the first half of the input matrices, :math:b is the second half.

Args

dim : int
the dimension on which to split the input. Default: -1

Initializes internal Module state, shared by both nn.Module and ScriptModule.

Ancestors

Class variables

var call_super_init : bool
var dump_patches : bool
var training : bool

Inherited members

class SwiGLU (dim: int = -1)
Expand source code
class SwiGLU(CustomGLU):
    """SiLU Gated Linear Unit activation.
    Applies SiLU Gated Linear Unit :math:`a * SiLU(b)` where :math:`a` is
    the first half of the input matrices, :math:`b` is the second half.

    Args:
        dim (int): the dimension on which to split the input. Default: -1
    """
    def __init__(self, dim: int = -1):
        super(SwiGLU, self).__init__(nn.SiLU(), dim)

SiLU Gated Linear Unit activation. Applies SiLU Gated Linear Unit :math:a * SiLU(b) where :math:a is the first half of the input matrices, :math:b is the second half.

Args

dim : int
the dimension on which to split the input. Default: -1

Initializes internal Module state, shared by both nn.Module and ScriptModule.

Ancestors

Class variables

var call_super_init : bool
var dump_patches : bool
var training : bool

Inherited members