-
Notifications
You must be signed in to change notification settings - Fork 38
docs: add tutorial on raising #1935
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
Draft
avik-pal
wants to merge
1
commit into
main
Choose a base branch
from
ap/stablehlo_raising
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.
+108
−1
Draft
Changes from all commits
Commits
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
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,104 @@ | ||
| # Raising | ||
|
|
||
| ## Raising GPU Kernels | ||
|
|
||
| <!-- TODO: write this section --> | ||
|
|
||
| ## Raising Scalar Loops to Tensor IR | ||
|
|
||
| We will implement a simple N body simulation code in Reactant. Instead of using | ||
| broadcasting or high-level abstractions, we will use loops and scalar operations | ||
| to implement this. | ||
|
|
||
| ```@example raising_stablehlo | ||
| using Reactant, PrettyChairmarks | ||
|
|
||
| Reactant.allowscalar(true) # generally not recommended to turn on globally | ||
| ``` | ||
|
|
||
| We will implement a naive function to compute the attractive force between each | ||
| pair of particles in a system. | ||
|
|
||
| ```@example raising_stablehlo | ||
| function compute_attractive_force( | ||
| positions::AbstractMatrix, masses::AbstractVector, G::Number | ||
| ) | ||
| N = size(positions, 2) | ||
| F = similar(positions, N, N) | ||
|
|
||
| @trace for i in 1:N | ||
| @trace for j in 1:N | ||
| dx = positions[1, i] - positions[1, j] | ||
| dy = positions[2, i] - positions[2, j] | ||
| dz = positions[3, i] - positions[3, j] | ||
|
|
||
| invr² = ifelse(i == j, dx, inv(dx^2 + dy^2 + dz^2)) | ||
|
|
||
| Fx = G * masses[i] * masses[j] * invr² * dx | ||
| Fy = G * masses[i] * masses[j] * invr² * dy | ||
| Fz = G * masses[i] * masses[j] * invr² * dz | ||
| F[i, j] = Fx + Fy + Fz | ||
| end | ||
| end | ||
|
|
||
| return F | ||
| end | ||
| ``` | ||
|
|
||
| ```@example raising_stablehlo | ||
| positions = randn(Float32, 3, 1024) | ||
| masses = rand(Float32, 1024) .* 10 | ||
|
|
||
| positions_ra = Reactant.to_rarray(positions) | ||
| masses_ra = Reactant.to_rarray(masses) | ||
| nothing # hide | ||
| ``` | ||
|
|
||
| Let's see what the HLO IR looks like for this function (without enabling the loop | ||
| raising). | ||
|
|
||
| ```@example raising_stablehlo | ||
| @code_hlo compile_options = CompileOptions(; | ||
| disable_auto_batching_passes=true | ||
| ) compute_attractive_force(positions_ra, masses_ra, 2.0f0) | ||
| ``` | ||
|
|
||
| This IR has a nested loop, but that won't work nicely for GPUs/TPUs. Even for CPUs, XLA | ||
| often doens't do a great job with loops. By default, we will attempt to raise loops to a | ||
| tensor IR. | ||
|
|
||
| ```@example raising_stablehlo | ||
| hlo = @code_hlo compute_attractive_force(positions_ra, masses_ra, 2.0f0) | ||
| @assert !contains(repr(hlo), "stablehlo.while") #hide | ||
| hlo | ||
| ``` | ||
|
|
||
| This IR won't have any loops, instead it will be written in a tensor IR! Let ensure that | ||
| the values are identical. | ||
|
|
||
| ```@example raising_stablehlo | ||
| y_jl = compute_attractive_force(positions, masses, 2.0f0) | ||
| y_ra = @jit compute_attractive_force(positions_ra, masses_ra, 2.0f0) | ||
| maximum(abs, Array(y_ra) .- y_jl) | ||
| ``` | ||
|
|
||
| Let's time the execution of the two versions. | ||
|
|
||
| ```@example raising_stablehlo | ||
| fn1 = @compile sync=true compile_options=CompileOptions(; | ||
| disable_auto_batching_passes=true | ||
| ) compute_attractive_force(positions_ra, masses_ra, 2.0f0) | ||
| fn2 = @compile sync=true compute_attractive_force(positions_ra, masses_ra, 2.0f0) | ||
| ``` | ||
|
|
||
| Runtime for non-raised function: | ||
|
|
||
| ```@example raising_stablehlo | ||
| @bs fn1(positions_ra, masses_ra, 2.0f0) | ||
| ``` | ||
|
|
||
| Runtime for raised function: | ||
|
|
||
| ```@example raising_stablehlo | ||
| @bs fn2(positions_ra, masses_ra, 2.0f0) | ||
| ``` | ||
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.
do we want to show the code here for example?
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.
you mean the manually written one with broadcasting? For the hlo, those will be printed out automatically
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.
ah awesome, yeah I just meant that it would be cool if we could see the before and after IR. If its automatically produced, even better!