Hajime Yagihara
commited on
Commit
•
c76f641
1
Parent(s):
2c9b82f
add gradient_checkpointing
Browse files- modeling_mpt.py +27 -1
modeling_mpt.py
CHANGED
@@ -30,11 +30,18 @@ class MPTPreTrainedModel(PreTrainedModel):
|
|
30 |
base_model_prefix = 'model'
|
31 |
_no_split_modules = ['MPTBlock']
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
class MPTModel(MPTPreTrainedModel):
|
34 |
|
35 |
def __init__(self, config: MPTConfig):
|
36 |
config._validate_config()
|
37 |
super().__init__(config)
|
|
|
38 |
self.attn_impl = config.attn_config['attn_impl']
|
39 |
self.prefix_lm = config.attn_config['prefix_lm']
|
40 |
self.attn_uses_sequence_id = config.attn_config['attn_uses_sequence_id']
|
@@ -144,6 +151,9 @@ class MPTModel(MPTPreTrainedModel):
|
|
144 |
def forward(self, input_ids: torch.LongTensor, past_key_values: Optional[List[Tuple[torch.FloatTensor]]]=None, attention_mask: Optional[torch.ByteTensor]=None, prefix_mask: Optional[torch.ByteTensor]=None, sequence_id: Optional[torch.LongTensor]=None, return_dict: Optional[bool]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, use_cache: Optional[bool]=None, inputs_embeds: Optional[torch.FloatTensor] = None):
|
145 |
return_dict = return_dict if return_dict is not None else self.config.return_dict
|
146 |
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
|
|
|
|
|
|
147 |
if input_ids is not None and inputs_embeds is not None:
|
148 |
raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time")
|
149 |
elif input_ids is not None:
|
@@ -218,7 +228,23 @@ class MPTModel(MPTPreTrainedModel):
|
|
218 |
assert all_hidden_states is not None
|
219 |
all_hidden_states = all_hidden_states + (x,)
|
220 |
past_key_value = past_key_values[b_idx] if past_key_values is not None else None
|
221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
if past_key_values is not None:
|
223 |
past_key_values[b_idx] = past_key_value
|
224 |
if output_attentions:
|
|
|
30 |
base_model_prefix = 'model'
|
31 |
_no_split_modules = ['MPTBlock']
|
32 |
|
33 |
+
supports_gradient_checkpointing = True
|
34 |
+
|
35 |
+
def _set_gradient_checkpointing(self, module, value=False):
|
36 |
+
if isinstance(module, MPTModel):
|
37 |
+
module.gradient_checkpointing = value
|
38 |
+
|
39 |
class MPTModel(MPTPreTrainedModel):
|
40 |
|
41 |
def __init__(self, config: MPTConfig):
|
42 |
config._validate_config()
|
43 |
super().__init__(config)
|
44 |
+
self.gradient_checkpointing = False
|
45 |
self.attn_impl = config.attn_config['attn_impl']
|
46 |
self.prefix_lm = config.attn_config['prefix_lm']
|
47 |
self.attn_uses_sequence_id = config.attn_config['attn_uses_sequence_id']
|
|
|
151 |
def forward(self, input_ids: torch.LongTensor, past_key_values: Optional[List[Tuple[torch.FloatTensor]]]=None, attention_mask: Optional[torch.ByteTensor]=None, prefix_mask: Optional[torch.ByteTensor]=None, sequence_id: Optional[torch.LongTensor]=None, return_dict: Optional[bool]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, use_cache: Optional[bool]=None, inputs_embeds: Optional[torch.FloatTensor] = None):
|
152 |
return_dict = return_dict if return_dict is not None else self.config.return_dict
|
153 |
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
154 |
+
if self.gradient_checkpointing and self.training:
|
155 |
+
if use_cache:
|
156 |
+
use_cache = False
|
157 |
if input_ids is not None and inputs_embeds is not None:
|
158 |
raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time")
|
159 |
elif input_ids is not None:
|
|
|
228 |
assert all_hidden_states is not None
|
229 |
all_hidden_states = all_hidden_states + (x,)
|
230 |
past_key_value = past_key_values[b_idx] if past_key_values is not None else None
|
231 |
+
if self.gradient_checkpointing and self.training:
|
232 |
+
def create_custom_forward(module):
|
233 |
+
def custom_forward(*inputs):
|
234 |
+
# None for past_key_value
|
235 |
+
return module(*inputs)
|
236 |
+
return custom_forward
|
237 |
+
(x, attn_weights, past_key_value) = torch.utils.checkpoint.checkpoint(
|
238 |
+
create_custom_forward(block),
|
239 |
+
x,
|
240 |
+
past_key_value,
|
241 |
+
attn_bias,
|
242 |
+
attention_mask,
|
243 |
+
self.is_causal,
|
244 |
+
)
|
245 |
+
else:
|
246 |
+
(x, attn_weights, past_key_value) = block(x, past_key_value=past_key_value, attn_bias=attn_bias, attention_mask=attention_mask, is_causal=self.is_causal)
|
247 |
+
# (x, attn_weights, past_key_value) = block(x, past_key_value=past_key_value, attn_bias=attn_bias, attention_mask=attention_mask, is_causal=self.is_causal)
|
248 |
if past_key_values is not None:
|
249 |
past_key_values[b_idx] = past_key_value
|
250 |
if output_attentions:
|