Skip to content

Commit

Permalink
cleanup example.py a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderViand-Intel committed Jan 24, 2025
1 parent 8f155ad commit d22470b
Showing 1 changed file with 18 additions and 50 deletions.
68 changes: 18 additions & 50 deletions heir_py/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,22 @@
# TODO (#1162): Allow for multiple functions in the same compilation. This requires switching from a decorator to a context thingy (`with heir.compile(...):`)
# TODO (#1162): Consider hardening the eval function against handcrafted ciphertexts with wrong metadata being passed in?

# TODO (Alex/Intel): For next meeting with Ro and Flavio, explain our Packing/Layout logic

# ### Simple Example
# @compile() # defaults to scheme="bgv", backend="openfhe", debug=False
# def foo(x : Secret[I16], y : Secret[I16]):
# sum = x + y
# diff = x - y
# mul = x * y
# expression = sum * diff + mul
# deadcode = expression * mul
# return expression

# foo.setup() # runs keygen/etc
# enc_x = foo.encrypt_x(7)
# enc_y = foo.encrypt_y(8)
# result_enc = foo.eval(enc_x, enc_y)
# result = foo.decrypt_result(result_enc)
# print(f"Expected result for `foo`: {foo(7,8)}, decrypted result: {result}")


# ### Rebalancing Example
# ### Simple Example
# @compile(backend=None, debug=True)
# def foo(x : Secret[I16]):
# return x * x * x * x * x * x * x * x
### Simple Example
@compile(debug=True) # defaults to scheme="bgv", backend="openfhe", debug=False
def foo(x : Secret[I16], y : Secret[I16]):
sum = x + y
diff = x - y
mul = x * y
expression = sum * diff + mul
deadcode = expression * mul
return expression

foo.setup() # runs keygen/etc
enc_x = foo.encrypt_x(7)
enc_y = foo.encrypt_y(8)
result_enc = foo.eval(enc_x, enc_y)
result = foo.decrypt_result(result_enc)
print(f"Expected result for `foo`: {foo(7,8)}, decrypted result: {result}")


# ### CKKS Example
Expand All @@ -61,21 +52,6 @@


# ### Ciphertext-Plaintext Example
# @compile(debug=True, backend=None, heir_opt_options=["--mlir-to-openfhe-bgv"])
# def baz(x: Secret[I16], y : I16):
# boring = y + y
# ptxt_mul = x * y
# ctxt_mul = x * x
# return ptxt_mul + ctxt_mul + boring

# baz.setup() # runs keygen/etc
# enc_x = baz.encrypt_x(7)
# result_enc = baz.eval(enc_x, 8)
# result = baz.decrypt_result(result_enc)
# print(f"Expected result for `baz`: {baz(7,8)}, decrypted result: {result}")


# ### Ciphertext-Plaintext Example 2
# @compile(debug=True)
# def baz2(x: Secret[I16], y : Secret[I16], z : I16):
# ptxt_mul = x * z
Expand Down Expand Up @@ -104,7 +80,7 @@
# return expression


# ### Heracles Backend Example
# ### Heracles SDK Backend Example
# @compile(backend="heracles", debug=True)
# def foo(x : Secret[I16], y : Secret[I16], z: I16):
# sum = x + z
Expand All @@ -113,15 +89,7 @@
# expression = sum * diff + mul
# return expression

# # foo.setup() # generate metadata such as keys, twiddles, etc? statically pre-generate these and pre-supply them for this
# # enc_x = foo.encrypt_x(5) # (0,0,1,2,134,4648,4874654,4876,4766,1,1665,67,6,44674) (0,4564648,148648646,.....)
# # enc_y = foo.encrypt_y(6) # (0,0,1,2,134,4648,4874654,4876,4766,1,1665,67,6,44674) (0,4564648,148648646,.....)
# # enc_res = foo.eval(enc_x,enc_y) # can we return the result from the func sim back to python here?
# # I just need a list of polynomials as the result, e.g., (0,0,1,2,134,4648,4874654,4876,4766,1,1665,67,6,44674) (0,4564648,148648646,..
# # Most of this won't be possible interactively until we have the data handling parts of the host runtime


# ### Matmul Exampl
# ### Matmul Example
# # secret.secret<tensor<4x4xf32>>
# @compile(scheme='ckks', debug=True)
# def qux(a : Secret[Tensor[4,4,F32]], b : Secret[Tensor[4,4,F32]]):
Expand Down

0 comments on commit d22470b

Please sign in to comment.