Skip to content
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

Reduce TTF@mtkmodel #3471

Merged
merged 10 commits into from
Mar 20, 2025
Merged

Conversation

hexaeder
Copy link
Contributor

This PR attemts to reduce invaldiations of @mtkmodel code and thus enables precompilation of said macro.

Still WIP, so far I've only removed invalidations on the toplevel _model_macro function, but this allready leads to a ~10s improvement in TTFX.

Below examples executed in new julia session

using ModelingToolkit
@time @eval @mtkmodel Mod begin
end;
# this PR
# 0.185190 seconds (735.61 k allocations: 37.985 MiB, 16.39% gc time, 97.47% compilation time: 91% of which was recompilation)
# master
# 16.592960 seconds (114.90 M allocations: 5.945 GiB, 9.04% gc time, 99.97% compilation time)
using ModelingToolkit
@time @eval @mtkmodel Mod begin
    @variables begin
        x(t)
        y(t)
    end
    @parameters begin
        p
    end
    @equations begin
        x ~ y + p
    end
end;
# this PR
# 12.499833 seconds (82.14 M allocations: 4.328 GiB, 9.96% gc time, 99.93% compilation time: 96% of which was recompilation)
# master
# 22.175370 seconds (121.23 M allocations: 6.330 GiB, 5.06% gc time, 99.96% compilation time: <1% of which was recompilation)

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

@AayushSabharwal
Copy link
Member

Awesome!

@hexaeder
Copy link
Contributor Author

hexaeder commented Mar 19, 2025

Hm in the end it seems all it takes is full despecialization of two recursive parser function which are very hard for the compiler.

My initial goal was to fix the invalidations (i.e. no "recompilation" on first call), but I didn't manage. However, the despecialization + precomile workload alone leads to quite the speedup...

using ModelingToolkit
@time @eval @mtkmodel Mod begin
end;
# this PR
# 0.851972 seconds (3.93 M allocations: 202.397 MiB, 4.32% gc time, 99.68% compilation time: 88% of which was recompilation)
# master
# 16.592960 seconds (114.90 M allocations: 5.945 GiB, 9.04% gc time, 99.97% compilation time)
using ModelingToolkit
@time @eval @mtkmodel Mod begin
    @variables begin
        x(t)
        y(t)
    end
    @parameters begin
        p
    end
    @equations begin
        x ~ y + p
    end
end;
# this PR
# 3.384142 seconds (5.87 M allocations: 305.992 MiB, 1.34% gc time, 99.75% compilation time: 41% of which was recompilation)
# master
# 22.175370 seconds (121.23 M allocations: 6.330 GiB, 5.06% gc time, 99.96% compilation time: <1% of which was recompilation)
@time using ModelingToolkit
@time @eval @mtkmodel Mod begin
    @constants begin
        c = 1.0
    end
    @structural_parameters begin
        structp = false
    end
    @variables begin
        x(t) = 0.0, [description="foo", guess=1.0]
    end
    @parameters begin
        a = 1.0, [description="bar"]
        if structp
            b=2*a, [description="if"]
        else
            c
        end
    end
    @equations begin
        x ~ a + b
    end
end;
# this PR
# 4.351150 seconds (7.32 M allocations: 378.812 MiB, 1.70% gc time, 99.69% compilation time: 47% of which was recompilation)
# master
# 24.336051 seconds (125.52 M allocations: 6.563 GiB, 6.53% gc time, 99.95% compilation time: <1% of which was recompilation)

@hexaeder hexaeder marked this pull request as ready for review March 20, 2025 06:14
@ChrisRackauckas ChrisRackauckas merged commit 3a4f44a into SciML:master Mar 20, 2025
38 of 45 checks passed
@ChrisRackauckas
Copy link
Member

Super awesome, thanks for digging into this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants