-
Notifications
You must be signed in to change notification settings - Fork 170
@jit
interface for LPython
#1708
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
Conversation
Hey @Thirumalai-Shaktivel . This PR is a work in progress. Let's have the discussion related to |
src/runtime/lpython/lpython.py
Outdated
class jit: | ||
def __init__(self, func): | ||
self.func = func | ||
|
||
def __call__(self, *args, **kwargs): | ||
# Get the source code of the function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EDIT: Are we handling this JITing based on types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know. I was just trying to follow the steps from what @certik wrote.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this does steps 1. and 2. from #703 (comment).
Now we need to do step 3.
Hey @Thirumalai-Shaktivel . How can I make |
My understanding of the implementation: @lpython.jit
def fast_sum(x: f64[n]) -> f64:
s: f64 = 0
for i in range(n):
s += x[i]
return s We compile this using Python.
def fast_sum(x: f64[n]) -> f64:
s: f64 = 0
for i in range(n):
s += x[i]
return s Then, Somehow create a shared_library with it. |
@@ -3405,6 +3405,8 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> { | |||
is_inline = true; | |||
} else if (name == "static") { | |||
is_static = true; | |||
} else if (name == "jit") { | |||
is_jit = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think jit
should not compile with LPython, but let's raise some informative message, like "@lpython.jit` decorator must be run from CPython, not compiled using LPython".
@harshsingh-24 Any updates on this? |
I am working on it. @Smit-create Is this an urgent issue? I was having my exams that is why I am a little slow on progress here. |
@harshsingh-24 I will take over this implementation. You can work on other issues like adding other string built-in functions. |
I am really sorry that I delayed it this much @Thirumalai-Shaktivel. I know this was a deliverable MVP but I have my end-semester going and I was not able to keep up. Yeah, please take over.😓 |
No issues, @harshsingh-24. Finish up your exams (it's very important. All the best with your exams!). |
Thanks for all your help @harshsingh-24 ! No worries, school is always higher priority. |
src/runtime/lpython/lpython.py
Outdated
numpy_path = "-I" "$CONDA_PREFIX/lib/python3.10/site-packages/" \ | ||
"numpy/core/include/" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this okay? or is there any other way to specify the path for Numpy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For NumPy use import numpy; print numpy.get_include()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the Python path, I think this works: from distutils.sysconfig import get_python_inc; print get_python_inc()
.
This PR is ready for testing! |
I would fix at the CI and merge this. This provides the foundation and we can then keep adding more tests and start fixing it until it is more usable. I think it works on macOS, but we need to fix it on Linux: 212/215 Test #215: test_jit_01 ......................***Failed 0.47 sec
gcc: error: unrecognized command-line option ‘-bundle’
gcc: error: unrecognized command-line option ‘-flat_namespace’
gcc: error: unrecognized command-line option ‘-rpath’
Traceback (most recent call last):
File "/home/runner/work/lpython/lpython/integration_tests/test_jit_01.py", line 5, in <module>
def fast_sum(n: i32, x: f64[:]) -> f64:
File "/home/runner/work/lpython/lpython/src/runtime/lpython/lpython.py", line 722, in __init__
assert r == 0, "Failed to create the shared library"
AssertionError: Failed to create the shared library There you just use |
It looks like the CI might pass. If it does, then let's rebase the history (squash @harshsingh-24's commits into some logical commits, and then add your commits, create some nice sequence) and mark this "Ready for review". I'll then do one final review and let's merge. I think you can keep the paths hardwired for now, if they work on both Linux and macOS, that's good enough. We will then make it completely general and Python version independent with subsequent PRs. |
@@ -4,6 +4,8 @@ | |||
import platform | |||
from dataclasses import dataclass as py_dataclass, is_dataclass as py_is_dataclass | |||
from goto import with_goto | |||
from numpy import get_include |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't want to include numpy here, since it would be imported with every "import lpython" in every file and things will be slow. Python's imports are slow, and we want "import lpython" to be immediate. For now I would "import numpy" when the decorator is processed, not here. This can be done in subsequent PR.
- Get the source code from Jit decorator and store it in a file - Create a C file using the --show-c option Co-authored-by: Harsh Singh Jadon <[email protected]>
- Process the arguments and return variable to suit C declared types - Pass these variables as arguments for the function call - Enclose everything within the Python wrapper - Write this C template into a file Co-authored-by: Ondřej Čertík <[email protected]>
… shared library Co-authored-by: Ondřej Čertík <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is good enough.
@JIT
interface to LPythonSo here is a list of steps:
Add a
jit
decorator tolpython.py
that does the following:fast_sum
as a string into a filefrom our_shared_library import fast_math_c
fast_math_c
as the result of the decoratorNow, the simple function works:
-shared
flag)PS: Work in Progress