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

@jit interface for LPython #1708

Merged
merged 6 commits into from
May 10, 2023
Merged

@jit interface for LPython #1708

merged 6 commits into from
May 10, 2023

Conversation

harshsingh-24
Copy link
Contributor

@harshsingh-24 harshsingh-24 commented Apr 13, 2023

So here is a list of steps:

Add a jit decorator to lpython.py that does the following:

  • Saves the source code of the function fast_sum as a string into a file
  • Call LPython on the file and create an object file via the LLVM backend
  • Generate a C file with the Python wrappers using Python C/API (I would not use Cython)
  • Compile the C file together with the object file and create a shared library (=Python extension module)
  • Import it from our_shared_library import fast_math_c
  • Return fast_math_c as the result of the decorator

Now, the simple function works:

  • Let's make it more general and remove hard coding
  • Test array functions (fast_sum)
  • Add Tests in integration_tests
  • Test this in Linux (use -shared flag)
  • Mangle the file_names used.

PS: Work in Progress

@harshsingh-24 harshsingh-24 marked this pull request as draft April 13, 2023 08:44
@harshsingh-24
Copy link
Contributor Author

Hey @Thirumalai-Shaktivel . This PR is a work in progress. Let's have the discussion related to JIT here.

Comment on lines 303 to 308
class jit:
def __init__(self, func):
self.func = func

def __call__(self, *args, **kwargs):
# Get the source code of the function
Copy link
Collaborator

@Smit-create Smit-create Apr 13, 2023

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@harshsingh-24 harshsingh-24 changed the title @JIT interface for LPython @jit interface for LPython Apr 13, 2023
@harshsingh-24
Copy link
Contributor Author

harshsingh-24 commented Apr 13, 2023

Hey @Thirumalai-Shaktivel . How can I make jit recognize in backend? I do understand that we need to make some changes in backend. I want to do it step by step like certik wrote. So as of now, I am just focusing on step 2 of it.

@Thirumalai-Shaktivel
Copy link
Collaborator

Thirumalai-Shaktivel commented Apr 13, 2023

My understanding of the implementation:
Consider the code:

@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.

lpython.jit saves the following code into a file, compiles it (LLVM or C), and creates an object file for it.

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.
Later, import and use this shared_libarary in the CPython code.

@@ -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;
Copy link
Contributor

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".

@Smit-create
Copy link
Collaborator

@harshsingh-24 Any updates on this?

@harshsingh-24
Copy link
Contributor Author

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.

@Thirumalai-Shaktivel
Copy link
Collaborator

@harshsingh-24 I will take over this implementation. You can work on other issues like adding other string built-in functions.

@harshsingh-24
Copy link
Contributor Author

harshsingh-24 commented May 3, 2023

@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.😓

@Thirumalai-Shaktivel
Copy link
Collaborator

No issues, @harshsingh-24. Finish up your exams (it's very important. All the best with your exams!).
You can work here anytime.

@certik
Copy link
Contributor

certik commented May 4, 2023

Thanks for all your help @harshsingh-24 ! No worries, school is always higher priority.

Comment on lines 604 to 605
numpy_path = "-I" "$CONDA_PREFIX/lib/python3.10/site-packages/" \
"numpy/core/include/"
Copy link
Collaborator

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?

Copy link
Contributor

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().

Copy link
Contributor

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().

@Thirumalai-Shaktivel
Copy link
Collaborator

This PR is ready for testing!
@certik if you have time, do you mind testing this? see if you can break something.
Meanwhile, I will try to clean up and add the required comments to explain what the code does.

@certik
Copy link
Contributor

certik commented May 7, 2023

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 -shared as an option.

@certik
Copy link
Contributor

certik commented May 10, 2023

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
Copy link
Contributor

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.

Thirumalai-Shaktivel and others added 6 commits May 10, 2023 09:32
- 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 <intelligent24harsh@gmail.com>
- 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 <ondrej@certik.us>
… shared library

Co-authored-by: Ondřej Čertík <ondrej@certik.us>
Copy link
Contributor

@certik certik left a 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.

@certik certik merged commit 9d965a8 into lcompilers:main May 10, 2023
5 checks passed
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.

None yet

4 participants