espnet2.asr.state_spaces.base.SequenceIdentity
espnet2.asr.state_spaces.base.SequenceIdentity
class espnet2.asr.state_spaces.base.SequenceIdentity(*args, transposed=False, **kwargs)
Bases: SequenceIdentity
Class SequenceIdentity
A simple implementation of the SequenceModule designed for testing purposes. This class acts as a direct identity mapping for input sequences, transforming input tensors of shape (batch, length, d_model) to output tensors of the same shape. It inherits from SequenceModule and fulfills the required interface for sequence models.
d_model
The input and output dimension of the model.
- Type: int
d_output
The output dimension, which is equal to d_model.
Type: int
Parameters:
- d_model (int) – Input dimension, which is also the hidden dimension.
- dropout (float) – Dropout rate for regularization (not utilized in this implementation).
- **kwargs – Additional keyword arguments passed to the parent class.
Returns: A tuple containing the output tensor and the state.
Return type: tuple
forward(x, state=None)
Performs the forward pass, returning the input as output.
default_state(*batch_shape, device=None)
Creates an initial state for a batch of inputs.
step(x, state=None, **kwargs)
Steps the model recurrently for one step of the input sequence.
####### Examples
>>> model = SequenceIdentity(d_model=128)
>>> input_tensor = torch.randn(10, 5, 128) # (batch_size, seq_len, d_model)
>>> output, state = model(input_tensor)
>>> assert output.shape == input_tensor.shape # Output shape matches input shape
NOTE
This module does not implement any form of learning or state management beyond passing the input through unchanged. It serves primarily for testing and benchmarking other sequence models.
Initialize SequenceModule.
d_model: input dimension (sometimes denoted H for hidden dimension) transposed: if True, inputs have axis ordering (B, H, L) instead of (B, H, L)
forward(x, state=None, **kwargs)
Forward pass.
This method performs a sequence-to-sequence transformation on the input tensor. It maps a tensor of shape (batch, length, self.d_model) to (batch, length, self.d_output). The method can also accept an optional state parameter, which can be used for recurrent models.
- Parameters:
- x (torch.Tensor) – Input tensor of shape (batch, length, d_model).
- state (optional) – Optional state information for recurrent models. Defaults to None.
- **kwargs – Additional keyword arguments.
- Returns: A tuple containing: : - torch.Tensor: Output tensor of shape (batch, length, d_output).
- state (optional): Updated state information, if applicable.
- Return type: tuple
####### Examples
>>> model = SequenceIdentity(d_model=128)
>>> input_tensor = torch.randn(32, 10, 128) # (batch, length, d_model)
>>> output, state = model.forward(input_tensor)
>>> output.shape
torch.Size([32, 10, 128])
NOTE
The state returned can vary depending on the specific implementation of the model, and may be None if no state is maintained.
- Raises:NotImplementedError – If the method is not implemented in a derived class.