|
| 1 | +module nf_cross_attention_layer |
| 2 | + use iso_fortran_env, only: stderr => error_unit |
| 3 | + use nf_activation, only: softmax |
| 4 | + use nf_linear2d_layer, only: linear2d_layer |
| 5 | + use nf_multihead_attention_layer, only: multihead_attention_layer |
| 6 | + |
| 7 | + implicit none |
| 8 | + |
| 9 | + type, extends(multihead_attention_layer) :: cross_attention_layer |
| 10 | + !! Cross Attention Layer |
| 11 | + !! Source: |
| 12 | + !! Bahdanau, D. (2014) |
| 13 | + !! Neural machine translation by jointly learning to align and translate. |
| 14 | + !! https://arxiv.org/pdf/1409.0473 |
| 15 | + real, allocatable :: gradient(:, :, :) |
| 16 | + contains |
| 17 | + procedure :: forward |
| 18 | + procedure :: backward |
| 19 | + procedure :: init |
| 20 | + end type cross_attention_layer |
| 21 | + |
| 22 | + interface cross_attention_layer |
| 23 | + module function cross_attention_layer_cons(n_heads) result(res) |
| 24 | + !! This function returns the `cross_attention_layer` instance. |
| 25 | + integer, intent(in) :: sequence_length, model_dimension, n_heads |
| 26 | + type(cross_attention_layer) :: res |
| 27 | + end function cross_attention_layer_cons |
| 28 | + end interface cross_attention_layer |
| 29 | + |
| 30 | +contains |
| 31 | + module function cross_attention_layer_cons(n_heads) result(res) |
| 32 | + !! This function returns the `cross_attention_layer` instance. |
| 33 | + integer, intent(in) :: n_heads |
| 34 | + type(cross_attention_layer) :: res |
| 35 | + res % n_heads = n_heads |
| 36 | + end function cross_attention_layer_cons |
| 37 | + |
| 38 | + pure module subroutine backward(self, input, gradient) |
| 39 | + !! Cross Attention Back propagation |
| 40 | + class(cross_attention_layer), intent(in out) :: self |
| 41 | + real, intent(in) :: input(:, :, :) |
| 42 | + real, intent(in) :: gradient(:, :) |
| 43 | + |
| 44 | + call self % common_backward(input(1, :, :), gradient) |
| 45 | + self % gradient(1, :, :) = self % query_layer % gradient |
| 46 | + self % gradient(2, :, :) = self % key_layer % gradient + self % value_layer % gradient |
| 47 | + end subroutine backward |
| 48 | + |
| 49 | + pure module subroutine forward(self, input) |
| 50 | + !! Cross Attention Forward propagation |
| 51 | + !! Input Shape (kind, sequence_length, model_dimension) |
| 52 | + !! where kind is 1 for Query and 2 for Key-Value |
| 53 | + class(cross_attention_layer), intent(in out) :: self |
| 54 | + real, intent(in) :: input(:, :, :) |
| 55 | + |
| 56 | + call self % common_forward(input(1, :, :), input(2, :, :), input(2, :, :)) |
| 57 | + end subroutine forward |
| 58 | + |
| 59 | + module subroutine init(self, input_shape) |
| 60 | + class(cross_attention_layer), intent(in out) :: self |
| 61 | + integer, intent(in) :: input_shape(:) |
| 62 | + |
| 63 | + call self % init_base(input_shape) |
| 64 | + allocate(self % gradient(2, self % sequence_length, self % model_dimension)) |
| 65 | + end subroutine init |
| 66 | +end module nf_cross_attention_layer |
0 commit comments