Aratako commited on
Commit
fa5a234
1 Parent(s): f02e707

Upload 2 files

Browse files
Files changed (2) hide show
  1. configuration_tanuki.py +169 -0
  2. modeling_tanuki.py +1759 -0
configuration_tanuki.py ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2023 Mixtral AI and the HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """Tanuki model configuration"""
16
+
17
+ from transformers.configuration_utils import PretrainedConfig
18
+ from transformers.utils import logging
19
+
20
+
21
+ logger = logging.get_logger(__name__)
22
+
23
+
24
+ class TanukiConfig(PretrainedConfig):
25
+ r"""
26
+ This is the configuration class to store the configuration of a [`TanukiModel`]. It is used to instantiate an
27
+ Tanuki model according to the specified arguments, defining the model architecture. Instantiating a configuration
28
+ with the defaults will yield a similar configuration to that of the Tanuki-7B-v0.1 or Tanuki-7B-Instruct-v0.1.
29
+
30
+ [mixtralai/Tanuki-8x7B](https://huggingface.co/mixtralai/Tanuki-8x7B)
31
+ [mixtralai/Tanuki-7B-Instruct-v0.1](https://huggingface.co/mixtralai/Tanuki-7B-Instruct-v0.1)
32
+
33
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
34
+ documentation from [`PretrainedConfig`] for more information.
35
+
36
+
37
+ Args:
38
+ vocab_size (`int`, *optional*, defaults to 32000):
39
+ Vocabulary size of the Tanuki model. Defines the number of different tokens that can be represented by the
40
+ `inputs_ids` passed when calling [`TanukiModel`]
41
+ hidden_size (`int`, *optional*, defaults to 4096):
42
+ Dimension of the hidden representations.
43
+ intermediate_size (`int`, *optional*, defaults to 14336):
44
+ Dimension of the MLP representations.
45
+ num_hidden_layers (`int`, *optional*, defaults to 32):
46
+ Number of hidden layers in the Transformer encoder.
47
+ num_attention_heads (`int`, *optional*, defaults to 32):
48
+ Number of attention heads for each attention layer in the Transformer encoder.
49
+ num_key_value_heads (`int`, *optional*, defaults to 8):
50
+ This is the number of key_value heads that should be used to implement Grouped Query Attention. If
51
+ `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
52
+ `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
53
+ converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
54
+ by meanpooling all the original heads within that group. For more details checkout [this
55
+ paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to `8`.
56
+ hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
57
+ The non-linear activation function (function or string) in the decoder.
58
+ max_position_embeddings (`int`, *optional*, defaults to `4096*32`):
59
+ The maximum sequence length that this model might ever be used with. Tanuki's sliding window attention
60
+ allows sequence of up to 4096*32 tokens.
61
+ initializer_range (`float`, *optional*, defaults to 0.02):
62
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
63
+ rms_norm_eps (`float`, *optional*, defaults to 1e-05):
64
+ The epsilon used by the rms normalization layers.
65
+ use_cache (`bool`, *optional*, defaults to `True`):
66
+ Whether or not the model should return the last key/values attentions (not used by all models). Only
67
+ relevant if `config.is_decoder=True`.
68
+ pad_token_id (`int`, *optional*):
69
+ The id of the padding token.
70
+ bos_token_id (`int`, *optional*, defaults to 1):
71
+ The id of the "beginning-of-sequence" token.
72
+ eos_token_id (`int`, *optional*, defaults to 2):
73
+ The id of the "end-of-sequence" token.
74
+ tie_word_embeddings (`bool`, *optional*, defaults to `False`):
75
+ Whether the model's input and output word embeddings should be tied.
76
+ rope_theta (`float`, *optional*, defaults to 1000000.0):
77
+ The base period of the RoPE embeddings.
78
+ sliding_window (`int`, *optional*):
79
+ Sliding window attention window size. If not specified, will default to `4096`.
80
+ attention_dropout (`float`, *optional*, defaults to 0.0):
81
+ The dropout ratio for the attention probabilities.
82
+ num_experts_per_tok (`int`, *optional*, defaults to 2):
83
+ The number of experts to route per-token, can be also interpreted as the `top-k` routing
84
+ parameter
85
+ num_local_experts (`int`, *optional*, defaults to 8):
86
+ Number of experts per Sparse MLP layer.
87
+ output_router_logits (`bool`, *optional*, defaults to `False`):
88
+ Whether or not the router logits should be returned by the model. Enabeling this will also
89
+ allow the model to output the auxiliary loss. See [here]() for more details
90
+ router_aux_loss_coef (`float`, *optional*, defaults to 0.001):
91
+ The aux loss factor for the total loss.
92
+ router_jitter_noise (`float`, *optional*, defaults to 0.0):
93
+ Amount of noise to add to the router.
94
+
95
+ ```python
96
+ >>> from transformers import TanukiModel, TanukiConfig
97
+
98
+ >>> # Initializing a Tanuki 7B style configuration
99
+ >>> configuration = TanukiConfig()
100
+
101
+ >>> # Initializing a model from the Tanuki 7B style configuration
102
+ >>> model = TanukiModel(configuration)
103
+
104
+ >>> # Accessing the model configuration
105
+ >>> configuration = model.config
106
+ ```"""
107
+
108
+ model_type = "Tanuki"
109
+ keys_to_ignore_at_inference = ["past_key_values"]
110
+
111
+ def __init__(
112
+ self,
113
+ vocab_size=32000,
114
+ hidden_size=4096,
115
+ intermediate_size=14336,
116
+ num_hidden_layers=32,
117
+ num_attention_heads=32,
118
+ num_key_value_heads=8,
119
+ hidden_act="silu",
120
+ max_position_embeddings=4096,
121
+ initializer_range=0.02,
122
+ rms_norm_eps=1e-5,
123
+ use_cache=True,
124
+ pad_token_id=None,
125
+ bos_token_id=1,
126
+ eos_token_id=2,
127
+ tie_word_embeddings=False,
128
+ rope_theta=500000.0,
129
+ sliding_window=None,
130
+ attention_dropout=0.0,
131
+ num_experts_per_tok=2,
132
+ num_local_experts=8,
133
+ output_router_logits=False,
134
+ router_aux_loss_coef=0.001,
135
+ router_jitter_noise=0.0,
136
+ **kwargs,
137
+ ):
138
+ self.vocab_size = vocab_size
139
+ self.max_position_embeddings = max_position_embeddings
140
+ self.hidden_size = hidden_size
141
+ self.intermediate_size = intermediate_size
142
+ self.num_hidden_layers = num_hidden_layers
143
+ self.num_attention_heads = num_attention_heads
144
+ self.sliding_window = sliding_window
145
+
146
+ # for backward compatibility
147
+ if num_key_value_heads is None:
148
+ num_key_value_heads = num_attention_heads
149
+
150
+ self.num_key_value_heads = num_key_value_heads
151
+ self.hidden_act = hidden_act
152
+ self.initializer_range = initializer_range
153
+ self.rms_norm_eps = rms_norm_eps
154
+ self.use_cache = use_cache
155
+ self.rope_theta = rope_theta
156
+ self.attention_dropout = attention_dropout
157
+
158
+ self.num_experts_per_tok = num_experts_per_tok
159
+ self.num_local_experts = num_local_experts
160
+ self.output_router_logits = output_router_logits
161
+ self.router_aux_loss_coef = router_aux_loss_coef
162
+ self.router_jitter_noise = router_jitter_noise
163
+ super().__init__(
164
+ pad_token_id=pad_token_id,
165
+ bos_token_id=bos_token_id,
166
+ eos_token_id=eos_token_id,
167
+ tie_word_embeddings=tie_word_embeddings,
168
+ **kwargs,
169
+ )
modeling_tanuki.py ADDED
@@ -0,0 +1,1759 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2023 Mistral AI and the HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
5
+ # and OPT implementations in this library. It has been modified from its
6
+ # original forms to accommodate minor architectural differences compared
7
+ # to GPT-NeoX and OPT used by the Meta AI team that trained the model.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ """PyTorch Tanuki model."""
21
+
22
+ import inspect
23
+ import math
24
+ from typing import List, Optional, Tuple, Union
25
+
26
+ import torch
27
+ import torch.nn.functional as F
28
+ import torch.utils.checkpoint
29
+ from torch import nn
30
+ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
31
+
32
+ from transformers.activations import ACT2FN
33
+ from transformers.cache_utils import Cache, DynamicCache, StaticCache
34
+ from transformers.modeling_attn_mask_utils import (
35
+ AttentionMaskConverter,
36
+ _prepare_4d_causal_attention_mask,
37
+ )
38
+ from transformers.modeling_outputs import (
39
+ MoeCausalLMOutputWithPast,
40
+ MoeModelOutputWithPast,
41
+ SequenceClassifierOutputWithPast,
42
+ TokenClassifierOutput,
43
+ )
44
+ from transformers.modeling_utils import PreTrainedModel
45
+ from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_13
46
+ from transformers.utils import (
47
+ add_start_docstrings,
48
+ add_start_docstrings_to_model_forward,
49
+ is_flash_attn_2_available,
50
+ is_flash_attn_greater_or_equal_2_10,
51
+ logging,
52
+ replace_return_docstrings,
53
+ )
54
+ from transformers.utils.import_utils import is_torch_fx_available
55
+ from .configuration_tanuki import TanukiConfig
56
+
57
+
58
+ if is_flash_attn_2_available():
59
+ #from transformers.modeling_flash_attention_utils import _flash_attention_forward
60
+ from flash_attn import flash_attn_func, flash_attn_varlen_func
61
+ from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa
62
+
63
+ _flash_supports_window_size = "window_size" in list(inspect.signature(flash_attn_func).parameters)
64
+
65
+ # This makes `_prepare_4d_causal_attention_mask` a leaf function in the FX graph.
66
+ # It means that the function will not be traced through and simply appear as a node in the graph.
67
+ if is_torch_fx_available():
68
+ if not is_torch_greater_or_equal_than_1_13:
69
+ import torch.fx
70
+
71
+ _prepare_4d_causal_attention_mask = torch.fx.wrap(_prepare_4d_causal_attention_mask)
72
+
73
+
74
+ logger = logging.get_logger(__name__)
75
+
76
+ _CONFIG_FOR_DOC = "TanukiConfig"
77
+
78
+
79
+ def load_balancing_loss_func(
80
+ gate_logits: torch.Tensor, num_experts: torch.Tensor = None, top_k=2, attention_mask: Optional[torch.Tensor] = None
81
+ ) -> float:
82
+ r"""
83
+ Computes auxiliary load balancing loss as in Switch Transformer - implemented in Pytorch.
84
+
85
+ See Switch Transformer (https://arxiv.org/abs/2101.03961) for more details. This function implements the loss
86
+ function presented in equations (4) - (6) of the paper. It aims at penalizing cases where the routing between
87
+ experts is too unbalanced.
88
+
89
+ Args:
90
+ gate_logits (Union[`torch.Tensor`, Tuple[torch.Tensor]):
91
+ Logits from the `gate`, should be a tuple of model.config.num_hidden_layers tensors of
92
+ shape [batch_size X sequence_length, num_experts].
93
+ attention_mask (`torch.Tensor`, None):
94
+ The attention_mask used in forward function
95
+ shape [batch_size X sequence_length] if not None.
96
+ num_experts (`int`, *optional*):
97
+ Number of experts
98
+
99
+ Returns:
100
+ The auxiliary loss.
101
+ """
102
+ if gate_logits is None or not isinstance(gate_logits, tuple):
103
+ return 0
104
+
105
+ if isinstance(gate_logits, tuple):
106
+ compute_device = gate_logits[0].device
107
+ concatenated_gate_logits = torch.cat([layer_gate.to(compute_device) for layer_gate in gate_logits], dim=0)
108
+
109
+ routing_weights = torch.nn.functional.softmax(concatenated_gate_logits, dim=-1)
110
+
111
+ _, selected_experts = torch.topk(routing_weights, top_k, dim=-1)
112
+
113
+ expert_mask = torch.nn.functional.one_hot(selected_experts, num_experts)
114
+
115
+ if attention_mask is None:
116
+ # Compute the percentage of tokens routed to each experts
117
+ tokens_per_expert = torch.mean(expert_mask.float(), dim=0)
118
+
119
+ # Compute the average probability of routing to these experts
120
+ router_prob_per_expert = torch.mean(routing_weights, dim=0)
121
+ else:
122
+ batch_size, sequence_length = attention_mask.shape
123
+ num_hidden_layers = concatenated_gate_logits.shape[0] // (batch_size * sequence_length)
124
+
125
+ # Compute the mask that masks all padding tokens as 0 with the same shape of expert_mask
126
+ expert_attention_mask = (
127
+ attention_mask[None, :, :, None, None]
128
+ .expand((num_hidden_layers, batch_size, sequence_length, top_k, num_experts))
129
+ .reshape(-1, top_k, num_experts)
130
+ .to(compute_device)
131
+ )
132
+
133
+ # Compute the percentage of tokens routed to each experts
134
+ tokens_per_expert = torch.sum(expert_mask.float() * expert_attention_mask, dim=0) / torch.sum(
135
+ expert_attention_mask, dim=0
136
+ )
137
+
138
+ # Compute the mask that masks all padding tokens as 0 with the same shape of tokens_per_expert
139
+ router_per_expert_attention_mask = (
140
+ attention_mask[None, :, :, None]
141
+ .expand((num_hidden_layers, batch_size, sequence_length, num_experts))
142
+ .reshape(-1, num_experts)
143
+ .to(compute_device)
144
+ )
145
+
146
+ # Compute the average probability of routing to these experts
147
+ router_prob_per_expert = torch.sum(routing_weights * router_per_expert_attention_mask, dim=0) / torch.sum(
148
+ router_per_expert_attention_mask, dim=0
149
+ )
150
+
151
+ overall_loss = torch.sum(tokens_per_expert * router_prob_per_expert.unsqueeze(0))
152
+ return overall_loss * num_experts
153
+
154
+ # Copied from transformers.models.llama.modeling_llama._get_unpad_data
155
+ def _get_unpad_data(attention_mask):
156
+ seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
157
+ indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
158
+ max_seqlen_in_batch = seqlens_in_batch.max().item()
159
+ cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.int32), (1, 0))
160
+ return (
161
+ indices,
162
+ cu_seqlens,
163
+ max_seqlen_in_batch,
164
+ )
165
+
166
+
167
+ # Copied from transformers.models.llama.modeling_llama.LlamaRMSNorm with Llama->Mixtral
168
+ class TanukiRMSNorm(nn.Module):
169
+ def __init__(self, hidden_size, eps=1e-6):
170
+ """
171
+ TanukiRMSNorm is equivalent to T5LayerNorm
172
+ """
173
+ super().__init__()
174
+ self.weight = nn.Parameter(torch.ones(hidden_size))
175
+ self.variance_epsilon = eps
176
+
177
+ def forward(self, hidden_states):
178
+ input_dtype = hidden_states.dtype
179
+ hidden_states = hidden_states.to(torch.float32)
180
+ variance = hidden_states.pow(2).mean(-1, keepdim=True)
181
+ hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
182
+ return self.weight * hidden_states.to(input_dtype)
183
+
184
+
185
+ # copied from transformers.models.mistral.modeling_mistral.MistralRotaryEmbedding with Mistral->Mixtral
186
+ # TODO @longjie no longer copied from Mistral after static cache
187
+ class TanukiRotaryEmbedding(nn.Module):
188
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
189
+ super().__init__()
190
+
191
+ self.dim = dim
192
+ self.max_position_embeddings = max_position_embeddings
193
+ self.base = base
194
+ inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2, dtype=torch.int64).float().to(device) / self.dim))
195
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
196
+
197
+ # Build here to make `torch.jit.trace` work.
198
+ self._set_cos_sin_cache(
199
+ seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
200
+ )
201
+
202
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
203
+ self.max_seq_len_cached = seq_len
204
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=torch.int64).type_as(self.inv_freq)
205
+
206
+ freqs = torch.outer(t, self.inv_freq)
207
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
208
+ emb = torch.cat((freqs, freqs), dim=-1)
209
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
210
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
211
+
212
+ def forward(self, x, seq_len=None):
213
+ # x: [bs, num_attention_heads, seq_len, head_size]
214
+ if seq_len > self.max_seq_len_cached:
215
+ self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
216
+
217
+ return (
218
+ self.cos_cached[:seq_len].to(dtype=x.dtype),
219
+ self.sin_cached[:seq_len].to(dtype=x.dtype),
220
+ )
221
+
222
+
223
+ # Copied from transformers.models.llama.modeling_llama.rotate_half
224
+ def rotate_half(x):
225
+ """Rotates half the hidden dims of the input."""
226
+ x1 = x[..., : x.shape[-1] // 2]
227
+ x2 = x[..., x.shape[-1] // 2 :]
228
+ return torch.cat((-x2, x1), dim=-1)
229
+
230
+
231
+ # copied from transformers.models.mistral.modeling_mistral.apply_rotary_pos_emb
232
+ # TODO @longjie no longer copied from Mistral after static cache
233
+ def apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1):
234
+ """Applies Rotary Position Embedding to the query and key tensors.
235
+
236
+ Args:
237
+ q (`torch.Tensor`): The query tensor.
238
+ k (`torch.Tensor`): The key tensor.
239
+ cos (`torch.Tensor`): The cosine part of the rotary embedding.
240
+ sin (`torch.Tensor`): The sine part of the rotary embedding.
241
+ position_ids (`torch.Tensor`):
242
+ The position indices of the tokens corresponding to the query and key tensors. For example, this can be
243
+ used to pass offsetted position ids when working with a KV-cache.
244
+ unsqueeze_dim (`int`, *optional*, defaults to 1):
245
+ The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
246
+ sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
247
+ that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
248
+ k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
249
+ cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
250
+ the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
251
+ Returns:
252
+ `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
253
+ """
254
+ cos = cos[position_ids].unsqueeze(unsqueeze_dim)
255
+ sin = sin[position_ids].unsqueeze(unsqueeze_dim)
256
+ q_embed = (q * cos) + (rotate_half(q) * sin)
257
+ k_embed = (k * cos) + (rotate_half(k) * sin)
258
+ return q_embed, k_embed
259
+
260
+
261
+ # Copied from transformers.models.llama.modeling_llama.repeat_kv
262
+ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
263
+ """
264
+ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
265
+ num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
266
+ """
267
+ batch, num_key_value_heads, slen, head_dim = hidden_states.shape
268
+ if n_rep == 1:
269
+ return hidden_states
270
+ hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
271
+ return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
272
+
273
+
274
+ # copied from transformers.models.mistral.modeling_mistral.MistralAttention with Mistral->Mixtral
275
+ # TODO @longjie no longer copied from Mistral after static cache
276
+ class TanukiAttention(nn.Module):
277
+ """
278
+ Multi-headed attention from 'Attention Is All You Need' paper. Modified to use sliding window attention: Longformer
279
+ and "Generating Long Sequences with Sparse Transformers".
280
+ """
281
+
282
+ def __init__(self, config: TanukiConfig, layer_idx: Optional[int] = None):
283
+ super().__init__()
284
+ self.config = config
285
+ self.layer_idx = layer_idx
286
+ if layer_idx is None:
287
+ logger.warning_once(
288
+ f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will "
289
+ "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` "
290
+ "when creating this class."
291
+ )
292
+
293
+ self.hidden_size = config.hidden_size
294
+ self.num_heads = config.num_attention_heads
295
+ self.head_dim = self.hidden_size // self.num_heads
296
+ self.num_key_value_heads = config.num_key_value_heads
297
+ self.num_key_value_groups = self.num_heads // self.num_key_value_heads
298
+ self.max_position_embeddings = config.max_position_embeddings
299
+ self.rope_theta = config.rope_theta
300
+ self.is_causal = True
301
+ self.attention_dropout = config.attention_dropout
302
+
303
+ if (self.head_dim * self.num_heads) != self.hidden_size:
304
+ raise ValueError(
305
+ f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
306
+ f" and `num_heads`: {self.num_heads})."
307
+ )
308
+ self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
309
+ self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False)
310
+ self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False)
311
+ self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False)
312
+
313
+ self.rotary_emb = TanukiRotaryEmbedding(
314
+ self.head_dim,
315
+ max_position_embeddings=self.max_position_embeddings,
316
+ base=self.rope_theta,
317
+ )
318
+
319
+ def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
320
+ return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous()
321
+
322
+ def forward(
323
+ self,
324
+ hidden_states: torch.Tensor,
325
+ attention_mask: Optional[torch.Tensor] = None,
326
+ position_ids: Optional[torch.LongTensor] = None,
327
+ past_key_value: Optional[Cache] = None,
328
+ output_attentions: bool = False,
329
+ use_cache: bool = False,
330
+ cache_position: Optional[torch.LongTensor] = None,
331
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
332
+ bsz, q_len, _ = hidden_states.size()
333
+
334
+ query_states = self.q_proj(hidden_states)
335
+ key_states = self.k_proj(hidden_states)
336
+ value_states = self.v_proj(hidden_states)
337
+
338
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
339
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
340
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
341
+
342
+ kv_seq_len = key_states.shape[-2]
343
+ if past_key_value is not None:
344
+ if self.layer_idx is None:
345
+ raise ValueError(
346
+ f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
347
+ "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
348
+ "with a layer index."
349
+ )
350
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
351
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
352
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
353
+
354
+ if past_key_value is not None:
355
+ cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position} # Specific to RoPE models
356
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
357
+
358
+ # repeat k/v heads if n_kv_heads < n_heads
359
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
360
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
361
+
362
+ attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim)
363
+
364
+ if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
365
+ raise ValueError(
366
+ f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
367
+ f" {attn_weights.size()}"
368
+ )
369
+
370
+ if attention_mask is not None: # no matter the length, we just slice it
371
+ causal_mask = attention_mask[:, :, :, : key_states.shape[-2]]
372
+ attn_weights = attn_weights + causal_mask
373
+
374
+ # upcast attention to fp32
375
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
376
+ attn_weights = nn.functional.dropout(attn_weights, p=self.attention_dropout, training=self.training)
377
+ attn_output = torch.matmul(attn_weights, value_states)
378
+
379
+ if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
380
+ raise ValueError(
381
+ f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
382
+ f" {attn_output.size()}"
383
+ )
384
+
385
+ attn_output = attn_output.transpose(1, 2).contiguous()
386
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
387
+
388
+ attn_output = self.o_proj(attn_output)
389
+
390
+ if not output_attentions:
391
+ attn_weights = None
392
+
393
+ return attn_output, attn_weights, past_key_value
394
+
395
+
396
+ # copied from transformers.models.mistral.modeling_mistral.MistralFlashAttention2 with Mistral->Mixtral
397
+ # TODO @longjie no longer copied from Mistral after static cache
398
+ class TanukiFlashAttention2(TanukiAttention):
399
+ """
400
+ Tanuki flash attention module. This module inherits from `TanukiAttention` as the weights of the module stays
401
+ untouched. The only required change would be on the forward pass where it needs to correctly call the public API of
402
+ flash attention and deal with padding tokens in case the input contains any of them.
403
+ """
404
+
405
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2.__init__
406
+ def __init__(self, *args, **kwargs):
407
+ super().__init__(*args, **kwargs)
408
+
409
+ # TODO: Should be removed once Flash Attention for RoCm is bumped to 2.1.
410
+ # flash_attn<2.1 generates top-left aligned causal mask, while what is needed here is bottom-right alignement, that was made default for flash_attn>=2.1. This attribute is used to handle this difference. Reference: https://github.com/Dao-AILab/flash-attention/releases/tag/v2.1.0.
411
+ # Beware that with flash_attn<2.1, using q_seqlen != k_seqlen (except for the case q_seqlen == 1) produces a wrong mask (top-left).
412
+ self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10()
413
+
414
+ def forward(
415
+ self,
416
+ hidden_states: torch.Tensor,
417
+ attention_mask: Optional[torch.Tensor] = None,
418
+ position_ids: Optional[torch.LongTensor] = None,
419
+ past_key_value: Optional[Cache] = None,
420
+ output_attentions: bool = False,
421
+ use_cache: bool = False,
422
+ cache_position: Optional[torch.LongTensor] = None,
423
+ ):
424
+ bsz, q_len, _ = hidden_states.size()
425
+
426
+ query_states = self.q_proj(hidden_states)
427
+ key_states = self.k_proj(hidden_states)
428
+ value_states = self.v_proj(hidden_states)
429
+
430
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
431
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
432
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
433
+
434
+ kv_seq_len = key_states.shape[-2]
435
+ if past_key_value is not None:
436
+ if self.layer_idx is None:
437
+ raise ValueError(
438
+ f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
439
+ "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
440
+ "with a layer index."
441
+ )
442
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
443
+
444
+ # Because the input can be padded, the absolute sequence length depends on the max position id.
445
+ rotary_seq_len = max(kv_seq_len, position_ids[:, -1].max().item()) + 1
446
+ cos, sin = self.rotary_emb(value_states, seq_len=rotary_seq_len)
447
+
448
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
449
+
450
+ use_sliding_windows = (
451
+ _flash_supports_window_size
452
+ and getattr(self.config, "sliding_window", None) is not None
453
+ and kv_seq_len > self.config.sliding_window
454
+ )
455
+
456
+ if not _flash_supports_window_size:
457
+ logger.warning_once(
458
+ "The current flash attention version does not support sliding window attention, for a more memory efficient implementation"
459
+ " make sure to upgrade flash-attn library."
460
+ )
461
+
462
+ if past_key_value is not None:
463
+ # Activate slicing cache only if the config has a value `sliding_windows` attribute
464
+ cache_has_contents = past_key_value.get_seq_length(self.layer_idx) > 0
465
+ if (
466
+ getattr(self.config, "sliding_window", None) is not None
467
+ and kv_seq_len > self.config.sliding_window
468
+ and cache_has_contents
469
+ ):
470
+ slicing_tokens = 1 - self.config.sliding_window
471
+
472
+ past_key = past_key_value[self.layer_idx][0]
473
+ past_value = past_key_value[self.layer_idx][1]
474
+
475
+ past_key = past_key[:, :, slicing_tokens:, :].contiguous()
476
+ past_value = past_value[:, :, slicing_tokens:, :].contiguous()
477
+
478
+ if past_key.shape[-2] != self.config.sliding_window - 1:
479
+ raise ValueError(
480
+ f"past key must have a shape of (`batch_size, num_heads, self.config.sliding_window-1, head_dim`), got"
481
+ f" {past_key.shape}"
482
+ )
483
+
484
+ if attention_mask is not None:
485
+ attention_mask = attention_mask[:, slicing_tokens:]
486
+ attention_mask = torch.cat([attention_mask, torch.ones_like(attention_mask[:, -1:])], dim=-1)
487
+
488
+ cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position} # Specific to RoPE models
489
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
490
+
491
+ # repeat k/v heads if n_kv_heads < n_heads
492
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
493
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
494
+ dropout_rate = 0.0 if not self.training else self.attention_dropout
495
+
496
+ # In PEFT, usually we cast the layer norms in float32 for training stability reasons
497
+ # therefore the input hidden states gets silently casted in float32. Hence, we need
498
+ # cast them back in float16 just to be sure everything works as expected.
499
+ input_dtype = query_states.dtype
500
+ if input_dtype == torch.float32:
501
+ if torch.is_autocast_enabled():
502
+ target_dtype = torch.get_autocast_gpu_dtype()
503
+ # Handle the case where the model is quantized
504
+ elif hasattr(self.config, "_pre_quantization_dtype"):
505
+ target_dtype = self.config._pre_quantization_dtype
506
+ else:
507
+ target_dtype = self.q_proj.weight.dtype
508
+
509
+ logger.warning_once(
510
+ f"The input hidden states seems to be silently casted in float32, this might be related to"
511
+ f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in"
512
+ f" {target_dtype}."
513
+ )
514
+
515
+ query_states = query_states.to(target_dtype)
516
+ key_states = key_states.to(target_dtype)
517
+ value_states = value_states.to(target_dtype)
518
+
519
+ # Reashape to the expected shape for Flash Attention
520
+ query_states = query_states.transpose(1, 2)
521
+ key_states = key_states.transpose(1, 2)
522
+ value_states = value_states.transpose(1, 2)
523
+
524
+ attn_output = self._flash_attention_forward(
525
+ query_states,
526
+ key_states,
527
+ value_states,
528
+ attention_mask,
529
+ q_len,
530
+ dropout=dropout_rate,
531
+ use_sliding_windows=use_sliding_windows,
532
+ )
533
+
534
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous()
535
+ attn_output = self.o_proj(attn_output)
536
+
537
+ if not output_attentions:
538
+ attn_weights = None
539
+
540
+ return attn_output, attn_weights, past_key_value
541
+
542
+ def _flash_attention_forward(
543
+ self,
544
+ query_states,
545
+ key_states,
546
+ value_states,
547
+ attention_mask,
548
+ query_length,
549
+ dropout=0.0,
550
+ softmax_scale=None,
551
+ use_sliding_windows=False,
552
+ ):
553
+ """
554
+ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token
555
+ first unpad the input, then computes the attention scores and pad the final attention scores.
556
+
557
+ Args:
558
+ query_states (`torch.Tensor`):
559
+ Input query states to be passed to Flash Attention API
560
+ key_states (`torch.Tensor`):
561
+ Input key states to be passed to Flash Attention API
562
+ value_states (`torch.Tensor`):
563
+ Input value states to be passed to Flash Attention API
564
+ attention_mask (`torch.Tensor`):
565
+ The padding mask - corresponds to a tensor of size `(batch_size, seq_len)` where 0 stands for the
566
+ position of padding tokens and 1 for the position of non-padding tokens.
567
+ dropout (`float`):
568
+ Attention dropout
569
+ softmax_scale (`float`, *optional*):
570
+ The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim)
571
+ use_sliding_windows (`bool`, *optional*):
572
+ Whether to activate sliding window attention.
573
+ """
574
+ if not self._flash_attn_uses_top_left_mask:
575
+ causal = self.is_causal
576
+ else:
577
+ # TODO: Remove the `query_length != 1` check once Flash Attention for RoCm is bumped to 2.1. For details, please see the comment in LlamaFlashAttention2 __init__.
578
+ causal = self.is_causal and query_length != 1
579
+
580
+ # Contains at least one padding token in the sequence
581
+ if attention_mask is not None:
582
+ batch_size = query_states.shape[0]
583
+ query_states, key_states, value_states, indices_q, cu_seq_lens, max_seq_lens = self._upad_input(
584
+ query_states, key_states, value_states, attention_mask, query_length
585
+ )
586
+
587
+ cu_seqlens_q, cu_seqlens_k = cu_seq_lens
588
+ max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens
589
+
590
+ if not use_sliding_windows:
591
+ attn_output_unpad = flash_attn_varlen_func(
592
+ query_states,
593
+ key_states,
594
+ value_states,
595
+ cu_seqlens_q=cu_seqlens_q,
596
+ cu_seqlens_k=cu_seqlens_k,
597
+ max_seqlen_q=max_seqlen_in_batch_q,
598
+ max_seqlen_k=max_seqlen_in_batch_k,
599
+ dropout_p=dropout,
600
+ softmax_scale=softmax_scale,
601
+ causal=causal,
602
+ )
603
+ else:
604
+ attn_output_unpad = flash_attn_varlen_func(
605
+ query_states,
606
+ key_states,
607
+ value_states,
608
+ cu_seqlens_q=cu_seqlens_q,
609
+ cu_seqlens_k=cu_seqlens_k,
610
+ max_seqlen_q=max_seqlen_in_batch_q,
611
+ max_seqlen_k=max_seqlen_in_batch_k,
612
+ dropout_p=dropout,
613
+ softmax_scale=softmax_scale,
614
+ causal=causal,
615
+ window_size=(self.config.sliding_window, self.config.sliding_window),
616
+ )
617
+
618
+ attn_output = pad_input(attn_output_unpad, indices_q, batch_size, query_length)
619
+ else:
620
+ if not use_sliding_windows:
621
+ attn_output = flash_attn_func(
622
+ query_states,
623
+ key_states,
624
+ value_states,
625
+ dropout,
626
+ softmax_scale=softmax_scale,
627
+ causal=causal,
628
+ )
629
+ else:
630
+ attn_output = flash_attn_func(
631
+ query_states,
632
+ key_states,
633
+ value_states,
634
+ dropout,
635
+ softmax_scale=softmax_scale,
636
+ causal=causal,
637
+ window_size=(self.config.sliding_window, self.config.sliding_window),
638
+ )
639
+
640
+ return attn_output
641
+
642
+ def _upad_input(self, query_layer, key_layer, value_layer, attention_mask, query_length):
643
+ batch_size, kv_seq_len, num_heads, head_dim = key_layer.shape
644
+
645
+ # On the first iteration we need to properly re-create the padding mask
646
+ # by slicing it on the proper place
647
+ if kv_seq_len != attention_mask.shape[-1]:
648
+ attention_mask_num_tokens = attention_mask.shape[-1]
649
+ attention_mask = attention_mask[:, attention_mask_num_tokens - kv_seq_len :]
650
+
651
+ indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
652
+
653
+ key_layer = index_first_axis(key_layer.reshape(batch_size * kv_seq_len, num_heads, head_dim), indices_k)
654
+ value_layer = index_first_axis(value_layer.reshape(batch_size * kv_seq_len, num_heads, head_dim), indices_k)
655
+
656
+ if query_length == kv_seq_len:
657
+ query_layer = index_first_axis(
658
+ query_layer.reshape(batch_size * kv_seq_len, num_heads, head_dim), indices_k
659
+ )
660
+ cu_seqlens_q = cu_seqlens_k
661
+ max_seqlen_in_batch_q = max_seqlen_in_batch_k
662
+ indices_q = indices_k
663
+ elif query_length == 1:
664
+ max_seqlen_in_batch_q = 1
665
+ cu_seqlens_q = torch.arange(
666
+ batch_size + 1, dtype=torch.int32, device=query_layer.device
667
+ ) # There is a memcpy here, that is very bad.
668
+ indices_q = cu_seqlens_q[:-1]
669
+ query_layer = query_layer.squeeze(1)
670
+ else:
671
+ # The -q_len: slice assumes left padding.
672
+ attention_mask = attention_mask[:, -query_length:]
673
+ query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input(query_layer, attention_mask)
674
+
675
+ return (
676
+ query_layer,
677
+ key_layer,
678
+ value_layer,
679
+ indices_q,
680
+ (cu_seqlens_q, cu_seqlens_k),
681
+ (max_seqlen_in_batch_q, max_seqlen_in_batch_k),
682
+ )
683
+
684
+
685
+ # copied from transformers.models.mistral.modeling_mistral.MistralSdpaAttention with Mistral->Mixtral
686
+ # TODO @longjie no longer copied from Mistral after static cache
687
+ class TanukiSdpaAttention(TanukiAttention):
688
+ """
689
+ Tanuki attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from
690
+ `TanukiAttention` as the weights of the module stays untouched. The only changes are on the forward pass to adapt to
691
+ SDPA API.
692
+ """
693
+
694
+ # Adapted from TanukiAttention.forward
695
+ def forward(
696
+ self,
697
+ hidden_states: torch.Tensor,
698
+ attention_mask: Optional[torch.Tensor] = None,
699
+ position_ids: Optional[torch.LongTensor] = None,
700
+ past_key_value: Optional[Cache] = None,
701
+ output_attentions: bool = False,
702
+ use_cache: bool = False,
703
+ cache_position: Optional[torch.LongTensor] = None,
704
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
705
+ if output_attentions:
706
+ # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented.
707
+ logger.warning_once(
708
+ "TanukiModel is using TanukiSdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, "
709
+ 'but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.'
710
+ )
711
+ return super().forward(
712
+ hidden_states=hidden_states,
713
+ attention_mask=attention_mask,
714
+ position_ids=position_ids,
715
+ past_key_value=past_key_value,
716
+ output_attentions=output_attentions,
717
+ use_cache=use_cache,
718
+ )
719
+
720
+ bsz, q_len, _ = hidden_states.size()
721
+
722
+ query_states = self.q_proj(hidden_states)
723
+ key_states = self.k_proj(hidden_states)
724
+ value_states = self.v_proj(hidden_states)
725
+
726
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
727
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
728
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
729
+
730
+ kv_seq_len = key_states.shape[-2]
731
+ if past_key_value is not None:
732
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
733
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
734
+
735
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
736
+
737
+ if past_key_value is not None:
738
+ cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position} # Specific to RoPE models
739
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
740
+
741
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
742
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
743
+
744
+ causal_mask = attention_mask
745
+ if attention_mask is not None: # no matter the length, we just slice it
746
+ causal_mask = attention_mask[:, :, :, : key_states.shape[-2]]
747
+
748
+ # SDPA with memory-efficient backend is currently (torch==2.1.2) bugged with non-contiguous inputs with custom attn_mask,
749
+ # Reference: https://github.com/pytorch/pytorch/issues/112577.
750
+ if query_states.device.type == "cuda" and attention_mask is not None:
751
+ query_states = query_states.contiguous()
752
+ key_states = key_states.contiguous()
753
+ value_states = value_states.contiguous()
754
+
755
+ # We dispatch to SDPA's Flash Attention or Efficient kernels via this `is_causal` if statement instead of an inline conditional assignment
756
+ # in SDPA to support both torch.compile's dynamic shapes and full graph options. An inline conditional prevents dynamic shapes from compiling.
757
+ # The q_len > 1 is necessary to match with AttentionMaskConverter.to_causal_4d that does not create a causal mask in case q_len == 1.
758
+ is_causal = True if causal_mask is None and q_len > 1 else False
759
+
760
+ attn_output = torch.nn.functional.scaled_dot_product_attention(
761
+ query_states,
762
+ key_states,
763
+ value_states,
764
+ attn_mask=causal_mask,
765
+ dropout_p=self.attention_dropout if self.training else 0.0,
766
+ is_causal=is_causal,
767
+ )
768
+
769
+ attn_output = attn_output.transpose(1, 2).contiguous()
770
+ attn_output = attn_output.view(bsz, q_len, self.hidden_size)
771
+
772
+ attn_output = self.o_proj(attn_output)
773
+
774
+ return attn_output, None, past_key_value
775
+
776
+
777
+ TANUKI_ATTENTION_CLASSES = {
778
+ "eager": TanukiAttention,
779
+ "flash_attention_2": TanukiFlashAttention2,
780
+ "sdpa": TanukiSdpaAttention,
781
+ }
782
+
783
+
784
+ class TanukiBlockSparseTop2MLP(nn.Module):
785
+ def __init__(self, config: TanukiConfig):
786
+ super().__init__()
787
+ self.ffn_dim = config.intermediate_size
788
+ self.hidden_dim = config.hidden_size
789
+
790
+ self.w1 = nn.Linear(self.hidden_dim, self.ffn_dim, bias=False)
791
+ self.w2 = nn.Linear(self.ffn_dim, self.hidden_dim, bias=False)
792
+ self.w3 = nn.Linear(self.hidden_dim, self.ffn_dim, bias=False)
793
+
794
+ self.act_fn = ACT2FN[config.hidden_act]
795
+
796
+ def forward(self, hidden_states):
797
+ current_hidden_states = self.act_fn(self.w1(hidden_states)) * self.w3(hidden_states)
798
+ current_hidden_states = self.w2(current_hidden_states)
799
+ return current_hidden_states
800
+
801
+
802
+ class TanukiSparseMoeBlock(nn.Module):
803
+ """
804
+ This implementation is
805
+ strictly equivalent to standard MoE with full capacity (no
806
+ dropped tokens). It's faster since it formulates MoE operations
807
+ in terms of block-sparse operations to accomodate imbalanced
808
+ assignments of tokens to experts, whereas standard MoE either
809
+ (1) drop tokens at the cost of reduced performance or (2) set
810
+ capacity factor to number of experts and thus waste computation
811
+ and memory on padding.
812
+ """
813
+
814
+ def __init__(self, config):
815
+ super().__init__()
816
+ self.hidden_dim = config.hidden_size
817
+ self.ffn_dim = config.intermediate_size
818
+ self.num_experts = config.num_local_experts
819
+ self.top_k = config.num_experts_per_tok
820
+
821
+ # gating
822
+ self.gate = nn.Linear(self.hidden_dim, self.num_experts, bias=False)
823
+
824
+ self.experts = nn.ModuleList([TanukiBlockSparseTop2MLP(config) for _ in range(self.num_experts)])
825
+
826
+ # Jitter parameters
827
+ self.jitter_noise = config.router_jitter_noise
828
+
829
+ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
830
+ """ """
831
+ batch_size, sequence_length, hidden_dim = hidden_states.shape
832
+ if self.training and self.jitter_noise > 0:
833
+ hidden_states *= torch.empty_like(hidden_states).uniform_(1.0 - self.jitter_noise, 1.0 + self.jitter_noise)
834
+ hidden_states = hidden_states.view(-1, hidden_dim)
835
+ # router_logits: (batch * sequence_length, n_experts)
836
+ router_logits = self.gate(hidden_states)
837
+
838
+ mean = router_logits.mean(dim=-1, keepdim=True)
839
+ std = router_logits.std(dim=-1, keepdim=True)
840
+ router_logits = (router_logits - mean) / (std + 1e-5)
841
+
842
+ routing_weights = F.softmax(router_logits, dim=1, dtype=torch.float)
843
+ routing_weights, selected_experts = torch.topk(routing_weights, self.top_k, dim=-1)
844
+ routing_weights /= routing_weights.sum(dim=-1, keepdim=True)
845
+ # we cast back to the input dtype
846
+ routing_weights = routing_weights.to(hidden_states.dtype)
847
+
848
+ final_hidden_states = torch.zeros(
849
+ (batch_size * sequence_length, hidden_dim), dtype=hidden_states.dtype, device=hidden_states.device
850
+ )
851
+
852
+ # One hot encode the selected experts to create an expert mask
853
+ # this will be used to easily index which expert is going to be sollicitated
854
+ expert_mask = torch.nn.functional.one_hot(selected_experts, num_classes=self.num_experts).permute(2, 1, 0)
855
+
856
+ # Loop over all available experts in the model and perform the computation on each expert
857
+ for expert_idx in range(self.num_experts):
858
+ expert_layer = self.experts[expert_idx]
859
+ idx, top_x = torch.where(expert_mask[expert_idx])
860
+ if top_x.shape[0] == 0:
861
+ continue
862
+
863
+ # Index the correct hidden states and compute the expert hidden state for
864
+ # the current expert. We need to make sure to multiply the output hidden
865
+ # states by `routing_weights` on the corresponding tokens (top-1 and top-2)
866
+ current_state = hidden_states[None, top_x].reshape(-1, hidden_dim)
867
+ current_hidden_states = expert_layer(current_state) * routing_weights[top_x, idx, None]
868
+
869
+ # However `index_add_` only support torch tensors for indexing so we'll use
870
+ # the `top_x` tensor here.
871
+ final_hidden_states.index_add_(0, top_x, current_hidden_states.to(hidden_states.dtype))
872
+ final_hidden_states = final_hidden_states.reshape(batch_size, sequence_length, hidden_dim)
873
+ return final_hidden_states, router_logits
874
+
875
+
876
+ class TanukiDecoderLayer(nn.Module):
877
+ def __init__(self, config: TanukiConfig, layer_idx: int):
878
+ super().__init__()
879
+ self.hidden_size = config.hidden_size
880
+
881
+ self.self_attn = TANUKI_ATTENTION_CLASSES[config._attn_implementation](config, layer_idx)
882
+
883
+ self.block_sparse_moe = TanukiSparseMoeBlock(config)
884
+ self.input_layernorm = TanukiRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
885
+ self.post_attention_layernorm = TanukiRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
886
+
887
+ def forward(
888
+ self,
889
+ hidden_states: torch.Tensor,
890
+ attention_mask: Optional[torch.Tensor] = None,
891
+ position_ids: Optional[torch.LongTensor] = None,
892
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
893
+ output_attentions: Optional[bool] = False,
894
+ output_router_logits: Optional[bool] = False,
895
+ use_cache: Optional[bool] = False,
896
+ cache_position: Optional[torch.LongTensor] = None,
897
+ **kwargs,
898
+ ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
899
+ """
900
+ Args:
901
+ hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
902
+ attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
903
+ `(batch, sequence_length)` where padding elements are indicated by 0.
904
+ past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
905
+ output_attentions (`bool`, *optional*):
906
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under
907
+ returned tensors for more detail.
908
+ output_router_logits (`bool`, *optional*):
909
+ Whether or not to return the logits of all the routers. They are useful for computing the router loss, and
910
+ should not be returned during inference.
911
+ use_cache (`bool`, *optional*):
912
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
913
+ (see `past_key_values`).
914
+ cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
915
+ Indices depicting the position of the input sequence tokens in the sequence.
916
+ kwargs (`dict`, *optional*):
917
+ Arbitrary kwargs to be ignored, used for FSDP and other methods that injects code
918
+ into the model
919
+ """
920
+
921
+ residual = hidden_states
922
+
923
+ hidden_states = self.input_layernorm(hidden_states)
924
+
925
+ # Self Attention
926
+ hidden_states, self_attn_weights, present_key_value = self.self_attn(
927
+ hidden_states=hidden_states,
928
+ attention_mask=attention_mask,
929
+ position_ids=position_ids,
930
+ past_key_value=past_key_value,
931
+ output_attentions=output_attentions,
932
+ use_cache=use_cache,
933
+ cache_position=cache_position,
934
+ )
935
+ hidden_states = residual + hidden_states
936
+
937
+ # Fully Connected
938
+ residual = hidden_states
939
+ hidden_states = self.post_attention_layernorm(hidden_states)
940
+ hidden_states, router_logits = self.block_sparse_moe(hidden_states)
941
+ hidden_states = residual + hidden_states
942
+
943
+ outputs = (hidden_states,)
944
+
945
+ if output_attentions:
946
+ outputs += (self_attn_weights,)
947
+
948
+ if use_cache:
949
+ outputs += (present_key_value,)
950
+
951
+ if output_router_logits:
952
+ outputs += (router_logits,)
953
+
954
+ return outputs
955
+
956
+ TANUKI_START_DOCSTRING = r"""
957
+ This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
958
+ library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
959
+ etc.)
960
+
961
+ This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
962
+ Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
963
+ and behavior.
964
+
965
+ Parameters:
966
+ config ([`TanukiConfig`]):
967
+ Model configuration class with all the parameters of the model. Initializing with a config file does not
968
+ load the weights associated with the model, only the configuration. Check out the
969
+ [`~PreTrainedModel.from_pretrained`] method to load the model weights.
970
+ """
971
+
972
+
973
+ @add_start_docstrings(
974
+ "The bare Tanuki Model outputting raw hidden-states without any specific head on top.",
975
+ TANUKI_START_DOCSTRING,
976
+ )
977
+ # Copied from transformers.models.qwen2.modeling_qwen2.Qwen2PreTrainedModel with Qwen2->Mixtral
978
+ class TanukiPreTrainedModel(PreTrainedModel):
979
+ config_class = TanukiConfig
980
+ base_model_prefix = "model"
981
+ supports_gradient_checkpointing = True
982
+ _no_split_modules = ["TanukiDecoderLayer"]
983
+ _skip_keys_device_placement = "past_key_values"
984
+ _supports_flash_attn_2 = True
985
+ _supports_sdpa = True
986
+ _supports_cache_class = True
987
+
988
+ def _init_weights(self, module):
989
+ std = self.config.initializer_range
990
+ if isinstance(module, nn.Linear):
991
+ module.weight.data.normal_(mean=0.0, std=std)
992
+ if module.bias is not None:
993
+ module.bias.data.zero_()
994
+ elif isinstance(module, nn.Embedding):
995
+ module.weight.data.normal_(mean=0.0, std=std)
996
+ if module.padding_idx is not None:
997
+ module.weight.data[module.padding_idx].zero_()
998
+
999
+
1000
+ TANUKI_INPUTS_DOCSTRING = r"""
1001
+ Args:
1002
+ input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
1003
+ Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
1004
+ it.
1005
+
1006
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
1007
+ [`PreTrainedTokenizer.__call__`] for details.
1008
+
1009
+ [What are input IDs?](../glossary#input-ids)
1010
+ attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
1011
+ Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
1012
+
1013
+ - 1 for tokens that are **not masked**,
1014
+ - 0 for tokens that are **masked**.
1015
+
1016
+ [What are attention masks?](../glossary#attention-mask)
1017
+
1018
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
1019
+ [`PreTrainedTokenizer.__call__`] for details.
1020
+
1021
+ If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
1022
+ `past_key_values`).
1023
+
1024
+ If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
1025
+ and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
1026
+ information on the default strategy.
1027
+
1028
+ - 1 indicates the head is **not masked**,
1029
+ - 0 indicates the head is **masked**.
1030
+ position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1031
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
1032
+ config.n_positions - 1]`.
1033
+
1034
+ [What are position IDs?](../glossary#position-ids)
1035
+ past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
1036
+ Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
1037
+ `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape
1038
+ `(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`.
1039
+
1040
+ Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
1041
+ blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.
1042
+
1043
+ If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that
1044
+ don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all
1045
+ `decoder_input_ids` of shape `(batch_size, sequence_length)`.
1046
+ inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
1047
+ Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
1048
+ is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
1049
+ model's internal embedding lookup matrix.
1050
+ use_cache (`bool`, *optional*):
1051
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
1052
+ `past_key_values`).
1053
+ output_attentions (`bool`, *optional*):
1054
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
1055
+ tensors for more detail.
1056
+ output_hidden_states (`bool`, *optional*):
1057
+ Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
1058
+ more detail.
1059
+ output_router_logits (`bool`, *optional*):
1060
+ Whether or not to return the logits of all the routers. They are useful for computing the router loss, and
1061
+ should not be returned during inference.
1062
+ return_dict (`bool`, *optional*):
1063
+ Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
1064
+ cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
1065
+ Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`,
1066
+ this tensor is not affected by padding. It is used to update the cache in the correct position and to infer
1067
+ the complete sequence length.
1068
+ """
1069
+
1070
+
1071
+ @add_start_docstrings(
1072
+ "The bare Tanuki Model outputting raw hidden-states without any specific head on top.",
1073
+ TANUKI_START_DOCSTRING,
1074
+ )
1075
+ # copied from transformers.models.mistral.modeling_mistral.MistralModel with MISTRAL->MIXTRAL,Mistral->Mixtral
1076
+ # TODO @longjie no longer copied from Mistral after static cache
1077
+ class TanukiModel(TanukiPreTrainedModel):
1078
+ """
1079
+ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`TanukiDecoderLayer`]
1080
+
1081
+ Args:
1082
+ config: TanukiConfig
1083
+ """
1084
+
1085
+ def __init__(self, config: TanukiConfig):
1086
+ super().__init__(config)
1087
+ self.padding_idx = config.pad_token_id
1088
+ self.vocab_size = config.vocab_size
1089
+
1090
+ self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
1091
+ self.layers = nn.ModuleList(
1092
+ [TanukiDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
1093
+ )
1094
+ self._attn_implementation = config._attn_implementation
1095
+ self.norm = TanukiRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
1096
+
1097
+ self.gradient_checkpointing = False
1098
+ # Initialize weights and apply final processing
1099
+ self.post_init()
1100
+
1101
+ def get_input_embeddings(self):
1102
+ return self.embed_tokens
1103
+
1104
+ def set_input_embeddings(self, value):
1105
+ self.embed_tokens = value
1106
+
1107
+ # Ignore copy
1108
+ @add_start_docstrings_to_model_forward(TANUKI_INPUTS_DOCSTRING)
1109
+ def forward(
1110
+ self,
1111
+ input_ids: torch.LongTensor = None,
1112
+ attention_mask: Optional[torch.Tensor] = None,
1113
+ position_ids: Optional[torch.LongTensor] = None,
1114
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1115
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1116
+ use_cache: Optional[bool] = None,
1117
+ output_attentions: Optional[bool] = None,
1118
+ output_hidden_states: Optional[bool] = None,
1119
+ output_router_logits: Optional[bool] = None,
1120
+ return_dict: Optional[bool] = None,
1121
+ cache_position: Optional[torch.LongTensor] = None,
1122
+ ) -> Union[Tuple, MoeModelOutputWithPast]:
1123
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
1124
+ output_router_logits = (
1125
+ output_router_logits if output_router_logits is not None else self.config.output_router_logits
1126
+ )
1127
+ output_hidden_states = (
1128
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
1129
+ )
1130
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
1131
+
1132
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1133
+
1134
+ if (input_ids is None) ^ (inputs_embeds is not None):
1135
+ raise ValueError(
1136
+ "You cannot specify both input_ids and inputs_embeds at the same time, and must specify either one"
1137
+ )
1138
+
1139
+ if self.gradient_checkpointing and self.training:
1140
+ if use_cache:
1141
+ logger.warning_once(
1142
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
1143
+ )
1144
+ use_cache = False
1145
+
1146
+ use_legacy_cache = False
1147
+ if use_cache and not isinstance(past_key_values, Cache):
1148
+ use_legacy_cache = True
1149
+ past_key_values = DynamicCache.from_legacy_cache(past_key_values)
1150
+ logger.warning_once(
1151
+ "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. "
1152
+ "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)"
1153
+ )
1154
+
1155
+ if inputs_embeds is None:
1156
+ inputs_embeds = self.embed_tokens(input_ids)
1157
+
1158
+ if cache_position is None:
1159
+ past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
1160
+ cache_position = torch.arange(
1161
+ past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device
1162
+ )
1163
+ if position_ids is None:
1164
+ position_ids = cache_position.unsqueeze(0)
1165
+
1166
+ causal_mask = self._update_causal_mask(
1167
+ attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions
1168
+ )
1169
+
1170
+ hidden_states = inputs_embeds
1171
+
1172
+ # decoder layers
1173
+ all_hidden_states = () if output_hidden_states else None
1174
+ all_self_attns = () if output_attentions else None
1175
+ all_router_logits = () if output_router_logits else None
1176
+ next_decoder_cache = None
1177
+
1178
+ for decoder_layer in self.layers:
1179
+ if output_hidden_states:
1180
+ all_hidden_states += (hidden_states,)
1181
+
1182
+ if self.gradient_checkpointing and self.training:
1183
+ layer_outputs = self._gradient_checkpointing_func(
1184
+ decoder_layer.__call__,
1185
+ hidden_states,
1186
+ causal_mask,
1187
+ position_ids,
1188
+ past_key_values,
1189
+ output_attentions,
1190
+ output_router_logits,
1191
+ use_cache,
1192
+ cache_position,
1193
+ )
1194
+ else:
1195
+ layer_outputs = decoder_layer(
1196
+ hidden_states,
1197
+ attention_mask=causal_mask,
1198
+ position_ids=position_ids,
1199
+ past_key_value=past_key_values,
1200
+ output_attentions=output_attentions,
1201
+ output_router_logits=output_router_logits,
1202
+ use_cache=use_cache,
1203
+ cache_position=cache_position,
1204
+ )
1205
+
1206
+ hidden_states = layer_outputs[0]
1207
+
1208
+ if use_cache:
1209
+ next_decoder_cache = layer_outputs[2 if output_attentions else 1]
1210
+
1211
+ if output_attentions:
1212
+ all_self_attns += (layer_outputs[1],)
1213
+
1214
+ if output_router_logits:
1215
+ all_router_logits += (layer_outputs[-1],)
1216
+
1217
+ hidden_states = self.norm(hidden_states)
1218
+
1219
+ # add hidden states from the last decoder layer
1220
+ if output_hidden_states:
1221
+ all_hidden_states += (hidden_states,)
1222
+
1223
+ next_cache = None
1224
+ if use_cache:
1225
+ next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache
1226
+
1227
+ if not return_dict:
1228
+ return tuple(
1229
+ v
1230
+ for v in [hidden_states, next_cache, all_hidden_states, all_self_attns, all_router_logits]
1231
+ if v is not None
1232
+ )
1233
+ return MoeModelOutputWithPast(
1234
+ last_hidden_state=hidden_states,
1235
+ past_key_values=next_cache,
1236
+ hidden_states=all_hidden_states,
1237
+ attentions=all_self_attns,
1238
+ router_logits=all_router_logits,
1239
+ )
1240
+
1241
+ # Copied from transformers.models.llama.modeling_llama.LlamaModel._update_causal_mask
1242
+ def _update_causal_mask(
1243
+ self,
1244
+ attention_mask: torch.Tensor,
1245
+ input_tensor: torch.Tensor,
1246
+ cache_position: torch.Tensor,
1247
+ past_key_values: Cache,
1248
+ output_attentions: bool,
1249
+ ):
1250
+ # TODO: As of torch==2.2.0, the `attention_mask` passed to the model in `generate` is 2D and of dynamic length even when the static
1251
+ # KV cache is used. This is an issue for torch.compile which then recaptures cudagraphs at each decode steps due to the dynamic shapes.
1252
+ # (`recording cudagraph tree for symint key 13`, etc.), which is VERY slow. A workaround is `@torch.compiler.disable`, but this prevents using
1253
+ # `fullgraph=True`. See more context in https://github.com/huggingface/transformers/pull/29114
1254
+
1255
+ if self.config._attn_implementation == "flash_attention_2":
1256
+ if attention_mask is not None and 0.0 in attention_mask:
1257
+ return attention_mask
1258
+ return None
1259
+
1260
+ # For SDPA, when possible, we will rely on its `is_causal` argument instead of its `attn_mask` argument, in
1261
+ # order to dispatch on Flash Attention 2. This feature is not compatible with static cache, as SDPA will fail
1262
+ # to infer the attention mask.
1263
+ past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
1264
+ using_static_cache = isinstance(past_key_values, StaticCache)
1265
+
1266
+ # When output attentions is True, sdpa implementation's forward method calls the eager implementation's forward
1267
+ if self.config._attn_implementation == "sdpa" and not using_static_cache and not output_attentions:
1268
+ if AttentionMaskConverter._ignore_causal_mask_sdpa(
1269
+ attention_mask,
1270
+ inputs_embeds=input_tensor,
1271
+ past_key_values_length=past_seen_tokens,
1272
+ is_training=self.training,
1273
+ ):
1274
+ return None
1275
+
1276
+ dtype, device = input_tensor.dtype, input_tensor.device
1277
+ min_dtype = torch.finfo(dtype).min
1278
+ sequence_length = input_tensor.shape[1]
1279
+ if using_static_cache:
1280
+ target_length = past_key_values.get_max_length()
1281
+ else:
1282
+ target_length = (
1283
+ attention_mask.shape[-1]
1284
+ if isinstance(attention_mask, torch.Tensor)
1285
+ else past_seen_tokens + sequence_length + 1
1286
+ )
1287
+
1288
+ if attention_mask is not None and attention_mask.dim() == 4:
1289
+ # in this case we assume that the mask comes already in inverted form and requires no inversion or slicing
1290
+ if attention_mask.max() != 0:
1291
+ raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`")
1292
+ causal_mask = attention_mask
1293
+ else:
1294
+ causal_mask = torch.full(
1295
+ (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device
1296
+ )
1297
+ if sequence_length != 1:
1298
+ causal_mask = torch.triu(causal_mask, diagonal=1)
1299
+ causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1)
1300
+ causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1)
1301
+ if attention_mask is not None:
1302
+ causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit
1303
+ mask_length = attention_mask.shape[-1]
1304
+ padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :]
1305
+ padding_mask = padding_mask == 0
1306
+ causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill(
1307
+ padding_mask, min_dtype
1308
+ )
1309
+ if (
1310
+ self.config._attn_implementation == "sdpa"
1311
+ and attention_mask is not None
1312
+ and attention_mask.device.type == "cuda"
1313
+ and not output_attentions
1314
+ ):
1315
+ # Attend to all tokens in fully masked rows in the causal_mask, for example the relevant first rows when
1316
+ # using left padding. This is required by F.scaled_dot_product_attention memory-efficient attention path.
1317
+ # Details: https://github.com/pytorch/pytorch/issues/110213
1318
+ causal_mask = AttentionMaskConverter._unmask_unattended(causal_mask, min_dtype)
1319
+
1320
+ return causal_mask
1321
+
1322
+
1323
+ class TanukiForCausalLM(TanukiPreTrainedModel):
1324
+ _tied_weights_keys = ["lm_head.weight"]
1325
+
1326
+ def __init__(self, config):
1327
+ super().__init__(config)
1328
+ self.model = TanukiModel(config)
1329
+ self.vocab_size = config.vocab_size
1330
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
1331
+ self.router_aux_loss_coef = config.router_aux_loss_coef
1332
+ self.num_experts = config.num_local_experts
1333
+ self.num_experts_per_tok = config.num_experts_per_tok
1334
+ # Initialize weights and apply final processing
1335
+ self.post_init()
1336
+
1337
+ def get_input_embeddings(self):
1338
+ return self.model.embed_tokens
1339
+
1340
+ def set_input_embeddings(self, value):
1341
+ self.model.embed_tokens = value
1342
+
1343
+ def get_output_embeddings(self):
1344
+ return self.lm_head
1345
+
1346
+ def set_output_embeddings(self, new_embeddings):
1347
+ self.lm_head = new_embeddings
1348
+
1349
+ def set_decoder(self, decoder):
1350
+ self.model = decoder
1351
+
1352
+ def get_decoder(self):
1353
+ return self.model
1354
+
1355
+ @add_start_docstrings_to_model_forward(TANUKI_INPUTS_DOCSTRING)
1356
+ @replace_return_docstrings(output_type=MoeCausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
1357
+ # Ignore copy
1358
+ def forward(
1359
+ self,
1360
+ input_ids: torch.LongTensor = None,
1361
+ attention_mask: Optional[torch.Tensor] = None,
1362
+ position_ids: Optional[torch.LongTensor] = None,
1363
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1364
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1365
+ labels: Optional[torch.LongTensor] = None,
1366
+ use_cache: Optional[bool] = None,
1367
+ output_attentions: Optional[bool] = None,
1368
+ output_hidden_states: Optional[bool] = None,
1369
+ output_router_logits: Optional[bool] = None,
1370
+ return_dict: Optional[bool] = None,
1371
+ cache_position: Optional[torch.LongTensor] = None,
1372
+ ) -> Union[Tuple, MoeCausalLMOutputWithPast]:
1373
+ r"""
1374
+ Args:
1375
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1376
+ Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
1377
+ config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
1378
+ (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
1379
+
1380
+ Returns:
1381
+
1382
+ Example:
1383
+
1384
+ ```python
1385
+ >>> from transformers import AutoTokenizer, TanukiForCausalLM
1386
+
1387
+ >>> model = TanukiForCausalLM.from_pretrained("mistralai/Tanuki-8x7B-v0.1")
1388
+ >>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Tanuki-8x7B-v0.1")
1389
+
1390
+ >>> prompt = "Hey, are you conscious? Can you talk to me?"
1391
+ >>> inputs = tokenizer(prompt, return_tensors="pt")
1392
+
1393
+ >>> # Generate
1394
+ >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
1395
+ >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
1396
+ "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
1397
+ ```"""
1398
+
1399
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
1400
+ output_router_logits = (
1401
+ output_router_logits if output_router_logits is not None else self.config.output_router_logits
1402
+ )
1403
+
1404
+ output_hidden_states = (
1405
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
1406
+ )
1407
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1408
+
1409
+ # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
1410
+ outputs = self.model(
1411
+ input_ids=input_ids,
1412
+ attention_mask=attention_mask,
1413
+ position_ids=position_ids,
1414
+ past_key_values=past_key_values,
1415
+ inputs_embeds=inputs_embeds,
1416
+ use_cache=use_cache,
1417
+ output_attentions=output_attentions,
1418
+ output_hidden_states=output_hidden_states,
1419
+ output_router_logits=output_router_logits,
1420
+ return_dict=return_dict,
1421
+ cache_position=cache_position,
1422
+ )
1423
+
1424
+ hidden_states = outputs[0]
1425
+ logits = self.lm_head(hidden_states)
1426
+ logits = logits.float()
1427
+
1428
+ loss = None
1429
+ if labels is not None:
1430
+ # Shift so that tokens < n predict n
1431
+ shift_logits = logits[..., :-1, :].contiguous()
1432
+ shift_labels = labels[..., 1:].contiguous()
1433
+ # Flatten the tokens
1434
+ loss_fct = CrossEntropyLoss()
1435
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
1436
+ shift_labels = shift_labels.view(-1)
1437
+ # Enable model parallelism
1438
+ shift_labels = shift_labels.to(shift_logits.device)
1439
+ loss = loss_fct(shift_logits, shift_labels)
1440
+
1441
+ aux_loss = None
1442
+ if output_router_logits:
1443
+ aux_loss = load_balancing_loss_func(
1444
+ outputs.router_logits if return_dict else outputs[-1],
1445
+ self.num_experts,
1446
+ self.num_experts_per_tok,
1447
+ attention_mask,
1448
+ )
1449
+ if labels is not None:
1450
+ loss += self.router_aux_loss_coef * aux_loss.to(loss.device) # make sure to reside in the same device
1451
+
1452
+ if not return_dict:
1453
+ output = (logits,) + outputs[1:]
1454
+ if output_router_logits:
1455
+ output = (aux_loss,) + output
1456
+ return (loss,) + output if loss is not None else output
1457
+
1458
+ return MoeCausalLMOutputWithPast(
1459
+ loss=loss,
1460
+ aux_loss=aux_loss,
1461
+ logits=logits,
1462
+ past_key_values=outputs.past_key_values,
1463
+ hidden_states=outputs.hidden_states,
1464
+ attentions=outputs.attentions,
1465
+ router_logits=outputs.router_logits,
1466
+ )
1467
+
1468
+ def prepare_inputs_for_generation(
1469
+ self,
1470
+ input_ids,
1471
+ past_key_values=None,
1472
+ attention_mask=None,
1473
+ inputs_embeds=None,
1474
+ output_router_logits=False,
1475
+ cache_position=None,
1476
+ use_cache=True,
1477
+ **kwargs,
1478
+ ):
1479
+ past_length = 0
1480
+ # Omit tokens covered by past_key_values
1481
+ if past_key_values is not None:
1482
+ # Past key values are always initialized with a `Cache` object -> no need for if-else anymore
1483
+ past_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length()
1484
+ max_cache_length = (
1485
+ torch.tensor(past_key_values.get_max_length(), device=input_ids.device)
1486
+ if past_key_values.get_max_length() is not None
1487
+ else None
1488
+ )
1489
+ cache_length = past_length if max_cache_length is None else torch.min(max_cache_length, past_length)
1490
+
1491
+ # Keep only the unprocessed tokens:
1492
+ # 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where
1493
+ # some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as
1494
+ # input)
1495
+ if attention_mask is not None and attention_mask.shape[1] > input_ids.shape[1]:
1496
+ input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]
1497
+ # 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
1498
+ # input_ids based on the past_length.
1499
+ elif past_length < input_ids.shape[1]:
1500
+ input_ids = input_ids[:, past_length:]
1501
+ # 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.
1502
+
1503
+ # If we are about to go beyond the maximum cache length, we need to crop the input attention mask.
1504
+ if (
1505
+ max_cache_length is not None
1506
+ and attention_mask is not None
1507
+ and cache_length + input_ids.shape[1] > max_cache_length
1508
+ ):
1509
+ attention_mask = attention_mask[:, -max_cache_length:]
1510
+
1511
+ position_ids = kwargs.get("position_ids", None)
1512
+ if attention_mask is not None and position_ids is None:
1513
+ # create position_ids on the fly for batch generation
1514
+ position_ids = attention_mask.long().cumsum(-1) - 1
1515
+ position_ids.masked_fill_(attention_mask == 0, 1)
1516
+ if past_key_values:
1517
+ position_ids = position_ids[:, -input_ids.shape[1] :]
1518
+
1519
+ # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
1520
+ if inputs_embeds is not None and past_length == 0:
1521
+ model_inputs = {"inputs_embeds": inputs_embeds}
1522
+ else:
1523
+ model_inputs = {"input_ids": input_ids}
1524
+
1525
+ input_length = position_ids.shape[-1] if position_ids is not None else input_ids.shape[-1]
1526
+ if cache_position is None:
1527
+ cache_position = torch.arange(past_length, past_length + input_length, device=input_ids.device)
1528
+ elif use_cache:
1529
+ cache_position = cache_position[-input_length:]
1530
+
1531
+ model_inputs.update(
1532
+ {
1533
+ "position_ids": position_ids,
1534
+ "past_key_values": past_key_values,
1535
+ "use_cache": use_cache,
1536
+ "attention_mask": attention_mask,
1537
+ "output_router_logits": output_router_logits,
1538
+ "cache_position": cache_position,
1539
+ }
1540
+ )
1541
+ return model_inputs
1542
+
1543
+ @staticmethod
1544
+ def _reorder_cache(past_key_values, beam_idx):
1545
+ reordered_past = ()
1546
+ for layer_past in past_key_values:
1547
+ reordered_past += (
1548
+ tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past),
1549
+ )
1550
+ return reordered_past
1551
+
1552
+
1553
+ @add_start_docstrings(
1554
+ """
1555
+ The Tanuki Model transformer with a sequence classification head on top (linear layer).
1556
+
1557
+ [`TanukiForSequenceClassification`] uses the last token in order to do the classification, as other causal models
1558
+ (e.g. GPT-2) do.
1559
+
1560
+ Since it does classification on the last token, it requires to know the position of the last token. If a
1561
+ `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
1562
+ no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
1563
+ padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
1564
+ each row of the batch).
1565
+ """,
1566
+ TANUKI_START_DOCSTRING,
1567
+ )
1568
+ # Copied from transformers.models.llama.modeling_llama.LlamaForSequenceClassification with Llama->Mixtral, LLAMA->MIXTRAL
1569
+ class TanukiForSequenceClassification(TanukiPreTrainedModel):
1570
+ def __init__(self, config):
1571
+ super().__init__(config)
1572
+ self.num_labels = config.num_labels
1573
+ self.model = TanukiModel(config)
1574
+ self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
1575
+
1576
+ # Initialize weights and apply final processing
1577
+ self.post_init()
1578
+
1579
+ def get_input_embeddings(self):
1580
+ return self.model.embed_tokens
1581
+
1582
+ def set_input_embeddings(self, value):
1583
+ self.model.embed_tokens = value
1584
+
1585
+ @add_start_docstrings_to_model_forward(TANUKI_INPUTS_DOCSTRING)
1586
+ def forward(
1587
+ self,
1588
+ input_ids: torch.LongTensor = None,
1589
+ attention_mask: Optional[torch.Tensor] = None,
1590
+ position_ids: Optional[torch.LongTensor] = None,
1591
+ past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
1592
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1593
+ labels: Optional[torch.LongTensor] = None,
1594
+ use_cache: Optional[bool] = None,
1595
+ output_attentions: Optional[bool] = None,
1596
+ output_hidden_states: Optional[bool] = None,
1597
+ return_dict: Optional[bool] = None,
1598
+ ) -> Union[Tuple, SequenceClassifierOutputWithPast]:
1599
+ r"""
1600
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1601
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
1602
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
1603
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
1604
+ """
1605
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1606
+
1607
+ transformer_outputs = self.model(
1608
+ input_ids,
1609
+ attention_mask=attention_mask,
1610
+ position_ids=position_ids,
1611
+ past_key_values=past_key_values,
1612
+ inputs_embeds=inputs_embeds,
1613
+ use_cache=use_cache,
1614
+ output_attentions=output_attentions,
1615
+ output_hidden_states=output_hidden_states,
1616
+ return_dict=return_dict,
1617
+ )
1618
+ hidden_states = transformer_outputs[0]
1619
+ logits = self.score(hidden_states)
1620
+
1621
+ if input_ids is not None:
1622
+ batch_size = input_ids.shape[0]
1623
+ else:
1624
+ batch_size = inputs_embeds.shape[0]
1625
+
1626
+ if self.config.pad_token_id is None and batch_size != 1:
1627
+ raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
1628
+ if self.config.pad_token_id is None:
1629
+ sequence_lengths = -1
1630
+ else:
1631
+ if input_ids is not None:
1632
+ # if no pad token found, use modulo instead of reverse indexing for ONNX compatibility
1633
+ sequence_lengths = torch.eq(input_ids, self.config.pad_token_id).int().argmax(-1) - 1
1634
+ sequence_lengths = sequence_lengths % input_ids.shape[-1]
1635
+ sequence_lengths = sequence_lengths.to(logits.device)
1636
+ else:
1637
+ sequence_lengths = -1
1638
+
1639
+ pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
1640
+
1641
+ loss = None
1642
+ if labels is not None:
1643
+ labels = labels.to(logits.device)
1644
+ if self.config.problem_type is None:
1645
+ if self.num_labels == 1:
1646
+ self.config.problem_type = "regression"
1647
+ elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
1648
+ self.config.problem_type = "single_label_classification"
1649
+ else:
1650
+ self.config.problem_type = "multi_label_classification"
1651
+
1652
+ if self.config.problem_type == "regression":
1653
+ loss_fct = MSELoss()
1654
+ if self.num_labels == 1:
1655
+ loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
1656
+ else:
1657
+ loss = loss_fct(pooled_logits, labels)
1658
+ elif self.config.problem_type == "single_label_classification":
1659
+ loss_fct = CrossEntropyLoss()
1660
+ loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1))
1661
+ elif self.config.problem_type == "multi_label_classification":
1662
+ loss_fct = BCEWithLogitsLoss()
1663
+ loss = loss_fct(pooled_logits, labels)
1664
+ if not return_dict:
1665
+ output = (pooled_logits,) + transformer_outputs[1:]
1666
+ return ((loss,) + output) if loss is not None else output
1667
+
1668
+ return SequenceClassifierOutputWithPast(
1669
+ loss=loss,
1670
+ logits=pooled_logits,
1671
+ past_key_values=transformer_outputs.past_key_values,
1672
+ hidden_states=transformer_outputs.hidden_states,
1673
+ attentions=transformer_outputs.attentions,
1674
+ )
1675
+
1676
+
1677
+ @add_start_docstrings(
1678
+ """
1679
+ The Tanuki Model transformer with a token classification head on top (a linear layer on top of the hidden-states
1680
+ output) e.g. for Named-Entity-Recognition (NER) tasks.
1681
+ """,
1682
+ TANUKI_START_DOCSTRING,
1683
+ )
1684
+ # Copied from transformers.models.llama.modeling_llama.LlamaForTokenClassification with Llama->Mixtral, LLAMA->MIXTRAL
1685
+ class TanukiForTokenClassification(TanukiPreTrainedModel):
1686
+ def __init__(self, config):
1687
+ super().__init__(config)
1688
+ self.num_labels = config.num_labels
1689
+ self.model = TanukiModel(config)
1690
+ if getattr(config, "classifier_dropout", None) is not None:
1691
+ classifier_dropout = config.classifier_dropout
1692
+ elif getattr(config, "hidden_dropout", None) is not None:
1693
+ classifier_dropout = config.hidden_dropout
1694
+ else:
1695
+ classifier_dropout = 0.1
1696
+ self.dropout = nn.Dropout(classifier_dropout)
1697
+ self.score = nn.Linear(config.hidden_size, config.num_labels)
1698
+
1699
+ # Initialize weights and apply final processing
1700
+ self.post_init()
1701
+
1702
+ def get_input_embeddings(self):
1703
+ return self.model.embed_tokens
1704
+
1705
+ def set_input_embeddings(self, value):
1706
+ self.model.embed_tokens = value
1707
+
1708
+ @add_start_docstrings_to_model_forward(TANUKI_INPUTS_DOCSTRING)
1709
+ def forward(
1710
+ self,
1711
+ input_ids: Optional[torch.LongTensor] = None,
1712
+ attention_mask: Optional[torch.Tensor] = None,
1713
+ position_ids: Optional[torch.LongTensor] = None,
1714
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1715
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1716
+ labels: Optional[torch.LongTensor] = None,
1717
+ use_cache: Optional[bool] = None,
1718
+ output_attentions: Optional[bool] = None,
1719
+ output_hidden_states: Optional[bool] = None,
1720
+ return_dict: Optional[bool] = None,
1721
+ ) -> Union[Tuple, TokenClassifierOutput]:
1722
+ r"""
1723
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1724
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
1725
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
1726
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
1727
+ """
1728
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1729
+
1730
+ outputs = self.model(
1731
+ input_ids,
1732
+ attention_mask=attention_mask,
1733
+ position_ids=position_ids,
1734
+ past_key_values=past_key_values,
1735
+ inputs_embeds=inputs_embeds,
1736
+ use_cache=use_cache,
1737
+ output_attentions=output_attentions,
1738
+ output_hidden_states=output_hidden_states,
1739
+ return_dict=return_dict,
1740
+ )
1741
+ sequence_output = outputs[0]
1742
+ sequence_output = self.dropout(sequence_output)
1743
+ logits = self.score(sequence_output)
1744
+
1745
+ loss = None
1746
+ if labels is not None:
1747
+ loss_fct = CrossEntropyLoss()
1748
+ loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
1749
+
1750
+ if not return_dict:
1751
+ output = (logits,) + outputs[2:]
1752
+ return ((loss,) + output) if loss is not None else output
1753
+
1754
+ return TokenClassifierOutput(
1755
+ loss=loss,
1756
+ logits=logits,
1757
+ hidden_states=outputs.hidden_states,
1758
+ attentions=outputs.attentions,
1759
+ )