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

[BUG] Assertion error from encode_single_position, #6366

Closed
oscarbenjamin opened this issue Aug 26, 2024 · 5 comments · Fixed by #6369
Closed

[BUG] Assertion error from encode_single_position, #6366

oscarbenjamin opened this issue Aug 26, 2024 · 5 comments · Fixed by #6369

Comments

@oscarbenjamin
Copy link
Contributor

Describe the bug

Currently python-flint has a CI job that tests against Cython master branch to measure coverage (gh-6341). That job was passing but has recently (today?) started failing.

Code to reproduce the behaviour:

I don't have a simple reproducer but here is the error message:

[34/111] Compiling Cython source /home/runner/work/python-flint/python-flint/src/flint/types/nmod.pyx
FAILED: src/flint/types/nmod.cpython-312-x86_64-linux-gnu.so.p/src/flint/types/nmod.pyx.c 
cython -M --fast-fail -3 -X embedsignature=True -X emit_code_comments=True -X linetrace=True /home/runner/work/python-flint/python-flint/src/flint/types/nmod.pyx -o src/flint/types/nmod.cpython-312-x86_64-linux-gnu.so.p/src/flint/types/nmod.pyx.c
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.12.5/x64/bin/cython", line 8, in <module>
    sys.exit(setuptools_main())
             ^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/Cython/Compiler/Main.py", line [74](https://github.com/flintlib/python-flint/actions/runs/10567756039/job/29278457452?pr=179#step:8:75)9, in setuptools_main
    return main(command_line = 1)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/Cython/Compiler/Main.py", line 772, in main
    result = compile(sources, options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/Cython/Compiler/Main.py", line 668, in compile
    return compile_multiple(source, options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/Cython/Compiler/Main.py", line 642, in compile_multiple
    result = run_pipeline(source, options,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/Cython/Compiler/Main.py", line 529, in run_pipeline
    err, enddata = Pipeline.run_pipeline(pipeline, source)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/Cython/Compiler/Pipeline.py", line 404, in run_pipeline
    data = run(phase, data)
           ^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/Cython/Compiler/Pipeline.py", line 386, in run
    return phase(data)
           ^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/Cython/Compiler/Pipeline.py", line 50, in generate_pyx_code_stage
    module_node.process_implementation(options, result)
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/Cython/Compiler/ModuleNode.py", line 226, in process_implementation
    self.generate_c_code(env, options, result)
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/Cython/Compiler/ModuleNode.py", line 548, in generate_c_code
    globalstate.finalize_main_c_code()
  File "Cython/Compiler/Code.py", line 1464, in Cython.Compiler.Code.GlobalState.finalize_main_c_code
  File "Cython/Compiler/Code.py", line 1495, in Cython.Compiler.Code.GlobalState.close_global_decls
  File "Cython/Compiler/Code.py", line 1727, in Cython.Compiler.Code.GlobalState.generate_const_declarations
  File "Cython/Compiler/Code.py", line 2005, in Cython.Compiler.Code.GlobalState.generate_codeobject_constants
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/Cython/Compiler/ExprNodes.py", line 10259, in generate_codeobj
    line_table = StringEncoding.bytes_literal(build_line_table(func.node_positions, first_lineno).encode('iso8[85](https://github.com/flintlib/python-flint/actions/runs/10567756039/job/29278457452?pr=179#step:8:86)9-1'), 'iso8859-1')
                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "Cython/Compiler/LineTable.py", line 16, in Cython.Compiler.LineTable.build_line_table
  File "Cython/Compiler/LineTable.py", line 39, in Cython.Compiler.LineTable.encode_single_position
AssertionError: 38 >= 39

Note that I am editing this source file so this might not be triggered by a very new change in Cython but this is only seen with Cython master branch and not with latest released Cython

Expected behaviour

No response

OS

No response

Python version

No response

Cython version

No response

Additional context

https://github.com/flintlib/python-flint/actions/runs/10567756039/job/29278457452?pr=179

@scoder
Copy link
Contributor

scoder commented Aug 27, 2024

Thanks for the quick report. The failing lines are (apparently) here:
https://github.com/flintlib/python-flint/blob/53a1c3f62161d6137bcbcdbda02ee4bca411004a/src/flint/types/nmod.pyx#L38-L39
The only special thing about them is the (converted) f-string, which injects optimised syntax tree nodes. I suspect that this might make a difference. However, I tried running the above cython command on the file and it succeeded on my side (latest master). I noticed that the setup.py is different from this (meson?) build command line in that it passes binding=False, so I tried that as well but it also succeeded. Thus, I cannot currently reproduce this failure locally.

That said, I pushed a rewrite of the position building code that should make it more robust. Could you try if that works for you?
ccc54f9

@oscarbenjamin
Copy link
Contributor Author

I noticed that the setup.py is different from this (meson?) build command line in that it passes binding=False, so I tried that as well but it also succeeded.

The meson build is what we use now. The setup.py is just there for compatibility purposes in case someone happens to be using it. After we have had a full release with the meson build if there seem to be no problems then we will remove it.

I'm not sure why setup.py sets binding=False and I don't think that it was intentional that we changed that in the meson build. Apparently I set binding=False in flintlib/python-flint#35 which was about Cython 3's change to __radd__ etc methods (c_api_binop_methods). The comments in the PR indicate that at the time I was having issues with different versions of cython so it may have just been something needed for some particular prerelease or it may have just been something that I decided was a good idea...

Does binding=True affect performance in any way when calling methods from cython code?

Thus, I cannot currently reproduce this failure locally.

I have just reproduced it locally using the commit cython 26fd2ba (current master^ which precedes your suggested commit above) but only in python-flint's coverage-enabled build with bin/coverage.sh:

https://github.com/flintlib/python-flint/blob/53a1c3f62161d6137bcbcdbda02ee4bca411004a/bin/coverage.sh#L18
https://github.com/flintlib/python-flint/blob/53a1c3f62161d6137bcbcdbda02ee4bca411004a/meson.build#L62-L65

I have just tried ccc54f9 (current master, i.e. your suggested commit) and I can still reproduce it so that change hasn't fixed it:

$ meson setup --wipe build --pkg-config-path=.local/lib/pkgconfig -Dadd_flint_rpath=true -Dbuildtype=debug -Dcoverage=true
$ bin/coverage.sh 
Directory already configured.

Just run your build command (e.g. ninja) and Meson will regenerate as necessary.
Run "meson setup --reconfigure to force Meson to regenerate.

If build failures persist, run "meson setup --wipe" to rebuild from scratch
using the same options as passed when configuring the build.
$ meson compile -C build
ninja: Entering directory `/home/oscar/current/active/python-flint/build'
[1/114] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/flint_base/flint_context.pyx
[2/114] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/pyflint.pyx
[3/114] Compiling C object src/flint/pyflint.cpython-312-x86_64-linux-gnu.so.p/meson-generated_src_flint_pyflint.pyx.c.o
[4/114] Linking target src/flint/pyflint.cpython-312-x86_64-linux-gnu.so
[5/114] Compiling C object src/flint/flint_base/flint_context.cpython-312-x86_64-linux-gnu.so.p/meson-generated_src_flint_flint_base_flint_context.pyx.c.o
[6/114] Linking target src/flint/flint_base/flint_context.cpython-312-x86_64-linux-gnu.so
[7/114] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/flint_base/flint_base.pyx
[8/114] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/types/fmpz_mat.pyx
[9/114] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/types/fmpz.pyx
[10/114] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/types/fmpz_poly.pyx
[11/114] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/types/fmpz_vec.pyx
[12/114] Compiling C object src/flint/types/fmpz_mat.cpython-312-x86_64-linux-gnu.so.p/meson-generated_src_flint_types_fmpz_mat.pyx.c.o
[13/114] Linking target src/flint/types/fmpz_mat.cpython-312-x86_64-linux-gnu.so
[14/114] Compiling C object src/flint/types/fmpz_vec.cpython-312-x86_64-linux-gnu.so.p/meson-generated_src_flint_types_fmpz_vec.pyx.c.o
[15/114] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/types/fmpz_series.pyx
[16/114] Linking target src/flint/types/fmpz_vec.cpython-312-x86_64-linux-gnu.so
[17/114] Compiling C object src/flint/types/fmpz.cpython-312-x86_64-linux-gnu.so.p/meson-generated_src_flint_types_fmpz.pyx.c.o
[18/114] Linking target src/flint/types/fmpz.cpython-312-x86_64-linux-gnu.so
[19/114] Compiling C object src/flint/types/fmpz_poly.cpython-312-x86_64-linux-gnu.so.p/meson-generated_src_flint_types_fmpz_poly.pyx.c.o
[20/114] Linking target src/flint/types/fmpz_poly.cpython-312-x86_64-linux-gnu.so
[21/114] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/types/fmpq.pyx
[22/114] Compiling C object src/flint/types/fmpz_series.cpython-312-x86_64-linux-gnu.so.p/meson-generated_src_flint_types_fmpz_series.pyx.c.o
[23/114] Linking target src/flint/types/fmpz_series.cpython-312-x86_64-linux-gnu.so
[24/114] Compiling C object src/flint/flint_base/flint_base.cpython-312-x86_64-linux-gnu.so.p/meson-generated_src_flint_flint_base_flint_base.pyx.c.o
[25/114] Linking target src/flint/flint_base/flint_base.cpython-312-x86_64-linux-gnu.so
[26/114] Compiling C object src/flint/types/fmpq.cpython-312-x86_64-linux-gnu.so.p/meson-generated_src_flint_types_fmpq.pyx.c.o
[27/114] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/types/fmpq_poly.pyx
[28/114] Linking target src/flint/types/fmpq.cpython-312-x86_64-linux-gnu.so
[29/114] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/types/fmpq_mat.pyx
[30/114] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/types/fmpq_vec.pyx
[31/114] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/types/nmod.pyx
FAILED: src/flint/types/nmod.cpython-312-x86_64-linux-gnu.so.p/src/flint/types/nmod.pyx.c 
cython -M --fast-fail -3 -X embedsignature=True -X emit_code_comments=True -X linetrace=True /home/oscar/current/active/python-flint/src/flint/types/nmod.pyx -o src/flint/types/nmod.cpython-312-x86_64-linux-gnu.so.p/src/flint/types/nmod.pyx.c
Traceback (most recent call last):
  File "/home/oscar/.pyenv/versions/python-flint-3.12/bin/cython", line 8, in <module>
    sys.exit(setuptools_main())
             ^^^^^^^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Main.py", line 749, in setuptools_main
    return main(command_line = 1)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Main.py", line 772, in main
    result = compile(sources, options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Main.py", line 668, in compile
    return compile_multiple(source, options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Main.py", line 642, in compile_multiple
    result = run_pipeline(source, options,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Main.py", line 529, in run_pipeline
    err, enddata = Pipeline.run_pipeline(pipeline, source)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Pipeline.py", line 404, in run_pipeline
    data = run(phase, data)
           ^^^^^^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Pipeline.py", line 386, in run
    return phase(data)
           ^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Pipeline.py", line 50, in generate_pyx_code_stage
    module_node.process_implementation(options, result)
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/ModuleNode.py", line 226, in process_implementation
    self.generate_c_code(env, options, result)
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/ModuleNode.py", line 548, in generate_c_code
    globalstate.finalize_main_c_code()
  File "Cython/Compiler/Code.py", line 1464, in Cython.Compiler.Code.GlobalState.finalize_main_c_code
  File "Cython/Compiler/Code.py", line 1495, in Cython.Compiler.Code.GlobalState.close_global_decls
  File "Cython/Compiler/Code.py", line 1727, in Cython.Compiler.Code.GlobalState.generate_const_declarations
  File "Cython/Compiler/Code.py", line 2005, in Cython.Compiler.Code.GlobalState.generate_codeobject_constants
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/ExprNodes.py", line 10259, in generate_codeobj
    line_table = StringEncoding.bytes_literal(build_line_table(func.node_positions, first_lineno).encode('iso8859-1'), 'iso8859-1')
                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "Cython/Compiler/LineTable.py", line 16, in Cython.Compiler.LineTable.build_line_table
  File "Cython/Compiler/LineTable.py", line 39, in Cython.Compiler.LineTable.encode_single_position
AssertionError: 38 >= 39
[32/114] Compiling C object src/flint/types/fmpq_vec.cpython-312-x86_64-linux-gnu.so.p/meson-generated_src_flint_types_fmpq_vec.pyx.c.o
[33/114] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/types/fmpq_series.pyx
[34/114] Compiling C object src/flint/types/fmpq_mat.cpython-312-x86_64-linux-gnu.so.p/meson-generated_src_flint_types_fmpq_mat.pyx.c.o
[35/114] Compiling C object src/flint/types/fmpq_poly.cpython-312-x86_64-linux-gnu.so.p/meson-generated_src_flint_types_fmpq_poly.pyx.c.o
[36/114] Compiling Cython source /home/oscar/current/active/python-flint/src/flint/types/nmod_poly.pyx
ninja: build stopped: subcommand failed.
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: /home/oscar/.pyenv/versions/python-flint-3.12/bin/ninja -C /home/oscar/current/active/python-flint/build

I can also reproduce it by running that cython command directly:

$ cython -M --fast-fail -3 -X embedsignature=True -X emit_code_comments=True -X linetrace=True /home/oscar/current/active/python-flint/src/flint/types/nmod.pyx -o src/flint/types/nmod.cpython-312-x86_64-linux-gnu.so.p/src/flint/types/nmod.pyx.c
Traceback (most recent call last):
  File "/home/oscar/.pyenv/versions/python-flint-3.12/bin/cython", line 8, in <module>
    sys.exit(setuptools_main())
             ^^^^^^^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Main.py", line 749, in setuptools_main
    return main(command_line = 1)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Main.py", line 772, in main
    result = compile(sources, options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Main.py", line 668, in compile
    return compile_multiple(source, options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Main.py", line 642, in compile_multiple
    result = run_pipeline(source, options,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Main.py", line 529, in run_pipeline
    err, enddata = Pipeline.run_pipeline(pipeline, source)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Pipeline.py", line 404, in run_pipeline
    data = run(phase, data)
           ^^^^^^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Pipeline.py", line 386, in run
    return phase(data)
           ^^^^^^^^^^^
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/Pipeline.py", line 50, in generate_pyx_code_stage
    module_node.process_implementation(options, result)
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/ModuleNode.py", line 226, in process_implementation
    self.generate_c_code(env, options, result)
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/ModuleNode.py", line 548, in generate_c_code
    globalstate.finalize_main_c_code()
  File "Cython/Compiler/Code.py", line 1464, in Cython.Compiler.Code.GlobalState.finalize_main_c_code
  File "Cython/Compiler/Code.py", line 1495, in Cython.Compiler.Code.GlobalState.close_global_decls
  File "Cython/Compiler/Code.py", line 1727, in Cython.Compiler.Code.GlobalState.generate_const_declarations
  File "Cython/Compiler/Code.py", line 2005, in Cython.Compiler.Code.GlobalState.generate_codeobject_constants
  File "/home/oscar/.pyenv/versions/3.12.0/envs/python-flint-3.12/lib/python3.12/site-packages/Cython/Compiler/ExprNodes.py", line 10259, in generate_codeobj
    line_table = StringEncoding.bytes_literal(build_line_table(func.node_positions, first_lineno).encode('iso8859-1'), 'iso8859-1')
                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "Cython/Compiler/LineTable.py", line 16, in Cython.Compiler.LineTable.build_line_table
  File "Cython/Compiler/LineTable.py", line 39, in Cython.Compiler.LineTable.encode_single_position
AssertionError: 38 >= 39

However I only see that failure in my PR branch and not with python-flint master: flintlib/python-flint#179.

So I guess these are the lines you wanted to point to:
https://github.com/flintlib/python-flint/blob/bf8c48b1b1ac39d55d904bb826f1f971c414167e/src/flint/types/nmod.pyx#L38-L39

@oscarbenjamin
Copy link
Contributor Author

I'm not sure why setup.py sets binding=False

Now I remember it was because binding=True made it 2-3x slower to get a coverage enabled build: #4425 (comment)

That seems less of an issue now that we have meson with fast incremental parallel rebuilds.

@scoder
Copy link
Contributor

scoder commented Aug 27, 2024

Thanks, I can also reproduce it with that branch. It's a bug. It considers 39 the first line of the function, but the first code in that function comes from the @staticmethod decorator, one line above. I'm glad I put in that assertion. :)

scoder added a commit to scoder/cython that referenced this issue Aug 27, 2024
…@cfunc functions, i.e. the one of the outermost (first) decorator, not necessarily the function signature line.

Closes cython#6366
scoder added a commit to scoder/cython that referenced this issue Aug 28, 2024
…@cfunc functions, i.e. the one of the outermost (first) decorator, not necessarily the function signature line.

Closes cython#6366
scoder added a commit that referenced this issue Aug 29, 2024
…@cfunc functions (GH-6369)

Python uses the line of the outermost (first) decorator, not necessarily the function signature line, as first line of the function.

Closes #6366
@scoder scoder added this to the 3.1 milestone Aug 29, 2024
@oscarbenjamin
Copy link
Contributor Author

I confirm that this is fixed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
@scoder @oscarbenjamin and others