Instructions to use enactic/avista-base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use enactic/avista-base with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("automatic-speech-recognition", model="enactic/avista-base", trust_remote_code=True)# Load model directly from transformers import AutoModelForSpeechSeq2Seq model = AutoModelForSpeechSeq2Seq.from_pretrained("enactic/avista-base", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
Update modeling_avhubert.py
Browse files- modeling_avhubert.py +53 -27
modeling_avhubert.py
CHANGED
|
@@ -7,7 +7,7 @@ import torch
|
|
| 7 |
import torch.nn as nn
|
| 8 |
import torch.nn.functional as F
|
| 9 |
from transformers import PreTrainedModel
|
| 10 |
-
from transformers.cache_utils import StaticCache
|
| 11 |
from transformers.generation import GenerationMixin
|
| 12 |
from transformers.generation.utils import GenerationConfig, GenerationMode
|
| 13 |
from transformers.integrations.deepspeed import is_deepspeed_zero3_enabled
|
|
@@ -31,7 +31,7 @@ NEED_SETUP_CACHE_CLASSES_MAPPING = {
|
|
| 31 |
|
| 32 |
|
| 33 |
@dataclass
|
| 34 |
-
class AVHubertOutput:
|
| 35 |
last_hidden_state: Optional[torch.Tensor] = None
|
| 36 |
hidden_states: Optional[torch.Tensor] = None
|
| 37 |
attentions: Optional[torch.Tensor] = None
|
|
@@ -70,6 +70,7 @@ class AVHubertPreTrainedModel(PreTrainedModel):
|
|
| 70 |
|
| 71 |
config_class = AVHubertConfig
|
| 72 |
base_model_prefix = "avhubert"
|
|
|
|
| 73 |
supports_gradient_checkpointing = False
|
| 74 |
|
| 75 |
def _init_weights(self, module):
|
|
@@ -214,6 +215,8 @@ class AVHubertModel(AVHubertPreTrainedModel):
|
|
| 214 |
|
| 215 |
|
| 216 |
class AVHubertForConditionalGeneration(AVHubertPreTrainedModel, GenerationMixin):
|
|
|
|
|
|
|
| 217 |
def __init__(
|
| 218 |
self,
|
| 219 |
config: AVHubertConfig,
|
|
@@ -282,27 +285,57 @@ class AVHubertForConditionalGeneration(AVHubertPreTrainedModel, GenerationMixin)
|
|
| 282 |
padding_mask: Optional[torch.Tensor] = None,
|
| 283 |
decoder_input_ids: Optional[torch.Tensor] = None,
|
| 284 |
decoder_attention_mask: Optional[torch.Tensor] = None,
|
|
|
|
|
|
|
| 285 |
labels: Optional[torch.Tensor] = None,
|
|
|
|
| 286 |
output_attentions: bool = False,
|
| 287 |
output_hidden_states: bool = False,
|
|
|
|
| 288 |
return_dict: bool = True,
|
| 289 |
) -> ModelOutput:
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
embed_tokens = self.embed_tokens(decoder_input_ids)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
hidden_states = self.decoder(
|
| 300 |
inputs_embeds=embed_tokens,
|
| 301 |
attention_mask=decoder_attention_mask,
|
| 302 |
encoder_hidden_states=encoder_outs.last_hidden_state,
|
| 303 |
-
encoder_attention_mask=
|
|
|
|
|
|
|
| 304 |
output_attentions=output_attentions,
|
| 305 |
output_hidden_states=output_hidden_states,
|
|
|
|
| 306 |
)
|
| 307 |
|
| 308 |
if self.config.share_decoder_input_output_embed:
|
|
@@ -318,10 +351,10 @@ class AVHubertForConditionalGeneration(AVHubertPreTrainedModel, GenerationMixin)
|
|
| 318 |
return Seq2SeqLMOutput(
|
| 319 |
loss=loss,
|
| 320 |
logits=logits,
|
| 321 |
-
past_key_values=
|
| 322 |
decoder_hidden_states=hidden_states.hidden_states,
|
| 323 |
decoder_attentions=hidden_states.attentions,
|
| 324 |
-
cross_attentions=
|
| 325 |
encoder_last_hidden_state=encoder_outs.last_hidden_state,
|
| 326 |
encoder_hidden_states=encoder_outs.hidden_states,
|
| 327 |
encoder_attentions=encoder_outs.attentions,
|
|
@@ -372,20 +405,13 @@ class AVHubertForConditionalGeneration(AVHubertPreTrainedModel, GenerationMixin)
|
|
| 372 |
def prepare_inputs_for_generation(
|
| 373 |
self,
|
| 374 |
input_ids: torch.Tensor = None,
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
decoder_input_ids: Optional[torch.Tensor] = None,
|
| 378 |
-
decoder_attention_mask: Optional[torch.Tensor] = None,
|
| 379 |
-
padding_mask: Optional[torch.Tensor] = None,
|
| 380 |
**kwargs,
|
| 381 |
):
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
"decoder_input_ids": decoder_input_ids,
|
| 389 |
-
"decoder_attention_mask": decoder_attention_mask,
|
| 390 |
-
"padding_mask": padding_mask,
|
| 391 |
-
}
|
|
|
|
| 7 |
import torch.nn as nn
|
| 8 |
import torch.nn.functional as F
|
| 9 |
from transformers import PreTrainedModel
|
| 10 |
+
from transformers.cache_utils import Cache, StaticCache
|
| 11 |
from transformers.generation import GenerationMixin
|
| 12 |
from transformers.generation.utils import GenerationConfig, GenerationMode
|
| 13 |
from transformers.integrations.deepspeed import is_deepspeed_zero3_enabled
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
@dataclass
|
| 34 |
+
class AVHubertOutput(ModelOutput):
|
| 35 |
last_hidden_state: Optional[torch.Tensor] = None
|
| 36 |
hidden_states: Optional[torch.Tensor] = None
|
| 37 |
attentions: Optional[torch.Tensor] = None
|
|
|
|
| 70 |
|
| 71 |
config_class = AVHubertConfig
|
| 72 |
base_model_prefix = "avhubert"
|
| 73 |
+
main_input_name = "input_values"
|
| 74 |
supports_gradient_checkpointing = False
|
| 75 |
|
| 76 |
def _init_weights(self, module):
|
|
|
|
| 215 |
|
| 216 |
|
| 217 |
class AVHubertForConditionalGeneration(AVHubertPreTrainedModel, GenerationMixin):
|
| 218 |
+
_supports_cache_class = True
|
| 219 |
+
|
| 220 |
def __init__(
|
| 221 |
self,
|
| 222 |
config: AVHubertConfig,
|
|
|
|
| 285 |
padding_mask: Optional[torch.Tensor] = None,
|
| 286 |
decoder_input_ids: Optional[torch.Tensor] = None,
|
| 287 |
decoder_attention_mask: Optional[torch.Tensor] = None,
|
| 288 |
+
encoder_outputs: Optional[ModelOutput] = None,
|
| 289 |
+
past_key_values: Optional[Cache] = None,
|
| 290 |
labels: Optional[torch.Tensor] = None,
|
| 291 |
+
use_cache: Optional[bool] = None,
|
| 292 |
output_attentions: bool = False,
|
| 293 |
output_hidden_states: bool = False,
|
| 294 |
+
cache_position: Optional[torch.Tensor] = None,
|
| 295 |
return_dict: bool = True,
|
| 296 |
) -> ModelOutput:
|
| 297 |
+
if encoder_outputs is None:
|
| 298 |
+
encoder_outs = self.avhubert(
|
| 299 |
+
input_values=input_values,
|
| 300 |
+
pixel_values=pixel_values,
|
| 301 |
+
padding_mask=padding_mask,
|
| 302 |
+
output_attentions=output_attentions,
|
| 303 |
+
output_hidden_states=output_hidden_states,
|
| 304 |
+
)
|
| 305 |
+
elif isinstance(encoder_outputs, ModelOutput):
|
| 306 |
+
encoder_outs = encoder_outputs
|
| 307 |
+
else:
|
| 308 |
+
encoder_outs = AVHubertOutput(
|
| 309 |
+
last_hidden_state=encoder_outputs[0],
|
| 310 |
+
hidden_states=encoder_outputs[1] if len(encoder_outputs) > 1 else None,
|
| 311 |
+
attentions=encoder_outputs[2] if len(encoder_outputs) > 2 else None,
|
| 312 |
+
)
|
| 313 |
+
|
| 314 |
+
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
| 315 |
+
if use_cache is None:
|
| 316 |
+
use_cache = True
|
| 317 |
+
if labels is not None:
|
| 318 |
+
use_cache = False
|
| 319 |
|
| 320 |
embed_tokens = self.embed_tokens(decoder_input_ids)
|
| 321 |
+
if padding_mask is None:
|
| 322 |
+
encoder_attention_mask = torch.ones(
|
| 323 |
+
encoder_outs.last_hidden_state.shape[:2],
|
| 324 |
+
dtype=torch.bool,
|
| 325 |
+
device=encoder_outs.last_hidden_state.device,
|
| 326 |
+
)
|
| 327 |
+
else:
|
| 328 |
+
encoder_attention_mask = ~padding_mask.bool()
|
| 329 |
hidden_states = self.decoder(
|
| 330 |
inputs_embeds=embed_tokens,
|
| 331 |
attention_mask=decoder_attention_mask,
|
| 332 |
encoder_hidden_states=encoder_outs.last_hidden_state,
|
| 333 |
+
encoder_attention_mask=encoder_attention_mask,
|
| 334 |
+
past_key_values=past_key_values,
|
| 335 |
+
use_cache=use_cache,
|
| 336 |
output_attentions=output_attentions,
|
| 337 |
output_hidden_states=output_hidden_states,
|
| 338 |
+
cache_position=cache_position,
|
| 339 |
)
|
| 340 |
|
| 341 |
if self.config.share_decoder_input_output_embed:
|
|
|
|
| 351 |
return Seq2SeqLMOutput(
|
| 352 |
loss=loss,
|
| 353 |
logits=logits,
|
| 354 |
+
past_key_values=hidden_states.past_key_values,
|
| 355 |
decoder_hidden_states=hidden_states.hidden_states,
|
| 356 |
decoder_attentions=hidden_states.attentions,
|
| 357 |
+
cross_attentions=hidden_states.cross_attentions,
|
| 358 |
encoder_last_hidden_state=encoder_outs.last_hidden_state,
|
| 359 |
encoder_hidden_states=encoder_outs.hidden_states,
|
| 360 |
encoder_attentions=encoder_outs.attentions,
|
|
|
|
| 405 |
def prepare_inputs_for_generation(
|
| 406 |
self,
|
| 407 |
input_ids: torch.Tensor = None,
|
| 408 |
+
past_key_values: Optional[Cache] = None,
|
| 409 |
+
cache_position: Optional[torch.Tensor] = None,
|
|
|
|
|
|
|
|
|
|
| 410 |
**kwargs,
|
| 411 |
):
|
| 412 |
+
return super().prepare_inputs_for_generation(
|
| 413 |
+
input_ids,
|
| 414 |
+
past_key_values=past_key_values,
|
| 415 |
+
cache_position=cache_position,
|
| 416 |
+
**kwargs,
|
| 417 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|