Add pydocstyle to the ruff rules (#52136)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Signed-off-by: Harry Mellor <hej.mellor@gmail.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
804e5377aa
commit
8c1557a79c
@@ -75,8 +75,7 @@ from .whisper import ISO639_1_SUPPORTED_LANGS, _create_fake_bias_for_k_proj
|
||||
|
||||
|
||||
class GlmAsrEncoderRotaryEmbedding(nn.Module):
|
||||
"""
|
||||
Rotary Position Embedding for GLM-ASR encoder.
|
||||
"""Rotary Position Embedding for GLM-ASR encoder.
|
||||
|
||||
Computes rotary position embeddings on-demand for efficiency.
|
||||
Only caches inv_freq as a buffer; cos/sin are computed during forward
|
||||
@@ -115,8 +114,7 @@ class GlmAsrEncoderRotaryEmbedding(nn.Module):
|
||||
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
||||
|
||||
def forward(self, seq_len: int) -> torch.Tensor:
|
||||
"""
|
||||
Compute rotary position frequencies for given sequence length.
|
||||
"""Compute rotary position frequencies for given sequence length.
|
||||
|
||||
Args:
|
||||
seq_len: The sequence length to compute embeddings for.
|
||||
@@ -124,6 +122,7 @@ class GlmAsrEncoderRotaryEmbedding(nn.Module):
|
||||
Returns:
|
||||
Frequency tensor with shape [seq_len, dim/2]. Use .cos() and
|
||||
.sin() to get the rotary embedding components.
|
||||
|
||||
"""
|
||||
# Compute on the same device as inv_freq (automatically correct after .to())
|
||||
seq = torch.arange(
|
||||
@@ -134,8 +133,7 @@ class GlmAsrEncoderRotaryEmbedding(nn.Module):
|
||||
|
||||
|
||||
class GlmAsrEncoderAttention(nn.Module):
|
||||
"""
|
||||
Optimized Multi-headed Grouped Query Attention for GLM-ASR encoder.
|
||||
"""Optimized Multi-headed Grouped Query Attention for GLM-ASR encoder.
|
||||
|
||||
Uses vLLM's QKVParallelLinear for fused projections, ApplyRotaryEmb for
|
||||
rotary position embeddings, and MMEncoderAttention for hardware-optimized
|
||||
@@ -208,14 +206,14 @@ class GlmAsrEncoderAttention(nn.Module):
|
||||
rotary_pos_emb_cos: torch.Tensor,
|
||||
rotary_pos_emb_sin: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
"""
|
||||
Args:
|
||||
"""Args:
|
||||
hidden_states: [batch_size, seq_len, hidden_size]
|
||||
rotary_pos_emb_cos: [seq_len, rotary_dim/2] - cosine of rotary embeddings
|
||||
rotary_pos_emb_sin: [seq_len, rotary_dim/2] - sine of rotary embeddings
|
||||
|
||||
Returns:
|
||||
[batch_size, seq_len, hidden_size]
|
||||
|
||||
"""
|
||||
batch_size, seq_len, _ = hidden_states.shape
|
||||
|
||||
@@ -255,8 +253,7 @@ class GlmAsrEncoderAttention(nn.Module):
|
||||
|
||||
|
||||
class GlmAsrEncoderMLP(nn.Module):
|
||||
"""
|
||||
Optimized MLP for GLM-ASR encoder.
|
||||
"""Optimized MLP for GLM-ASR encoder.
|
||||
Uses vLLM's parallel linear layers for better performance.
|
||||
"""
|
||||
|
||||
@@ -297,8 +294,7 @@ class GlmAsrEncoderMLP(nn.Module):
|
||||
|
||||
|
||||
class GlmAsrEncoderLayer(nn.Module):
|
||||
"""
|
||||
Optimized Transformer encoder layer for GLM-ASR.
|
||||
"""Optimized Transformer encoder layer for GLM-ASR.
|
||||
Combines attention and MLP with residual connections and layer norms.
|
||||
"""
|
||||
|
||||
@@ -335,14 +331,14 @@ class GlmAsrEncoderLayer(nn.Module):
|
||||
rotary_pos_emb_cos: torch.Tensor,
|
||||
rotary_pos_emb_sin: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
"""
|
||||
Args:
|
||||
"""Args:
|
||||
hidden_states: [batch_size, seq_len, hidden_size]
|
||||
rotary_pos_emb_cos: [seq_len, rotary_dim/2] - cosine of rotary embeddings
|
||||
rotary_pos_emb_sin: [seq_len, rotary_dim/2] - sine of rotary embeddings
|
||||
|
||||
Returns:
|
||||
[batch_size, seq_len, hidden_size]
|
||||
|
||||
"""
|
||||
# Self-attention with residual
|
||||
residual = hidden_states
|
||||
@@ -364,8 +360,7 @@ class GlmAsrEncoderLayer(nn.Module):
|
||||
|
||||
|
||||
class _GlmAsrEncoderOutput:
|
||||
"""
|
||||
Simple output container compatible with transformers' BaseModelOutput.
|
||||
"""Simple output container compatible with transformers' BaseModelOutput.
|
||||
|
||||
This lightweight container holds the encoder output and is compatible
|
||||
with the transformers library's output format while being more efficient
|
||||
@@ -374,6 +369,7 @@ class _GlmAsrEncoderOutput:
|
||||
Attributes:
|
||||
last_hidden_state: Final layer hidden states from the encoder.
|
||||
Shape: [batch_size, seq_len, hidden_size]
|
||||
|
||||
"""
|
||||
|
||||
__slots__ = ("last_hidden_state",)
|
||||
@@ -383,8 +379,7 @@ class _GlmAsrEncoderOutput:
|
||||
|
||||
|
||||
class GlmAsrEncoder(nn.Module):
|
||||
"""
|
||||
Optimized GLM-ASR Audio Encoder with vLLM native implementation.
|
||||
"""Optimized GLM-ASR Audio Encoder with vLLM native implementation.
|
||||
|
||||
This encoder processes audio features through convolutional layers
|
||||
followed by transformer layers with rotary position embeddings.
|
||||
@@ -454,14 +449,14 @@ class GlmAsrEncoder(nn.Module):
|
||||
def _get_feat_extract_output_lengths(
|
||||
self, input_lengths: torch.Tensor
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""
|
||||
Compute the output length after convolutions.
|
||||
"""Compute the output length after convolutions.
|
||||
|
||||
Args:
|
||||
input_lengths: Input sequence lengths [batch_size]
|
||||
|
||||
Returns:
|
||||
Tuple of (output after conv1, output after conv2)
|
||||
|
||||
"""
|
||||
# Conv1: kernel=3, stride=1, padding=1
|
||||
output_lengths_conv1 = (input_lengths + 2 * 1 - 3) // 1 + 1
|
||||
@@ -472,8 +467,7 @@ class GlmAsrEncoder(nn.Module):
|
||||
return output_lengths_conv1, output_lengths_conv2
|
||||
|
||||
def forward(self, input_features: torch.Tensor) -> _GlmAsrEncoderOutput:
|
||||
"""
|
||||
Forward pass through the encoder.
|
||||
"""Forward pass through the encoder.
|
||||
|
||||
Args:
|
||||
input_features: [batch_size, num_mel_bins, seq_len]
|
||||
@@ -482,6 +476,7 @@ class GlmAsrEncoder(nn.Module):
|
||||
_GlmAsrEncoderOutput: Object with .last_hidden_state attribute \
|
||||
containing [batch_size, seq_len', hidden_size] where seq_len' \
|
||||
is the sequence length after convolutions
|
||||
|
||||
"""
|
||||
# Apply convolutional layers with GELU activation
|
||||
hidden_states = torch.nn.functional.gelu(self.conv1(input_features))
|
||||
@@ -515,11 +510,10 @@ class GlmAsrEncoder(nn.Module):
|
||||
|
||||
|
||||
class GlmAsrFeatureInputs(TensorSchema):
|
||||
"""
|
||||
Dimensions:
|
||||
- num_chunks: Number of audio chunks (flattened)
|
||||
- nmb: Number of mel bins
|
||||
- num_audios: Number of original audio files
|
||||
"""Dimensions:
|
||||
- num_chunks: Number of audio chunks (flattened)
|
||||
- nmb: Number of mel bins
|
||||
- num_audios: Number of original audio files
|
||||
"""
|
||||
|
||||
type: Literal["audio_features"]
|
||||
@@ -538,12 +532,11 @@ class GlmAsrFeatureInputs(TensorSchema):
|
||||
|
||||
|
||||
class GlmAsrEmbeddingInputs(TensorSchema):
|
||||
"""
|
||||
Dimensions:
|
||||
- bn: Batch size
|
||||
- naf: Number of audio features
|
||||
- hs: Hidden size (must match the hidden size of language model
|
||||
backbone)
|
||||
"""Dimensions:
|
||||
- bn: Batch size
|
||||
- naf: Number of audio features
|
||||
- hs: Hidden size (must match the hidden size of language model
|
||||
backbone)
|
||||
"""
|
||||
|
||||
type: Literal["audio_embeds"] = "audio_embeds"
|
||||
@@ -557,8 +550,7 @@ GlmAsrInputs: TypeAlias = GlmAsrFeatureInputs | GlmAsrEmbeddingInputs
|
||||
|
||||
|
||||
class GlmAsrMultiModalProjector(nn.Module):
|
||||
"""
|
||||
Projects audio encoder outputs to language model hidden space.
|
||||
"""Projects audio encoder outputs to language model hidden space.
|
||||
|
||||
This projector uses a two-layer MLP to map audio features from the
|
||||
encoder's intermediate size to the language model's hidden size.
|
||||
@@ -601,8 +593,7 @@ class GlmAsrMultiModalProjector(nn.Module):
|
||||
def _glmasr_field_config(
|
||||
hf_inputs: Mapping[str, torch.Tensor],
|
||||
) -> dict[str, MultiModalFieldConfig]:
|
||||
"""
|
||||
Configure multimodal field batching strategy for GLM-ASR.
|
||||
"""Configure multimodal field batching strategy for GLM-ASR.
|
||||
|
||||
Determines how to batch audio inputs based on whether chunking is used.
|
||||
When chunk_counts is present, features are flattened across chunks;
|
||||
@@ -614,6 +605,7 @@ def _glmasr_field_config(
|
||||
Returns:
|
||||
Dictionary mapping field names to MultiModalFieldConfig objects \
|
||||
that specify batching behavior.
|
||||
|
||||
"""
|
||||
chunk_counts = hf_inputs.get("chunk_counts")
|
||||
if chunk_counts is not None:
|
||||
@@ -636,8 +628,7 @@ def _glmasr_field_config(
|
||||
|
||||
|
||||
class GlmAsrMultiModalDataParser(MultiModalDataParser):
|
||||
"""
|
||||
Custom parser for GLM-ASR multimodal data.
|
||||
"""Custom parser for GLM-ASR multimodal data.
|
||||
|
||||
Extends the base parser to handle GLM-ASR specific audio data formats,
|
||||
including both pre-computed audio embeddings and raw audio features.
|
||||
@@ -658,8 +649,7 @@ class GlmAsrMultiModalDataParser(MultiModalDataParser):
|
||||
|
||||
|
||||
class GlmAsrProcessingInfo(BaseProcessingInfo):
|
||||
"""
|
||||
Processing information provider for GLM-ASR model.
|
||||
"""Processing information provider for GLM-ASR model.
|
||||
|
||||
Provides access to model configuration, processor, and feature extractor
|
||||
needed for audio preprocessing and multimodal integration.
|
||||
@@ -687,8 +677,7 @@ class GlmAsrProcessingInfo(BaseProcessingInfo):
|
||||
|
||||
|
||||
class GlmAsrDummyInputsBuilder(BaseDummyInputsBuilder[GlmAsrProcessingInfo]):
|
||||
"""
|
||||
Builder for dummy inputs used in profiling and testing.
|
||||
"""Builder for dummy inputs used in profiling and testing.
|
||||
|
||||
Generates dummy text prompts and audio data that match the expected
|
||||
format for GLM-ASR model inputs. Used for memory profiling and
|
||||
@@ -727,8 +716,7 @@ class GlmAsrDummyInputsBuilder(BaseDummyInputsBuilder[GlmAsrProcessingInfo]):
|
||||
|
||||
|
||||
class GlmAsrMultiModalProcessor(BaseMultiModalProcessor["GlmAsrProcessingInfo"]):
|
||||
"""
|
||||
GLM-ASR processor that inherits directly from BaseMultiModalProcessor
|
||||
"""GLM-ASR processor that inherits directly from BaseMultiModalProcessor
|
||||
for better performance and cleaner implementation.
|
||||
"""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user