Python Frontend: Use Numba Type Inference#1253
Conversation
704f176 to
e4dbe7e
Compare
|
Sorry for the lack of clean-up/updates on this one, I was feeling a bit under the weather. The big ToDo, as discussed last week, to move from Numba-style inference to something that supports statically shaped tensors is still open, but I did do some other smaller cleanup
Here's a current example: from heir_py import compile
from heir_py.mlir import *
from heir_py.types import *
@compile() # defaults to scheme="bgv"", backend="openfhe", debug=False
def foo(x : Secret[I16], y : I16):
sum = x + y
mul = x * y
expression = sum * mul
return expression
foo.setup() # runs keygen/etc
enc_x = foo.encrypt_x(7)
result_enc = foo.eval(enc_x, 8)
result = foo.decrypt_result(result_enc)
print(f"Expected result for `foo`: {foo(7,8)}, decrypted result: {result}") |
4cb79a7 to
d22470b
Compare
|
As mentioned in yesterday's meeting, there's been a bit more progress, in addition to what I mentioned above:
Since this PR is getting a bit large already, I'm leaning towards cleaning this up (once #1302 is merged, allowing for full scheme/backend separation) and then making a new PR for the tensor shape inference stuff (which will probably be an MLIR pass after all). |
11a2d35 to
15628c0
Compare
❤️ !! Feel free to clean/split it up as you see fit. I have no problems reviewing a large PR for this one. |
|
Part of #1105 |
ec7680b to
c8ab6e5
Compare
|
This is essentially ready to review, I just need some help getting the test setup working properly, as the e2e test currenlty fails if run via bazel. Since the last update, major changes include
|
@j2kun : let me now if this is too large, and I'll figure out how to split it into more sensible chunks |
|
The testing issue is because you put the python package one step further nested under Edit: see next comment |
|
Ohh! Researching this I found on https://bazel.build/reference/be/python So with this patch I can get it to run in bazel, although the test fails diff --git a/frontend/BUILD b/frontend/BUILD
index 190aee59..342bf248 100644
--- a/frontend/BUILD
+++ b/frontend/BUILD
@@ -34,6 +34,7 @@ py_library(
),
data = DATA_DEPS,
deps = [
+ "@frontend_pip_deps_colorama//:pkg",
"@frontend_pip_deps_numba//:pkg",
"@frontend_pip_deps_pybind11//:pkg",
"@frontend_pip_deps_pybind11_global//:pkg",
diff --git a/frontend/testing.bzl b/frontend/testing.bzl
index 3d24a318..902f7dd6 100644
--- a/frontend/testing.bzl
+++ b/frontend/testing.bzl
@@ -29,6 +29,7 @@ def frontend_test(name, srcs, deps = [], data = [], tags = []):
":frontend",
"@com_google_absl_py//absl/testing:absltest",
],
+ imports = ["."],
data = data,
tags = tags,
env = {Test failure: https://gist.github.com/j2kun/40fe73429938e814cf3b08beb514dd44 |
|
Awesome, thanks! I just realized the test failure is likely because my current approach still relies on a nasty local patch to numba to support sizes smaller than |
c8ab6e5 to
b444e4c
Compare
|
Now the bazel tests work locally but fail on the CI, probably because I messed something up with openfhe_config... I'll look into it tomorrow! |
The command is using That said, I am trying to wrap your |
I was able to figure out why this is: bazel doesn't set the same environment variables for binary targets vs test targets, so I need to add another env var check for |
|
Thanks for the detailed review and all the bazel-debugging! I'll try and get this pull-ready later today :) |
|
FYI @AlexanderViand-Intel Asra's loop support is in review, so you may have a big rebase ahead of you. |
Sorry for letting this sit for so long, as you've probably noticed I've not had a lot of bandwidth the last few weeks. I guess the best move is to wait for #1307 to be merged, then rebase and split this PR into two, one with the various QoL features and another one for the actual type inference? |
SGTM. I'd give it maybe two weeks before we have to take it over and get it merged so we can have the issues ironed out before the conferences :) |
b444e4c to
9e6d0ce
Compare
|
I just checked, and there's actually not much to do for the rebase, just needed to replace a hardcoded More generally, HEIR doesn't currently support doing things such as |
ed43165 to
5a77b1a
Compare
|
I've integrated the review feedback and removed all the matmul/intrinsic related things for now, as I need to dig deeper into numba for that. I've also found a workaround for the issue mentioned in #1252 (numba will upcast, e.g., int8 + int8 to int32/int64). It's not very pretty, so there's a good chance some internal tool will not accept it (I recall some C++ linking hack also getting stopped by internal checks), but I guess loading a modified version of a module via importlib is technically "normal" python?
|
0872113 to
da20760
Compare
|
@j2kun: I think everything I was planning to do (and a lot of semi-related QoL things...feature creep I guess 😓) are in now. :) EDIT: I just found one more annoying thing - numba assumes all literal ints are There is one more "QoL" thing I'd love to tackle, and that's the inability to have multiple instances of the frontend/HEIR (the "PublicKey registerd" error), but I think I might need your assistance to tackle that one as it's less about the frontend and more about the bindings. Next steps would be
|
bbb31a5 to
ee056db
Compare
ee056db to
4a74c35
Compare
ba4d558 to
4a74c35
Compare
This does a few things:
@mlirdecorator that is a slightly modified version of numba's@intrinsic, with the main difference being that one can provide a pure python implementation and run it under python. This allows us to define, e.g.,linalg.matmuland assign itnp.matmulas its python implementation.Secret[...]), in preparation for grabbing signature types from type annotations (rathter than from a string passed into the decorator, as is Numba's approach for ahead-of-time compilation)PS: fun fact - in numba/numpy world,
i4is not a 4-bit integer (as in MLIR), but shorthand for the 4-byte typeint32.