Spaces:
Running
on
A100
Running
on
A100
File size: 5,379 Bytes
174ae06 |
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 |
# Copyright (c) 2025 NVIDIA CORPORATION.
# Licensed under the MIT license.
# Adapted from https://github.com/NVlabs/VILA/tree/main under the Apache 2.0 license.
# LICENSE is in incl_licenses directory.
# Copyright 2024 NVIDIA CORPORATION & AFFILIATES
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# This file is modified from https://github.com/haotian-liu/LLaVA/
from abc import abstractmethod
import torch
import torch.nn as nn
import torch.nn.functional as F
class SoundTower(nn.Module):
def __init__(self, sound_tower, args, delay_load=False):
super().__init__()
self.is_loaded = False
self.sound_tower_name = sound_tower
self.cfg_only = None
def _get_feat_extract_output_lengths(self, input_lengths: torch.LongTensor):
"""
Computes the output length of the convolutional layers and the output length of the audio encoder
"""
input_lengths = (input_lengths - 1) // 2 + 1
output_lengths = (input_lengths - 2) // 2 + 1
return input_lengths, output_lengths
def forward(self, sounds, mask=None):
if type(sounds) is list:
sound_features = []
for sound in sounds:
# Calculate attention mask
audio_feat_lengths, audio_output_lengths = self._get_feat_extract_output_lengths(mask.sum(-1))
# for cases where only one window is there for the audio_clip
batch_size, _, max_mel_seq_len = sound.shape
max_seq_len = (max_mel_seq_len - 2) // 2 + 1
seq_range = (
torch.arange(0, max_seq_len, dtype=audio_feat_lengths.dtype, device=audio_feat_lengths.device)
.unsqueeze(0)
.expand(batch_size, max_seq_len)
)
lengths_expand = audio_feat_lengths.unsqueeze(1).expand(batch_size, max_seq_len)
padding_mask = seq_range >= lengths_expand
audio_attention_mask_ = padding_mask.view(batch_size, 1, 1, max_seq_len).expand(
batch_size, 1, max_seq_len, max_seq_len
)
audio_attention_mask = audio_attention_mask_.to(
dtype=self.sound_tower.conv1.weight.dtype, device=self.sound_tower.conv1.weight.device
)
audio_attention_mask[audio_attention_mask_] = float("-inf")
# Calculate features
sound_feature = self.sound_tower(sound, attention_mask=audio_attention_mask)
sound_feature = sound_feature.to(sound.dtype)
sound_feature = sound_feature.last_hidden_state
sound_features.append(sound_feature)
else:
# Calculate attention mask
if len(sounds.shape) == 5:
sounds = sounds.squeeze(0).squeeze(1)
mask = mask.squeeze(0)
audio_feat_lengths, audio_output_lengths = self._get_feat_extract_output_lengths(mask.sum(-1))
# for cases where only one window is there for the audio_clip
batch_size, _, max_mel_seq_len = sounds.shape
max_seq_len = (max_mel_seq_len - 2) // 2 + 1
seq_range = (
torch.arange(0, max_seq_len, dtype=audio_feat_lengths.dtype, device=audio_feat_lengths.device)
.unsqueeze(0)
.expand(batch_size, max_seq_len)
)
lengths_expand = audio_feat_lengths.expand(batch_size, max_seq_len)
padding_mask = seq_range >= lengths_expand
audio_attention_mask_ = padding_mask.view(batch_size, 1, 1, max_seq_len).expand(
batch_size, 1, max_seq_len, max_seq_len
)
audio_attention_mask = audio_attention_mask_.to(
dtype=self.sound_tower.conv1.weight.dtype, device=self.sound_tower.conv1.weight.device
)
audio_attention_mask[audio_attention_mask_] = float("-inf")
# Calculate features
sound_features = self.sound_tower(sounds, attention_mask=audio_attention_mask)
sound_features = sound_features.last_hidden_state
sound_features = sound_features.to(sounds.dtype)
return sound_features
@property
def dummy_feature(self):
return torch.zeros(1, self.hidden_size, device=self.device, dtype=self.dtype)
@property
def dtype(self):
return self.sound_tower.dtype
@property
def config(self):
if self.is_loaded:
return self.sound_tower.config
else:
return self.cfg_only
@property
def device(self):
return self.sound_tower.device
@property
def hidden_size(self):
return self.config.hidden_size
|