|
| 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 | + real, allocatable :: gradient(:, :, :) |
| 11 | + contains |
| 12 | + procedure :: forward |
| 13 | + procedure :: backward |
| 14 | + procedure :: init |
| 15 | + end type cross_attention_layer |
| 16 | + |
| 17 | + interface cross_attention_layer |
| 18 | + module function cross_attention_layer_cons(sequence_length, model_dimension, n_heads) result(res) |
| 19 | + !! This function returns the `cross_attention_layer` instance. |
| 20 | + integer, intent(in) :: sequence_length, model_dimension, n_heads |
| 21 | + type(cross_attention_layer) :: res |
| 22 | + end function cross_attention_layer_cons |
| 23 | + end interface cross_attention_layer |
| 24 | + |
| 25 | +contains |
| 26 | + module function cross_attention_layer_cons(sequence_length, model_dimension, n_heads) result(res) |
| 27 | + !! This function returns the `cross_attention_layer` instance. |
| 28 | + integer, intent(in) :: sequence_length, model_dimension, n_heads |
| 29 | + type(cross_attention_layer) :: res |
| 30 | + res % sequence_length = sequence_length |
| 31 | + res % model_dimension = model_dimension |
| 32 | + res % n_heads = n_heads |
| 33 | + |
| 34 | + if (mod(model_dimension, n_heads) /= 0) then |
| 35 | + write(stderr, '(a)'), 'Number of heads must be divisible by model dimension' |
| 36 | + error stop |
| 37 | + end if |
| 38 | + res % head_size = model_dimension / n_heads |
| 39 | + |
| 40 | + res % query_layer = linear2d_layer(sequence_length, model_dimension, model_dimension) |
| 41 | + res % key_layer = linear2d_layer(sequence_length, model_dimension, model_dimension) |
| 42 | + res % value_layer = linear2d_layer(sequence_length, model_dimension, model_dimension) |
| 43 | + res % output_layer = linear2d_layer(sequence_length, model_dimension, model_dimension) |
| 44 | + call res % query_layer % init([0]) |
| 45 | + call res % key_layer % init([0]) |
| 46 | + call res % value_layer % init([0]) |
| 47 | + call res % output_layer % init([0]) |
| 48 | + |
| 49 | + res % softmax_func = softmax() |
| 50 | + end function cross_attention_layer_cons |
| 51 | + |
| 52 | + module subroutine backward(self, input, gradient) |
| 53 | + class(cross_attention_layer), intent(in out) :: self |
| 54 | + real, intent(in) :: input(:, :, :) |
| 55 | + real, intent(in) :: gradient(:, :) |
| 56 | + |
| 57 | + call self % common_backward(input(1, :, :), gradient) |
| 58 | + self % gradient(1, :, :) = self % query_layer % gradient |
| 59 | + self % gradient(2, :, :) = self % key_layer % gradient + self % value_layer % gradient |
| 60 | + end subroutine backward |
| 61 | + |
| 62 | + module subroutine forward(self, input) |
| 63 | + class(cross_attention_layer), intent(in out) :: self |
| 64 | + real, intent(in) :: input(:, :, :) |
| 65 | + |
| 66 | + call self % common_forward(input(1, :, :), input(2, :, :), input(2, :, :)) |
| 67 | + end subroutine forward |
| 68 | + |
| 69 | + module subroutine init(self, input_shape) |
| 70 | + class(cross_attention_layer), intent(in out) :: self |
| 71 | + integer, intent(in) :: input_shape(:) |
| 72 | + |
| 73 | + call self % init_base(input_shape) |
| 74 | + allocate(self % gradient(2, self % sequence_length, self % model_dimension)) |
| 75 | + end subroutine init |
| 76 | +end module nf_cross_attention_layer |
0 commit comments