-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
892187a
commit 3a6cd70
Showing
10 changed files
with
283 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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,3 @@ | ||
# ParametricOperators.jl | ||
|
||
Documentation for ParametricOperators.jl |
This file contains 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,3 @@ | ||
# ParametricOperators.jl | ||
|
||
Documentation for ParametricOperators.jl |
This file contains 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,39 @@ | ||
### Parametrized Convolution on 3D Tensor | ||
|
||
Make sure to add necessary dependencies to compute the gradient | ||
|
||
```julia | ||
julia> using Pkg | ||
julia> Pkg.activate("path/to/your/environment") | ||
julia> Pkg.add("Zygote") | ||
``` | ||
|
||
```julia | ||
using Pkg | ||
Pkg.activate("./path/to/your/environment") | ||
|
||
using ParametricOperators | ||
using Zygote | ||
|
||
T = Float32 | ||
|
||
gt, gx, gy = 100, 100, 100 | ||
|
||
# Define a transform along each dimension | ||
St = ParMatrix(T, gt, gt) | ||
Sx = ParMatrix(T, gx, gx) | ||
Sy = ParMatrix(T, gy, gy) | ||
|
||
# Create a Kronecker operator than chains together the transforms | ||
S = Sy ⊗ Sx ⊗ St | ||
|
||
# Parametrize our transform | ||
θ = init(S) |> gpu | ||
|
||
# Apply the transform on a random input | ||
x = rand(T, gt, gx, gy) |> gpu | ||
y = S(θ) * vec(x) | ||
|
||
# Compute the gradient wrt some objective of our parameters | ||
θ′ = gradient(θ -> sum(S(θ) * vec(x)), θ) | ||
``` |
This file contains 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,62 @@ | ||
### Distributed Parametrized Convolution of a 3D Tensor: | ||
|
||
Make sure to add necessary dependencies. You might also need to load a proper MPI implementation based on your hardware. | ||
|
||
```julia | ||
julia> using Pkg | ||
julia> Pkg.activate("path/to/your/environment") | ||
julia> Pkg.add("MPI") | ||
julia> Pkg.add("CUDA") | ||
julia> Pkg.add("Zygote") | ||
``` | ||
|
||
Copy the following code into a `.jl` file | ||
```julia | ||
using Pkg | ||
Pkg.activate("./path/to/your/environment") | ||
|
||
using ParametricOperators | ||
using CUDA | ||
using MPI | ||
|
||
MPI.Init() | ||
|
||
comm = MPI.COMM_WORLD | ||
rank = MPI.Comm_rank(comm) | ||
size = MPI.Comm_size(comm) | ||
|
||
# Julia requires you to manually assign the gpus, modify to your case. | ||
CUDA.device!(rank % 4) | ||
partition = [1, 1, size] | ||
|
||
T = Float32 | ||
|
||
# Define your Global Size and Data Partition | ||
gt, gx, gy = 100, 100, 100 | ||
nt, nx, ny = [gt, gx, gy] .÷ partition | ||
|
||
# Define a transform along each dimension | ||
St = ParMatrix(T, gt, gt) | ||
Sx = ParMatrix(T, gx, gx) | ||
Sy = ParMatrix(T, gy, gy) | ||
|
||
# Create and distribute the Kronecker operator than chains together the transforms | ||
S = Sy ⊗ Sx ⊗ St | ||
S = distribute(S, partition) | ||
|
||
# Parametrize our transform | ||
θ = init(S) |> gpu | ||
|
||
# Apply the transform on a random input | ||
x = rand(T, nt, nx, ny) |> gpu | ||
y = S(θ) * vec(x) | ||
|
||
# Compute the gradient wrt some objective of our parameters | ||
θ′ = gradient(θ -> sum(S(θ) * vec(x)), θ) | ||
|
||
MPI.Finalize() | ||
``` | ||
|
||
You can run the above by doing: | ||
|
||
`srun -n N_TASKS julia code_above.jl` |
This file contains 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,55 @@ | ||
### Distributed FFT of a 3D Tensor: | ||
|
||
Make sure to add necessary dependencies. You might also need to load a proper MPI implementation based on your hardware. | ||
|
||
```julia | ||
julia> using Pkg | ||
julia> Pkg.activate("path/to/your/environment") | ||
julia> Pkg.add("MPI") | ||
julia> Pkg.add("CUDA") | ||
``` | ||
|
||
Copy the following code into a `.jl` file | ||
```julia | ||
using Pkg | ||
Pkg.activate("./path/to/your/environment") | ||
|
||
using ParametricOperators | ||
using CUDA | ||
using MPI | ||
|
||
MPI.Init() | ||
|
||
comm = MPI.COMM_WORLD | ||
rank = MPI.Comm_rank(comm) | ||
size = MPI.Comm_size(comm) | ||
|
||
# Julia requires you to manually assign the gpus, modify to your case. | ||
CUDA.device!(rank % 4) | ||
partition = [1, 1, size] | ||
|
||
T = Float32 | ||
|
||
# Define your Global Size and Data Partition | ||
gt, gx, gy = 100, 100, 100 | ||
nt, nx, ny = [gt, gx, gy] .÷ partition | ||
|
||
# Define a transform along each dimension | ||
Ft = ParDFT(T, gt) | ||
Fx = ParDFT(Complex{T}, gx) | ||
Fy = ParDFT(Complex{T}, gy) | ||
|
||
# Create and distribute the Kronecker operator than chains together the transforms | ||
F = Fy ⊗ Fx ⊗ Ft | ||
F = distribute(F, partition) | ||
|
||
# Apply the transform on a random input | ||
x = rand(T, nt, nx, ny) |> gpu | ||
y = F * vec(x) | ||
|
||
MPI.Finalize() | ||
``` | ||
|
||
You can run the above by doing: | ||
|
||
`srun -n N_TASKS julia code_above.jl` |
This file contains 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,24 @@ | ||
### FFT of 3D Tensor | ||
|
||
```julia | ||
using Pkg | ||
Pkg.activate("./path/to/your/environment") | ||
|
||
using ParametricOperators | ||
|
||
T = Float32 | ||
|
||
gt, gx, gy = 100, 100, 100 | ||
|
||
# Define a transform along each dimension | ||
Ft = ParDFT(T, gt) | ||
Fx = ParDFT(Complex{T}, gx) | ||
Fy = ParDFT(Complex{T}, gy) | ||
|
||
# Create a Kronecker operator than chains together the transforms | ||
F = Fy ⊗ Fx ⊗ Ft | ||
|
||
# Apply the transform on a random input | ||
x = rand(T, gt, gx, gy) |> gpu | ||
y = F * vec(x) | ||
``` |
This file contains 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,3 +1,3 @@ | ||
# ParametricOperators.jl | ||
### ParametricOperators.jl | ||
|
||
Documentation for ParametricOperators.jl |
This file contains 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,72 @@ | ||
## Installation | ||
|
||
Add `ParametricOperators.jl` as a dependency to your environment. | ||
|
||
To add, either do: | ||
|
||
```julia | ||
julia> ] | ||
(v1.9) add ParametricOperators | ||
``` | ||
|
||
OR | ||
|
||
```julia | ||
julia> using Pkg | ||
julia> Pkg.activate("path/to/your/environment") | ||
julia> Pkg.add("ParametricOperators") | ||
``` | ||
|
||
## Simple Operator | ||
|
||
Make sure to include the package in your environment | ||
|
||
```julia | ||
using ParametricOperators | ||
``` | ||
|
||
Lets start by defining a Matrix Operator of size `10x10`: | ||
|
||
```julia | ||
A = ParMatrix(10, 10) | ||
``` | ||
|
||
Now, we parametrize our operator with some weights `θ`: | ||
|
||
```julia | ||
θ = init(A) | ||
``` | ||
|
||
We can now apply our operator on some random input: | ||
|
||
```julia | ||
x = rand(10) | ||
A(θ) * x | ||
``` | ||
|
||
## Gradient Computation | ||
|
||
!!! note "Limited AD support" | ||
Current support only provided for Zygote.jl | ||
|
||
Make sure to include an AD package in your environment | ||
|
||
```julia | ||
using Zygote | ||
``` | ||
|
||
Using the example above, one can find the gradient of the weights or your input w.r.t to some objective using a standard AD package: | ||
|
||
```julia | ||
# Gradient w.r.t weights | ||
θ′ = gradient(θ -> sum(A(θ) * x), θ) | ||
|
||
# Gradient w.r.t input | ||
x′ = gradient(x -> sum(A(θ) * x), x) | ||
``` | ||
|
||
## Chaining Operators | ||
|
||
## Kronecker Operator | ||
|
||
## Distributing Kronecker Operator |