Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serving_output for TF composite models (encoder-decoder like models) #22743

Merged
merged 3 commits into from
Apr 13, 2023

Conversation

ydshieh
Copy link
Collaborator

@ydshieh ydshieh commented Apr 13, 2023

What does this PR do?

[If the concept is approved, I will apply the same changes to other TF encoder-decoder family of models]

The composite models use its components' configurations. See for example

# Encoder Decoder models delegate the application of the configuration options to their inner models.
if "EncoderDecoder" in self.__class__.__name__:
config = None
else:
config = self.config

However, in some places, our codebase still try to access some attributes at the top level of the configuration (i.e. not inside the 2 components), like

def serving_output(self, output):
pkv = tf.tuple(output.past_key_values)[1] if self.config.use_cache else None
dec_hs = tf.convert_to_tensor(output.decoder_hidden_states) if self.config.output_hidden_states else None
dec_attns = tf.convert_to_tensor(output.decoder_attentions) if self.config.output_attentions else None
enc_hs = tf.convert_to_tensor(output.encoder_hidden_states) if self.config.output_hidden_states else None
enc_attns = tf.convert_to_tensor(output.encoder_attentions) if self.config.output_attentions else None

In particular, self.config may not have use_cache, for example, for this checkpoint "nlpconnect/vit-gpt2-image-captioning". We should instead look self.config.deocder.use_cache.

This PR try to follow the rule of # Encoder Decoder models delegate the application of the configuration options to their inner models..

This PR is also (another) one necessary step to fix #22731.

cross_attns = (
tf.convert_to_tensor(output.cross_attentions)
if self.config.output_attentions and output.cross_attentions is not None
if self.config.decoder.output_attentions and output.cross_attentions is not None
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the attributes in the components' configurations (i.e. self.config.encoder and self.config.decoder)

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Apr 13, 2023

The documentation is not available anymore as the PR was closed or merged.

@ydshieh ydshieh requested review from amyeroberts, sgugger and Rocketknight1 and removed request for amyeroberts, sgugger and Rocketknight1 April 13, 2023 12:34
Copy link
Collaborator

@sgugger sgugger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me but would like for a TF expert to have a look too!

@ydshieh ydshieh requested a review from gante April 13, 2023 18:54
Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me!

@ydshieh ydshieh removed the request for review from amyeroberts April 13, 2023 19:05
@ydshieh ydshieh removed the request for review from gante April 13, 2023 20:23
@ydshieh ydshieh merged commit a6752a7 into main Apr 13, 2023
4 checks passed
@ydshieh ydshieh deleted the fix_save_2 branch April 13, 2023 21:45
novice03 pushed a commit to novice03/transformers that referenced this pull request Jun 23, 2023
…dels) (huggingface#22743)

* fix

* style

* fix

---------

Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants