-
-
Notifications
You must be signed in to change notification settings - Fork 42
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
Showing
26 changed files
with
855 additions
and
695 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
name: CI (NonlinearSolveFirstOrder) | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- master | ||
paths: | ||
- "lib/NonlinearSolveFirstOrder/**" | ||
- ".github/workflows/CI_NonlinearSolveFirstOrder.yml" | ||
- "lib/NonlinearSolveBase/**" | ||
- "lib/SciMLJacobianOperators/**" | ||
push: | ||
branches: | ||
- master | ||
|
||
concurrency: | ||
# Skip intermediate builds: always. | ||
# Cancel intermediate builds: only if it is a pull request build. | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }} | ||
|
||
jobs: | ||
test: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
version: | ||
- "lts" | ||
- "1" | ||
os: | ||
- ubuntu-latest | ||
- macos-latest | ||
- windows-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: julia-actions/setup-julia@v2 | ||
with: | ||
version: ${{ matrix.version }} | ||
- uses: actions/cache@v4 | ||
env: | ||
cache-name: cache-artifacts | ||
with: | ||
path: ~/.julia/artifacts | ||
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }} | ||
restore-keys: | | ||
${{ runner.os }}-test-${{ env.cache-name }}- | ||
${{ runner.os }}-test- | ||
${{ runner.os }}- | ||
- name: "Install Dependencies and Run Tests" | ||
run: | | ||
import Pkg | ||
Pkg.Registry.update() | ||
# Install packages present in subdirectories | ||
dev_pks = Pkg.PackageSpec[] | ||
for path in ("lib/SciMLJacobianOperators", "lib/NonlinearSolveBase") | ||
push!(dev_pks, Pkg.PackageSpec(; path)) | ||
end | ||
Pkg.develop(dev_pks) | ||
Pkg.instantiate() | ||
Pkg.test(; coverage="user") | ||
shell: julia --color=yes --code-coverage=user --depwarn=yes --project=lib/NonlinearSolveFirstOrder {0} | ||
- uses: julia-actions/julia-processcoverage@v1 | ||
with: | ||
directories: lib/NonlinearSolveFirstOrder/src,lib/NonlinearSolveBase/src,lib/NonlinearSolveBase/ext,lib/SciMLJacobianOperators/src | ||
- uses: codecov/codecov-action@v4 | ||
with: | ||
file: lcov.info | ||
token: ${{ secrets.CODECOV_TOKEN }} | ||
verbose: true | ||
fail_ci_if_error: true |
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,49 @@ | ||
using NonlinearSolveBase, SciMLBase, StableRNGs, ForwardDiff, Random, LinearAlgebra | ||
|
||
true_function(x, θ) = @. θ[1] * exp(θ[2] * x) * cos(θ[3] * x + θ[4]) | ||
true_function(y, x, θ) = (@. y = θ[1] * exp(θ[2] * x) * cos(θ[3] * x + θ[4])) | ||
|
||
θ_true = [1.0, 0.1, 2.0, 0.5] | ||
x = [-1.0, -0.5, 0.0, 0.5, 1.0] | ||
|
||
const y_target = true_function(x, θ_true) | ||
|
||
function loss_function(θ, p) | ||
ŷ = true_function(p, θ) | ||
return ŷ .- y_target | ||
end | ||
|
||
function loss_function(resid, θ, p) | ||
true_function(resid, p, θ) | ||
resid .= resid .- y_target | ||
return resid | ||
end | ||
|
||
θ_init = θ_true .+ randn!(StableRNG(0), similar(θ_true)) * 0.1 | ||
|
||
function vjp(v, θ, p) | ||
resid = zeros(length(p)) | ||
J = ForwardDiff.jacobian((resid, θ) -> loss_function(resid, θ, p), resid, θ) | ||
return vec(v' * J) | ||
end | ||
|
||
function vjp!(Jv, v, θ, p) | ||
resid = zeros(length(p)) | ||
J = ForwardDiff.jacobian((resid, θ) -> loss_function(resid, θ, p), resid, θ) | ||
mul!(vec(Jv), transpose(J), v) | ||
return nothing | ||
end | ||
|
||
prob_oop = NonlinearLeastSquaresProblem{false}(loss_function, θ_init, x) | ||
prob_iip = NonlinearLeastSquaresProblem{true}( | ||
NonlinearFunction(loss_function; resid_prototype = zero(y_target)), θ_init, x | ||
) | ||
prob_oop_vjp = NonlinearLeastSquaresProblem( | ||
NonlinearFunction{false}(loss_function; vjp), θ_init, x | ||
) | ||
prob_iip_vjp = NonlinearLeastSquaresProblem( | ||
NonlinearFunction{true}(loss_function; resid_prototype = zero(y_target), vjp = vjp!), | ||
θ_init, x | ||
) | ||
|
||
export prob_oop, prob_iip, prob_oop_vjp, prob_iip_vjp |
File renamed without changes.
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
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
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
Oops, something went wrong.