support returning attention weights from transformer forward pass - #62
support returning attention weights from transformer forward pass#62TahaZahid05 wants to merge 2 commits into
Conversation
|
@TahaZahid05 I am not sure if its necessary we are doing extra calculation , outside the method which is specialized for this task. |
|
@aaishwarymishra how exactly is this specialized? It is just giving attention matrix which can be used for any task? |
|
My concern is we are doing extra calculation for attention, for which we have a specialized method ig |
|
@aaishwarymishra SPDA does not return attention matrix. the manual calculation is necessary to support return of attention matrix? |
|
Is it necessary though? Like we actually need it? If yes you can check if some other pytorch method returns it ig. |
|
@aaishwarymishra i have checked and i don't think there is any other function that returns attention matrix, we need to do it manually. |
|
Why do we need it though |
|
Well, after training if user wants to visualize model's attention matrix on inputs, how would they do so without an option for attention matrix to be returned in the code? |
|
They can add it themselves ig, I am not sure about this if we do manual extra calculation then what's the point of using spda |
|
manual calculation is only happening when output_attentions is True. for general training it stays False. I agree they can add it themselves, my intent for adding this in trainite was actually support for debug mode meaning in future if we provide the user with a debug mode and pre-built component of visualizing attention matrix then we would need this. |
|
hmm lets wait for the meeting ig. |
|
@TahaZahid05 |
|
Converting this to a draft for now. We can work on this if our direction of debug mode aligns with having pre-made templates of debugging like visualizing attention matrix to add to Trainite. Would open another PR to megre attention matrix code for our string-reversal example |
Description
Context & Goals
Exposing attention weights from transformer models is crucial for interpretability, diagnostic audits, and verifying model alignment (e.g. visualizing the diagonal reverse-mapping learned in the string-reversal task).
This PR adds the optional capability to return the self-attention weights from the forward pass of the model, blocks, and attention layers, while maintaining maximum performance optimizations during standard training.
Key Changes
trainite/models/transformer.py):output_attentionsboolean flag to theAttention.forwardsignature.output_attentions=True, it bypassesnn.functional.scaled_dot_product_attention(which does not expose internal weights) and manually calculates the attention weights matrix via Query-Key dot products and Softmax, applying both causal and padding masks.(output, context, attention_weights).scaled_dot_product_attentioninvocation code inAttention.forwardby dynamically setting themaskandis_causalvariables first. This avoids duplicate code lines and maintains hardware-optimized FlashAttention execution whenoutput_attentionsisFalse.output_attentionsflag down fromTransformerModelthroughTransformerBlocktoAttention.TransformerModelreturns a list of attention weight tensors (one per layer/block) along with the final model logits whenoutput_attentions=True.tests/models/transformer_test.py):Attention.forward.