From 440f39754bb68630c7d3419d855f60e7cab0cf3e Mon Sep 17 00:00:00 2001 From: Joao Gante Date: Fri, 24 Feb 2023 11:21:00 +0000 Subject: [PATCH] Generate - update cookie cutters to not initialize cache with training and gradient checkpointing (#21759) --- ...ng_{{cookiecutter.lowercase_modelname}}.py | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/modeling_{{cookiecutter.lowercase_modelname}}.py b/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/modeling_{{cookiecutter.lowercase_modelname}}.py index 8cd81a927b..4899e19598 100755 --- a/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/modeling_{{cookiecutter.lowercase_modelname}}.py +++ b/templates/adding_a_new_model/cookiecutter-template-{{cookiecutter.modelname}}/modeling_{{cookiecutter.lowercase_modelname}}.py @@ -184,7 +184,7 @@ class {{cookiecutter.camelcase_modelname}}Embeddings(nn.Module): token_type_ids = buffered_token_type_ids_expanded else: token_type_ids = torch.zeros(input_shape, dtype=torch.long, device=self.position_ids.device) - + if inputs_embeds is None: inputs_embeds = self.word_embeddings(input_ids) token_type_embeddings = self.token_type_embeddings(token_type_ids) @@ -525,11 +525,17 @@ class {{cookiecutter.camelcase_modelname}}Encoder(nn.Module): output_hidden_states=False, return_dict=True, ): + if self.gradient_checkpointing and self.training and use_cache: + logger.warning( + "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." + ) + use_cache = False + all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None - next_decoder_cache = () if use_cache else None + for i, layer_module in enumerate(self.layer): if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) @@ -538,13 +544,6 @@ class {{cookiecutter.camelcase_modelname}}Encoder(nn.Module): past_key_value = past_key_values[i] if past_key_values is not None else None if self.gradient_checkpointing and self.training: - - if use_cache: - logger.warning( - "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." - ) - use_cache = False - def create_custom_forward(module): def custom_forward(*inputs): return module(*inputs, past_key_value, output_attentions) @@ -2024,7 +2023,7 @@ class {{cookiecutter.camelcase_modelname}}PreTrainedModel(PreTrainedModel): module.weight.data.normal_(mean=0.0, std=std) if module.padding_idx is not None: module.weight.data[module.padding_idx].zero_() - + def _set_gradient_checkpointing(self, module, value=False): if isinstance(module, ({{cookiecutter.camelcase_modelname}}Decoder, {{cookiecutter.camelcase_modelname}}Encoder)): module.gradient_checkpointing = value @@ -2525,6 +2524,10 @@ class {{cookiecutter.camelcase_modelname}}Decoder({{cookiecutter.camelcase_model hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training) # decoder layers + if self.gradient_checkpointing and self.training and use_cache: + logger.warning("`use_cache = True` is incompatible with gradient checkpointing`. Setting `use_cache = False`...") + use_cache = False + all_hidden_states = () if output_hidden_states else None all_self_attns = () if output_attentions else None all_cross_attentions = () if (output_attentions and encoder_hidden_states is not None) else None @@ -2547,11 +2550,6 @@ class {{cookiecutter.camelcase_modelname}}Decoder({{cookiecutter.camelcase_model past_key_value = past_key_values[idx] if past_key_values is not None else None if self.gradient_checkpointing and self.training: - - if use_cache: - logger.warning("`use_cache = True` is incompatible with gradient checkpointing`. Setting `use_cache = False`...") - use_cache = False - def create_custom_forward(module): def custom_forward(*inputs): # None for past_key_value