Repository navigation
Add Fortran (Flang + Enzyme) support to Enzyme Explorer #34
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
Open
wsmoses
wants to merge
2
commits into
main
Choose a base branch
from
fortran
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #!/bin/bash | ||
| set -e | ||
|
|
||
| # Refresh Fortran module files from the live Enzyme checkout if available so | ||
| # module checksums always match each installed Flang binary and stay up to date. | ||
| if [ -f /opt/compiler-explorer/main/fortran/enzyme_function_hooks.f90 ] && \ | ||
| [ -f /opt/compiler-explorer/main/fortran/enzyme.f90 ]; then | ||
| for flang_bin in /usr/bin/flang-[0-9]*; do | ||
| [ -x "$flang_bin" ] || continue | ||
| v="${flang_bin##*/flang-}" | ||
| [[ "$v" =~ ^[0-9]+$ ]] || continue | ||
| mod_dir="/var/cache/enzyme-fortran-modules/${v}" | ||
| tmp_dir=$(mktemp -d) | ||
| if "$flang_bin" -fsyntax-only -J"$tmp_dir" -I"$tmp_dir" \ | ||
| /opt/compiler-explorer/main/fortran/enzyme_function_hooks.f90 && \ | ||
| "$flang_bin" -fsyntax-only -J"$tmp_dir" -I"$tmp_dir" \ | ||
| /opt/compiler-explorer/main/fortran/enzyme.f90; then | ||
| mkdir -p "$mod_dir" | ||
| cp "$tmp_dir"/*.mod "$mod_dir"/ | ||
| else | ||
| echo "Warning: failed to recompile Fortran modules with $flang_bin; keeping build-time modules" >&2 | ||
| fi | ||
| rm -rf "$tmp_dir" | ||
| done | ||
| fi | ||
|
|
||
| # Sync builder-rendered properties (with live commit hashes) if present | ||
| if [ -f /app/compiler-explorer/etc/config/fortran.enzyme.properties ]; then | ||
| cp /app/compiler-explorer/etc/config/*.enzyme.properties /app/etc/config/ | ||
| fi | ||
|
|
||
| exec node ./out/dist/app.js --static ./out/webpack/static --env enzyme "$@" |
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,60 @@ | ||
| ! Custom reverse-mode derivative registration in pure Fortran. | ||
| module custom_rule_mod | ||
| implicit none | ||
| private | ||
| public :: evaluate | ||
|
|
||
| type :: gradient_registration | ||
| procedure(scaled_op), pointer, nopass :: primal => scaled_op | ||
| procedure(augment_scaled_op), pointer, nopass :: augmented => augment_scaled_op | ||
| procedure(reverse_scaled_op), pointer, nopass :: reverse => reverse_scaled_op | ||
| end type gradient_registration | ||
|
|
||
| ! Enzyme reads this static initializer at compile time to register the custom rule. | ||
| type(gradient_registration) :: f__enzyme_register_gradient_scaled_op | ||
|
|
||
| contains | ||
|
|
||
| subroutine scaled_op(x, y) | ||
| real, intent(in) :: x | ||
| real, intent(out) :: y | ||
| y = 2.0 * x | ||
| end subroutine scaled_op | ||
|
|
||
| subroutine augment_scaled_op(x, dx, y, dy) | ||
| real, intent(in) :: x, dx | ||
| real, intent(out) :: y | ||
| real, intent(inout) :: dy | ||
| call scaled_op(x, y) | ||
| end subroutine augment_scaled_op | ||
|
|
||
| subroutine reverse_scaled_op(x, dx, y, dy) | ||
| real, intent(in) :: x, y | ||
| real, intent(inout) :: dx, dy | ||
| ! Custom rule: supply d/dx log(1 + x) = 1 / (1 + x) | ||
| dx = dx + dy / (1.0 + x) | ||
| dy = 0.0 | ||
| end subroutine reverse_scaled_op | ||
|
|
||
| function evaluate(x) result(y) | ||
| real, intent(in) :: x | ||
| real :: y | ||
| call scaled_op(x, y) | ||
| end function evaluate | ||
|
|
||
| end module custom_rule_mod | ||
|
|
||
| program main | ||
| use enzyme, only: enzyme_autodiff | ||
| use custom_rule_mod, only: evaluate | ||
| implicit none | ||
| real :: x, dx, y | ||
|
|
||
| x = 2.0 | ||
| dx = 0.0 | ||
| y = evaluate(x) | ||
| call enzyme_autodiff(evaluate, x, dx) | ||
|
|
||
| write(*, '(a,f6.4)') 'y = ', y | ||
| write(*, '(a,f6.4)') 'dx = ', dx | ||
| end program main |
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,36 @@ | ||
| module square_mod | ||
| use enzyme, only: enzyme_autodiff | ||
| implicit none | ||
| private | ||
| public :: square, dsquare | ||
|
|
||
| contains | ||
|
|
||
| real function square(x) | ||
| real, intent(in) :: x | ||
| square = x * x | ||
| end function square | ||
|
|
||
| real function dsquare(x) | ||
| real, intent(in) :: x | ||
| real :: dx | ||
| dx = 0.0 | ||
| ! Returns the derivative of square(x), i.e. 2 * x | ||
| call enzyme_autodiff(square, x, dx) | ||
| dsquare = dx | ||
| end function dsquare | ||
|
|
||
| end module square_mod | ||
|
|
||
| program main | ||
| use square_mod, only: square, dsquare | ||
| implicit none | ||
| real :: x | ||
| integer :: i | ||
|
|
||
| do i = 1, 4 | ||
| x = real(i) | ||
| write(*, '(2(a,f3.1,a,f4.1))') & | ||
| 'square(', x, ')=', square(x), ', dsquare(', x, ')=', dsquare(x) | ||
| end do | ||
| end program main |
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,41 @@ | ||
| ! Reverse-mode gradient of a vector dot product using activity descriptors. | ||
| program main | ||
| use enzyme, only: enzyme_autodiff, enzyme_const, enzyme_dup | ||
| implicit none | ||
| integer, parameter :: n = 4 | ||
| real, dimension(n) :: a, da, b, db | ||
| real :: res, dres | ||
| integer :: i | ||
|
|
||
| a = [1.0, 2.0, 3.0, 4.0] | ||
| b = [10.0, 20.0, 30.0, 40.0] | ||
| da = 0.0 | ||
| db = 0.0 | ||
| dres = 1.0 | ||
|
|
||
| call enzyme_autodiff(vec_dot, enzyme_const, n, & | ||
| enzyme_dup, a, da, & | ||
| enzyme_dup, b, db, & | ||
| enzyme_dup, res, dres) | ||
|
|
||
| write(*, '(a,f6.1)') 'dot(a, b) =', res | ||
| do i = 1, n | ||
| write(*, '(2(a,i1,a,f5.1))') & | ||
| 'd(dot)/da(', i, ') =', da(i), ', d(dot)/db(', i, ') =', db(i) | ||
| end do | ||
|
|
||
| contains | ||
|
|
||
| subroutine vec_dot(n, a, b, res) | ||
| integer, intent(in) :: n | ||
| real, dimension(n), intent(in) :: a, b | ||
| real, intent(out) :: res | ||
| integer :: i | ||
|
|
||
| res = 0.0 | ||
| do i = 1, n | ||
| res = res + a(i) * b(i) | ||
| end do | ||
| end subroutine vec_dot | ||
|
|
||
| end program main |
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,33 @@ | ||
| ! Forward-mode directional derivative using enzyme_fwddiff. | ||
| program main | ||
| use enzyme, only: enzyme_fwddiff, enzyme_const, enzyme_dup | ||
| implicit none | ||
| integer, parameter :: n = 3 | ||
| real, dimension(n) :: x, dx | ||
| real :: y, dy | ||
|
|
||
| x = [2.0, 3.0, 6.0] | ||
| dx = [1.0, 0.5, -1.0] | ||
|
|
||
| call enzyme_fwddiff(l2_norm_sq, enzyme_const, n, & | ||
| enzyme_dup, x, dx, & | ||
| enzyme_dup, y, dy) | ||
|
|
||
| write(*, '(a,f6.2)') 'y = ||x||^2 =', y | ||
| write(*, '(a,f6.2)') 'dy = grad(y) . dx =', dy | ||
|
|
||
| contains | ||
|
|
||
| subroutine l2_norm_sq(n, x, y) | ||
| integer, intent(in) :: n | ||
| real, dimension(n), intent(in) :: x | ||
| real, intent(out) :: y | ||
| integer :: i | ||
|
|
||
| y = 0.0 | ||
| do i = 1, n | ||
| y = y + x(i) * x(i) | ||
| end do | ||
| end subroutine l2_norm_sq | ||
|
|
||
| end program main |
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,38 @@ | ||
| ! Registering a function-like mathematical derivative rule via procedure pointer. | ||
| module function_like_mod | ||
| implicit none | ||
| private | ||
| public :: wrapper | ||
|
|
||
| procedure(approx_log1p), pointer, private :: & | ||
| fn__enzyme_function_like__log1p => approx_log1p | ||
|
|
||
| contains | ||
|
|
||
| function approx_log1p(x) result(y) | ||
| real, value :: x | ||
| real :: y | ||
| y = x - 0.5 * x * x | ||
| end function approx_log1p | ||
|
|
||
| function wrapper(x) result(y) | ||
| real, intent(in) :: x | ||
| real :: y | ||
| y = approx_log1p(x) | ||
| end function wrapper | ||
|
|
||
| end module function_like_mod | ||
|
|
||
| program main | ||
| use enzyme, only: enzyme_autodiff | ||
| use function_like_mod, only: wrapper | ||
| implicit none | ||
| real :: x, dx | ||
|
|
||
| x = 2.0 | ||
| dx = 0.0 | ||
| call enzyme_autodiff(wrapper, x, dx) | ||
|
|
||
| write(*, '(a,f7.4)') 'wrapper(2.0) =', wrapper(x) | ||
| write(*, '(a,f7.4)') 'd/dx via rule =', dx | ||
| end program main |
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,25 @@ | ||
| # Default settings for Fortran | ||
| compilers=&flang-enzyme-main | ||
| defaultCompiler=flang{{ flang_compilers[-1].version }}-enzyme-main | ||
| demangler=c++filt | ||
| objdumper=objdump | ||
| postProcess= | ||
| supportsBinary=true | ||
| supportsExecute=true | ||
| supportsLibraryCodeFilter=true | ||
| binaryHideFuncRe=^(__.*|_(init|start|fini)|(de)?register_tm_clones|call_gmon_start|frame_dummy|\.plt.*|.*@plt.*|_dl_relocate_static_pie|_GLOBAL__sub_I_.*|_Z.*|_FortranA.*|CFI_.*|atexit|backtrace_|(Convert|Validate|AllocateValidated).*|.*\(.*)$ | ||
|
|
||
| group.flang-enzyme-main.compilers={% for compiler in flang_compilers %}flang{{ compiler.version }}-enzyme-main{% if not loop.last %}:{% endif %}{% endfor %} | ||
| group.flang-enzyme-main.intelAsm=-masm=intel | ||
| group.flang-enzyme-main.compilerType=flang | ||
| group.flang-enzyme-main.compilerCategories=flang | ||
| group.flang-enzyme-main.supportsExecute=true | ||
| group.flang-enzyme-main.isSemVer=true | ||
| group.flang-enzyme-main.groupName=FLANG + ENZYME (main) | ||
|
|
||
| {% for compiler in flang_compilers %} | ||
| compiler.flang{{ compiler.version }}-enzyme-main.exe=/usr/bin/flang-{{ compiler.version }} | ||
| compiler.flang{{ compiler.version }}-enzyme-main.options=--config=/opt/compiler-explorer/main/FlangEnzyme-{{ compiler.version }}.cfg -Xflang -load -Xflang /opt/compiler-explorer/main/FlangEnzyme-{{ compiler.version }}.so -I/var/cache/enzyme-fortran-modules/{{ compiler.version }} | ||
| compiler.flang{{ compiler.version }}-enzyme-main.semver={{ compiler.semver }} | ||
| compiler.flang{{ compiler.version }}-enzyme-main.name=flang {{ compiler.version }} ({{ compiler.enzyme_commit }}) | ||
| {% endfor %} |
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.
It is nowadays considered bad practice to have procedures that don't belong to a module or program. Please could you either put the functions in a module that gets
used in the program, or put them following acontainsstatement in the program?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.
Moved
squareanddsquareintomodule square_mod(used inprogram main) in 3a3587c.