Skip to content

add cython.inline_module - #555

Open
nevion wants to merge 1 commit into
cython:masterfrom
nevion:master
Open

nevion wants to merge 1 commit into
cython:masterfrom
nevion:master

Conversation

@nevion

@nevion nevion commented Aug 14, 2016

Copy link
Copy Markdown

allows JIT/slightly AOC module compilation, similar to that of OpenCL/PyCUDA/PyOpenCL.

This is useful for maximum performance with simpler reusable modules and simplifies flow code-fragment acceleration. The intent is to compile a (C/C++) module in initialization when all algorithm details are known (say those dependent on configuration or input data) - then put it in immediate high-performance use.

It is based off the cython.inline implementation.

@scoder

scoder commented Aug 14, 2016

Copy link
Copy Markdown
Contributor

There seems to be a major code redundancy with cython.inline() now, though. Wouldn't a "scope='module'" argument for cython.inline() be enough here? Duplicating non-trivial code is almost always the wrong thing to do.

I admit that the name isn't ideal then...

@nevion

nevion commented Aug 14, 2016 •

Copy link
Copy Markdown
Author

@scoder I feel your pain but 53 lines is hardly a major code redundancy and it looked pretty ugly to handle differing behaviors inside inline. I considered making them one, as a matter of fact I really just started with inline but it wasn't worth it to me. I think inline could actually now be based on inline_module... but that's a 2nd optional pass and leaves a question of how to handle the signatures inline presently extracts from arguments. Maybe the hash should be on the result pxd file and not the input?

Handling all the argument behavior variations in cython_inline will make up the difference and make the functions as not simple/not straightforward/fragile.

inline expects to work with arguments immediately and does lots of inspection on them, module_inline is strictly just for quasi-JIT/AOC without bringing in the arguments yet. I really wanted to use compile instead but it's already taken and honestly I think what it is doing now is stupid and should be replaced with module_inline's implementation - the behavior given to it now is different though and also works with arguments/slow on reuse due to repeated calls to cython_inline - see RuntimeCompiledFunction for what I'm talking about - it only offers half the utility such a function should have, in terms of the libraries promise of performance.

Comment thread Cython/Build/Inline.py
if force or not os.path.isfile(module_path):
qualified = re.compile(r'([.\w]+)[.]')
#assume everybody wants numpy
if include_numpy:

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.

Couldn't this be automatically detected?

@robertwb

Copy link
Copy Markdown
Contributor

I find 53 lines to be significant redundancy. I agree that it makes more sense for the current inline could be based on module_inline (or a shared function that encapsulates the core functionality from the two different interfaces).

@nevion

nevion commented Aug 15, 2016

Copy link
Copy Markdown
Author

In that case, I think you should just break API on compile, renaming module_inline. I'm not really sure how the signatures matter to the compilation key/file but if you remove them, then inline will have no problem using compile/module_inline.

@ARF1

ARF1 commented Sep 15, 2019

Copy link
Copy Markdown

@nevion Thank you very much for your work. I refactored it as was requested above in #3145. I was sorely missing this functionality...

@scoder

There seems to be a major code redundancy with cython.inline() now, though. Wouldn't a "scope='module'" argument for cython.inline() be enough here? Duplicating non-trivial code is almost always the wrong thing to do.

I fixed the code duplication with refactoring. As @nevion already mentioned, it was far from simple and is not all that pretty. This however cannot be avoided IMHO due to the logic flow of cython_inline.

I did implement your suggestion of "scope='module'" argument for cython.inline(). I decided to roll it back as the behaviour is to obscure for users in my opinion.

In analogy to current behaviour, users would expect the following to be possible:

a = 3
foo = inline("""
    def foo(x):
        return a + x""",
    scope='module')['foo']
foo(7) == 10

That will however fail:

  1. module members cannot be accessed with module['member_name'] only with module.member_name
  2. It is impossible to dynamically pull unbounds from the enclosing scope into the scope of an entire module. At least I cannot think of a reasonable way of doing this. With cython_inline this is only possible because a new enclosing scope within a function is generated on each call. This however prevents nested cdef function(): definitions. - A module can be imported only once however.

Let me know if you agree with my reasoning.

@robertwb

I find 53 lines to be significant redundancy. I agree that it makes more sense for the current inline could be based on module_inline (or a shared function that encapsulates the core functionality from the two different interfaces).

I took your suggestion and implemented a "bare bones" interface that does the minimal amount of code processing to allow users the flexibility to implement custom variations without having to copy the entire core functionality.

@nevion

nevion commented Sep 16, 2019

Copy link
Copy Markdown
Author

sounds good, hope this next pass makes it in

@Tagar

Tagar commented Nov 30, 2019

Copy link
Copy Markdown

Following

+1 for something like this to be added into cython to be able to dynamically compile/cythonize some python code.

Thanks!

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.

5 participants