Instructions to use cointegrated/SONAR_200_converted_text_decoder with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use cointegrated/SONAR_200_converted_text_decoder with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="cointegrated/SONAR_200_converted_text_decoder", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("cointegrated/SONAR_200_converted_text_decoder", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload model
Browse files- config.json +7 -2
- sonar_decoder.py +154 -0
config.json
CHANGED
|
@@ -2,9 +2,14 @@
|
|
| 2 |
"activation_dropout": 0.1,
|
| 3 |
"activation_function": "relu",
|
| 4 |
"architectures": [
|
| 5 |
-
"
|
| 6 |
],
|
| 7 |
"attention_dropout": 0.1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
"bos_token_id": 0,
|
| 9 |
"d_model": 1024,
|
| 10 |
"decoder_attention_heads": 16,
|
|
@@ -22,7 +27,7 @@
|
|
| 22 |
"init_std": 0.02,
|
| 23 |
"is_encoder_decoder": true,
|
| 24 |
"max_position_embeddings": 1024,
|
| 25 |
-
"model_type": "
|
| 26 |
"num_hidden_layers": 24,
|
| 27 |
"pad_token_id": 1,
|
| 28 |
"scale_embedding": true,
|
|
|
|
| 2 |
"activation_dropout": 0.1,
|
| 3 |
"activation_function": "relu",
|
| 4 |
"architectures": [
|
| 5 |
+
"SonarDecoderModel"
|
| 6 |
],
|
| 7 |
"attention_dropout": 0.1,
|
| 8 |
+
"auto_map": {
|
| 9 |
+
"AutoConfig": "sonar_decoder.SonarDecoderConfig",
|
| 10 |
+
"AutoModel": "sonar_decoder.SonarDecoderModel",
|
| 11 |
+
"AutoModelForSeq2SeqLM": "sonar_decoder.SonarDecoderModel"
|
| 12 |
+
},
|
| 13 |
"bos_token_id": 0,
|
| 14 |
"d_model": 1024,
|
| 15 |
"decoder_attention_heads": 16,
|
|
|
|
| 27 |
"init_std": 0.02,
|
| 28 |
"is_encoder_decoder": true,
|
| 29 |
"max_position_embeddings": 1024,
|
| 30 |
+
"model_type": "SonarDecoderModel",
|
| 31 |
"num_hidden_layers": 24,
|
| 32 |
"pad_token_id": 1,
|
| 33 |
"scale_embedding": true,
|
sonar_decoder.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import copy
|
| 2 |
+
|
| 3 |
+
from typing import Optional, Tuple, Union
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
from torch import nn
|
| 7 |
+
|
| 8 |
+
from transformers.models.m2m_100.modeling_m2m_100 import (
|
| 9 |
+
M2M100Config,
|
| 10 |
+
|
| 11 |
+
M2M100ScaledWordEmbedding,
|
| 12 |
+
M2M100Decoder,
|
| 13 |
+
M2M100PreTrainedModel,
|
| 14 |
+
GenerationMixin,
|
| 15 |
+
|
| 16 |
+
Seq2SeqLMOutput,
|
| 17 |
+
BaseModelOutput,
|
| 18 |
+
|
| 19 |
+
shift_tokens_right,
|
| 20 |
+
Cache,
|
| 21 |
+
CrossEntropyLoss,
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# override model type to register AutoModels
|
| 25 |
+
class SonarDecoderConfig(M2M100Config):
|
| 26 |
+
model_type = "SonarDecoderModel"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class SonarDecoderModel(M2M100PreTrainedModel, GenerationMixin):
|
| 30 |
+
# override confing class to register AutoModels
|
| 31 |
+
config_class = SonarDecoderConfig
|
| 32 |
+
_tied_weights_keys = {
|
| 33 |
+
"decoder.embed_tokens.weight": "shared.weight",
|
| 34 |
+
"lm_head.weight": "shared.weight",
|
| 35 |
+
}
|
| 36 |
+
_keys_to_ignore_on_load_unexpected = [r"encoder"]
|
| 37 |
+
|
| 38 |
+
def __init__(self, config: M2M100Config):
|
| 39 |
+
super().__init__(config)
|
| 40 |
+
self.shared = nn.Embedding(config.vocab_size, config.d_model)
|
| 41 |
+
|
| 42 |
+
decoder_config = copy.deepcopy(config)
|
| 43 |
+
decoder_config.use_cache = False
|
| 44 |
+
decoder_config.is_encoder_decoder = False
|
| 45 |
+
self.decoder = M2M100Decoder(decoder_config)
|
| 46 |
+
self.lm_head = nn.Linear(config.d_model, self.shared.num_embeddings, bias=False)
|
| 47 |
+
|
| 48 |
+
# Initialize weights and apply final processing
|
| 49 |
+
self.post_init()
|
| 50 |
+
|
| 51 |
+
def get_input_embeddings(self):
|
| 52 |
+
return self.shared
|
| 53 |
+
|
| 54 |
+
def set_input_embeddings(self, value):
|
| 55 |
+
self.shared = value
|
| 56 |
+
self.decoder.embed_tokens = self.shared
|
| 57 |
+
|
| 58 |
+
def _tie_weights(self):
|
| 59 |
+
if self.config.tie_word_embeddings:
|
| 60 |
+
self._tie_or_clone_weights(self.decoder.embed_tokens, self.shared)
|
| 61 |
+
# in SONAR models, input and output projections are tied (ideally, this should be configurable)
|
| 62 |
+
self._tie_or_clone_weights(self.lm_head, self.shared)
|
| 63 |
+
|
| 64 |
+
def get_decoder(self):
|
| 65 |
+
return self.decoder
|
| 66 |
+
|
| 67 |
+
def forward(
|
| 68 |
+
self,
|
| 69 |
+
input_ids: torch.LongTensor | None = None,
|
| 70 |
+
attention_mask: torch.Tensor | None = None,
|
| 71 |
+
decoder_input_ids: torch.LongTensor | None = None,
|
| 72 |
+
decoder_attention_mask: torch.LongTensor | None = None,
|
| 73 |
+
encoder_outputs: tuple[tuple[torch.FloatTensor]] | None = None,
|
| 74 |
+
past_key_values: Cache | None = None,
|
| 75 |
+
inputs_embeds: torch.FloatTensor | None = None,
|
| 76 |
+
decoder_inputs_embeds: torch.FloatTensor | None = None,
|
| 77 |
+
labels: torch.LongTensor | None = None,
|
| 78 |
+
use_cache: bool | None = None,
|
| 79 |
+
output_attentions: bool | None = None,
|
| 80 |
+
output_hidden_states: bool | None = None,
|
| 81 |
+
return_dict: bool | None = None,
|
| 82 |
+
cache_position: torch.Tensor | None = None,
|
| 83 |
+
**kwargs,
|
| 84 |
+
) -> tuple[torch.Tensor] | Seq2SeqLMOutput:
|
| 85 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 86 |
+
|
| 87 |
+
if labels is not None:
|
| 88 |
+
if decoder_input_ids is None:
|
| 89 |
+
decoder_input_ids = shift_tokens_right(
|
| 90 |
+
labels, self.config.pad_token_id, self.config.decoder_start_token_id
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
if encoder_outputs is None:
|
| 94 |
+
raise ValueError("M2M100DecoderModel expects the `encoder_outputs` to be always present.")
|
| 95 |
+
|
| 96 |
+
if return_dict and not isinstance(encoder_outputs, BaseModelOutput):
|
| 97 |
+
encoder_outputs = BaseModelOutput(
|
| 98 |
+
last_hidden_state=encoder_outputs[0],
|
| 99 |
+
hidden_states=encoder_outputs[1] if len(encoder_outputs) > 1 else None,
|
| 100 |
+
attentions=encoder_outputs[2] if len(encoder_outputs) > 2 else None,
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# decoder outputs consists of (dec_features, past_key_value, dec_hidden, dec_attn)
|
| 104 |
+
decoder_outputs = self.decoder(
|
| 105 |
+
input_ids=decoder_input_ids,
|
| 106 |
+
attention_mask=decoder_attention_mask,
|
| 107 |
+
encoder_hidden_states=encoder_outputs[0],
|
| 108 |
+
encoder_attention_mask=attention_mask,
|
| 109 |
+
past_key_values=past_key_values,
|
| 110 |
+
inputs_embeds=decoder_inputs_embeds,
|
| 111 |
+
use_cache=use_cache,
|
| 112 |
+
output_attentions=output_attentions,
|
| 113 |
+
output_hidden_states=output_hidden_states,
|
| 114 |
+
return_dict=return_dict,
|
| 115 |
+
cache_position=cache_position,
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
lm_logits = self.lm_head(decoder_outputs[0])
|
| 119 |
+
|
| 120 |
+
masked_lm_loss = None
|
| 121 |
+
if labels is not None:
|
| 122 |
+
# move labels to the correct device to enable PP
|
| 123 |
+
labels = labels.to(lm_logits.device)
|
| 124 |
+
loss_fct = CrossEntropyLoss()
|
| 125 |
+
masked_lm_loss = loss_fct(lm_logits.view(-1, self.config.vocab_size), labels.view(-1))
|
| 126 |
+
|
| 127 |
+
if not return_dict:
|
| 128 |
+
output = (lm_logits,) + decoder_outputs[1:]
|
| 129 |
+
return ((masked_lm_loss,) + output) if masked_lm_loss is not None else output
|
| 130 |
+
|
| 131 |
+
return Seq2SeqLMOutput(
|
| 132 |
+
loss=masked_lm_loss,
|
| 133 |
+
logits=lm_logits,
|
| 134 |
+
past_key_values=decoder_outputs.past_key_values,
|
| 135 |
+
decoder_hidden_states=decoder_outputs.hidden_states,
|
| 136 |
+
decoder_attentions=decoder_outputs.attentions,
|
| 137 |
+
cross_attentions=decoder_outputs.cross_attentions,
|
| 138 |
+
encoder_last_hidden_state=encoder_outputs.last_hidden_state,
|
| 139 |
+
encoder_hidden_states=encoder_outputs.hidden_states,
|
| 140 |
+
encoder_attentions=encoder_outputs.attentions,
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
@staticmethod
|
| 144 |
+
def _reorder_cache(past_key_values, beam_idx):
|
| 145 |
+
reordered_past = ()
|
| 146 |
+
for layer_past in past_key_values:
|
| 147 |
+
reordered_past += (
|
| 148 |
+
tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past),
|
| 149 |
+
)
|
| 150 |
+
return reordered_past
|
| 151 |
+
|
| 152 |
+
@classmethod
|
| 153 |
+
def _can_set_experts_implementation(cls) -> bool:
|
| 154 |
+
return False
|