- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 233
          Reduce TTF@mtkmodel
          #3471
        
          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
          
     Merged
      
      
            ChrisRackauckas
  merged 10 commits into
  SciML:master
from
hexaeder:hw/precompile_mtkmodel
  
      
      
   
  Mar 20, 2025 
      
    
                
     Merged
            
            
  
    Reduce TTF@mtkmodel
  
  #3471
              
                    ChrisRackauckas
  merged 10 commits into
  SciML:master
from
hexaeder:hw/precompile_mtkmodel
  
      
      
   
  Mar 20, 2025 
              
            Conversation
  
    
      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
    
  
  
    
    | Awesome! | 
| 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) | 
| 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
  
      
  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.
  
    
  
    
This PR attemts to reduce invaldiations of
@mtkmodelcode and thus enables precompilation of said macro.Still WIP, so far I've only removed invalidations on the toplevel
_model_macrofunction, but this allready leads to a ~10s improvement in TTFX.Below examples executed in new julia session
Checklist
contributor guidelines, in particular the SciML Style Guide and
COLPRAC.
Additional context
Add any other context about the problem here.