Spaces:
Running
on
Zero
Running
on
Zero
File size: 13,719 Bytes
fc605f9 f37d52e fc605f9 4925c32 f37d52e fc605f9 f37d52e fc605f9 |
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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# Copyright (c) Meta Platforms, Inc. and affiliates. All Rights Reserved\n
import math
import re
from dataclasses import dataclass
from typing import Any, Dict, Optional
import torch
from core.audio_visual_encoder import PEAudioFrame, PEAudioFrameTransform
from torchdiffeq import odeint
from sam_audio.model.align import AlignModalities
from sam_audio.model.base import BaseModel
from sam_audio.model.codec import DACVAE
from sam_audio.model.config import SAMAudioConfig
from sam_audio.model.text_encoder import T5TextEncoder
from sam_audio.model.transformer import DiT
from sam_audio.model.vision_encoder import PerceptionEncoder
from sam_audio.processor import Batch
from sam_audio.ranking import create_ranker
DFLT_ODE_OPT = {"method": "midpoint", "options": {"step_size": 2 / 32}}
class SinusoidalEmbedding(torch.nn.Module):
def __init__(self, dim, theta=10000):
super().__init__()
assert (dim % 2) == 0
half_dim = dim // 2
inv_freq = torch.exp(
-math.log(theta) * torch.arange(half_dim).float() / half_dim
)
self.register_buffer("inv_freq", inv_freq, persistent=False)
def forward(self, x, pos=None):
if pos is None:
seq_len, device = x.shape[1], x.device
pos = torch.arange(seq_len, device=device)
emb = torch.einsum("i, j -> i j", pos, self.inv_freq)
emb = torch.cat((emb.cos(), emb.sin()), dim=-1)
return emb
class EmbedAnchors(torch.nn.Module):
def __init__(self, num_embeddings: int, embedding_dim: int, out_dim: int):
super().__init__()
self.embed = torch.nn.Embedding(
num_embeddings + 1, embedding_dim, padding_idx=num_embeddings
)
self.gate = torch.nn.Parameter(torch.tensor([0.0]))
self.proj = torch.nn.Linear(embedding_dim, out_dim, bias=False)
def forward(
self,
x: torch.Tensor,
anchor_ids: Optional[torch.Tensor] = None,
anchor_alignment: Optional[torch.Tensor] = None,
):
if anchor_ids is None:
return x
embs = self.embed(anchor_ids.gather(1, anchor_alignment))
proj = self.proj(embs)
return x + self.gate.tanh() * proj
@dataclass
class SeparationResult:
target: torch.Tensor
residual: torch.Tensor
noise: torch.Tensor
class SAMAudio(BaseModel):
config_cls = SAMAudioConfig
revision = None
def __init__(self, cfg: SAMAudioConfig):
super().__init__()
self.audio_codec = DACVAE(cfg.audio_codec)
self.text_encoder = T5TextEncoder(cfg.text_encoder)
self.vision_encoder = PerceptionEncoder(cfg.vision_encoder)
self.transformer = DiT(cfg.transformer)
self.proj = torch.nn.Linear(cfg.in_channels, cfg.transformer.dim)
self.align_masked_video = AlignModalities(
cfg.vision_encoder.dim, cfg.transformer.dim
)
self.embed_anchors = EmbedAnchors(
cfg.num_anchors, cfg.anchor_embedding_dim, cfg.transformer.dim
)
self.memory_proj = torch.nn.Linear(cfg.text_encoder.dim, cfg.transformer.dim)
self.timestep_emb = SinusoidalEmbedding(cfg.transformer.dim)
self.visual_ranker = create_ranker(cfg.visual_ranker)
self.text_ranker = create_ranker(cfg.text_ranker)
if cfg.span_predictor is not None:
self.span_predictor = PEAudioFrame.from_config(
cfg.span_predictor, pretrained=True
)
self.span_predictor_transform = PEAudioFrameTransform.from_config(
cfg.span_predictor
)
@property
def sample_rate(self):
return self.audio_codec.sample_rate
def align_inputs(
self,
noisy_audio,
audio_features: torch.Tensor,
masked_video_features: Optional[torch.Tensor] = None,
anchor_ids: Optional[torch.Tensor] = None,
anchor_alignment: Optional[torch.Tensor] = None,
):
x = torch.cat(
[
noisy_audio,
torch.zeros_like(audio_features),
audio_features,
],
dim=2,
)
projected = self.proj(x)
aligned = self.align_masked_video(projected, masked_video_features)
aligned = self.embed_anchors(aligned, anchor_ids, anchor_alignment)
return aligned
def forward(
self,
noisy_audio: torch.Tensor,
audio_features: torch.Tensor,
text_features: torch.Tensor,
time: torch.Tensor,
masked_video_features: Optional[torch.Tensor] = None,
text_mask: Optional[torch.Tensor] = None,
anchor_ids: Optional[torch.Tensor] = None,
anchor_alignment: Optional[torch.Tensor] = None,
audio_pad_mask: Optional[torch.Tensor] = None,
):
"""
Forward pass for the model. Represents one function evaluation of the ODE.
In the below descriptions, B is batch size, T is sequence length, C is channel size.
Note that the size of C and T may vary across arguments (ex. text_features vs. audio_features),
it is used only to designate a Channel or time/sequence-length dimension respectively.
Args:
noisy_audio (torch.Tensor): Noisy audio input tensor (being denoised).
audio_features (torch.Tensor): Clean audio features [B x T x C].
text_features (torch.Tensor): Encoded text features tensor [B x T x C].
time (torch.Tensor): Timestep tensor for positional encoding [B].
masked_video_features (Optional[torch.Tensor], optional): Masked video features tensor. [B x C x T].
text_mask (Optional[torch.Tensor], optional): Padding mask for text features. [B x T].
anchor_ids (Optional[torch.Tensor], optional): Anchor IDs tensor. Defaults to None [B x T].
anchor_alignment (Optional[torch.Tensor], optional): Anchor alignment tensor. B x T.
audio_pad_mask (Optional[torch.Tensor], optional): Padding mask for audio input. [B x T].
Returns:
torch.Tensor
"""
aligned_inputs = self.align_inputs(
noisy_audio,
audio_features,
masked_video_features=masked_video_features,
anchor_ids=anchor_ids,
anchor_alignment=anchor_alignment,
)
memory = timestep_emb = self.timestep_emb(time, pos=time).unsqueeze(1)
if text_features is not None:
memory = self.memory_proj(text_features) + timestep_emb
return self.transformer(
aligned_inputs,
time,
padding_mask=audio_pad_mask,
memory=memory,
memory_padding_mask=text_mask,
)
def _get_audio_features(self, audios: torch.Tensor):
audio_features = self.audio_codec(audios).transpose(1, 2)
return torch.cat([audio_features, audio_features], dim=2)
def _get_video_features(self, video, audio_features):
B, T, _ = audio_features.shape
if video is None:
return audio_features.new_zeros(B, self.vision_encoder.dim, T)
else:
return self.vision_encoder(video).transpose(1, 2)
def _repeat_for_reranking(self, tensor, candidates):
if candidates > 1:
B = tensor.size(0)
rest = tensor.shape[1:]
return (
tensor.unsqueeze(1)
.expand(B, candidates, *rest)
.reshape(B * candidates, *rest)
)
else:
return tensor
def _unrepeat_from_reranking(self, tensor, candidates):
return tensor[::candidates]
def _get_forward_args(self, batch: Batch, candidates: int = 1):
audio_features = self._get_audio_features(batch.audios)
text_features, text_mask = self.text_encoder(batch.descriptions)
masked_video_features = self._get_video_features(
batch.masked_video, audio_features
)
return {
"audio_features": self._repeat_for_reranking(audio_features, candidates),
"text_features": self._repeat_for_reranking(text_features, candidates),
"text_mask": self._repeat_for_reranking(text_mask, candidates),
"masked_video_features": self._repeat_for_reranking(
masked_video_features, candidates
),
"anchor_ids": self._repeat_for_reranking(batch.anchor_ids, candidates),
"anchor_alignment": self._repeat_for_reranking(
batch.anchor_alignment, candidates
),
"audio_pad_mask": self._repeat_for_reranking(
batch.audio_pad_mask, candidates
),
}
def predict_spans(
self, batch: Batch, audio_features: torch.Tensor, audio_pad_mask: torch.Tensor
) -> Batch:
input = self.span_predictor_transform(text=batch.descriptions).to(
audio_features.device
)
output = self.span_predictor(
input_features=audio_features[:, :, :128],
padding_mask=audio_pad_mask,
return_spans=True,
**input,
)
anchors = [[["+"] + anchor for anchor in anchors] for anchors in output.spans]
batch.process_anchors(anchors)
return batch
@torch.inference_mode()
def separate(
self,
batch: Batch,
noise: Optional[torch.Tensor] = None,
ode_opt: Dict[str, Any] = DFLT_ODE_OPT,
reranking_candidates: int = 1,
predict_spans: bool = False,
) -> SeparationResult:
# Encode audio
forward_args = self._get_forward_args(batch, candidates=reranking_candidates)
if predict_spans and hasattr(self, "span_predictor") and batch.anchors is None:
batch = self.predict_spans(
batch=batch,
audio_features=self._unrepeat_from_reranking(
forward_args["audio_features"], reranking_candidates
),
audio_pad_mask=self._unrepeat_from_reranking(
forward_args["audio_pad_mask"], reranking_candidates
),
)
audio_features = forward_args["audio_features"]
B, T, C = audio_features.shape
C = C // 2 # we stack audio_features, so the actual channels is half
if noise is None:
noise = torch.randn_like(audio_features)
def vector_field(t, noisy_audio):
res = self.forward(
noisy_audio=noisy_audio,
time=t.expand(noisy_audio.size(0)),
**forward_args,
)
return res
states = odeint(
vector_field,
noise,
torch.tensor([0.0, 1.0], device=noise.device),
**ode_opt,
)
generated_features = states[-1].transpose(1, 2)
# generated_features has shape [B, 2C, T]. Reshape to stack along the batch dimension
wavs = self.audio_codec.decode(generated_features.reshape(2 * B, C, T)).view(
B, 2, -1
)
bsz = wavs.size(0) // reranking_candidates
sizes = self.audio_codec.feature_idx_to_wav_idx(batch.sizes)
target_wavs = self.unbatch(
wavs[:, 0].view(bsz, reranking_candidates, -1), sizes
)
residual_wavs = self.unbatch(
wavs[:, 1].view(bsz, reranking_candidates, -1), sizes
)
if (
reranking_candidates > 1
and batch.masked_video is not None
and self.visual_ranker is not None
):
scores = self.visual_ranker(
extracted_audio=target_wavs,
videos=batch.masked_video,
sample_rate=self.audio_codec.sample_rate,
)
idxs = scores.argmax(dim=1)
elif reranking_candidates > 1 and self.text_ranker is not None:
input_audio = [
audio[:, :size].expand(reranking_candidates, -1)
for audio, size in zip(batch.audios, sizes, strict=False)
]
scores = self.text_ranker(
extracted_audio=target_wavs,
input_audio=input_audio,
descriptions=batch.descriptions,
sample_rate=self.audio_codec.sample_rate,
)
idxs = scores.argmax(dim=1)
else:
idxs = torch.zeros(bsz, dtype=torch.long, device=noise.device)
return SeparationResult(
target=[wav[idx] for wav, idx in zip(target_wavs, idxs, strict=False)],
residual=[
wavs[idx] for wavs, idx in zip(residual_wavs, idxs, strict=False)
],
noise=noise,
)
def unbatch(self, wavs: torch.Tensor, sizes: torch.Tensor, time_dim: int = -1):
result = []
for row, size in zip(wavs, sizes, strict=False):
result.append(row.narrow(dim=time_dim, start=0, length=size))
return result
def load_state_dict(self, state_dict, strict=True):
if strict:
missing_keys, unexpected_keys = super().load_state_dict(
state_dict, strict=False
)
# We load this directly from HF, not in checkpoint
skip_regex = re.compile(
"(^text_encoder|^visual_ranker|^text_ranker|^span_predictor)"
)
missing_keys = [x for x in missing_keys if not re.search(skip_regex, x)]
if len(missing_keys) > 0 or len(unexpected_keys) > 0:
raise RuntimeError(
f"Missing keys: {missing_keys}, unexpected_keys: {unexpected_keys}"
)
__all__ = ["SAMAudio"]
|