8000 Stop F2PY caching by HaoZeke · Pull Request #127 · impy-project/chromo · GitHub
[go: up one dir, main page]

Skip to content

Stop F2PY caching #127

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
merged 37 commits into from
Sep 28, 2023
Merged

Stop F2PY caching #127

merged 37 commits into from
Sep 28, 2023

Conversation

HaoZeke
Copy link
Collaborator
@HaoZeke HaoZeke commented Feb 11, 2023

@HDembinski could you try building numpy with numpy/numpy#22657 and running this branch? It should pass. Closes #117.

The version in rangen.fpp which calls rm48 is used in the existing
bindings, keeping this addtionally causes f2py to create two bindings to
the same function, which is an error.
@HDembinski
Copy link
Collaborator

Thank you for your help, we appreciate it very much, I am currently traveling, but I will try to check this later today.

@HDembinski
Copy link
Collaborator
HDembinski commented Feb 13, 2023

@HaoZeke We don't want to touch epos-random.f, because it is external code that we just vendored in. I use epos-random-dummy.f instead to generate the interface, which does not contain rm48 or rmmard, so it should be the same fix as 2e3a0b8.

I further modified our F2PY.cmake so that it does generate the cache files anymore.

I copied again the f2py folder from numpy/numpy#22657 into my local numpy installation 1.23.5 and tried to build with python setup.py develop. The build still aborts when trying to generate _phojet191.pyf, as before.

With this setup, the module eposlhc which uses epos-random.f builds correctly.

If you could spend some more time helping to debug this, I would be very grateful. You can restrict this project to only build the offending phojet191 module by generating a file called models.cfg in the project root folder which contains only the models / modules you want to build. For example, to only make the _phojet191 module, the content of models.cfg is

phojet191

(module name without leading underscore). This helps to speed up the development. If you want to go back to building everything, just delete models.cfg. models.cfg is not tracked by git on purpose, as it is supposed to be a temporary override.

To see the log from running f2py, do cat build/temp.<your platform>/_phojet191.log

@HDembinski
Copy link
Collaborator

Here is the log from my machine for reference.

_phojet191.log

@HDembinski
Copy link
Collaborator
HDembinski commented Feb 13, 2023

To confirm that this branch still works with f2py from numpy-v1.19.5, I deleted the f2py folder again in my local numpy installation and copied that from numpy-v1.19.5. I then deleted the build folder and ran python setup.py develop. This builds the module _phojet191 correctly.

You can optionally test whether the code not only compiles but also runs correctly with this command python -m pytest tests/test_generators.py -k phojet191 -n0. You need a couple of extra packages for the tests to run:

    pytest
    pytest-benchmark
    pytest-xdist
    pyhepmc>=2.9.0
    graphviz
    uproot
    awkward
    pyyaml
    boost_histogram
    matplotlib

I don't think that the run-test is needed, but I wanted to mention it just in case.

@HDembinski
Copy link
Collaborator
HDembinski commented Feb 13, 2023

For the record, one can install the @HaoZeke branch with pip install -U -v git+https://github.com/HaoZeke/numpy@fix22648

If someone knows to use such a URL in pyproject.toml for build-system.requires, I could change the CI jobs to build with this version of numpy, so that we see CI jobs passing if things work.

@HDembinski
Copy link
Collaborator

As another test, I now used pip install -U -v git+https://github.com/HaoZeke/numpy@fix22648 to install the branch with the fix locally, and building _phojet191 fails. When I just copy crackfortran.py (only this file) from numpy-v1.19.5, it works again.

HDembinski and others added 5 commits February 13, 2023 12:05
The version in rangen.fpp which calls rm48 is used in the existing
bindings, keeping this addtionally causes f2py to create two bindings to
the same function, which is an error.
@HaoZeke
Copy link
Collaborator Author
HaoZeke commented Jun 5, 2023

@HDembinski could you enable the workflow for this? I added a dependency on my branch with the fix in numpy/numpy#22657. Locally I'm able to build it, and run the test in the readme. If it works on CI we can wait for it to be merged into main and pin it.

@HDembinski
Copy link
Collaborator

Thank you very much, I started the workflow.

@HDembinski
Copy link
Collaborator
HDembinski commented Jun 5, 2023

The failure is in the installation step for setuptools, we need to fix this before we can test the patch by @HaoZeke

@HaoZeke
Copy link
Collaborator Author
HaoZeke commented Jun 5, 2023

The failure is in the installation step for setuptools, we need to fix this before we can test the patch by @HaoZeke

Sorry I had a typo in my last commit, could you try running it now?

@HDembinski
Copy link
Collaborator

@HaoZeke It is not failing because the directory src/f2py has been removed. Let me fix that in the cmake scripts.

@HDembinski
Copy link
Collaborator
HDembinski commented Jun 5, 2023

@HaoZeke I fixed the main build issue. The macos-latest test fails, because some dependency (numpy? setuptools?) wants Python-3.9 as the minimum. I would like to keep 3.8 as the minimum for chromo, but this is something we don't have to worry about here for now.

The crackfortran code now seems to fail in chromo/src/fortran/urqmd-3.4/sources/make22.f:5976 and following, see
https://github.com/impy-project/chromo/actions/runs/5175988472/jobs/9324260629?pr=127#step:7:6568

@HaoZeke
10000 Copy link
Collaborator Author
HaoZeke commented Jun 5, 2023

@HaoZeke I fixed the main build issue. The macos-latest test fails, because some dependency (numpy? setuptools?) wants Python-3.9 as the minimum. I would like to keep 3.8 as the minimum for chromo, but this is something we don't have to worry about here for now.

Makes sense, yup.

The crackfortran code now seems to fail in chromo/src/fortran/urqmd-3.4/sources/make22.f:5976 and following, see https://github.com/impy-project/chromo/actions/runs/5175988472/jobs/9324260629?pr=127#step:7:6568

I'll look into this, I can reproduce this error with the pip install, but not with python setup.py develop oddly enough.

@afedynitch
Copy link
Member
afedynitch commented Jun 5, 2023

@HaoZeke, I’ll look into this. It could be rather an issue with this legacy code.

Edit: I meant, this file shouldn’t be in the list those to be parsed.

Copy link
Member
@afedynitch afedynitch left a comment

Choose a reason for hiding this comment

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

Few things remain to fix. Check also if some the changes in the model Fortran codes can be reverted to default with latest version.

@jncots
Copy link
Collaborator
jncots commented Sep 19, 2023

Hello @HaoZeke, we have an issue with f2py again. All models fails to produce f2py for a recent release of numpy==1.26.0. Everything worked for numpy==1.26.0rc1.

The simplest model sophia fails at producing .pyf file with traceback:

Traceback (most recent call last):
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/__main__.py", line 5, in <module>
    main()
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/f2py2e.py", line 734, in main
    run_main(sys.argv[1:])
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/f2py2e.py", line 462, in run_main
    postlist = callcrackfortran(files, options)
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/f2py2e.py", line 363, in callcrackfortran
    postlist = crackfortran.crackfortran(files)
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/crackfortran.py", line 3355, in crackfortran
    readfortrancode(files, crackline)
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/crackfortran.py", line 552, in readfortrancode
    dowithline(finalline)
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/crackfortran.py", line 858, in crackline
    analyzeline(m, pat[1], line)
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/crackfortran.py", line 1452, in analyzeline
    vtype = vars[v].get('typespec')
KeyError: 'pi'
Reading fortran codes...
	Reading file 'CMakeFiles/_sophia.dir/SOPHIA20.f' (format:fix,strict)

Other models also have the similar problems related to vtype = vars[v].get('typespec') and giving KeyError: 'name_of_data' where name_of_data in Fortran code is kind of:

DATA name_of_data /some_value/

For sophia it is :

DATA pi /3.141593D0/

Some models (dpmjetIII191) output the following traceback (only last 2 lines):

  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/crackfortran.py", line 1471, in analyzeline
    vars[v]['='] = matches[idx]
IndexError: list index out of range

It seems all the problems are related to recently changed code in numpy/numpy@0df1365 , specifically in the file [numpy/f2py/crackfortran.py](https://github.com/numpy/numpy/commit/0df1365f8e4d1ee7502312956b59ed1bd7675144#diff-3cebebbb97dffa3ec8e404c3a72222457ad6e043303ae23e36bcff24d473addb):

        vtype = vars[v].get('typespec')
          vdim = getdimension(vars[v])

          if (vtype == 'complex'):
              cmplxpat = r"\(.*?\)"
              matches = re.findall(cmplxpat, l[1])
          else:
              matches = l[1].split(',')

          if v not in vars:
              vars[v] = {}
          if '=' in vars[v] and not vars[v]['='] == matches[idx]:
              outmess('analyzeline: changing init expression of "%s" ("%s") to "%s"\n' % (
                  v, vars[v]['='], matches[idx]))

          if vdim is not None:
              # Need to assign multiple values to one variable
              vars[v]['='] = "(/{}/)".format(", ".join(matches))
          else:
              vars[v]['='] = matches[idx]
          last_name = v

Should I add a new issue for numpy to fix this?

@HaoZeke
Copy link
Collaborator Author
HaoZeke commented Sep 19, 2023

Hello @HaoZeke, we have an issue with f2py again. All models fails to produce f2py for a recent release of numpy==1.26.0. Everything worked for numpy==1.26.0rc1.

The simplest model sophia fails at producing .pyf file with traceback:

Traceback (most recent call last):
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/__main__.py", line 5, in <module>
    main()
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/f2py2e.py", line 734, in main
    run_main(sys.argv[1:])
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/f2py2e.py", line 462, in run_main
    postlist = callcrackfortran(files, options)
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/f2py2e.py", line 363, in callcrackfortran
    postlist = crackfortran.crackfortran(files)
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/crackfortran.py", line 3355, in crackfortran
    readfortrancode(files, crackline)
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/crackfortran.py", line 552, in readfortrancode
    dowithline(finalline)
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/crackfortran.py", line 858, in crackline
    analyzeline(m, pat[1], line)
  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/crackfortran.py", line 1452, in analyzeline
    vtype = vars[v].get('typespec')
KeyError: 'pi'
Reading fortran codes...
	Reading file 'CMakeFiles/_sophia.dir/SOPHIA20.f' (format:fix,strict)

Other models also have the similar problems related to vtype = vars[v].get('typespec') and giving KeyError: 'name_of_data' where name_of_data in Fortran code is kind of:

DATA name_of_data /some_value/

For sophia it is :

DATA pi /3.141593D0/

Some models (dpmjetIII191) output the following traceback (only last 2 lines):

  File "/hetghome/antonpr/miniconda3/envs/env_test/lib/python3.9/site-packages/numpy/f2py/crackfortran.py", line 1471, in analyzeline
    vars[v]['='] = matches[idx]
IndexError: list index out of range

It seems all the problems are related to recently changed code in numpy/numpy@0df1365 , specifically in the file [numpy/f2py/crackfortran.py](https://github.com/numpy/numpy/commit/0df1365f8e4d1ee7502312956b59ed1bd7675144#diff-3cebebbb97dffa3ec8e404c3a72222457ad6e043303ae23e36bcff24d473addb):

        vtype = vars[v].get('typespec')
          vdim = getdimension(vars[v])

          if (vtype == 'complex'):
              cmplxpat = r"\(.*?\)"
              matches = re.findall(cmplxpat, l[1])
          else:
              matches = l[1].split(',')

          if v not in vars:
              vars[v] = {}
          if '=' in vars[v] and not vars[v]['='] == matches[idx]:
              outmess('analyzeline: changing ini
17A7
t expression of "%s" ("%s") to "%s"\n' % (
                  v, vars[v]['='], matches[idx]))

          if vdim is not None:
              # Need to assign multiple values to one variable
              vars[v]['='] = "(/{}/)".format(", ".join(matches))
          else:
              vars[v]['='] = matches[idx]
          last_name = v

Should I add a new issue for numpy to fix this?

Ouch, sorry, yes please open an issue and I'll get to it ASAP.

@jncots
Copy link
Collaborator
jncots commented Sep 19, 2023

Ouch, sorry, yes please open an issue and I'll get to it ASAP.

@HaoZeke, issue is opened numpy/numpy#24746

@jncots
Copy link
Collaborator
jncots commented Sep 25, 2023

@jncots We only do backports during a release cycle, which is generally ~6 months.

@charris, in relation to backport, we encountered the following problem. We started to get reports from users about the error RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xd. It occurs for chromo installed from PyPI using pip install --pre chromo. The problem can be fixed by installing pip install numpy==1.23.0 or higher. I guess, it is because we built the wheels with numpy==1.23.

The comment in https://github.com/numpy/numpy/blob/maintenance/1.26.x/numpy/core/setup_common.py suggests:

By default, when compiling downstream libraries against NumPy,
pick an older feature version.

However, to use bugfix in f2py we can build chromo only with numpy==1.26.0rc1 (or in the future 1.26.1). C_API_VERSION version for 1.25 and 1.26 is the same. Does it mean that our package can only be used with numpy>=1.25?

We didn't noticed the problem with API, because when we build wheels locally we don't have this problem, i.e. if our package is built locally with numpy==1.26.0 it still can be used with any numpy version > 1.19.5. However, wheels build on CI using cibuildwheel produce the "API version" error if numpy version being used is lower than the one used for the build. Maybe you know the reason behind this difference between the local and CI versions?

@charris
Copy link
charris commented Sep 25, 2023

Maybe you know the reason behind this difference

That's odd, sounds like some sort of bug. How do you build locally?

@jncots
Copy link
Collaborator
jncots commented Sep 26, 2023

That's odd, sounds like some sort of bug. How do you build locally?

@charris, the initial steps in both CI and local builds are the same:

  1. Build *.pyf file
  2. Using *.pyf file create *module.c and *f2pywrappers.f
  3. Compile fortranobject.c, *module.c with gcc and *f2pywrappers.f, sources in fortran with gfotran
  4. Link them. Here is linking stage of simplest model:
/hetghome/antonpr/miniconda3/envs/env_test/bin/gfortran -fPIC -O3 -shared  -o /hetghome/antonpr/chromo/build/lib.linux-x86_64-cpython-39/chromo/models/_sib21.cpython-39-x86_64-linux-gnu.so CMakeFiles/_sib21.dir/fortranobject.c.o CMakeFiles/_sib21.dir/_sib21module.c.o "CMakeFiles/_sib21.dir/_sib21-f2pywrappers.f.o" CMakeFiles/_sib21.dir/src/fortran/sibyll/sibyll_21.f.o CMakeFiles/_sib21.dir/src/fortran/sibyll/sib21aux.f.o CMakeFiles/_sib21.dir/src/fortran/sibyll/sibyll_init.fpp.o CMakeFiles/_sib21.dir/src/fortran/logging.f.o CMakeFiles/_sib21.dir/src/fortran/rangen.fpp.o CMakeFiles/_sib21.dir/src/fortran/rangen.c.o CMakeFiles/_sib21.dir/src/fortran/normal.c.o

Of course, it is all done with CMake. Another point which maybe relevant is that the compilation occurs with the flag "NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION".

The difference between CI and local build is that CI uses cibuildwheel which fixes extension files to be dependent on general dynamical libraries, like, for example, on libraries contained in manylinux.

So do you expect that the wheels build with numpy==1.26 should not work with numpy < 1.25? Or, on opposite, they should?

@charris
Copy link
charris commented Sep 26, 2023

Or, on opposite, they should?

They should. The versioning is based on the hash of the C API and 1,.25 and 1.26 have the same hash.

# Version 16 (NumPy 1.23)
# NonNull attributes removed from numpy_api.py
# Version 16 (NumPy 1.24) No change.
0x00000010 = 04a7bf1e65350926a0e528798da263c0

# Version 17 (NumPy 1.25) No actual change.
# Version 17 (NumPy 1.26) No change.
0x00000011 = ca1aebdad799358149567d9d93cbca09

@jncots
Copy link
Collaborator
jncots commented Sep 26, 2023

The versioning is based on the hash of the C API and 1,.25 and 1.26 have the same hash

@charris, it is clear about 1.25. What about lower version? Should the package work with numpy==1.20 to numpy=1.24 if it is built with numpy==1.26?

@charris
Copy link
charris commented Sep 26, 2023

Should the package work with numpy==1.20 to numpy=1.24 if it is built with numpy==1.26?

In general, no. The hash changes when new features are added to the API, so any code that makes use of those features will fail on earlier versions. We try to keep forward compatibility, not backward compatibility. If you are making wheels for distribution, you should compile against the earliest version of NumPy that supports what you need.

Copy link
Member
@afedynitch afedynitch left a comment

Choose a reason for hiding this comment

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

Looks all ready to be merged, except the likely issue with 3.12, which can be pushed to a different PR.

@jncots jncots merged commit 9e4d765 into impy-project:main Sep 28, 2023
afedynitch added a commit that referenced this pull request Sep 28, 2023
This is a PR towards chromo's 0.4 release. I hope it should be quick.
There are a couple of issues.

* The biggest current issue is #127. It is a problem for the source
code, but wheels should work. This should be ignored for this release.
* There is a small bug #163 which I will fix.
* Licenses:
```
DPMJet/PhoJet -> Anatoli Fedynitch
EPOS -> Tanguy Pierog and Klaus Werner  (permission of  usage)
Sibyll -> Felix Riehn (permission of usage)
QGSJet -> Sergei Ostapchenko (permission of usage)
UrQMD-3.4 - ?
Sophia - ?
Pythia6 - ?
Pythia8 - GPL2.0
```
Anatoli suggested to put 3 permissions to corresponding source code
directories. I guess `license.txt` with something like:

```
Sibyll- Redistribution Notice

This software is distributed as part of "Chromo" with the permission of the original author. Please refer to the original license for terms and conditions. No warranty provided.
```
should be fine.

What else should be done for release?

---------

Co-authored-by: Hans Dembinski <hans.dembinski@gmail.com>
Co-authored-by: afedynitch <afedynitch@gmail.com>
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.

Stop f2py caching
5 participants
0