Feature Extraction
Transformers
Safetensors
audio_embeddings
audio
custom_code
self-supervised-learning
audio-embeddings
best-rq-2
audioset
Instructions to use ltuncay/BEST-RQ-2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ltuncay/BEST-RQ-2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="ltuncay/BEST-RQ-2", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("ltuncay/BEST-RQ-2", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 9,208 Bytes
86dc2b6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | # MIT License
#
# Copyright (c) 2026 audio-embeddings contributors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import annotations
from collections.abc import Sequence
from typing import Callable
import torch
import torch.nn as nn
from timm.layers import DropPath
from timm.layers import Mlp
from .normalization import MixedPrecisionRMSNorm
from .rope import RoPEAttention
from .rope import RotaryEmbedding1D
from .rope import RotaryEmbedding2D
class FullAttentionResidual(nn.Module):
"""Softmax attention over all preceding outputs along model depth."""
def __init__(self, dim: int) -> None:
super().__init__()
self.dim = dim
self.norm = MixedPrecisionRMSNorm(dim)
self.query = nn.Parameter(torch.zeros(dim))
def forward(self, values: Sequence[torch.Tensor]) -> torch.Tensor:
if not values:
raise ValueError("FullAttentionResidual requires at least one value")
expected_shape = values[0].shape
if len(expected_shape) != 3 or expected_shape[-1] != self.dim:
raise ValueError(
"FullAttentionResidual values must have shape [B, N, D] with "
f"D={self.dim}, got {expected_shape}"
)
for index, value in enumerate(values[1:], start=1):
if value.shape != expected_shape:
raise ValueError(
"FullAttentionResidual values must have identical shapes; "
f"value 0 has {expected_shape}, value {index} has {value.shape}"
)
stacked_values = torch.stack(tuple(values), dim=0)
keys = self.norm(stacked_values)
logits = torch.einsum("d,l b n d->l b n", self.query, keys)
weights = logits.softmax(dim=0)
return torch.einsum("l b n,l b n d->b n d", weights, stacked_values)
def build_norm_layer(
*,
dim: int,
norm_type: str = "layernorm",
norm_layer: Callable[[int], nn.Module] | None = None,
norm_eps: float | None = None,
) -> nn.Module:
"""Build a token-channel normalization layer.
`norm_layer` is kept for backward compatibility with direct Python
construction. Configs should prefer `norm_type` so choices are explicit in
Hydra overrides and experiment files.
"""
if norm_layer is not None:
if norm_eps is not None:
raise ValueError("Set norm_eps or a custom norm_layer, not both")
return norm_layer(dim)
if norm_eps is not None and norm_eps <= 0:
raise ValueError(f"norm_eps must be positive, got {norm_eps}")
kwargs = {} if norm_eps is None else {"eps": norm_eps}
normalized = norm_type.strip().lower().replace("_", "")
if normalized == "layernorm":
return nn.LayerNorm(dim, **kwargs)
if normalized == "rmsnorm":
return MixedPrecisionRMSNorm(dim, **kwargs)
raise ValueError(
f"Unknown norm_type={norm_type!r}; expected 'layernorm' or 'rmsnorm'"
)
def build_gelu_mlp(
*,
dim: int,
mlp_ratio: float,
act_layer: type[nn.Module],
drop: float,
bias: bool = True,
) -> nn.Module:
"""Build the baseline ViT feed-forward layer used before ablations."""
return Mlp(
in_features=dim,
hidden_features=int(dim * mlp_ratio),
act_layer=act_layer,
bias=bias,
drop=drop,
)
class SwiGLUMlp(nn.Module):
"""SwiGLU feed-forward layer for transformer ablations.
The hidden dimension is controlled by `mlp_ratio` in the same place as the
baseline MLP. GLU variants normally use a smaller ratio such as 8/3 because
they have two input projections before the output projection.
"""
def __init__(
self,
*,
dim: int,
hidden_features: int,
drop: float,
bias: bool = True,
) -> None:
super().__init__()
self.gate = nn.Linear(dim, hidden_features, bias=bias)
self.value = nn.Linear(dim, hidden_features, bias=bias)
self.proj = nn.Linear(hidden_features, dim, bias=bias)
self.drop = nn.Dropout(drop)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = nn.functional.silu(self.gate(x)) * self.value(x)
x = self.drop(x)
x = self.proj(x)
return self.drop(x)
def build_mlp(
*,
dim: int,
mlp_ratio: float,
mlp_type: str = "gelu_mlp",
act_layer: type[nn.Module],
drop: float,
bias: bool = True,
) -> nn.Module:
"""Build the transformer feed-forward layer selected by config."""
hidden_features = int(dim * mlp_ratio)
normalized = mlp_type.strip().lower().replace("-", "_")
if normalized == "gelu_mlp":
return build_gelu_mlp(
dim=dim,
mlp_ratio=mlp_ratio,
act_layer=act_layer,
drop=drop,
bias=bias,
)
if normalized == "swiglu":
return SwiGLUMlp(
dim=dim,
hidden_features=hidden_features,
drop=drop,
bias=bias,
)
raise ValueError(f"Unknown mlp_type={mlp_type!r}; expected 'gelu_mlp' or 'swiglu'")
class RoPEBlock(nn.Module):
"""Pre-norm transformer block with RoPE-aware attention."""
def __init__(
self,
dim: int,
num_heads: int,
mlp_ratio: float = 4.0,
qkv_bias: bool = False,
proj_bias: bool = True,
mlp_bias: bool = True,
qk_norm: bool = False,
qk_norm_type: str = "layernorm",
mlp_type: str = "gelu_mlp",
proj_drop: float = 0.0,
attn_drop: float = 0.0,
drop_path: float = 0.0,
act_layer: type[nn.Module] = nn.GELU,
norm_type: str = "layernorm",
norm_layer: Callable[[int], nn.Module] | None = None,
rope: RotaryEmbedding1D | RotaryEmbedding2D | None = None,
residual_type: str = "standard",
norm_eps: float | None = None,
) -> None:
super().__init__()
self.residual_type = residual_type.strip().lower().replace("-", "_")
if self.residual_type not in {"standard", "full_attnres"}:
raise ValueError(
f"Unknown residual_type={residual_type!r}; expected 'standard' "
"or 'full_attnres'"
)
self.norm1 = build_norm_layer(
dim=dim,
norm_type=norm_type,
norm_layer=norm_layer,
norm_eps=norm_eps,
)
self.attn = RoPEAttention(
dim,
num_heads=num_heads,
qkv_bias=qkv_bias,
proj_bias=proj_bias,
attn_drop=attn_drop,
proj_drop=proj_drop,
rope=rope,
qk_norm=qk_norm,
qk_norm_type=qk_norm_type,
)
self.norm2 = build_norm_layer(
dim=dim,
norm_type=norm_type,
norm_layer=norm_layer,
norm_eps=norm_eps,
)
self.mlp = build_mlp(
dim=dim,
mlp_ratio=mlp_ratio,
mlp_type=mlp_type,
act_layer=act_layer,
drop=proj_drop,
bias=mlp_bias,
)
self.drop_path = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
if self.residual_type == "full_attnres":
self.attention_residual = FullAttentionResidual(dim)
self.mlp_residual = FullAttentionResidual(dim)
else:
self.attention_residual = None
self.mlp_residual = None
def attention_output(
self,
x: torch.Tensor,
pos_ids: torch.Tensor | None = None,
grid_size: tuple[int, int] | None = None,
) -> torch.Tensor:
return self.drop_path(
self.attn(self.norm1(x), pos_ids=pos_ids, grid_size=grid_size)
)
def mlp_output(self, x: torch.Tensor) -> torch.Tensor:
return self.drop_path(self.mlp(self.norm2(x)))
def forward(
self,
x: torch.Tensor,
pos_ids: torch.Tensor | None = None,
grid_size: tuple[int, int] | None = None,
) -> torch.Tensor:
x = x + self.attention_output(x, pos_ids=pos_ids, grid_size=grid_size)
x = x + self.mlp_output(x)
return x
|