Skip to content

Changes to how the parameter object is scattered in dask#1048

Merged
rickecon merged 7 commits into
PSLmodels:masterfrom
jdebacker:dask_graph
Aug 21, 2025
Merged

Changes to how the parameter object is scattered in dask#1048
rickecon merged 7 commits into
PSLmodels:masterfrom
jdebacker:dask_graph

Conversation

@jdebacker

Copy link
Copy Markdown
Member

This PR makes some changes to how the parameter object is scattered and referenced by dask futures calls in SS.py and TPI.py. In particular:

  • dask futures are used rather than delayed objects
  • Function definitions are not done in a delayed object since they are called with each future
  • The Specifications object, p, is scattered outside of the TPI loop since it's not changed in the while loop (so only need to define once)

@codecov-commenter

codecov-commenter commented Aug 20, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 53.57143% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.61%. Comparing base (d476a06) to head (0273913).
⚠️ Report is 272 commits behind head on master.

Files with missing lines Patch % Lines
ogcore/TPI.py 0.00% 13 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1048      +/-   ##
==========================================
- Coverage   72.67%   72.61%   -0.06%     
==========================================
  Files          20       20              
  Lines        5068     5076       +8     
==========================================
+ Hits         3683     3686       +3     
- Misses       1385     1390       +5     
Flag Coverage Δ
unittests 72.61% <53.57%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
ogcore/SS.py 72.64% <100.00%> (+0.18%) ⬆️
ogcore/__init__.py 100.00% <100.00%> (ø)
ogcore/TPI.py 36.73% <0.00%> (-0.46%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jdebacker jdebacker requested a review from Copilot August 20, 2025 22:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the parallel computation approach in the OG-Core economic modeling library by migrating from dask delayed objects to dask futures for better performance and resource management.

  • Replaces delayed objects with client.submit() futures for parallel execution
  • Optimizes parameter scattering by moving the Specifications object outside the TPI iteration loop
  • Implements proper serial fallback logic when no dask client is available

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
ogcore/TPI.py Refactors TPI loop to use futures instead of delayed objects and moves parameter scattering outside the loop
ogcore/SS.py Updates steady-state solver to use futures pattern with proper serial fallback implementation

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread ogcore/SS.py Outdated
method=p.FOC_root_method,
tol=MINIMIZER_TOL,
)
def solve_for_j(

Copilot AI Aug 20, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function solve_for_j is now defined inside inner_loop but is called in both parallel and serial execution paths. Consider moving this function definition outside of inner_loop to improve code organization and avoid redefining the same function multiple times during execution.

Copilot uses AI. Check for mistakes.
@rickecon rickecon marked this pull request as ready for review August 21, 2025 20:52
@rickecon

Copy link
Copy Markdown
Member

@jdebacker. We definitely want to update the version with this PR. Please make the changes in CHANGELOG.md, setup.py, and ogcore/__init__.py: 0.14.6 -> 0.14.7.

@rickecon

Copy link
Copy Markdown
Member

@jdebacker. I am just running these changes in the run_ogcore_example.py script. Then can merge. Great update.

@jdebacker

Copy link
Copy Markdown
Member Author

@rickecon I've updated the files for a new release. I think this PR is ready.

@rickecon

Copy link
Copy Markdown
Member

@jdebacker. Performance stats from my local run of run_ogcore_example.py. All looks great. Merging now.

Baseline steady-state (2 min)

  • No Dask warning about distributing data size.
GE loop errors = [2.3224477896377493e-14, 2.1350976542322542e-14, 1.2365664048274994e-12, np.float64(0.0), 2.3292479056635784e-13, np.float64(1.2382456171522449e-14), np.float64(-3.965577866082981e-15), np.float64(1.4054729602364091e-14), np.float64(2.8971790244636253e-13), np.float64(1.206153232846674e-14), np.float64(7.042977312465837e-16), np.float64(5.2046041087994155e-15), 2.0962398483703737e-14, 5.954958748333183e-14]
Iteration: 1  Distance: 1.395507834151389e-11
SS debt = 0.6252962397934748, 0.00794992146845252
IO: (1, 1), C: (1,)
Foreign debt holdings = 0.25011849591738994
Foreign capital holdings = 0.05620836283593522
resource constraint: [-1.15879528e-14]
Checking constraints on capital, labor, and consumption.
	There were no violations of the constraints on labor  supply.
	There were no violations of the constraints on  consumption.
Maximum error in labor FOC = 1.0658141036401503e-13
Maximum error in savings FOC = 1.176836406102666e-13
JUST SAVED SS output to  /Users/richardevans/Docs/Economics/OSE/OG/OG-Core/run_examples/OG-Core-Example/OUTPUT_BASELINE/SS/SS_vars.pkl

Baseline transition path equilibrium (23 min, 2.9 sec)

  • No Dask warning about distributing data size.
Maximum debt ratio:  1.623089202706317
w diff: 7.562077934952782e-07, -3.7450276324513254e-07
r diff: 5.8892526050291494e-08, -1.2326341539831143e-07
r_p diff: 8.935178777425001e-10, -1.3025323768178776e-07
p_m diff: 0.0, 0.0
BQ diff: 1.2743259007047536e-07, -1.727531616195488e-08
TR diff: 5.555568013959622e-08, -3.6277384272975777e-07
Iteration: 21
	Distance: 7.501911761051849e-06
Max absolute value resource constraint error: 8.459743881805704e-07
Checking time path for violations of constraints.
Max Euler error, savings: 3.4737768217496523e-12
Max Euler error labor supply: 1.3353762540191383e-12
Time path iteration complete.
It took 1382.93496966362 seconds to get that part done.
run time =  1382.936401128769
making dir:  /Users/richardevans/Docs/Economics/OSE/OG/OG-Core/run_examples/OG-Core-Example/OUTPUT_REFORM/SS
making dir:  /Users/richardevans/Docs/Economics/OSE/OG/OG-Core/run_examples/OG-Core-Example/OUTPUT_REFORM/TPI

Reform steady-state (1 min)

  • No Dask warning about distributing data size.
GE loop errors = [4.85722573273506e-17, 4.85722573273506e-17, -4.440892098500626e-16, np.float64(0.0), -3.3306690738754696e-16, np.float64(-1.0408340855860843e-17), np.float64(-6.938893903907228e-18), np.float64(-2.0816681711721685e-17), np.float64(-1.5612511283791264e-17), np.float64(-1.734723475976807e-17), np.float64(-2.0816681711721685e-17), np.float64(-3.469446951953614e-18), -2.7755575615628914e-17]
Iteration: 1  Distance: 8.460555395304066e-16
SS debt = 0.6228816990786425, 0.007919223364348076
IO: (1, 1), C: (1,)
Foreign debt holdings = 0.24915267963145704
Foreign capital holdings = 0.05260193167620883
resource constraint: [-2.3349378e-14]
Checking constraints on capital, labor, and consumption.
	There were no violations of the constraints on labor  supply.
	There were no violations of the constraints on  consumption.
Maximum error in labor FOC = 1.0702549957386509e-13
Maximum error in savings FOC = 9.303668946358812e-14
JUST SAVED SS output to  /Users/richardevans/Docs/Economics/OSE/OG/OG-Core/run_examples/OG-Core-Example/OUTPUT_REFORM/SS/SS_vars.pkl

Reform transition path equilibrium (26 min, 6.8 sec)

  • No Dask warning about distributing data size.
Maximum debt ratio:  1.4790841176512344
w diff: 7.448194998538327e-07, -4.7274221026682994e-07
r diff: 7.45354062151038e-08, -1.2010352863633944e-07
r_p diff: 1.715385472556008e-10, -1.2534476614284307e-07
p_m diff: 0.0, 0.0
BQ diff: 1.196840882580763e-07, -1.6932682098472207e-08
TR diff: 6.15183639862682e-08, -3.318398965890257e-07
Iteration: 21
	Distance: 6.942822334127482e-06
Max absolute value resource constraint error: 8.089560778780369e-07
Checking time path for violations of constraints.
Max Euler error, savings: 4.681366405634435e-12
Max Euler error labor supply: 1.1830536550405668e-12
Time path iteration complete.
It took 1566.823054075241 seconds to get that part done.
run time =  1566.8246309757233
total time was  2956.845354795456
Percentage changes in aggregates: Year                    Variable  2021  2022  2023  ...  2029  2030  2021-2030    SS
0                    GDP ($Y_t$) -0.17 -0.14 -0.22  ... -0.10 -0.07      -0.16 -0.39
1            Consumption ($C_t$) -0.08  1.05  0.13  ... -0.43 -0.44      -0.12 -0.72
2          Capital Stock ($K_t$) -0.37 -0.41 -0.66  ... -0.60 -0.54      -0.62 -1.51
3                  Labor ($L_t$) -0.05  0.00  0.02  ...  0.17  0.18       0.08  0.22
4     Real interest rate ($r_t$) -3.05 -2.94 -2.64  ... -2.61 -2.65      -2.64 -1.40
5            Wage rate ($w_{t}$) -0.11 -0.14 -0.24  ... -0.27 -0.25      -0.25 -0.61

[6 rows x 13 columns]

@rickecon rickecon merged commit ef6695e into PSLmodels:master Aug 21, 2025
8 checks passed
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.

4 participants