Question Answering
Transformers
Safetensors
English
doge
text-generation
trl
sft
dpo
custom_code
JingzeShi commited on
Commit
f7fb1fb
·
verified ·
1 Parent(s): 980f395

Upload modeling_doge.py for SmallDoge/Doge-160M-Instruct

Browse files
Files changed (1) hide show
  1. modeling_doge.py +354 -730
modeling_doge.py CHANGED
@@ -5,10 +5,9 @@
5
  # modular_doge.py file directly. One of our CI enforces this.
6
  # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
7
  # coding=utf-8
8
- # Copyright 2024 Jingze Shi and the HuggingFace Inc. team. All rights reserved.
9
  #
10
- # This code is based on the Wonderful Matrices paper implementation.
11
- # The Doge family of small language models is trained by Jingze Shi.
12
  #
13
  # Licensed under the Apache License, Version 2.0 (the "License");
14
  # you may not use this file except in compliance with the License.
@@ -23,38 +22,33 @@
23
  # limitations under the License.
24
 
25
  import math
26
- from typing import Callable, List, Optional, Tuple, Union
27
 
28
  import torch
29
  import torch.nn.functional as F
30
  from torch import nn
31
 
32
  from transformers.activations import ACT2FN
33
- from transformers.cache_utils import Cache, DynamicCache, StaticCache
34
  from transformers.generation import GenerationMixin
35
- from transformers.modeling_attn_mask_utils import AttentionMaskConverter
36
- from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPast
37
- from transformers.modeling_rope_utils import ROPE_INIT_FUNCTIONS
38
- from transformers.modeling_utils import PreTrainedModel
 
 
 
39
  from transformers.processing_utils import Unpack
40
- from transformers.utils import (
41
- LossKwargs,
42
- add_start_docstrings,
43
- add_start_docstrings_to_model_forward,
44
- is_torch_flex_attn_available,
45
- logging,
46
- replace_return_docstrings,
47
- )
48
  from .configuration_doge import DogeConfig
49
 
50
- if is_torch_flex_attn_available():
51
- from torch.nn.attention.flex_attention import flex_attention
52
-
53
- logger = logging.get_logger(__name__)
54
 
55
- _CONFIG_FOR_DOC = "DogeConfig"
 
56
 
57
 
 
58
  class DogeRMSNorm(nn.Module):
59
  def __init__(self, hidden_size, eps=1e-6):
60
  """
@@ -91,7 +85,7 @@ class DogeRotaryEmbedding(nn.Module):
91
  def __init__(self, config: DogeConfig, device=None):
92
  super().__init__()
93
  # BC: "rope_type" was originally "type"
94
- if hasattr(config, "rope_scaling") and config.rope_scaling is not None:
95
  self.rope_type = config.rope_scaling.get("rope_type", config.rope_scaling.get("type"))
96
  else:
97
  self.rope_type = "default"
@@ -105,45 +99,18 @@ class DogeRotaryEmbedding(nn.Module):
105
  self.register_buffer("inv_freq", inv_freq, persistent=False)
106
  self.original_inv_freq = self.inv_freq
107
 
108
- def _dynamic_frequency_update(self, position_ids, device):
109
- """
110
- dynamic RoPE layers should recompute `inv_freq` in the following situations:
111
- 1 - growing beyond the cached sequence length (allow scaling)
112
- 2 - the current sequence length is in the original scale (avoid losing precision with small sequences)
113
- """
114
- seq_len = torch.max(position_ids) + 1
115
- if seq_len > self.max_seq_len_cached: # growth
116
- inv_freq, self.attention_scaling = self.rope_init_fn(self.config, device, seq_len=seq_len)
117
- self.register_buffer("inv_freq", inv_freq, persistent=False) # TODO joao: may break with compilation
118
- self.max_seq_len_cached = seq_len
119
-
120
- if seq_len < self.original_max_seq_len and self.max_seq_len_cached > self.original_max_seq_len: # reset
121
- # This .to() is needed if the model has been moved to a device after being initialized (because
122
- # the buffer is automatically moved, but not the original copy)
123
- self.original_inv_freq = self.original_inv_freq.to(device)
124
- self.register_buffer("inv_freq", self.original_inv_freq, persistent=False)
125
- self.max_seq_len_cached = self.original_max_seq_len
126
-
127
  @torch.no_grad()
 
128
  def forward(self, x, position_ids):
129
- if "dynamic" in self.rope_type:
130
- self._dynamic_frequency_update(position_ids, device=x.device)
131
-
132
- # Core RoPE block
133
- inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1)
134
  position_ids_expanded = position_ids[:, None, :].float()
135
- # Force float32 (see https://github.com/huggingface/transformers/pull/29285)
136
- device_type = x.device.type
137
- device_type = device_type if isinstance(device_type, str) and device_type != "mps" else "cpu"
138
- with torch.autocast(device_type=device_type, enabled=False):
139
  freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)
140
  emb = torch.cat((freqs, freqs), dim=-1)
141
- cos = emb.cos()
142
- sin = emb.sin()
143
-
144
- # Advanced RoPE types (e.g. yarn) apply a post-processing scaling factor, equivalent to scaling attention
145
- cos = cos * self.attention_scaling
146
- sin = sin * self.attention_scaling
147
 
148
  return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)
149
 
@@ -202,120 +169,60 @@ def eager_attention_forward(
202
  attention_mask: Optional[torch.Tensor],
203
  scaling: float,
204
  dropout: float = 0.0,
205
- **kwargs,
206
- ) -> Tuple[torch.Tensor, torch.Tensor]:
207
  key_states = repeat_kv(key, module.num_key_value_groups)
208
  value_states = repeat_kv(value, module.num_key_value_groups)
209
 
210
- attn_weights = torch.matmul(query, key_states.transpose(-1, -2)) * scaling
211
  if attention_mask is not None:
212
  causal_mask = attention_mask[:, :, :, : key_states.shape[-2]]
213
  attn_weights = attn_weights + causal_mask
214
 
215
- attn_weights = F.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query.dtype)
216
- attn_weights = F.dropout(attn_weights, p=dropout, training=module.training)
217
  attn_output = torch.matmul(attn_weights, value_states)
218
  attn_output = attn_output.transpose(1, 2).contiguous()
219
 
220
  return attn_output, attn_weights
221
 
222
 
223
- def sdpa_attention_forward(
224
- module: nn.Module,
225
- query: torch.Tensor,
226
- key: torch.Tensor,
227
- value: torch.Tensor,
228
- attention_mask: Optional[torch.Tensor],
229
- dropout: float = 0.0,
230
- scaling: Optional[float] = None,
231
- is_causal: Optional[bool] = None,
232
- **kwargs,
233
- ) -> Tuple[torch.Tensor, None]:
234
- key = repeat_kv(key, module.num_key_value_groups)
235
- value = repeat_kv(value, module.num_key_value_groups)
236
-
237
- causal_mask = attention_mask
238
- if attention_mask is not None:
239
- causal_mask = causal_mask[:, :, :, : key.shape[-2]]
240
-
241
- # SDPA with memory-efficient backend is bugged with non-contiguous inputs and custom attn_mask for some torch versions
242
- # Reference: https://github.com/pytorch/pytorch/issues/112577.
243
- query = query.contiguous()
244
- key = key.contiguous()
245
- value = value.contiguous()
246
-
247
- # We dispatch to SDPA's Flash Attention or Efficient kernels via this `is_causal` if statement instead of an inline conditional assignment
248
- # in SDPA to support both torch.compile's dynamic shapes and full graph options. An inline conditional prevents dynamic shapes from compiling.
249
- if is_causal is None:
250
- is_causal = causal_mask is None and query.shape[2] > 1
251
-
252
- # Shapes (e.g. query.shape[2]) are tensors during jit tracing, resulting in `is_causal` being a tensor.
253
- # We convert it to a bool for the SDPA kernel that only accepts bools.
254
- if torch.jit.is_tracing() and isinstance(is_causal, torch.Tensor):
255
- is_causal = is_causal.item()
256
-
257
- # NOTE: As of pytorch 2.5.1, SDPA backward pass of cuDNN is still incorrect, so we disable cuDNN SDPA (see https://github.com/pytorch/pytorch/issues/138581)
258
- torch.backends.cuda.enable_cudnn_sdp(False)
259
- attn_output = F.scaled_dot_product_attention(
260
- query=query,
261
- key=key,
262
- value=value,
263
- attn_mask=causal_mask,
264
- dropout_p=dropout,
265
- scale=scaling,
266
- is_causal=is_causal,
267
- )
268
- attn_output = attn_output.transpose(1, 2).contiguous()
269
-
270
- return attn_output, None
271
-
272
-
273
  def flex_attention_forward(
274
  module: nn.Module,
275
  query: torch.Tensor,
276
  key: torch.Tensor,
277
  value: torch.Tensor,
278
- attention_mask: Optional[torch.Tensor],
279
  scaling: Optional[float] = None,
280
- is_causal: Optional[bool] = None,
281
  softcap: Optional[float] = None,
282
  head_mask: Optional[torch.Tensor] = None,
283
  **kwargs,
284
- ) -> Tuple[torch.Tensor, torch.Tensor]:
285
- causal_mask = attention_mask
286
- if attention_mask is not None:
 
 
 
 
 
 
287
  causal_mask = causal_mask[:, :, :, : key.shape[-2]]
288
 
289
- if is_causal is None:
290
- is_causal = causal_mask is None and query.shape[2] > 1
291
-
292
- def causal_mod(score, batch, head, q_idx, kv_idx):
293
  if softcap is not None:
294
  score = softcap * torch.tanh(score / softcap)
295
  if causal_mask is not None:
296
- score = score + causal_mask[batch][0][q_idx][kv_idx]
297
  if head_mask is not None:
298
- score = score + head_mask[batch][head][0][0]
299
  return score
300
 
301
- def dynamic_mod(score, batch, head, q_idx, kv_idx):
302
- if softcap is not None:
303
- score = softcap * torch.tanh(score / softcap)
304
- if causal_mask is not None:
305
- score = score + causal_mask[batch][head][q_idx][kv_idx]
306
- if head_mask is not None:
307
- score = score + head_mask[batch][head][0][0]
308
- return score
309
-
310
- # TODO: flex_attention: As of pytorch 2.5.1, captured buffers that require grad are not yet supported.
311
- # NOTE: So we only use flex_attention in inference mode.
312
- mask_mod = causal_mod if is_causal or module.training else dynamic_mod
313
-
314
- attn_output, attention_weights = flex_attention(
315
- query=query,
316
- key=key,
317
- value=value,
318
- score_mod=mask_mod,
319
  enable_gqa=True,
320
  scale=scaling,
321
  # Last time checked on PyTorch == 2.5.1: Flex Attention always computes the lse regardless.
@@ -329,16 +236,11 @@ def flex_attention_forward(
329
  return attn_output, attention_weights
330
 
331
 
332
- ALL_ATTENTION_FUNCTIONS = {
333
- "eager": eager_attention_forward,
334
- "sdpa": sdpa_attention_forward,
335
- "flex_attention": flex_attention_forward,
336
- }
337
-
338
 
339
- class DogeDynamicMaskAttention(nn.Module):
340
- """Dynamic Mask Attention from 'Wonderful Matrices' paper."""
341
 
 
342
  def __init__(self, config: DogeConfig, layer_idx: Optional[int] = None):
343
  super().__init__()
344
  self.config = config
@@ -347,35 +249,35 @@ class DogeDynamicMaskAttention(nn.Module):
347
  self.num_key_value_groups = config.num_attention_heads // config.num_key_value_heads
348
  self.scaling = self.head_dim**-0.5
349
  self.attention_dropout = config.attention_dropout
350
- self.dynamic_mask_ratio = config.dynamic_mask_ratio
351
 
352
  self.q_proj = nn.Linear(
353
- config.hidden_size, config.num_attention_heads * self.head_dim, bias=config.hidden_bias
354
  )
355
  self.k_proj = nn.Linear(
356
- config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.hidden_bias
357
  )
358
  self.v_proj = nn.Linear(
359
- config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.hidden_bias
360
  )
361
  # dynamic mask for the QK^T attention weights matrix
362
  self.A = nn.Parameter(torch.zeros(config.num_attention_heads))
363
  self.dt_proj = nn.Linear(
364
- config.num_key_value_heads * self.head_dim, config.num_attention_heads, bias=config.hidden_bias
365
  )
366
  self.o_proj = nn.Linear(
367
- config.num_attention_heads * self.head_dim, config.hidden_size, bias=config.hidden_bias
368
  )
369
 
370
  def forward(
371
  self,
372
  hidden_states: torch.Tensor,
373
- position_embeddings: Tuple[torch.Tensor, torch.Tensor],
374
  attention_mask: Optional[torch.Tensor] = None,
375
  past_key_value: Optional[Cache] = None,
376
  cache_position: Optional[torch.LongTensor] = None,
377
  **kwargs,
378
- ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
379
  input_shape = hidden_states.shape[:-1]
380
  hidden_shape = (*input_shape, -1, self.head_dim)
381
 
@@ -395,23 +297,17 @@ class DogeDynamicMaskAttention(nn.Module):
395
  dt_states = self.dt_proj(
396
  value_states.transpose(1, 2).reshape(value_states.shape[0], value_states.shape[-2], -1)
397
  )
398
- dynamic_mask = torch.exp(self.A * F.softplus(dt_states)).transpose(-1, -2)
399
  attn_mask = self.prepare_dynamic_mask(
400
  hidden_states=hidden_states,
401
- dynamic_mask=dynamic_mask,
402
- dynamic_mask_ratio=self.dynamic_mask_ratio,
403
  attention_mask=attention_mask,
404
  )
405
 
406
  attention_interface: Callable = eager_attention_forward
407
  if self.config._attn_implementation != "eager":
408
- if self.config._attn_implementation == "sdpa" and kwargs.get("output_attentions", False):
409
- logger.warning_once(
410
- "`torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to "
411
- 'eager attention. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.'
412
- )
413
- else:
414
- attention_interface = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation]
415
 
416
  attn_output, attn_weights = attention_interface(
417
  self,
@@ -431,77 +327,80 @@ class DogeDynamicMaskAttention(nn.Module):
431
  def prepare_dynamic_mask(
432
  self,
433
  hidden_states: torch.Tensor,
434
- dynamic_mask: torch.Tensor,
435
- dynamic_mask_ratio: float = 0.0,
436
  attention_mask: Optional[torch.Tensor] = None,
437
  ):
438
  """
439
- Combine `dynamic_mask` with `attention_mask` to generate the final `attn_mask`.
 
 
440
 
441
  Args:
442
  hidden_states (`torch.Tensor`): The input hidden_states, used to determine the minimum value of the current input precision.
443
- dynamic_mask (`torch.Tensor`): dynamic mask of shape `(batch_size, num_heads, key_sequence_length)`.
444
- dynamic_mask_ratio (`float`, *optional*): Ratio from 0.0 to 1.0 used to control the proportion of the dynamic mask filled with the minimum value.
445
  attention_mask (`torch.Tensor`, *optional*): attention mask of shape `(batch_size, 1, query_sequence_length, key_sequence_length)`.
446
  """
447
- attn_mask = None
448
- if dynamic_mask is not None:
449
- attn_mask = dynamic_mask[:, :, None, :]
450
- if 0.0 < dynamic_mask_ratio < 1.0:
451
- min_type = torch.finfo(hidden_states.dtype).min
452
- num_dynamic_mask = int(attn_mask.shape[-1] * dynamic_mask_ratio)
453
- if num_dynamic_mask > 0:
454
- rate_value = torch.kthvalue(attn_mask, num_dynamic_mask, dim=-1, keepdim=True).values
455
- attn_mask = attn_mask.masked_fill(attn_mask < rate_value, min_type)
456
- if attention_mask is not None:
457
- attn_mask = attn_mask + attention_mask[:, :, :, : attn_mask.shape[-1]]
458
- else:
459
- attn_mask = attention_mask
460
-
 
 
 
461
  return attn_mask
462
 
463
 
464
  class DogeMLP(nn.Module):
465
- def __init__(self, config: DogeConfig):
466
  super().__init__()
467
- self.hidden_dim = config.hidden_size
468
- self.intermediate_dim = config.intermediate_size
 
 
 
 
469
  self.act_fn = ACT2FN[config.hidden_act]
470
 
471
- self.gate_proj = nn.Linear(self.hidden_dim, self.intermediate_dim, bias=config.hidden_bias)
472
- self.up_proj = nn.Linear(self.hidden_dim, self.intermediate_dim, bias=config.hidden_bias)
473
- self.down_proj = nn.Linear(self.intermediate_dim, self.hidden_dim, bias=config.hidden_bias)
474
 
475
- def forward(
476
- self,
477
- hidden_states: torch.Tensor,
478
- **kwargs,
479
- ) -> torch.Tensor:
480
- hidden_states = self.down_proj(self.act_fn(self.gate_proj(hidden_states)) * self.up_proj(hidden_states))
481
- return hidden_states
482
-
483
-
484
- class DogeCDMoE(DogeMLP):
485
- """Cross Domain Mixture of Experts from 'Wonderful Matrices' paper."""
486
 
 
487
  def __init__(self, config: DogeConfig):
488
- super().__init__(config)
489
- self.hidden_dim = config.hidden_size
 
490
  self.act_fn = ACT2FN[config.hidden_act]
491
 
492
- self.expert_retrieval_dim = config.expert_retrieval_size
493
- self.num_cdmoe_experts = config.num_cdmoe_experts
494
- self.num_cdmoe_heads = config.num_cdmoe_heads
495
- self.num_cdmoe_experts_per_head = config.num_cdmoe_experts_per_head
496
- self.num_keys = int(math.sqrt(self.num_cdmoe_experts))
 
 
 
 
497
 
498
- # queries and keys for retrieval experts
499
- self.queries_proj = nn.Linear(self.hidden_dim, self.num_cdmoe_heads * self.expert_retrieval_dim, bias=False)
500
- self.keys = nn.Parameter(torch.zeros(self.num_cdmoe_heads, self.expert_retrieval_dim, self.num_keys))
501
 
502
- # experts
503
- self.down_embed = nn.Embedding(self.num_cdmoe_experts, self.hidden_dim)
504
- self.up_embed = nn.Embedding(self.num_cdmoe_experts, self.hidden_dim)
505
 
506
  def forward(
507
  self,
@@ -510,293 +409,169 @@ class DogeCDMoE(DogeMLP):
510
  ) -> torch.Tensor:
511
  bsz, seq_len, _ = hidden_states.shape
512
 
513
- # get routing weights with queries and keys
514
- queries = self.queries_proj(hidden_states)
515
- queries = queries.view(2, self.num_cdmoe_heads, bsz * seq_len, -1)
516
- keys = self.keys.view(2, self.num_cdmoe_heads, -1, self.num_keys)
517
- routing_weights = torch.matmul(queries, keys)
518
- routing_weights = routing_weights.transpose(-2, -3).view(2, bsz, seq_len, self.num_cdmoe_heads, self.num_keys)
519
 
520
- # get experts with the highest routing weights
521
- (scores_x, scores_y), (indices_x, indices_y) = routing_weights.topk(self.num_cdmoe_experts_per_head, dim=-1)
522
  all_scores = scores_x.unsqueeze(-1) + scores_y.unsqueeze(-2)
523
- all_scores = all_scores.view(*scores_x.shape[:-1], -1)
524
- all_indices = (indices_x.unsqueeze(-1) * self.num_keys) + indices_y.unsqueeze(-2)
525
- all_indices = all_indices.view(*indices_x.shape[:-1], -1)
526
- scores, pk_indices = all_scores.topk(self.num_cdmoe_experts_per_head, dim=-1)
527
- indices = all_indices.gather(-1, pk_indices)
 
 
 
 
 
528
  down_embed = self.down_embed(indices)
529
  up_embed = self.up_embed(indices)
530
-
531
- # mix experts states with cross domain states
532
- experts_weights = torch.sum(hidden_states[:, :, None, None, :] * down_embed, dim=-1)
533
- experts_weights = self.act_fn(experts_weights) * scores.softmax(dim=-1)
534
- experts_states = torch.sum(experts_weights[:, :, :, :, None] * up_embed, dim=(-2, -3))
535
  hidden_states = self.down_proj(self.act_fn(self.gate_proj(hidden_states)) * self.up_proj(hidden_states))
536
  hidden_states = hidden_states + experts_states
537
- return hidden_states
538
 
539
 
540
- class DogeDecoderLayer(nn.Module):
541
  def __init__(self, config: DogeConfig, layer_idx: Optional[int] = None):
542
  super().__init__()
543
  self.hidden_dropout = config.hidden_dropout
544
 
545
- self.pre_layernorm = DogeRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
546
- self.self_attn = DogeDynamicMaskAttention(config=config, layer_idx=layer_idx)
547
- self.pre_residual = DogeResidual(config.hidden_size)
548
 
549
- self.post_layernorm = DogeRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
550
- self.feed_forward = DogeMLP(config) if not config.is_moe else DogeCDMoE(config)
551
- self.post_residual = DogeResidual(config.hidden_size)
552
 
553
  def forward(
554
  self,
555
  hidden_states: torch.Tensor,
 
556
  attention_mask: Optional[torch.Tensor] = None,
557
  position_ids: Optional[torch.LongTensor] = None,
558
- past_key_value: Optional[Cache] = None,
559
- output_attentions: Optional[bool] = False,
560
  use_cache: Optional[bool] = False,
561
  cache_position: Optional[torch.LongTensor] = None,
562
- position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC
563
- **kwargs,
564
- ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
565
  # sequence transformation
566
  residual = hidden_states
567
- hidden_states = self.pre_layernorm(hidden_states)
568
  hidden_states, self_attn_weights = self.self_attn(
569
  hidden_states=hidden_states,
 
570
  attention_mask=attention_mask,
571
  position_ids=position_ids,
572
  past_key_value=past_key_value,
573
- output_attentions=output_attentions,
574
  use_cache=use_cache,
575
  cache_position=cache_position,
576
- position_embeddings=position_embeddings,
577
  **kwargs,
578
  )
579
- self_attn_weights = None
580
  hidden_states = F.dropout(hidden_states, p=self.hidden_dropout, training=self.training)
581
- hidden_states = self.pre_residual(residual, hidden_states)
582
 
583
  # state transformation
584
  residual = hidden_states
585
- hidden_states = self.post_layernorm(hidden_states)
586
- hidden_states = self.feed_forward(hidden_states)
587
  hidden_states = F.dropout(hidden_states, p=self.hidden_dropout, training=self.training)
588
- hidden_states = self.post_residual(residual, hidden_states)
589
-
590
- outputs = (hidden_states,)
591
- if output_attentions:
592
- outputs += (self_attn_weights,)
593
 
594
- return outputs
595
-
596
-
597
- DOGE_START_DOCSTRING = r"""
598
- This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
599
- library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
600
- etc.)
601
-
602
- This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
603
- Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
604
- and behavior.
605
-
606
- Parameters:
607
- config ([`DogeConfig`]):
608
- Model configuration class with all the parameters of the model. Initializing with a config file does not
609
- load the weights associated with the model, only the configuration. Check out the
610
- [`~PreTrainedModel.from_pretrained`] method to load the model weights.
611
- """
612
 
613
 
614
- @add_start_docstrings(
615
- "The bare Doge Model outputting raw hidden-states without any specific head on top.",
616
- DOGE_START_DOCSTRING,
617
- )
618
  class DogePreTrainedModel(PreTrainedModel):
619
- config_class = DogeConfig
620
  base_model_prefix = "model"
621
  supports_gradient_checkpointing = True
622
  _no_split_modules = ["DogeDecoderLayer"]
623
  _skip_keys_device_placement = ["past_key_values"]
 
624
  _supports_sdpa = True
625
- # _supports_flex_attn = True # TODO: enable this when flex_attention is fully supported
626
- _supports_cache_class = True
627
- _supports_quantized_cache = True
628
- _supports_static_cache = True
 
 
 
 
629
 
630
  def _init_weights(self, module):
631
- std = self.config.initializer_range
632
- if isinstance(module, (nn.Linear)):
633
- module.weight.data.normal_(mean=0.0, std=std)
634
- if module.bias is not None:
635
- module.bias.data.zero_()
636
- elif isinstance(module, nn.Embedding):
637
- module.weight.data.normal_(mean=0.0, std=std)
638
- if module.padding_idx is not None:
639
- module.weight.data[module.padding_idx].zero_()
640
-
641
-
642
- DOGE_INPUTS_DOCSTRING = r"""
643
- Args:
644
- input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
645
- Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
646
- it.
647
-
648
- Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
649
- [`PreTrainedTokenizer.__call__`] for details.
650
-
651
- [What are input IDs?](../glossary#input-ids)
652
- attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
653
- Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
654
-
655
- - 1 for tokens that are **not masked**,
656
- - 0 for tokens that are **masked**.
657
-
658
- [What are attention masks?](../glossary#attention-mask)
659
-
660
- Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
661
- [`PreTrainedTokenizer.__call__`] for details.
662
-
663
- If `past_key_values` is used, optionally only the last `input_ids` have to be input (see
664
- `past_key_values`).
665
-
666
- If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
667
- and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
668
- information on the default strategy.
669
-
670
- - 1 indicates the head is **not masked**,
671
- - 0 indicates the head is **masked**.
672
- position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
673
- Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
674
- config.n_positions - 1]`.
675
-
676
- [What are position IDs?](../glossary#position-ids)
677
- past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
678
- Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
679
- blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
680
- returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.
681
-
682
- Two formats are allowed:
683
- - a [`~cache_utils.Cache`] instance, see our
684
- [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache);
685
- - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
686
- shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
687
- cache format.
688
-
689
- The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
690
- legacy cache format will be returned.
691
-
692
- If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
693
- have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
694
- of shape `(batch_size, sequence_length)`.
695
- inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
696
- Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
697
- is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
698
- model's internal embedding lookup matrix.
699
- use_cache (`bool`, *optional*):
700
- If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
701
- `past_key_values`).
702
- output_attentions (`bool`, *optional*):
703
- Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
704
- tensors for more detail.
705
- output_hidden_states (`bool`, *optional*):
706
- Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
707
- more detail.
708
- return_dict (`bool`, *optional*):
709
- Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
710
- cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
711
- Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`,
712
- this tensor is not affected by padding. It is used to update the cache in the correct position and to infer
713
- the complete sequence length.
714
- """
715
-
716
-
717
- @add_start_docstrings(
718
- "The bare Doge Model outputting raw hidden-states without any specific head on top.",
719
- DOGE_START_DOCSTRING,
720
- )
721
  class DogeModel(DogePreTrainedModel):
722
- """
723
- Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`DogeDecoderLayer`]
724
-
725
- Args:
726
- config: DogeConfig
727
- """
728
-
729
  def __init__(self, config: DogeConfig):
730
  super().__init__(config)
731
- self.config = config
732
  self.padding_idx = config.pad_token_id
733
  self.vocab_size = config.vocab_size
734
 
735
- self.word_embed = nn.Embedding(config.vocab_size, config.hidden_size, padding_idx=config.pad_token_id)
736
- self.rotary_emb = DogeRotaryEmbedding(config)
737
  self.layers = nn.ModuleList(
738
  [DogeDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
739
  )
740
- self.final_layernorm = DogeRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
 
741
  self.gradient_checkpointing = False
742
 
743
  # Initialize weights and apply final processing
744
  self.post_init()
745
 
746
- def get_input_embeddings(self):
747
- return self.word_embed
748
-
749
- def set_input_embeddings(self, value):
750
- self.word_embed = value
751
-
752
- @add_start_docstrings_to_model_forward(DOGE_INPUTS_DOCSTRING)
753
  def forward(
754
  self,
755
- input_ids: torch.LongTensor = None,
756
  attention_mask: Optional[torch.Tensor] = None,
757
  position_ids: Optional[torch.LongTensor] = None,
758
- past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
759
  inputs_embeds: Optional[torch.FloatTensor] = None,
760
  use_cache: Optional[bool] = None,
761
- output_attentions: Optional[bool] = None,
762
- output_hidden_states: Optional[bool] = None,
763
- return_dict: Optional[bool] = None,
764
  cache_position: Optional[torch.LongTensor] = None,
765
- **kwargs,
766
- ) -> Union[Tuple, BaseModelOutputWithPast]:
767
- output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
768
- output_hidden_states = (
769
- output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
770
- )
771
- use_cache = use_cache if use_cache is not None else self.config.use_cache
772
- return_dict = return_dict if return_dict is not None else self.config.use_return_dict
773
-
774
  if (input_ids is None) ^ (inputs_embeds is not None):
775
- raise ValueError("You cannot specify both input_ids and inputs_embeds")
776
-
777
- if self.gradient_checkpointing and self.training and use_cache:
778
- logger.warning_once(
779
- "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
780
- )
781
- use_cache = False
782
-
783
- if inputs_embeds is None:
784
- inputs_embeds = self.word_embed(input_ids)
785
 
786
  if use_cache and past_key_values is None:
787
  past_key_values = DynamicCache()
788
 
 
 
 
789
  if cache_position is None:
790
  past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
791
  cache_position = torch.arange(
792
  past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device
793
  )
794
-
795
  if position_ids is None:
796
  position_ids = cache_position.unsqueeze(0)
797
 
798
- causal_mask = self._update_causal_mask(
799
- attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions
 
 
 
 
 
 
800
  )
801
 
802
  hidden_states = inputs_embeds
@@ -804,236 +579,185 @@ class DogeModel(DogePreTrainedModel):
804
  # create position embeddings to be shared across the decoder layers
805
  position_embeddings = self.rotary_emb(hidden_states, position_ids)
806
 
807
- # decoder layers
808
- all_hidden_states = () if output_hidden_states else None
809
- all_self_attns = () if output_attentions else None
810
-
811
  for decoder_layer in self.layers[: self.config.num_hidden_layers]:
812
- if output_hidden_states:
813
- all_hidden_states += (hidden_states,)
814
-
815
- if self.gradient_checkpointing and self.training:
816
- layer_outputs = self._gradient_checkpointing_func(
817
- decoder_layer.__call__,
818
- hidden_states,
819
- causal_mask,
820
- position_ids,
821
- past_key_values,
822
- output_attentions,
823
- use_cache,
824
- cache_position,
825
- position_embeddings,
826
- )
827
- else:
828
- layer_outputs = decoder_layer(
829
- hidden_states,
830
- attention_mask=causal_mask,
831
- position_ids=position_ids,
832
- past_key_value=past_key_values,
833
- output_attentions=output_attentions,
834
- use_cache=use_cache,
835
- cache_position=cache_position,
836
- position_embeddings=position_embeddings,
837
- **kwargs,
838
- )
839
 
840
- hidden_states = layer_outputs[0]
841
 
842
- if output_attentions:
843
- all_self_attns += (layer_outputs[1],)
 
 
844
 
845
- hidden_states = self.final_layernorm(hidden_states)
846
 
847
- # add hidden states from the last decoder layer
848
- if output_hidden_states:
849
- all_hidden_states += (hidden_states,)
 
 
 
 
 
 
850
 
851
- output = BaseModelOutputWithPast(
852
- last_hidden_state=hidden_states,
853
- past_key_values=past_key_values if use_cache else None,
854
- hidden_states=all_hidden_states,
855
- attentions=all_self_attns,
856
- )
857
- return output if return_dict else output.to_tuple()
858
 
859
- def _update_causal_mask(
860
- self,
861
- attention_mask: torch.Tensor,
862
- input_tensor: torch.Tensor,
863
- cache_position: torch.Tensor,
864
- past_key_values: Cache,
865
- output_attentions: bool,
866
- ):
867
- # We have to provide attention_mask for dynamic mask computation
868
- past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
869
- using_static_cache = isinstance(past_key_values, StaticCache)
870
-
871
- dtype, device = input_tensor.dtype, input_tensor.device
872
- sequence_length = input_tensor.shape[1]
873
- if using_static_cache:
874
- target_length = past_key_values.get_max_cache_shape()
875
- else:
876
- target_length = (
877
- attention_mask.shape[-1]
878
- if isinstance(attention_mask, torch.Tensor)
879
- else past_seen_tokens + sequence_length + 1
880
- )
881
 
882
- # In case the provided `attention` mask is 2D, we generate a causal mask here (4D).
883
- causal_mask = self._prepare_4d_causal_attention_mask_with_cache_position(
884
- attention_mask,
885
- sequence_length=sequence_length,
886
- target_length=target_length,
887
- dtype=dtype,
888
- device=device,
889
- cache_position=cache_position,
890
- batch_size=input_tensor.shape[0],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
891
  )
 
892
 
893
- if (
894
- self.config._attn_implementation == "sdpa"
895
- and attention_mask is not None
896
- and attention_mask.device.type in ["cuda", "xpu"]
897
- and not output_attentions
898
- ):
899
- # Attend to all tokens in fully masked rows in the causal_mask, for example the relevant first rows when
900
- # using left padding. This is required by F.scaled_dot_product_attention memory-efficient attention path.
901
- # Details: https://github.com/pytorch/pytorch/issues/110213
902
- min_dtype = torch.finfo(dtype).min
903
- causal_mask = AttentionMaskConverter._unmask_unattended(causal_mask, min_dtype)
904
-
905
- return causal_mask
906
-
907
- @staticmethod
908
- def _prepare_4d_causal_attention_mask_with_cache_position(
909
- attention_mask: torch.Tensor,
910
- sequence_length: int,
911
- target_length: int,
912
- dtype: torch.dtype,
913
- device: torch.device,
914
- cache_position: torch.Tensor,
915
- batch_size: int,
916
- **kwargs,
917
- ):
918
- """
919
- Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape
920
- `(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing.
921
 
922
- Args:
923
- attention_mask (`torch.Tensor`):
924
- A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape
925
- `(batch_size, 1, query_length, key_value_length)`.
926
- sequence_length (`int`):
927
- The sequence length being processed.
928
- target_length (`int`):
929
- The target length: when generating with static cache, the mask should be as long as the static cache,
930
- to account for the 0 padding, the part of the cache that is not filled yet.
931
- dtype (`torch.dtype`):
932
- The dtype to use for the 4D attention mask.
933
- device (`torch.device`):
934
- The device to plcae the 4D attention mask on.
935
- cache_position (`torch.Tensor`):
936
- Indices depicting the position of the input sequence tokens in the sequence.
937
- batch_size (`torch.Tensor`):
938
- Batch size.
939
- """
940
- if attention_mask is not None and attention_mask.dim() == 4:
941
- # In this case we assume that the mask comes already in inverted form and requires no inversion or slicing.
942
- causal_mask = attention_mask
943
- else:
944
- min_dtype = torch.finfo(dtype).min
945
- causal_mask = torch.full(
946
- (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device
947
- )
948
- if sequence_length != 1:
949
- causal_mask = torch.triu(causal_mask, diagonal=1)
950
- causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1)
951
- causal_mask = causal_mask[None, None, :, :].expand(batch_size, 1, -1, -1)
952
- if attention_mask is not None:
953
- causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit
954
- mask_length = attention_mask.shape[-1]
955
- padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :]
956
- padding_mask = padding_mask == 0
957
- causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill(
958
- padding_mask, min_dtype
959
- )
960
 
961
- return causal_mask
 
962
 
963
 
 
964
  class DogeForCausalLM(DogePreTrainedModel, GenerationMixin):
965
  _tied_weights_keys = ["lm_head.weight"]
966
  _tp_plan = {"lm_head": "colwise_rep"}
 
967
 
968
- def __init__(self, config: DogeConfig):
969
  super().__init__(config)
970
- self.config = config
971
  self.model = DogeModel(config)
972
  self.vocab_size = config.vocab_size
973
  self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
 
 
 
974
 
975
  # Initialize weights and apply final processing
976
  self.post_init()
977
 
978
- def get_input_embeddings(self):
979
- return self.model.word_embed
980
-
981
- def set_input_embeddings(self, value):
982
- self.model.word_embed = value
983
-
984
- def get_output_embeddings(self):
985
- return self.lm_head
986
-
987
- def set_output_embeddings(self, new_embeddings):
988
- self.lm_head = new_embeddings
989
 
990
  def get_decoder(self):
991
  return self.model
992
 
993
- def set_decoder(self, decoder):
994
- self.model = decoder
995
-
996
- @add_start_docstrings_to_model_forward(DOGE_INPUTS_DOCSTRING)
997
- @replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
998
  def forward(
999
  self,
1000
- input_ids: torch.LongTensor = None,
1001
  attention_mask: Optional[torch.Tensor] = None,
1002
  position_ids: Optional[torch.LongTensor] = None,
1003
- past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
1004
  inputs_embeds: Optional[torch.FloatTensor] = None,
1005
  labels: Optional[torch.LongTensor] = None,
1006
  use_cache: Optional[bool] = None,
1007
- output_attentions: Optional[bool] = None,
1008
- output_hidden_states: Optional[bool] = None,
1009
- return_dict: Optional[bool] = None,
1010
  cache_position: Optional[torch.LongTensor] = None,
1011
  logits_to_keep: Union[int, torch.Tensor] = 0,
1012
- **kwargs: Unpack[LossKwargs],
1013
- ) -> Union[Tuple, CausalLMOutputWithPast]:
 
1014
  r"""
1015
- Args:
1016
- labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1017
- Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
1018
- config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
1019
- (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
1020
-
1021
- logits_to_keep (`int`, *optional*):
1022
- If an `int`, compute logits for the last `logits_to_keep` tokens. If `0`, calculate logits for all
1023
- `input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
1024
- token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
1025
- If a `torch.Tensor`, must be 1D corresponding to the indices to keep in the sequence length dimension.
1026
- This is useful when using packed tensor format (single dimension for batch and sequence length).
1027
-
1028
- Returns:
1029
 
1030
  Example:
1031
 
1032
  ```python
1033
- >>> from transformers import AutoTokenizer, AutoModelForCausalLM
1034
 
1035
- >>> model = AutoModelForCausalLM.from_pretrained("SmallDoge/Doge-20M")
1036
- >>> tokenizer = AutoTokenizer.from_pretrained("SmallDoge/Doge-20M")
1037
 
1038
  >>> prompt = "Hey, are you conscious? Can you talk to me?"
1039
  >>> inputs = tokenizer(prompt, return_tensors="pt")
@@ -1043,156 +767,56 @@ class DogeForCausalLM(DogePreTrainedModel, GenerationMixin):
1043
  >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
1044
  "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
1045
  ```"""
1046
- output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
1047
- output_hidden_states = (
1048
- output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
1049
  )
1050
- return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1051
 
1052
- # decoder output consists of (dec_features, layer_state, dec_hidden, dec_attn)
1053
- outputs = self.model(
1054
  input_ids=input_ids,
1055
  attention_mask=attention_mask,
1056
  position_ids=position_ids,
1057
  past_key_values=past_key_values,
1058
  inputs_embeds=inputs_embeds,
1059
  use_cache=use_cache,
1060
- output_attentions=output_attentions,
1061
- output_hidden_states=output_hidden_states,
1062
- return_dict=return_dict,
1063
  cache_position=cache_position,
1064
  **kwargs,
1065
  )
1066
 
1067
- hidden_states = outputs[0]
1068
- # only compute necessary logits, and do not upcast them to float if we are not computing the loss
1069
  slice_indices = slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep
1070
  logits = self.lm_head(hidden_states[:, slice_indices, :])
1071
 
1072
  loss = None
1073
  if labels is not None:
1074
- loss = self.loss_function(logits=logits, labels=labels, vocab_size=self.vocab_size, **kwargs)
1075
-
1076
- if not return_dict:
1077
- output = (logits,) + outputs[1:]
1078
- return (loss,) + output if loss is not None else output
 
 
 
 
 
 
 
 
1079
 
1080
- return CausalLMOutputWithPast(
1081
  loss=loss,
 
1082
  logits=logits,
1083
  past_key_values=outputs.past_key_values,
1084
  hidden_states=outputs.hidden_states,
1085
  attentions=outputs.attentions,
 
1086
  )
1087
 
1088
 
1089
- @add_start_docstrings(
1090
- """
1091
- The Doge Model transformer with a sequence classification head on top (linear layer).
1092
-
1093
- [`DogeForSequenceClassification`] uses the last token in order to do the classification, as other causal models
1094
- (e.g. GPT-2) do.
1095
-
1096
- Since it does classification on the last token, it requires to know the position of the last token. If a
1097
- `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
1098
- no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
1099
- padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
1100
- each row of the batch).
1101
- """,
1102
- DOGE_START_DOCSTRING,
1103
- )
1104
- class DogeForSequenceClassification(DogePreTrainedModel):
1105
- def __init__(self, config: DogeConfig):
1106
- super().__init__(config)
1107
- self.num_labels = config.num_labels
1108
-
1109
- self.model = DogeModel(config)
1110
- self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
1111
- self.config = config
1112
-
1113
- # Initialize weights and apply final processing
1114
- self.post_init()
1115
-
1116
- def get_input_embeddings(self):
1117
- return self.model.word_embed
1118
-
1119
- def set_input_embeddings(self, value):
1120
- self.model.word_embed = value
1121
-
1122
- @add_start_docstrings_to_model_forward(DOGE_INPUTS_DOCSTRING)
1123
- def forward(
1124
- self,
1125
- input_ids: Optional[torch.LongTensor] = None,
1126
- attention_mask: Optional[torch.Tensor] = None,
1127
- position_ids: Optional[torch.LongTensor] = None,
1128
- past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
1129
- inputs_embeds: Optional[torch.FloatTensor] = None,
1130
- labels: Optional[torch.LongTensor] = None,
1131
- use_cache: Optional[bool] = None,
1132
- output_attentions: Optional[bool] = None,
1133
- output_hidden_states: Optional[bool] = None,
1134
- return_dict: Optional[bool] = None,
1135
- ) -> Union[Tuple, SequenceClassifierOutputWithPast]:
1136
- r"""
1137
- labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1138
- Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
1139
- config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
1140
- `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
1141
- """
1142
- return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1143
-
1144
- transformer_outputs = self.model(
1145
- input_ids,
1146
- attention_mask=attention_mask,
1147
- position_ids=position_ids,
1148
- past_key_values=past_key_values,
1149
- inputs_embeds=inputs_embeds,
1150
- use_cache=use_cache,
1151
- output_attentions=output_attentions,
1152
- output_hidden_states=output_hidden_states,
1153
- return_dict=return_dict,
1154
- )
1155
- hidden_states = transformer_outputs[0]
1156
- logits = self.score(hidden_states)
1157
-
1158
- if input_ids is not None:
1159
- batch_size = input_ids.shape[0]
1160
- else:
1161
- batch_size = inputs_embeds.shape[0]
1162
-
1163
- if self.config.pad_token_id is None and batch_size != 1:
1164
- raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
1165
- if self.config.pad_token_id is None:
1166
- last_non_pad_token = -1
1167
- elif input_ids is not None:
1168
- # To handle both left- and right- padding, we take the rightmost token that is not equal to pad_token_id
1169
- non_pad_mask = (input_ids != self.config.pad_token_id).to(logits.device, torch.int32)
1170
- token_indices = torch.arange(input_ids.shape[-1], device=logits.device)
1171
- last_non_pad_token = (token_indices * non_pad_mask).argmax(-1)
1172
- else:
1173
- last_non_pad_token = -1
1174
- logger.warning_once(
1175
- f"{self.__class__.__name__} will not detect padding tokens in `inputs_embeds`. Results may be "
1176
- "unexpected if using padding tokens in conjunction with `inputs_embeds.`"
1177
- )
1178
-
1179
- pooled_logits = logits[torch.arange(batch_size, device=logits.device), last_non_pad_token]
1180
-
1181
- loss = None
1182
- if labels is not None:
1183
- loss = self.loss_function(logits=logits, labels=labels, pooled_logits=pooled_logits, config=self.config)
1184
-
1185
- if not return_dict:
1186
- output = (pooled_logits,) + transformer_outputs[1:]
1187
- return ((loss,) + output) if loss is not None else output
1188
-
1189
- return SequenceClassifierOutputWithPast(
1190
- loss=loss,
1191
- logits=pooled_logits,
1192
- past_key_values=transformer_outputs.past_key_values,
1193
- hidden_states=transformer_outputs.hidden_states,
1194
- attentions=transformer_outputs.attentions,
1195
- )
1196
 
1197
 
1198
  __all__ = ["DogeForCausalLM", "DogeModel", "DogePreTrainedModel", "DogeForSequenceClassification"]
 
5
  # modular_doge.py file directly. One of our CI enforces this.
6
  # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
7
  # coding=utf-8
8
+ # Copyright 2025 Jingze Shi and the HuggingFace Inc. team. All rights reserved.
9
  #
10
+ # The Doge family of small language models is trained by SmallDoge Team.
 
11
  #
12
  # Licensed under the Apache License, Version 2.0 (the "License");
13
  # you may not use this file except in compliance with the License.
 
22
  # limitations under the License.
23
 
24
  import math
25
+ from typing import Callable, Optional, Union
26
 
27
  import torch
28
  import torch.nn.functional as F
29
  from torch import nn
30
 
31
  from transformers.activations import ACT2FN
32
+ from transformers.cache_utils import Cache, DynamicCache
33
  from transformers.generation import GenerationMixin
34
+ from transformers.integrations import use_kernel_forward_from_hub
35
+ from transformers.integrations.flex_attention import compile_friendly_flex_attention
36
+ from transformers.masking_utils import create_causal_mask, create_sliding_window_causal_mask
37
+ from transformers.modeling_layers import GenericForSequenceClassification, GradientCheckpointingLayer
38
+ from transformers.modeling_outputs import MoeCausalLMOutputWithPast, MoeModelOutputWithPast
39
+ from transformers.modeling_rope_utils import ROPE_INIT_FUNCTIONS, dynamic_rope_update
40
+ from transformers.modeling_utils import AttentionInterface, PreTrainedModel
41
  from transformers.processing_utils import Unpack
42
+ from transformers.utils import TransformersKwargs, auto_docstring, can_return_tuple, is_torch_flex_attn_available
43
+ from transformers.utils.generic import OutputRecorder, check_model_inputs
 
 
 
 
 
 
44
  from .configuration_doge import DogeConfig
45
 
 
 
 
 
46
 
47
+ if is_torch_flex_attn_available():
48
+ from torch.nn.attention.flex_attention import BlockMask
49
 
50
 
51
+ @use_kernel_forward_from_hub("RMSNorm")
52
  class DogeRMSNorm(nn.Module):
53
  def __init__(self, hidden_size, eps=1e-6):
54
  """
 
85
  def __init__(self, config: DogeConfig, device=None):
86
  super().__init__()
87
  # BC: "rope_type" was originally "type"
88
+ if hasattr(config, "rope_scaling") and isinstance(config.rope_scaling, dict):
89
  self.rope_type = config.rope_scaling.get("rope_type", config.rope_scaling.get("type"))
90
  else:
91
  self.rope_type = "default"
 
99
  self.register_buffer("inv_freq", inv_freq, persistent=False)
100
  self.original_inv_freq = self.inv_freq
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  @torch.no_grad()
103
+ @dynamic_rope_update # power user: used with advanced RoPE types (e.g. dynamic rope)
104
  def forward(self, x, position_ids):
105
+ inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1).to(x.device)
 
 
 
 
106
  position_ids_expanded = position_ids[:, None, :].float()
107
+
108
+ device_type = x.device.type if isinstance(x.device.type, str) and x.device.type != "mps" else "cpu"
109
+ with torch.autocast(device_type=device_type, enabled=False): # Force float32
 
110
  freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)
111
  emb = torch.cat((freqs, freqs), dim=-1)
112
+ cos = emb.cos() * self.attention_scaling
113
+ sin = emb.sin() * self.attention_scaling
 
 
 
 
114
 
115
  return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)
116
 
 
169
  attention_mask: Optional[torch.Tensor],
170
  scaling: float,
171
  dropout: float = 0.0,
172
+ **kwargs: Unpack[TransformersKwargs],
173
+ ):
174
  key_states = repeat_kv(key, module.num_key_value_groups)
175
  value_states = repeat_kv(value, module.num_key_value_groups)
176
 
177
+ attn_weights = torch.matmul(query, key_states.transpose(2, 3)) * scaling
178
  if attention_mask is not None:
179
  causal_mask = attention_mask[:, :, :, : key_states.shape[-2]]
180
  attn_weights = attn_weights + causal_mask
181
 
182
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query.dtype)
183
+ attn_weights = nn.functional.dropout(attn_weights, p=dropout, training=module.training)
184
  attn_output = torch.matmul(attn_weights, value_states)
185
  attn_output = attn_output.transpose(1, 2).contiguous()
186
 
187
  return attn_output, attn_weights
188
 
189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  def flex_attention_forward(
191
  module: nn.Module,
192
  query: torch.Tensor,
193
  key: torch.Tensor,
194
  value: torch.Tensor,
195
+ attention_mask: Union[torch.Tensor, "BlockMask"],
196
  scaling: Optional[float] = None,
 
197
  softcap: Optional[float] = None,
198
  head_mask: Optional[torch.Tensor] = None,
199
  **kwargs,
200
+ ) -> tuple[torch.Tensor, torch.Tensor]:
201
+ block_mask = None
202
+ causal_mask = None
203
+ if isinstance(attention_mask, BlockMask):
204
+ block_mask = attention_mask
205
+ else:
206
+ causal_mask = attention_mask
207
+
208
+ if causal_mask is not None:
209
  causal_mask = causal_mask[:, :, :, : key.shape[-2]]
210
 
211
+ def score_mod(score, batch_idx, head_idx, q_idx, kv_idx):
 
 
 
212
  if softcap is not None:
213
  score = softcap * torch.tanh(score / softcap)
214
  if causal_mask is not None:
215
+ score = score + causal_mask[batch_idx][head_idx][q_idx][kv_idx]
216
  if head_mask is not None:
217
+ score = score + head_mask[batch_idx][head_idx][0][0]
218
  return score
219
 
220
+ attn_output, attention_weights = compile_friendly_flex_attention(
221
+ query,
222
+ key,
223
+ value,
224
+ score_mod=score_mod,
225
+ block_mask=block_mask,
 
 
 
 
 
 
 
 
 
 
 
 
226
  enable_gqa=True,
227
  scale=scaling,
228
  # Last time checked on PyTorch == 2.5.1: Flex Attention always computes the lse regardless.
 
236
  return attn_output, attention_weights
237
 
238
 
239
+ ALL_ATTENTION_FUNCTIONS = AttentionInterface()
240
+ ALL_ATTENTION_FUNCTIONS["doge_flex_attention"] = flex_attention_forward
 
 
 
 
241
 
 
 
242
 
243
+ class DogeAttention(nn.Module):
244
  def __init__(self, config: DogeConfig, layer_idx: Optional[int] = None):
245
  super().__init__()
246
  self.config = config
 
249
  self.num_key_value_groups = config.num_attention_heads // config.num_key_value_heads
250
  self.scaling = self.head_dim**-0.5
251
  self.attention_dropout = config.attention_dropout
252
+ self.keep_window_size = config.keep_window_size
253
 
254
  self.q_proj = nn.Linear(
255
+ config.hidden_size, config.num_attention_heads * self.head_dim, bias=config.attention_bias
256
  )
257
  self.k_proj = nn.Linear(
258
+ config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.attention_bias
259
  )
260
  self.v_proj = nn.Linear(
261
+ config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.attention_bias
262
  )
263
  # dynamic mask for the QK^T attention weights matrix
264
  self.A = nn.Parameter(torch.zeros(config.num_attention_heads))
265
  self.dt_proj = nn.Linear(
266
+ config.num_key_value_heads * self.head_dim, config.num_attention_heads, bias=config.attention_bias
267
  )
268
  self.o_proj = nn.Linear(
269
+ config.num_attention_heads * self.head_dim, config.hidden_size, bias=config.attention_bias
270
  )
271
 
272
  def forward(
273
  self,
274
  hidden_states: torch.Tensor,
275
+ position_embeddings: tuple[torch.Tensor, torch.Tensor],
276
  attention_mask: Optional[torch.Tensor] = None,
277
  past_key_value: Optional[Cache] = None,
278
  cache_position: Optional[torch.LongTensor] = None,
279
  **kwargs,
280
+ ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]:
281
  input_shape = hidden_states.shape[:-1]
282
  hidden_shape = (*input_shape, -1, self.head_dim)
283
 
 
297
  dt_states = self.dt_proj(
298
  value_states.transpose(1, 2).reshape(value_states.shape[0], value_states.shape[-2], -1)
299
  )
300
+ dt_states = torch.exp(self.A * F.softplus(dt_states)).transpose(-1, -2)
301
  attn_mask = self.prepare_dynamic_mask(
302
  hidden_states=hidden_states,
303
+ dt_states=dt_states,
304
+ keep_window_size=self.keep_window_size,
305
  attention_mask=attention_mask,
306
  )
307
 
308
  attention_interface: Callable = eager_attention_forward
309
  if self.config._attn_implementation != "eager":
310
+ attention_interface = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation]
 
 
 
 
 
 
311
 
312
  attn_output, attn_weights = attention_interface(
313
  self,
 
327
  def prepare_dynamic_mask(
328
  self,
329
  hidden_states: torch.Tensor,
330
+ dt_states: torch.Tensor,
331
+ keep_window_size: int = 2048,
332
  attention_mask: Optional[torch.Tensor] = None,
333
  ):
334
  """
335
+ The core idea of DMA is to calculate the dynamic attention mask to mask the tokens that should be masked, so as to form sparse attention.
336
+
337
+ Combine `dt_states` with `attention_mask` to generate the final `attn_mask`.
338
 
339
  Args:
340
  hidden_states (`torch.Tensor`): The input hidden_states, used to determine the minimum value of the current input precision.
341
+ dt_states (`torch.Tensor`): dt_states of shape `(batch_size, num_heads, key_sequence_length)`.
342
+ keep_window_size (`int`): The window size of tokens that are not dynamically masked, and dynamic masking is only performed when the sequence length exceeds this value.
343
  attention_mask (`torch.Tensor`, *optional*): attention mask of shape `(batch_size, 1, query_sequence_length, key_sequence_length)`.
344
  """
345
+ min_dtype = torch.finfo(hidden_states.dtype).min
346
+ dtype = hidden_states.dtype
347
+ attn_mask = dt_states[:, :, None, :].expand(
348
+ -1, -1, hidden_states.shape[1], -1
349
+ ) # [batch_size, num_heads, query_len, key_len]
350
+ if attention_mask is not None and not isinstance(attention_mask, BlockMask):
351
+ if attention_mask.dtype == torch.bool:
352
+ dtype = hidden_states.dtype
353
+ attention_mask = torch.where(
354
+ attention_mask, torch.tensor(0.0, device=attention_mask.device, dtype=dtype), min_dtype
355
+ )
356
+ attn_mask = attn_mask.masked_fill(attention_mask[:, :, :, : attn_mask.shape[-1]] != 0, min_dtype)
357
+ if attn_mask.shape[-1] > keep_window_size:
358
+ active_mask = torch.zeros_like(attn_mask, dtype=dtype, device=attn_mask.device)
359
+ topk_indices = torch.topk(attn_mask, keep_window_size, dim=-1, largest=True, sorted=False).indices
360
+ active_mask = active_mask.scatter(-1, topk_indices, 1.0)
361
+ attn_mask = attn_mask.masked_fill(active_mask == 0.0, min_dtype)
362
  return attn_mask
363
 
364
 
365
  class DogeMLP(nn.Module):
366
+ def __init__(self, config):
367
  super().__init__()
368
+ self.config = config
369
+ self.hidden_size = config.hidden_size
370
+ self.intermediate_size = config.intermediate_size
371
+ self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=config.mlp_bias)
372
+ self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=config.mlp_bias)
373
+ self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=config.mlp_bias)
374
  self.act_fn = ACT2FN[config.hidden_act]
375
 
376
+ def forward(self, x):
377
+ down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
378
+ return down_proj
379
 
 
 
 
 
 
 
 
 
 
 
 
380
 
381
+ class DogeCDMoE(nn.Module):
382
  def __init__(self, config: DogeConfig):
383
+ super().__init__()
384
+ self.hidden_size = config.hidden_size
385
+ self.intermediate_size = config.intermediate_size
386
  self.act_fn = ACT2FN[config.hidden_act]
387
 
388
+ self.num_experts = config.num_experts
389
+ self.num_keys = math.floor(math.sqrt(self.num_experts))
390
+ self.top_k = config.num_experts_per_tok
391
+ self.norm_topk_prob = config.norm_topk_prob
392
+
393
+ # shared expert
394
+ self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=config.mlp_bias)
395
+ self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=config.mlp_bias)
396
+ self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=config.mlp_bias)
397
 
398
+ # router gate for retrieval experts
399
+ self.router_gate = nn.Linear(self.hidden_size, self.num_keys * 2, bias=False)
 
400
 
401
+ # routed experts
402
+ self.down_embed = nn.Embedding(self.num_experts, self.hidden_size)
403
+ self.up_embed = nn.Embedding(self.num_experts, self.hidden_size)
404
 
405
  def forward(
406
  self,
 
409
  ) -> torch.Tensor:
410
  bsz, seq_len, _ = hidden_states.shape
411
 
412
+ # get routing logits with router gate
413
+ router_logits = self.router_gate(hidden_states).view(2, bsz * seq_len, -1)
 
 
 
 
414
 
415
+ # get experts with the highest routing logits
416
+ (scores_x, scores_y), (indices_x, indices_y) = router_logits.topk(self.num_keys, dim=-1)
417
  all_scores = scores_x.unsqueeze(-1) + scores_y.unsqueeze(-2)
418
+ all_indices = indices_x.unsqueeze(-1) * self.num_keys + indices_y.unsqueeze(-2)
419
+ all_scores = all_scores.view(*all_scores.shape[:-2], -1)
420
+ all_indices = all_indices.view(*all_indices.shape[:-2], -1)
421
+ scores, position_indices = all_scores.topk(self.top_k, dim=-1)
422
+ indices = all_indices.gather(-1, position_indices)
423
+ routing_weights = F.softmax(scores, dim=-1)
424
+ if self.norm_topk_prob:
425
+ routing_weights /= routing_weights.sum(dim=-1, keepdim=True)
426
+
427
+ # mix routed experts states with shared expert states
428
  down_embed = self.down_embed(indices)
429
  up_embed = self.up_embed(indices)
430
+ experts_weights = torch.matmul(down_embed, hidden_states.view(bsz * seq_len, -1, 1)).view(bsz * seq_len, -1)
431
+ experts_weights = self.act_fn(experts_weights) * routing_weights
432
+ experts_states = torch.matmul(experts_weights.view(bsz * seq_len, 1, -1), up_embed).view(bsz, seq_len, -1)
 
 
433
  hidden_states = self.down_proj(self.act_fn(self.gate_proj(hidden_states)) * self.up_proj(hidden_states))
434
  hidden_states = hidden_states + experts_states
435
+ return hidden_states, router_logits
436
 
437
 
438
+ class DogeDecoderLayer(GradientCheckpointingLayer):
439
  def __init__(self, config: DogeConfig, layer_idx: Optional[int] = None):
440
  super().__init__()
441
  self.hidden_dropout = config.hidden_dropout
442
 
443
+ self.input_layernorm = DogeRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
444
+ self.self_attn = DogeAttention(config=config, layer_idx=layer_idx)
445
+ self.input_residual = nn.Parameter(torch.ones(config.hidden_size))
446
 
447
+ self.post_attention_layernorm = DogeRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
448
+ self.mlp = DogeMLP(config) if not config.is_moe else DogeCDMoE(config)
449
+ self.post_attention_residual = nn.Parameter(torch.ones(config.hidden_size))
450
 
451
  def forward(
452
  self,
453
  hidden_states: torch.Tensor,
454
+ position_embeddings: tuple[torch.Tensor, torch.Tensor],
455
  attention_mask: Optional[torch.Tensor] = None,
456
  position_ids: Optional[torch.LongTensor] = None,
457
+ past_key_value: Optional[tuple[torch.Tensor]] = None,
 
458
  use_cache: Optional[bool] = False,
459
  cache_position: Optional[torch.LongTensor] = None,
460
+ **kwargs: Unpack[TransformersKwargs],
461
+ ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]:
 
462
  # sequence transformation
463
  residual = hidden_states
464
+ hidden_states = self.input_layernorm(hidden_states)
465
  hidden_states, self_attn_weights = self.self_attn(
466
  hidden_states=hidden_states,
467
+ position_embeddings=position_embeddings,
468
  attention_mask=attention_mask,
469
  position_ids=position_ids,
470
  past_key_value=past_key_value,
 
471
  use_cache=use_cache,
472
  cache_position=cache_position,
 
473
  **kwargs,
474
  )
 
475
  hidden_states = F.dropout(hidden_states, p=self.hidden_dropout, training=self.training)
476
+ hidden_states = self.input_residual * residual + hidden_states
477
 
478
  # state transformation
479
  residual = hidden_states
480
+ hidden_states = self.post_attention_layernorm(hidden_states)
481
+ hidden_states = self.mlp(hidden_states)
482
  hidden_states = F.dropout(hidden_states, p=self.hidden_dropout, training=self.training)
483
+ hidden_states = self.post_attention_residual * residual + hidden_states
 
 
 
 
484
 
485
+ return hidden_states
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486
 
487
 
488
+ @auto_docstring
 
 
 
489
  class DogePreTrainedModel(PreTrainedModel):
490
+ config: DogeConfig
491
  base_model_prefix = "model"
492
  supports_gradient_checkpointing = True
493
  _no_split_modules = ["DogeDecoderLayer"]
494
  _skip_keys_device_placement = ["past_key_values"]
495
+ _supports_flash_attn = False
496
  _supports_sdpa = True
497
+ _supports_flex_attn = True
498
+ _can_compile_fullgraph = False
499
+ _supports_attention_backend = True
500
+ _can_record_outputs = {
501
+ "router_logits": OutputRecorder(DogeCDMoE, index=1),
502
+ "hidden_states": DogeDecoderLayer,
503
+ "attentions": DogeAttention,
504
+ }
505
 
506
  def _init_weights(self, module):
507
+ """Initialize the weights"""
508
+ super()._init_weights(module)
509
+ if isinstance(module, DogeAttention):
510
+ if hasattr(module, "A"):
511
+ module.A.data.zero_()
512
+ elif isinstance(module, DogeDecoderLayer):
513
+ if hasattr(module, "input_residual"):
514
+ module.input_residual.data.fill_(1.0)
515
+ if hasattr(module, "post_attention_residual"):
516
+ module.post_attention_residual.data.fill_(1.0)
517
+
518
+
519
+ @auto_docstring
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
520
  class DogeModel(DogePreTrainedModel):
 
 
 
 
 
 
 
521
  def __init__(self, config: DogeConfig):
522
  super().__init__(config)
 
523
  self.padding_idx = config.pad_token_id
524
  self.vocab_size = config.vocab_size
525
 
526
+ self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
 
527
  self.layers = nn.ModuleList(
528
  [DogeDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
529
  )
530
+ self.norm = DogeRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
531
+ self.rotary_emb = DogeRotaryEmbedding(config=config)
532
  self.gradient_checkpointing = False
533
 
534
  # Initialize weights and apply final processing
535
  self.post_init()
536
 
537
+ @check_model_inputs
538
+ @auto_docstring
 
 
 
 
 
539
  def forward(
540
  self,
541
+ input_ids: Optional[torch.LongTensor] = None,
542
  attention_mask: Optional[torch.Tensor] = None,
543
  position_ids: Optional[torch.LongTensor] = None,
544
+ past_key_values: Optional[Cache] = None,
545
  inputs_embeds: Optional[torch.FloatTensor] = None,
546
  use_cache: Optional[bool] = None,
 
 
 
547
  cache_position: Optional[torch.LongTensor] = None,
548
+ **kwargs: Unpack[TransformersKwargs],
549
+ ) -> MoeModelOutputWithPast:
 
 
 
 
 
 
 
550
  if (input_ids is None) ^ (inputs_embeds is not None):
551
+ raise ValueError("You must specify exactly one of input_ids or inputs_embeds")
 
 
 
 
 
 
 
 
 
552
 
553
  if use_cache and past_key_values is None:
554
  past_key_values = DynamicCache()
555
 
556
+ if inputs_embeds is None:
557
+ inputs_embeds = self.embed_tokens(input_ids)
558
+
559
  if cache_position is None:
560
  past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
561
  cache_position = torch.arange(
562
  past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device
563
  )
 
564
  if position_ids is None:
565
  position_ids = cache_position.unsqueeze(0)
566
 
567
+ mask_function = create_causal_mask if self.config.sliding_window is None else create_sliding_window_causal_mask
568
+ causal_mask = mask_function(
569
+ config=self.config,
570
+ input_embeds=inputs_embeds,
571
+ attention_mask=attention_mask,
572
+ cache_position=cache_position,
573
+ past_key_values=past_key_values,
574
+ position_ids=position_ids,
575
  )
576
 
577
  hidden_states = inputs_embeds
 
579
  # create position embeddings to be shared across the decoder layers
580
  position_embeddings = self.rotary_emb(hidden_states, position_ids)
581
 
 
 
 
 
582
  for decoder_layer in self.layers[: self.config.num_hidden_layers]:
583
+ hidden_states = decoder_layer(
584
+ hidden_states,
585
+ position_embeddings=position_embeddings,
586
+ attention_mask=causal_mask,
587
+ position_ids=position_ids,
588
+ past_key_value=past_key_values,
589
+ use_cache=use_cache,
590
+ cache_position=cache_position,
591
+ **kwargs,
592
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
593
 
594
+ hidden_states = self.norm(hidden_states)
595
 
596
+ return MoeModelOutputWithPast( # only diff with Mistral is the output type, we need MoE
597
+ last_hidden_state=hidden_states,
598
+ past_key_values=past_key_values,
599
+ )
600
 
 
601
 
602
+ def load_balancing_loss_func(
603
+ gate_logits: Union[torch.Tensor, tuple[torch.Tensor], None],
604
+ num_experts: Optional[int] = None,
605
+ num_keys: Optional[int] = None,
606
+ top_k: int = 2,
607
+ attention_mask: Optional[torch.Tensor] = None,
608
+ ) -> Union[torch.Tensor, int]:
609
+ r"""
610
+ Computes auxiliary load balancing loss as in Switch Transformer - implemented in Pytorch.
611
 
612
+ See Switch Transformer (https://arxiv.org/abs/2101.03961) for more details. This function implements the loss
613
+ function presented in equations (4) - (6) of the paper. It aims at penalizing cases where the routing between
614
+ experts is too unbalanced.
 
 
 
 
615
 
616
+ Args:
617
+ gate_logits:
618
+ Logits from the `router_gate`, should be a tuple of model.config.num_hidden_layers tensors of
619
+ shape [2, batch_size * sequence_length, num_keys].
620
+ num_experts:
621
+ Number of experts
622
+ num_keys:
623
+ Number of keys
624
+ top_k:
625
+ The number of experts to route per-token, can be also interpreted as the `top-k` routing
626
+ parameter.
627
+ attention_mask (`torch.Tensor`, *optional*):
628
+ The attention_mask used in forward function
629
+ shape [batch_size X sequence_length] if not None.
 
 
 
 
 
 
 
 
630
 
631
+ Returns:
632
+ The auxiliary loss.
633
+ """
634
+ if gate_logits is None or not isinstance(gate_logits, tuple):
635
+ return 0
636
+
637
+ compute_dtype = gate_logits[0].dtype
638
+ compute_device = gate_logits[0].device
639
+ all_expert_indices = []
640
+ all_routing_weights = []
641
+
642
+ for layer_gate_logits in gate_logits:
643
+ layer_gate_logits = layer_gate_logits.to(compute_device)
644
+
645
+ (scores_x, scores_y), (indices_x, indices_y) = layer_gate_logits.topk(num_keys, dim=-1)
646
+
647
+ all_scores = scores_x.unsqueeze(-1) + scores_y.unsqueeze(-2)
648
+ all_indices = indices_x.unsqueeze(-1) * num_keys + indices_y.unsqueeze(-2)
649
+ all_scores = all_scores.view(*all_scores.shape[:-2], -1)
650
+ all_indices = all_indices.view(*all_indices.shape[:-2], -1)
651
+
652
+ _, position_indices = all_scores.topk(top_k, dim=-1)
653
+ expert_indices = all_indices.gather(-1, position_indices)
654
+
655
+ routing_weights = F.softmax(all_scores, dim=-1)
656
+
657
+ all_expert_indices.append(expert_indices)
658
+ all_routing_weights.append(routing_weights)
659
+ all_expert_indices = torch.cat(all_expert_indices, dim=0)
660
+ all_routing_weights = torch.cat(all_routing_weights, dim=0)
661
+
662
+ if attention_mask is None:
663
+ # Compute the percentage of tokens routed to each experts
664
+ all_expert_indices = all_expert_indices.view(-1)
665
+ tokens_per_expert = torch.zeros(num_experts, dtype=compute_dtype, device=compute_device)
666
+ pad = torch.ones_like(all_expert_indices, dtype=compute_dtype, device=compute_device)
667
+ tokens_per_expert = tokens_per_expert.scatter_add_(0, all_expert_indices, pad) / all_expert_indices.shape[0]
668
+
669
+ # Compute the average probability of routing to these experts
670
+ router_prob_per_expert = torch.mean(all_routing_weights, dim=0)
671
+ else:
672
+ batch_size, sequence_length = attention_mask.shape
673
+ num_hidden_layers = len(gate_logits)
674
+
675
+ # Compute the mask that masks all padding tokens as 0 with the same shape of expert_mask
676
+ expert_attention_mask = (
677
+ attention_mask[None, :, :, None]
678
+ .expand((num_hidden_layers, batch_size, sequence_length, top_k))
679
+ .reshape(-1)
680
+ .to(compute_device)
681
  )
682
+ all_expert_indices = all_expert_indices.view(-1)[expert_attention_mask.bool()]
683
 
684
+ # Compute the percentage of tokens routed to each experts
685
+ tokens_per_expert = torch.zeros(num_experts, dtype=compute_dtype, device=compute_device)
686
+ pad = torch.ones_like(all_expert_indices, dtype=compute_dtype, device=compute_device)
687
+ tokens_per_expert = tokens_per_expert.scatter_add_(0, all_expert_indices, pad) / torch.sum(
688
+ expert_attention_mask
689
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
690
 
691
+ # Compute the mask that masks all padding tokens as 0 with the same shape of tokens_per_expert
692
+ router_per_expert_attention_mask = (
693
+ attention_mask[None, :, :, None]
694
+ .expand((num_hidden_layers, batch_size, sequence_length, num_experts))
695
+ .reshape(-1, num_experts)
696
+ .to(compute_device)
697
+ )
698
+
699
+ # Compute the average probability of routing to these experts
700
+ router_prob_per_expert = torch.sum(all_routing_weights * router_per_expert_attention_mask, dim=0) / torch.sum(
701
+ router_per_expert_attention_mask, dim=0
702
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
703
 
704
+ overall_loss = torch.sum(tokens_per_expert * router_prob_per_expert)
705
+ return overall_loss * num_experts
706
 
707
 
708
+ @auto_docstring
709
  class DogeForCausalLM(DogePreTrainedModel, GenerationMixin):
710
  _tied_weights_keys = ["lm_head.weight"]
711
  _tp_plan = {"lm_head": "colwise_rep"}
712
+ _pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
713
 
714
+ def __init__(self, config):
715
  super().__init__(config)
 
716
  self.model = DogeModel(config)
717
  self.vocab_size = config.vocab_size
718
  self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
719
+ self.router_aux_loss_coef = config.router_aux_loss_coef
720
+ self.num_experts = config.num_experts
721
+ self.num_experts_per_tok = config.num_experts_per_tok
722
 
723
  # Initialize weights and apply final processing
724
  self.post_init()
725
 
726
+ def set_decoder(self, decoder):
727
+ self.model = decoder
 
 
 
 
 
 
 
 
 
728
 
729
  def get_decoder(self):
730
  return self.model
731
 
732
+ @can_return_tuple
733
+ @auto_docstring
 
 
 
734
  def forward(
735
  self,
736
+ input_ids: Optional[torch.LongTensor] = None,
737
  attention_mask: Optional[torch.Tensor] = None,
738
  position_ids: Optional[torch.LongTensor] = None,
739
+ past_key_values: Optional[list[torch.FloatTensor]] = None,
740
  inputs_embeds: Optional[torch.FloatTensor] = None,
741
  labels: Optional[torch.LongTensor] = None,
742
  use_cache: Optional[bool] = None,
 
 
 
743
  cache_position: Optional[torch.LongTensor] = None,
744
  logits_to_keep: Union[int, torch.Tensor] = 0,
745
+ output_router_logits: Optional[bool] = None,
746
+ **kwargs: Unpack[TransformersKwargs],
747
+ ) -> MoeCausalLMOutputWithPast:
748
  r"""
749
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
750
+ Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
751
+ config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
752
+ (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
 
 
 
 
 
 
 
 
 
 
753
 
754
  Example:
755
 
756
  ```python
757
+ >>> from transformers import AutoTokenizer, DogeForCausalLM
758
 
759
+ >>> model = DogeForCausalLM.from_pretrained("SmallDoge/Doge-320M")
760
+ >>> tokenizer = AutoTokenizer.from_pretrained("SmallDoge/Doge-320M")
761
 
762
  >>> prompt = "Hey, are you conscious? Can you talk to me?"
763
  >>> inputs = tokenizer(prompt, return_tensors="pt")
 
767
  >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
768
  "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
769
  ```"""
770
+ output_router_logits = (
771
+ output_router_logits if output_router_logits is not None else self.config.output_router_logits
 
772
  )
 
773
 
774
+ # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
775
+ outputs: MoeModelOutputWithPast = self.model(
776
  input_ids=input_ids,
777
  attention_mask=attention_mask,
778
  position_ids=position_ids,
779
  past_key_values=past_key_values,
780
  inputs_embeds=inputs_embeds,
781
  use_cache=use_cache,
 
 
 
782
  cache_position=cache_position,
783
  **kwargs,
784
  )
785
 
786
+ hidden_states = outputs.last_hidden_state
787
+ # Only compute necessary logits, and do not upcast them to float if we are not computing the loss
788
  slice_indices = slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep
789
  logits = self.lm_head(hidden_states[:, slice_indices, :])
790
 
791
  loss = None
792
  if labels is not None:
793
+ loss = self.loss_function(logits, labels, self.vocab_size, **kwargs)
794
+
795
+ aux_loss = None
796
+ if output_router_logits:
797
+ aux_loss = load_balancing_loss_func(
798
+ outputs.router_logits,
799
+ self.num_experts,
800
+ math.floor(math.sqrt(self.num_experts)),
801
+ self.num_experts_per_tok,
802
+ attention_mask,
803
+ )
804
+ if labels is not None:
805
+ loss += self.router_aux_loss_coef * aux_loss.to(loss.device) # make sure to reside in the same device
806
 
807
+ return MoeCausalLMOutputWithPast(
808
  loss=loss,
809
+ aux_loss=aux_loss,
810
  logits=logits,
811
  past_key_values=outputs.past_key_values,
812
  hidden_states=outputs.hidden_states,
813
  attentions=outputs.attentions,
814
+ router_logits=outputs.router_logits,
815
  )
816
 
817
 
818
+ class DogeForSequenceClassification(GenericForSequenceClassification, DogePreTrainedModel):
819
+ pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
820
 
821
 
822
  __all__ = ["DogeForCausalLM", "DogeModel", "DogePreTrainedModel", "DogeForSequenceClassification"]