From 0f0f6572216e59711f94223cc6463d20a377197d Mon Sep 17 00:00:00 2001 From: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> Date: Sun, 16 Nov 2025 09:51:28 +0100 Subject: [PATCH] exp: investigate new allocations on sparse ForwardDiff Jacobian --- .github/workflows/Documentation.yml | 57 -------- .github/workflows/Test.yml | 79 +---------- .../test/Back/ForwardDiff/benchmark.jl | 14 ++ .../test/Back/ForwardDiff/test.jl | 126 ------------------ 4 files changed, 15 insertions(+), 261 deletions(-) delete mode 100644 .github/workflows/Documentation.yml delete mode 100644 DifferentiationInterface/test/Back/ForwardDiff/test.jl diff --git a/.github/workflows/Documentation.yml b/.github/workflows/Documentation.yml deleted file mode 100644 index 30144baa0..000000000 --- a/.github/workflows/Documentation.yml +++ /dev/null @@ -1,57 +0,0 @@ -name: Docs - -on: - push: - branches: - - main - tags: ['*'] - pull_request: - workflow_dispatch: - -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: - - docs: - name: ${{ matrix.pkg.shortcut }} - runs-on: ubuntu-latest - if: ${{ !contains(github.event.pull_request.labels.*.name, 'skipci') }} - permissions: - contents: write - statuses: write - strategy: - fail-fast: false - matrix: - pkg: - - name: DifferentiationInterface - shortcut: DI - dir: './DifferentiationInterface' - - name: DifferentiationInterfaceTest - shortcut: DIT - dir: './DifferentiationInterfaceTest' - - steps: - - uses: actions/checkout@v5 - - uses: julia-actions/setup-julia@v2 - with: - version: '1' # TODO: 1 - - uses: julia-actions/cache@v1 - - name: Install dependencies - run: julia --project=${{ matrix.pkg.dir}}/docs/ --color=yes -e ' - using Pkg; - Pkg.Registry.update(); - if "${{ matrix.pkg.name}}" == "DifferentiationInterface"; - Pkg.develop(PackageSpec(path="./DifferentiationInterface")); - else; - Pkg.develop([PackageSpec(path="./DifferentiationInterface"), PackageSpec(path="${{ matrix.pkg.dir}}")]); - end; - Pkg.instantiate();' - - name: Build and deploy - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # If authenticating with GitHub Actions token - DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }} # If authenticating with SSH deploy key - run: julia --project=${{ matrix.pkg.dir}}/docs/ --color=yes ${{ matrix.pkg.dir}}/docs/make.jl diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 89223f597..546b6edd6 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -25,32 +25,14 @@ jobs: actions: write contents: read strategy: - fail-fast: true # TODO: toggle + fail-fast: false # TODO: toggle matrix: version: - '1.10' - '1.11' - '1.12' group: - - Core/Internals - - Back/DifferentiateWith - - Core/SimpleFiniteDiff - - Back/SparsityDetector - - Core/ZeroBackends - - Back/ChainRules - # - Back/Diffractor - - Back/Enzyme - - Back/FastDifferentiation - - Back/FiniteDiff - - Back/FiniteDifferences - Back/ForwardDiff - - Back/GTPSA - - Back/Mooncake - - Back/PolyesterForwardDiff - - Back/ReverseDiff - - Back/Symbolics - - Back/Tracker - - Back/Zygote skip_lts: - ${{ github.event.pull_request.draft }} skip_pre: @@ -103,62 +85,3 @@ jobs: flags: DI token: ${{ secrets.CODECOV_TOKEN }} fail_ci_if_error: false - - test-DIT: - name: ${{ matrix.version }} - DIT (${{ matrix.group }}) - runs-on: ubuntu-latest - if: ${{ !contains(github.event.pull_request.labels.*.name, 'skipci') }} - timeout-minutes: 60 - permissions: # needed to allow julia-actions/cache to proactively delete old caches that it has created - actions: write - contents: read - strategy: - fail-fast: true - matrix: - version: - - '1.10' - - '1.11' - - '1.12' - group: - - Formalities - - Zero - - Standard - - Weird - skip_lts: - - ${{ github.event.pull_request.draft }} - skip_pre: - - ${{ github.event.pull_request.draft }} - exclude: - - skip_lts: true - version: '1.10' - - skip_pre: true - version: '1.12' - env: - JULIA_DIT_TEST_GROUP: ${{ matrix.group }} - JULIA_DI_PR_DRAFT: ${{ github.event.pull_request.draft }} - steps: - - uses: actions/checkout@v5 - - uses: julia-actions/setup-julia@v2 - with: - version: ${{ matrix.version }} - arch: x64 - - uses: julia-actions/cache@v2 - - name: Install dependencies & run tests - run: julia --project=./DifferentiationInterfaceTest --color=yes -e ' - using Pkg; - Pkg.Registry.update(); - Pkg.develop(path="./DifferentiationInterface"); - if ENV["JULIA_DI_PR_DRAFT"] == "true"; - Pkg.test("DifferentiationInterfaceTest"; allow_reresolve=false, coverage=true, julia_args=["-O1"]); - else; - Pkg.test("DifferentiationInterfaceTest"; allow_reresolve=false, coverage=true); - end;' - - uses: julia-actions/julia-processcoverage@v1 - with: - directories: ./DifferentiationInterfaceTest/src,./DifferentiationInterfaceTest/ext,./DifferentiationInterfaceTest/test - - uses: codecov/codecov-action@v5 - with: - files: lcov.info - flags: DIT - token: ${{ secrets.CODECOV_TOKEN }} - fail_ci_if_error: false diff --git a/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl b/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl index 256c8aaa2..6e0097090 100644 --- a/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl +++ b/DifferentiationInterface/test/Back/ForwardDiff/benchmark.jl @@ -11,6 +11,20 @@ using Test LOGGING = get(ENV, "CI", "false") == "false" +@info "Printing allocs" +const f! = DIT.diffsquare! +x, y = rand(10), zeros(9); +backend = MyAutoSparse(AutoForwardDiff()); +prep = prepare_jacobian(f!, y, backend, x); +J = similar(sparsity_pattern(prep), Float64) +jacobian!(f!, y, J, prep, backend, x) + +Profile.Allocs.clear() +Profile.Allocs.@profile sample_rate = 1 jacobian!(f!, y, J, prep, backend, x) +Profile.Allocs.print() + +@info "Printing allocs done" + @testset verbose = true "Benchmarking static" begin filtered_static_scenarios = filter(static_scenarios(; include_batchified = false)) do scen DIT.function_place(scen) == :out && DIT.operator_place(scen) == :out diff --git a/DifferentiationInterface/test/Back/ForwardDiff/test.jl b/DifferentiationInterface/test/Back/ForwardDiff/test.jl deleted file mode 100644 index ef501aec1..000000000 --- a/DifferentiationInterface/test/Back/ForwardDiff/test.jl +++ /dev/null @@ -1,126 +0,0 @@ -using Pkg -Pkg.add("ForwardDiff") - -using ADTypes: ADTypes -using ComponentArrays: ComponentArrays -using DifferentiationInterface, DifferentiationInterfaceTest -import DifferentiationInterface as DI -import DifferentiationInterfaceTest as DIT -using ForwardDiff: ForwardDiff -using StaticArrays: StaticArrays, @SVector -using JLArrays: JLArrays -using Test - -using ExplicitImports -check_no_implicit_imports(DifferentiationInterface) - -LOGGING = get(ENV, "CI", "false") == "false" - -struct MyTag end - -backends = [ - AutoForwardDiff(), - AutoForwardDiff(; chunksize = 5), - AutoForwardDiff(; tag = ForwardDiff.Tag(MyTag(), Float64)), -] - -for backend in backends - @test check_available(backend) - @test check_inplace(backend) -end - -@testset "Dense" begin - test_differentiation( - backends, default_scenarios(; include_constantified = true); logging = LOGGING - ) - - test_differentiation( - AutoForwardDiff(), - default_scenarios(; - include_normal = false, - include_batchified = false, - include_cachified = true, - include_constantorcachified = true, - use_tuples = true, - include_smaller = true, - ); - logging = LOGGING, - ) - - test_differentiation( - AutoForwardDiff(); - correctness = false, - type_stability = safetypestab(:prepared), - logging = LOGGING, - ) - - test_differentiation( - AutoForwardDiff(; chunksize = 5); - correctness = false, - type_stability = safetypestab(:full), - excluded = [:hessian], - logging = LOGGING, - ) -end - -@testset "Sparse" begin - test_differentiation( - MyAutoSparse(AutoForwardDiff()), default_scenarios(); logging = LOGGING - ) - - test_differentiation( - MyAutoSparse(AutoForwardDiff()), - sparse_scenarios(; include_constantified = true); - sparsity = true, - logging = LOGGING, - ) -end - -@testset "Weird" begin - test_differentiation(AutoForwardDiff(), component_scenarios(); logging = LOGGING) - test_differentiation(AutoForwardDiff(), static_scenarios(); logging = LOGGING) - test_differentiation( - DI.AutoForwardFromPrimitive(AutoForwardDiff()), gpu_scenarios(); logging = LOGGING - ) - - @testset "Batch size" begin - @test DI.pick_batchsize(AutoForwardDiff(), rand(7)) isa DI.BatchSizeSettings{7} - @test DI.pick_batchsize(AutoForwardDiff(; chunksize = 5), rand(7)) isa - DI.BatchSizeSettings{5} - @test (@inferred DI.pick_batchsize(AutoForwardDiff(), @SVector(rand(7)))) isa - DI.BatchSizeSettings{7} - @test ( - @inferred DI.pick_batchsize( - AutoForwardDiff(; chunksize = 5), @SVector(rand(7)) - ) - ) isa DI.BatchSizeSettings{5} - end -end - -@testset verbose = true "Overloaded inputs" begin - backend = AutoForwardDiff() - sparse_backend = MyAutoSparse(AutoForwardDiff()) - - # Derivative - x = 1.0 - y = [1.0, 1.0] - @test DI.overloaded_input_type(prepare_derivative(copy, backend, x)) == - ForwardDiff.Dual{ForwardDiff.Tag{typeof(copy), Float64}, Float64, 1} - @test DI.overloaded_input_type(prepare_derivative(copyto!, y, backend, x)) == - Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(copyto!), Float64}, Float64, 1}} - - # Gradient - x = [1.0, 1.0] - @test DI.overloaded_input_type(prepare_gradient(sum, backend, x)) == - Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(sum), Float64}, Float64, 2}} - - # Jacobian - x = [1.0, 0.0, 0.0] - @test DI.overloaded_input_type(prepare_jacobian(copy, backend, x)) == - ForwardDiff.Dual{ForwardDiff.Tag{typeof(copy), Float64}, Float64, 3} - @test DI.overloaded_input_type(prepare_jacobian(copyto!, similar(x), backend, x)) == - Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(copyto!), Float64}, Float64, 3}} - @test DI.overloaded_input_type( - prepare_jacobian(copyto!, similar(x), sparse_backend, x) - ) == Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(copyto!), Float64}, Float64, 1}} -end;