-
Notifications
You must be signed in to change notification settings - Fork 91
Layernorm #203
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
Merged
Layernorm #203
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
362015d
layernorm: initial implementation
OneAdder 005daf2
layernorm: rename source file
OneAdder d657fa7
layernorm: remove redundant arguments
OneAdder 0dbaf07
layernorm: remove stack allocated arrays
OneAdder 612db46
layernorm: rearrange into submodule
OneAdder c4a3e3c
layernorm: add error to stop in test
OneAdder bdefd02
layernorm: add gradient updates
OneAdder ccc180e
layernorm: public api
OneAdder 0667000
layernorm: update tests
OneAdder c2a1e70
layernorm: update cmake
OneAdder ddcd204
layernorm: use mold for temp allocation
OneAdder 54d081f
layernorm: rename to layernorm
OneAdder 6ec65ac
layernorm: allow usage of layernorm at the end
OneAdder 720b79b
layernorm: integration test for layernorm
OneAdder 981addd
layernorm: memory allocation optimization
OneAdder 55077b3
Tidy up
milancurcic 249485f
Bump version
milancurcic 3e3776b
Add layernorm to the table of layers
milancurcic 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
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name = "neural-fortran" | ||
version = "0.19.0" | ||
version = "0.20.0" | ||
license = "MIT" | ||
author = "Milan Curcic" | ||
maintainer = "[email protected]" | ||
|
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
module nf_layernorm_layer | ||
use nf_activation, only: activation_function | ||
use nf_base_layer, only: base_layer | ||
|
||
implicit none | ||
|
||
private | ||
public :: layernorm_layer | ||
|
||
type, extends(base_layer) :: layernorm_layer | ||
!! Layer Normalization | ||
!! ((x − mean(x)) / sqrt(variance(x) + eps) * gamma + beta | ||
!! Based upon `Ba, Jimmy Lei, Jamie Ryan Kiros, and Geoffrey E. Hinton(2016)`: | ||
!! https://arxiv.org/abs/1607.06450v1 | ||
integer :: sequence_length | ||
integer :: model_dimension | ||
|
||
real :: eps | ||
real, allocatable :: gamma(:) | ||
real, allocatable :: beta(:) | ||
|
||
real, allocatable :: d_gamma(:) | ||
real, allocatable :: d_beta(:) | ||
real, allocatable :: gradient(:, :) | ||
|
||
real, allocatable :: mu(:, :) | ||
real, allocatable :: sigma(:) | ||
|
||
real, allocatable :: output(:, :) | ||
|
||
! temp storages | ||
real, allocatable, private :: normalized(:, :) | ||
real, allocatable, private :: one_over_sigma(:, :) | ||
real, allocatable, private :: gradient_by_gamma_over_sigma(:, :) | ||
contains | ||
procedure :: forward | ||
procedure :: backward | ||
procedure :: init | ||
procedure :: get_num_params | ||
procedure :: get_params | ||
procedure :: get_gradients | ||
procedure :: set_params | ||
end type layernorm_layer | ||
|
||
interface layernorm_layer | ||
module function layernorm_layer_cons() & | ||
result(res) | ||
type(layernorm_layer) :: res | ||
end function layernorm_layer_cons | ||
end interface layernorm_layer | ||
|
||
interface | ||
pure module subroutine forward(self, input) | ||
class(layernorm_layer), intent(in out) :: self | ||
real, intent(in) :: input(:, :) | ||
end subroutine forward | ||
|
||
pure module subroutine backward(self, input, gradient) | ||
class(layernorm_layer), intent(in out) :: self | ||
real, intent(in) :: input(:, :) | ||
real, intent(in) :: gradient(:, :) | ||
end subroutine backward | ||
|
||
module subroutine init(self, input_shape) | ||
class(layernorm_layer), intent(in out) :: self | ||
integer, intent(in) :: input_shape(:) | ||
end subroutine init | ||
|
||
pure module function get_num_params(self) result(num_params) | ||
class(layernorm_layer), intent(in) :: self | ||
integer :: num_params | ||
end function get_num_params | ||
|
||
|
||
module function get_params(self) result(params) | ||
class(layernorm_layer), intent(in), target :: self | ||
real, allocatable :: params(:) | ||
end function get_params | ||
|
||
|
||
module function get_gradients(self) result(gradients) | ||
class(layernorm_layer), intent(in), target :: self | ||
real, allocatable :: gradients(:) | ||
end function get_gradients | ||
|
||
|
||
module subroutine set_params(self, params) | ||
class(layernorm_layer), intent(in out) :: self | ||
real, intent(in), target :: params(:) | ||
end subroutine set_params | ||
end interface | ||
end module nf_layernorm_layer |
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.
@OneAdder can you please check that I did this correctly?
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.
Yes, looks good!