BioSTEAMDevelopmentGroup / biosteam

The Biorefinery Simulation and Techno-Economic Analysis Modules; Life Cycle Assessment; Chemical Process Simulation Under Uncertainty
Other
180 stars 35 forks source link

numba inlines biosteam.CE value into compiled code #169

Closed siddharthab closed 1 year ago

siddharthab commented 1 year ago

Describe the bug In some functions like compute_vertical_vessel_purchase_cost, which access biosteam.CE and other settings, the settings constants get inlined into the cached native code. Changing the biosteam.settings value either in the same interpreter session, or in a new session then has no effect. To reset the cached value, one has to delete the __pycache__ directories in the site-packages biosteam directory.

To Reproduce Run the following code in your terminal environment where biosteam is installed.

cat > bug_demo.py <<EOF
import sys

import biosteam
from biosteam.units.design_tools.flash_vessel_design import (
    compute_vertical_vessel_purchase_cost,
)

biosteam.settings.CEPCI = int(sys.argv[1])
print(compute_vertical_vessel_purchase_cost(10000))
EOF

python3 ./bug_demo.py 500
python3 ./bug_demo.py 1000

Expected behavior The second output cost to be double that of the first.

Actual behavior The second output cost was the same as the first.

41904.56388137536
41904.56388137536

Version GitHub latest

Additional context

Numba version is 0.57.1 Python version is 3.11.5

siddharthab commented 1 year ago

This seems to be documented in the "Note" section at https://numba.readthedocs.io/en/stable/user/jit.html#cache.

I think it might be good to remove the cache directive everywhere, or do a thorough sweep of all the cached functions to verify that they are self-contained and do not reference other modules or globals.

yoelcortes commented 1 year ago

@siddharthab, thanks for noticing this! No worries, I'm working on updating all places with numba to use objmode to access dynamic values. If you would like to disable numba just in case, you can do so by running these lines at the top of your script (before loading biosteam):

import os
os.environ["NUMBA_DISABLE_JIT"] = "1"

Thanks!

yoelcortes commented 1 year ago

Fixed! Thanks again for posting

siddharthab commented 1 year ago

Thanks! I did not know about objmode at all.