cyrilvallez HF Staff commited on
Commit
bc77d93
·
verified ·
1 Parent(s): c6c4970

Update modeling_dummy_llama.py

Browse files
Files changed (1) hide show
  1. modeling_dummy_llama.py +1 -152
modeling_dummy_llama.py CHANGED
@@ -358,27 +358,6 @@ class DummyLlamaDecoderLayer(nn.Module):
358
  return outputs
359
 
360
 
361
- LLAMA_START_DOCSTRING = r"""
362
- This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
363
- library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
364
- etc.)
365
-
366
- This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
367
- Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
368
- and behavior.
369
-
370
- Parameters:
371
- config ([`DummyLlamaConfig`]):
372
- Model configuration class with all the parameters of the model. Initializing with a config file does not
373
- load the weights associated with the model, only the configuration. Check out the
374
- [`~PreTrainedModel.from_pretrained`] method to load the model weights.
375
- """
376
-
377
-
378
- @add_start_docstrings(
379
- "The bare LLaMA Model outputting raw hidden-states without any specific head on top.",
380
- LLAMA_START_DOCSTRING,
381
- )
382
  class DummyLlamaPreTrainedModel(PreTrainedModel):
383
  config_class = DummyLlamaConfig
384
  base_model_prefix = "model"
@@ -393,97 +372,8 @@ class DummyLlamaPreTrainedModel(PreTrainedModel):
393
  _supports_static_cache = True
394
  _supports_attention_backend = True
395
 
396
- def _init_weights(self, module):
397
- std = self.config.initializer_range
398
- if isinstance(module, nn.Linear):
399
- module.weight.data.normal_(mean=0.0, std=std)
400
- if module.bias is not None:
401
- module.bias.data.zero_()
402
- elif isinstance(module, nn.Embedding):
403
- module.weight.data.normal_(mean=0.0, std=std)
404
- if module.padding_idx is not None:
405
- module.weight.data[module.padding_idx].zero_()
406
 
407
 
408
- LLAMA_INPUTS_DOCSTRING = r"""
409
- Args:
410
- input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
411
- Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
412
- it.
413
-
414
- Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
415
- [`PreTrainedTokenizer.__call__`] for details.
416
-
417
- [What are input IDs?](../glossary#input-ids)
418
- attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
419
- Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
420
-
421
- - 1 for tokens that are **not masked**,
422
- - 0 for tokens that are **masked**.
423
-
424
- [What are attention masks?](../glossary#attention-mask)
425
-
426
- Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
427
- [`PreTrainedTokenizer.__call__`] for details.
428
-
429
- If `past_key_values` is used, optionally only the last `input_ids` have to be input (see
430
- `past_key_values`).
431
-
432
- If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
433
- and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
434
- information on the default strategy.
435
-
436
- - 1 indicates the head is **not masked**,
437
- - 0 indicates the head is **masked**.
438
- position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
439
- Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
440
- config.n_positions - 1]`.
441
-
442
- [What are position IDs?](../glossary#position-ids)
443
- past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
444
- Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
445
- blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
446
- returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.
447
-
448
- Two formats are allowed:
449
- - a [`~cache_utils.Cache`] instance, see our
450
- [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache);
451
- - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
452
- shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
453
- cache format.
454
-
455
- The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
456
- legacy cache format will be returned.
457
-
458
- If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
459
- have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
460
- of shape `(batch_size, sequence_length)`.
461
- inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
462
- Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
463
- is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
464
- model's internal embedding lookup matrix.
465
- use_cache (`bool`, *optional*):
466
- If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
467
- `past_key_values`).
468
- output_attentions (`bool`, *optional*):
469
- Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
470
- tensors for more detail.
471
- output_hidden_states (`bool`, *optional*):
472
- Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
473
- more detail.
474
- return_dict (`bool`, *optional*):
475
- Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
476
- cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
477
- Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`,
478
- this tensor is not affected by padding. It is used to update the cache in the correct position and to infer
479
- the complete sequence length.
480
- """
481
-
482
-
483
- @add_start_docstrings(
484
- "The bare LLaMA Model outputting raw hidden-states without any specific head on top.",
485
- LLAMA_START_DOCSTRING,
486
- )
487
  class DummyLlamaModel(DummyLlamaPreTrainedModel):
488
  """
489
  Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`DummyLlamaDecoderLayer`]
@@ -514,7 +404,6 @@ class DummyLlamaModel(DummyLlamaPreTrainedModel):
514
  def set_input_embeddings(self, value):
515
  self.embed_tokens = value
516
 
517
- @add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
518
  def forward(
519
  self,
520
  input_ids: torch.LongTensor = None,
@@ -747,7 +636,7 @@ class KwargsForCausalLM(FlashAttentionKwargs): ...
747
 
748
 
749
  class DummyLlamaForCausalLM(DummyLlamaPreTrainedModel, GenerationMixin):
750
- _tied_weights_keys = ["lm_head.weight"]
751
  _tp_plan = {"lm_head": "colwise_rep"}
752
 
753
  def __init__(self, config):
@@ -777,9 +666,6 @@ class DummyLlamaForCausalLM(DummyLlamaPreTrainedModel, GenerationMixin):
777
  def get_decoder(self):
778
  return self.model
779
 
780
- @deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
781
- @add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
782
- @replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
783
  def forward(
784
  self,
785
  input_ids: torch.LongTensor = None,
@@ -871,21 +757,6 @@ class DummyLlamaForCausalLM(DummyLlamaPreTrainedModel, GenerationMixin):
871
  )
872
 
873
 
874
- @add_start_docstrings(
875
- """
876
- The LLaMa Model transformer with a sequence classification head on top (linear layer).
877
-
878
- [`DummyLlamaForSequenceClassification`] uses the last token in order to do the classification, as other causal models
879
- (e.g. GPT-2) do.
880
-
881
- Since it does classification on the last token, it requires to know the position of the last token. If a
882
- `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
883
- no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
884
- padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
885
- each row of the batch).
886
- """,
887
- LLAMA_START_DOCSTRING,
888
- )
889
  class DummyLlamaForSequenceClassification(DummyLlamaPreTrainedModel):
890
  def __init__(self, config):
891
  super().__init__(config)
@@ -902,7 +773,6 @@ class DummyLlamaForSequenceClassification(DummyLlamaPreTrainedModel):
902
  def set_input_embeddings(self, value):
903
  self.model.embed_tokens = value
904
 
905
- @add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
906
  def forward(
907
  self,
908
  input_ids: Optional[torch.LongTensor] = None,
@@ -975,13 +845,6 @@ class DummyLlamaForSequenceClassification(DummyLlamaPreTrainedModel):
975
  )
976
 
977
 
978
- @add_start_docstrings(
979
- """
980
- The DummyLlama Model transformer with a span classification head on top for extractive question-answering tasks like
981
- SQuAD (a linear layer on top of the hidden-states output to compute `span start logits` and `span end logits`).
982
- """,
983
- LLAMA_START_DOCSTRING,
984
- )
985
  class DummyLlamaForQuestionAnswering(DummyLlamaPreTrainedModel):
986
  base_model_prefix = "transformer"
987
 
@@ -1000,7 +863,6 @@ class DummyLlamaForQuestionAnswering(DummyLlamaPreTrainedModel):
1000
  def set_input_embeddings(self, value):
1001
  self.transformer.embed_tokens = value
1002
 
1003
- @add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
1004
  def forward(
1005
  self,
1006
  input_ids: Optional[torch.LongTensor] = None,
@@ -1062,13 +924,6 @@ class DummyLlamaForQuestionAnswering(DummyLlamaPreTrainedModel):
1062
  )
1063
 
1064
 
1065
- @add_start_docstrings(
1066
- """
1067
- The DummyLlama Model transformer with a token classification head on top (a linear layer on top of the hidden-states
1068
- output) e.g. for Named-Entity-Recognition (NER) tasks.
1069
- """,
1070
- LLAMA_START_DOCSTRING,
1071
- )
1072
  class DummyLlamaForTokenClassification(DummyLlamaPreTrainedModel):
1073
  def __init__(self, config):
1074
  super().__init__(config)
@@ -1092,12 +947,6 @@ class DummyLlamaForTokenClassification(DummyLlamaPreTrainedModel):
1092
  def set_input_embeddings(self, value):
1093
  self.model.embed_tokens = value
1094
 
1095
- @add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING)
1096
- @add_code_sample_docstrings(
1097
- checkpoint=_CHECKPOINT_FOR_DOC,
1098
- output_type=TokenClassifierOutput,
1099
- config_class=_CONFIG_FOR_DOC,
1100
- )
1101
  def forward(
1102
  self,
1103
  input_ids: Optional[torch.LongTensor] = None,
 
358
  return outputs
359
 
360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
  class DummyLlamaPreTrainedModel(PreTrainedModel):
362
  config_class = DummyLlamaConfig
363
  base_model_prefix = "model"
 
372
  _supports_static_cache = True
373
  _supports_attention_backend = True
374
 
 
 
 
 
 
 
 
 
 
 
375
 
376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
377
  class DummyLlamaModel(DummyLlamaPreTrainedModel):
378
  """
379
  Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`DummyLlamaDecoderLayer`]
 
404
  def set_input_embeddings(self, value):
405
  self.embed_tokens = value
406
 
 
407
  def forward(
408
  self,
409
  input_ids: torch.LongTensor = None,
 
636
 
637
 
638
  class DummyLlamaForCausalLM(DummyLlamaPreTrainedModel, GenerationMixin):
639
+ _tied_weights_keys = {"lm_head.weight": "embed_tokens.weight"}
640
  _tp_plan = {"lm_head": "colwise_rep"}
641
 
642
  def __init__(self, config):
 
666
  def get_decoder(self):
667
  return self.model
668
 
 
 
 
669
  def forward(
670
  self,
671
  input_ids: torch.LongTensor = None,
 
757
  )
758
 
759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
760
  class DummyLlamaForSequenceClassification(DummyLlamaPreTrainedModel):
761
  def __init__(self, config):
762
  super().__init__(config)
 
773
  def set_input_embeddings(self, value):
774
  self.model.embed_tokens = value
775
 
 
776
  def forward(
777
  self,
778
  input_ids: Optional[torch.LongTensor] = None,
 
845
  )
846
 
847
 
 
 
 
 
 
 
 
848
  class DummyLlamaForQuestionAnswering(DummyLlamaPreTrainedModel):
849
  base_model_prefix = "transformer"
850
 
 
863
  def set_input_embeddings(self, value):
864
  self.transformer.embed_tokens = value
865
 
 
866
  def forward(
867
  self,
868
  input_ids: Optional[torch.LongTensor] = None,
 
924
  )
925
 
926
 
 
 
 
 
 
 
 
927
  class DummyLlamaForTokenClassification(DummyLlamaPreTrainedModel):
928
  def __init__(self, config):
929
  super().__init__(config)
 
947
  def set_input_embeddings(self, value):
948
  self.model.embed_tokens = value
949
 
 
 
 
 
 
 
950
  def forward(
951
  self,
952
  input_ids: Optional[torch.LongTensor] = None,