ssslakter
/

Slava Chaunin commited on
Commit
ca08856
·
1 Parent(s): 05f5a83

update to transformers 5 format

Browse files
Files changed (2) hide show
  1. configuration_llada.py +3 -0
  2. modeling_llada.py +7 -13
configuration_llada.py CHANGED
@@ -2,6 +2,7 @@
2
  LLaDA configuration
3
  """
4
  from transformers import AutoConfig, PretrainedConfig
 
5
 
6
  from enum import Enum
7
  from os import PathLike
@@ -430,9 +431,11 @@ class ActivationCheckpointingStrategy(StrEnum):
430
  """
431
 
432
 
 
433
  class LLaDAConfig(PretrainedConfig):
434
  model_type = "llada"
435
  keys_to_ignore_at_inference = ["past_key_values"] # TODO: confirm
 
436
 
437
  def __init__(self, use_cache: bool = False, **kwargs):
438
  model_config = ModelConfig()
 
2
  LLaDA configuration
3
  """
4
  from transformers import AutoConfig, PretrainedConfig
5
+ from huggingface_hub.dataclasses import strict
6
 
7
  from enum import Enum
8
  from os import PathLike
 
431
  """
432
 
433
 
434
+ @strict
435
  class LLaDAConfig(PretrainedConfig):
436
  model_type = "llada"
437
  keys_to_ignore_at_inference = ["past_key_values"] # TODO: confirm
438
+ tie_word_embeddings: bool = False
439
 
440
  def __init__(self, use_cache: bool = False, **kwargs):
441
  model_config = ModelConfig()
modeling_llada.py CHANGED
@@ -27,6 +27,7 @@ import torch.nn as nn
27
  import torch.nn.functional as F
28
  from torch import einsum
29
  from transformers import PreTrainedModel
 
30
  from transformers.modeling_outputs import CausalLMOutputWithPast
31
  from transformers.models.auto import AutoModel
32
  from transformers.cache_utils import Cache
@@ -1393,6 +1394,7 @@ class LLaDAModelLM(PreTrainedModel):
1393
  config_class = LLaDAConfig
1394
  base_model_prefix = "model"
1395
  _no_split_modules = ["LLaDABlock", "LLaDASequentialBlock", "LLaDALlamaBlock"]
 
1396
 
1397
  def __init__(self, config: LLaDAConfig, model: Optional[LLaDAModel] = None, init_params: bool = False):
1398
  super().__init__(config)
@@ -1404,10 +1406,12 @@ class LLaDAModelLM(PreTrainedModel):
1404
  self.model = LLaDAModel(model_config, init_params=init_params)
1405
  else:
1406
  self.model = model
 
1407
 
 
1408
  def forward(
1409
  self,
1410
- input_ids: torch.LongTensor = None,
1411
  inputs_embeds: Optional[torch.FloatTensor] = None,
1412
  attention_mask: Optional[torch.Tensor] = None,
1413
  attention_bias: Optional[torch.Tensor] = None,
@@ -1416,8 +1420,7 @@ class LLaDAModelLM(PreTrainedModel):
1416
  use_cache: Optional[bool] = None,
1417
  output_attentions: Optional[bool] = None,
1418
  output_hidden_states: Optional[bool] = None,
1419
- return_dict: Optional[bool] = None,
1420
- cache_position: Optional[Cache] = None, # This is a hack mitigation of an issue in transformers `4.39.x`
1421
  ) -> Union[Tuple, CausalLMOutputWithPast]:
1422
  if use_cache is None:
1423
  use_cache = self.config.use_cache
@@ -1425,7 +1428,6 @@ class LLaDAModelLM(PreTrainedModel):
1425
  if output_attentions:
1426
  raise ValueError("output_attentions is not yet supported in LLaDA")
1427
 
1428
- return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1429
 
1430
  # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
1431
  outputs = self.model.forward(
@@ -1441,14 +1443,10 @@ class LLaDAModelLM(PreTrainedModel):
1441
  logits = outputs.logits
1442
  hidden_states = outputs.hidden_states
1443
 
1444
- loss = None
1445
  if labels is not None:
1446
  import warnings
1447
  warnings.warn("Note that for LLaDA, you cannot calculate the loss here.", UserWarning)
1448
- if not return_dict:
1449
- output = (logits,) + outputs[1:]
1450
- return (loss,) + output if loss is not None else output
1451
-
1452
  return CausalLMOutputWithPast(
1453
  logits=logits,
1454
  past_key_values=outputs.attn_key_values,
@@ -1498,9 +1496,5 @@ class LLaDAModelLM(PreTrainedModel):
1498
  else:
1499
  self.model.transformer.ff_out = value
1500
 
1501
- def tie_weights(self):
1502
- if self.config.weight_tying:
1503
- self.model.transformer.ff_out = self.model.transformer.wte
1504
-
1505
  # Register the model so that it is available for transformer pipelines, auto-loading, etc.
1506
  AutoModel.register(LLaDAConfig, LLaDAModelLM)
 
27
  import torch.nn.functional as F
28
  from torch import einsum
29
  from transformers import PreTrainedModel
30
+ from transformers.utils import can_return_tuple
31
  from transformers.modeling_outputs import CausalLMOutputWithPast
32
  from transformers.models.auto import AutoModel
33
  from transformers.cache_utils import Cache
 
1394
  config_class = LLaDAConfig
1395
  base_model_prefix = "model"
1396
  _no_split_modules = ["LLaDABlock", "LLaDASequentialBlock", "LLaDALlamaBlock"]
1397
+ _tied_weights_keys = {"model.transformer.ff_out.weight": "model.transformer.wte"}
1398
 
1399
  def __init__(self, config: LLaDAConfig, model: Optional[LLaDAModel] = None, init_params: bool = False):
1400
  super().__init__(config)
 
1406
  self.model = LLaDAModel(model_config, init_params=init_params)
1407
  else:
1408
  self.model = model
1409
+ self.post_init()
1410
 
1411
+ @can_return_tuple
1412
  def forward(
1413
  self,
1414
+ input_ids: torch.LongTensor | None = None,
1415
  inputs_embeds: Optional[torch.FloatTensor] = None,
1416
  attention_mask: Optional[torch.Tensor] = None,
1417
  attention_bias: Optional[torch.Tensor] = None,
 
1420
  use_cache: Optional[bool] = None,
1421
  output_attentions: Optional[bool] = None,
1422
  output_hidden_states: Optional[bool] = None,
1423
+ **kwargs
 
1424
  ) -> Union[Tuple, CausalLMOutputWithPast]:
1425
  if use_cache is None:
1426
  use_cache = self.config.use_cache
 
1428
  if output_attentions:
1429
  raise ValueError("output_attentions is not yet supported in LLaDA")
1430
 
 
1431
 
1432
  # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
1433
  outputs = self.model.forward(
 
1443
  logits = outputs.logits
1444
  hidden_states = outputs.hidden_states
1445
 
 
1446
  if labels is not None:
1447
  import warnings
1448
  warnings.warn("Note that for LLaDA, you cannot calculate the loss here.", UserWarning)
1449
+
 
 
 
1450
  return CausalLMOutputWithPast(
1451
  logits=logits,
1452
  past_key_values=outputs.attn_key_values,
 
1496
  else:
1497
  self.model.transformer.ff_out = value
1498
 
 
 
 
 
1499
  # Register the model so that it is available for transformer pipelines, auto-loading, etc.
1500
  AutoModel.register(LLaDAConfig, LLaDAModelLM)