-
Notifications
You must be signed in to change notification settings - Fork 91
Multihead Attention Fixes #209
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
Merged
milancurcic
merged 9 commits into
modern-fortran:main
from
OneAdder:multihead_attention_optimization
Mar 26, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cad795c
multihead_attention_optimization: allocate in init
OneAdder f1d6fde
multihead_attention_optimization: remove last runtime allocation
OneAdder b064882
multihead_attention_optimization: make attention mask actually useable
OneAdder 81d3869
multihead_attention_optimization: tests cleanup
OneAdder 7d1a10d
multihead_attention_optimization: cleanup
OneAdder aa59523
multihead_attention_optimization: refactoring, split methods even mor…
OneAdder a076246
multihead_attention_optimization: make attributes public
OneAdder 0a399cf
multihead_attention_optimization: move heads separation out of sdpa b…
OneAdder 1f3be86
multihead_attention_optimization: add attention mask to self_attention
OneAdder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,10 +39,25 @@ module nf_multihead_attention_layer | |
real, allocatable :: k_input(:, :) | ||
real, allocatable :: v_input(:, :) | ||
real, allocatable :: o_input(:, :) | ||
|
||
! temporary storages for forward and backward passes | ||
real, allocatable :: normalized_attention(:, :, :) | ||
real, allocatable :: q_or_dq(:, :, :) | ||
real, allocatable :: k_or_dk(:, :, :) | ||
real, allocatable :: v_or_dv(:, :, :) | ||
real, allocatable :: d_output(:, :, :) | ||
real, allocatable :: v_heads(:, :, :) | ||
real, allocatable :: k_heads(:, :, :) | ||
real, allocatable :: q_heads(:, :, :) | ||
real, allocatable :: d_sdpa(:, :) | ||
real, allocatable :: jacobian(:, :) | ||
real, allocatable :: d_normalize(:, :, :) | ||
contains | ||
|
||
procedure :: common_backward | ||
procedure :: common_forward | ||
procedure :: sdpa_forward | ||
procedure :: sdpa_backward | ||
procedure :: get_num_params | ||
procedure :: get_params | ||
procedure :: get_gradients | ||
|
@@ -68,25 +83,38 @@ end function multihead_attention_layer_cons | |
|
||
interface | ||
|
||
pure module subroutine common_backward(self, input, gradient) | ||
pure module subroutine common_backward(self, input, gradient, attention_mask) | ||
!! General backprop for MultiHead Attention mechanism | ||
!! Might be used for both Self and Cross Attention | ||
!! Self Attention: sum output gradients | ||
!! Cross Attention: use them separately | ||
class(multihead_attention_layer), intent(in out) :: self | ||
real, intent(in) :: input(:, :) | ||
real, intent(in) :: gradient(:, :) | ||
real, optional, intent(in) :: attention_mask(:, :) | ||
end subroutine common_backward | ||
|
||
pure module subroutine common_forward(self, query, key, value) | ||
pure module subroutine common_forward(self, query, key, value, attention_mask) | ||
!! General forward propagation for MultiHead Attention Mechanism | ||
!! Might be used for both Self and Cross Attention | ||
!! Self Attention: pass the same value thrice | ||
!! Cross Attention: pass three values for your query, key and value | ||
class(multihead_attention_layer), intent(in out) :: self | ||
real, intent(in) :: query(:, :), key(:, :), value(:, :) | ||
real, optional, intent(in) :: attention_mask(:, :) | ||
end subroutine common_forward | ||
|
||
pure module subroutine sdpa_forward(self, attention_mask) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put Scaled Dot Product Attention into a separate method. This adds more flexibility. |
||
class(multihead_attention_layer), intent(in out) :: self | ||
real, intent(in), optional :: attention_mask(:, :) | ||
end subroutine sdpa_forward | ||
|
||
pure module subroutine sdpa_backward(self, gradient, attention_mask) | ||
class(multihead_attention_layer), intent(in out) :: self | ||
real, intent(in) :: gradient(:, :) | ||
real, intent(in), optional :: attention_mask(:, :) | ||
end subroutine sdpa_backward | ||
|
||
pure module subroutine init(self, input_shape) | ||
!! Initialize the layer data structures. | ||
!! | ||
|
@@ -119,7 +147,7 @@ pure module subroutine normalize_attention_matrix(self, attention_mask) | |
!! Output dims: sequence_length, sequence_length, n_heads | ||
class(multihead_attention_layer), intent(in out) :: self | ||
!! (sequence_length, sequence_length, n_heads) | ||
real, optional, intent(in) :: attention_mask(:, :, :) | ||
real, optional, intent(in) :: attention_mask(:, :) | ||
!! (sequence_length, sequence_length, n_heads) | ||
end subroutine normalize_attention_matrix | ||
|
||
|
@@ -143,18 +171,18 @@ elemental module function get_num_params(self) result(num_params) | |
end function get_num_params | ||
|
||
module function get_params(self) result(params) | ||
class(multihead_attention_layer), intent(in), target :: self | ||
class(multihead_attention_layer), intent(in) :: self | ||
real, allocatable :: params(:) | ||
end function get_params | ||
|
||
module function get_gradients(self) result(gradients) | ||
class(multihead_attention_layer), intent(in), target :: self | ||
class(multihead_attention_layer), intent(in) :: self | ||
real, allocatable :: gradients(:) | ||
end function get_gradients | ||
|
||
module subroutine set_params(self, params) | ||
class(multihead_attention_layer), intent(in out) :: self | ||
real, intent(in), target :: params(:) | ||
real, intent(in) :: params(:) | ||
end subroutine set_params | ||
|
||
module subroutine init_base(self, input_shape) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering what was
sdpa
until I found it in one of your comments below (that is, Scaled Dot Product Attention).I suggest to add a comment to explain it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do!