-
Notifications
You must be signed in to change notification settings - Fork 286
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
Deep Dive NB: Quick Fix for AttributeError: 'CLIPTextTransformer' object has no attribute '_build_causal_attention_mask' #37
Comments
@drscotthawley another fix, without having to downgrade, could be to use what the function
PS: Thanks for the mps support changes |
import torch
def build_causal_attention_mask(bsz, seq_len, dtype):
mask = torch.empty(bsz, seq_len, seq_len, dtype=dtype)
mask.fill_(torch.tensor(torch.finfo(dtype).min)) # fill with large negative number (acts like -inf)
mask = mask.triu_(1) # zero out the lower diagonal to enforce causality
return mask.unsqueeze(1) # add a batch dimension
# Update your function call to use the new mask function
def get_output_embeds(input_embeddings):
bsz, seq_len = input_embeddings.shape[:2]
causal_attention_mask = build_causal_attention_mask(bsz, seq_len, dtype=input_embeddings.dtype)
# Getting the output embeddings involves calling the model with passing output_hidden_states=True
# so that it doesn't just return the pooled final predictions:
encoder_outputs = text_encoder.text_model.encoder(
inputs_embeds=input_embeddings,
attention_mask=None, # We aren't using an attention mask so that can be None
causal_attention_mask=causal_attention_mask.to(torch_device),
output_attentions=None,
output_hidden_states=True, # We want the output embs not the final output
return_dict=None,
)
# We're interested in the output hidden state only
output = encoder_outputs[0]
# There is a final layer norm we need to pass these through
output = text_encoder.text_model.final_layer_norm(output)
# And now they're ready!
return output
out_embs_test = get_output_embeds(input_embeddings) # Feed through the model with our new function
print(out_embs_test.shape) # Check the output shape
out_embs_test # Inspect the output |
if you install the new transformers library like version of 4.40.2,you can implement as below:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the Stable Diffusion Deep Dive notebook, in the code plot immediately following the Transformer diagram, there is the definition of
get_output_embeds
which includes a call totext_encoder.text_model._build_causal_attention_mask
:That is currently generating an error for me when I run the notebook on Colab (from a fresh instance) or my home computer:
Everything in the notebook prior to that line runs fine.
Perhaps I'm doing something wrong, or perhaps something has changed with the HF libraries that being used, since the notebook's original conception?
UPDATE:
I see the same issue here: drboog/ProFusion#12. It seems that
transformers
has changed. Downgrading to version 4.25.1 fixed the problem.Thus changing the the
pip install
line at the top of the notebook to!pip install -q --upgrade transformers==4.25.1 diffusers ftfy
...will restore full functionality.
Feel free to close this issue at your convenience. Perhaps a PR is in order.
Presumably some way to keep up to date with
transformers
will be preferable, but for now this is a quick fix.The text was updated successfully, but these errors were encountered: