Skip to content

Commit

Permalink
Non-linear functions (#53)
Browse files Browse the repository at this point in the history
* feat: adding project example with non-linear functions

* feat: integrate non-linear functions into nada-numpy library

* fix: pylint and mypy rules

* feat: add test programs under test_all.py
  • Loading branch information
manel1874 authored Aug 26, 2024
1 parent 8dbf08c commit dd112a0
Show file tree
Hide file tree
Showing 11 changed files with 3,283 additions and 52 deletions.
664 changes: 662 additions & 2 deletions nada_numpy/array.py

Large diffs are not rendered by default.

543 changes: 543 additions & 0 deletions nada_numpy/funcs.py

Large diffs are not rendered by default.

1,754 changes: 1,705 additions & 49 deletions nada_numpy/types.py

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions tests/nada-tests/nada-project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,16 @@ prime_size = 128

[[programs]]
path = "src/type_guardrails.py"
prime_size = 128

[[programs]]
path = "src/fxpmath_funcs.py"
prime_size = 128

[[programs]]
path = "src/fxpmath_methods.py"
prime_size = 128

[[programs]]
path = "src/fxpmath_arrays.py"
prime_size = 128
64 changes: 64 additions & 0 deletions tests/nada-tests/src/fxpmath_arrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import numpy as np
from nada_dsl import *

import nada_numpy as na


def nada_main():

parties = na.parties(2)

# We use na.SecretRational to create a secret rational number for party 0
a = na.secret_rational("my_input_0", parties[0])

c = na.NadaArray(np.array([a, na.rational(1.5)]))

result_sign = c.sign()
result_abs = c.abs()
result_exp = c.exp()
result_log = c.log()
result_rec_NR = c.reciprocal(method="NR")
result_rec_log = c.reciprocal(method="log")
result_isqrt = c.inv_sqrt()
result_sqrt = c.sqrt()
result_sin = c.sin()
result_cos = c.cos()
result_tan = c.tan()
result_tanh = c.tanh()
result_tanh_che = c.tanh(method="chebyshev")
result_tanh_motz = c.tanh(method="motzkin")
result_sig = c.sigmoid()
result_sig_che = c.sigmoid(method="chebyshev")
result_sig_motz = c.sigmoid(method="motzkin")
result_gelu = c.gelu()
result_gelu_motz = c.gelu(method="motzkin")
result_silu = c.silu()
result_silu_che = c.silu(method_sigmoid="chebyshev")
result_silu_motz = c.silu(method_sigmoid="motzkin")

final_result = (
result_sign.output(parties[1], "result_sign")
+ result_abs.output(parties[1], "result_abs")
+ result_exp.output(parties[1], "result_exp")
+ result_log.output(parties[1], "result_log")
+ result_rec_NR.output(parties[1], "result_rec_NR")
+ result_rec_log.output(parties[1], "result_rec_log")
+ result_isqrt.output(parties[1], "result_isqrt")
+ result_sqrt.output(parties[1], "result_sqrt")
+ result_sin.output(parties[1], "result_sin")
+ result_cos.output(parties[1], "result_cos")
+ result_tan.output(parties[1], "result_tan")
+ result_tanh.output(parties[1], "result_tanh")
+ result_tanh_che.output(parties[1], "result_tanh_che")
+ result_tanh_motz.output(parties[1], "result_tanh_motz")
+ result_sig.output(parties[1], "result_sig")
+ result_sig_che.output(parties[1], "result_sig_che")
+ result_sig_motz.output(parties[1], "result_sig_motz")
+ result_gelu.output(parties[1], "result_gelu")
+ result_gelu_motz.output(parties[1], "result_gelu_motz")
+ result_silu.output(parties[1], "result_silu")
+ result_silu_che.output(parties[1], "result_silu_che")
+ result_silu_motz.output(parties[1], "result_silu_motz")
)

return final_result
55 changes: 55 additions & 0 deletions tests/nada-tests/src/fxpmath_funcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from nada_dsl import *

import nada_numpy as na


def nada_main():

parties = na.parties(2)

# We use na.SecretRational to create a secret rational number for party 0
a = na.secret_rational("my_input_0", parties[0])

result_exp = na.exp(a * na.rational(2))
result_log = na.log(a * na.rational(100))
result_rec_NR = na.reciprocal(a * na.rational(2), method="NR")
result_rec_log = na.reciprocal(a * na.rational(4), method="log")
result_isqrt = na.inv_sqrt(a * na.rational(210))
result_sqrt = na.sqrt(a * na.rational(16))
result_sin = na.sin(a * na.rational(2.1))
result_cos = na.cos(a * na.rational(2.1))
result_tan = na.tan(a * na.rational(4.8))
result_tanh = na.tanh(a * na.rational(1.3))
result_tanh_che = na.tanh(a * na.rational(0.3), method="chebyshev")
result_tanh_motz = na.tanh(a * na.rational(0.4), method="motzkin")
result_sig = na.sigmoid(a * na.rational(0.1))
result_sig_che = na.sigmoid(a * na.rational(-0.1), method="chebyshev")
result_sig_motz = na.sigmoid(a * na.rational(10), method="motzkin")
result_gelu = na.gelu(a * na.rational(-13))
result_gelu_motz = na.gelu(a * na.rational(-13), method="motzkin")
result_silu = na.silu(a * na.rational(10))
result_silu_che = na.silu(a * na.rational(-10), method_sigmoid="chebyshev")
result_silu_motz = na.silu(a * na.rational(0), method_sigmoid="motzkin")

return [
Output(result_exp.value, "result_exp", parties[1]),
Output(result_log.value, "result_log", parties[1]),
Output(result_rec_NR.value, "result_rec_NR", parties[1]),
Output(result_rec_log.value, "result_rec_log", parties[1]),
Output(result_isqrt.value, "result_isqrt", parties[1]),
Output(result_sqrt.value, "result_sqrt", parties[1]),
Output(result_sin.value, "result_sin", parties[1]),
Output(result_cos.value, "result_cos", parties[1]),
Output(result_tan.value, "result_tan", parties[1]),
Output(result_tanh.value, "result_tanh", parties[1]),
Output(result_tanh_che.value, "result_tanh_che", parties[1]),
Output(result_tanh_motz.value, "result_tanh_motz", parties[1]),
Output(result_sig.value, "result_sig", parties[1]),
Output(result_sig_che.value, "result_sig_che", parties[1]),
Output(result_sig_motz.value, "result_sig_motz", parties[1]),
Output(result_gelu.value, "result_gelu", parties[1]),
Output(result_gelu_motz.value, "result_gelu_motz", parties[1]),
Output(result_silu.value, "result_silu", parties[1]),
Output(result_silu_che.value, "result_silu_che", parties[1]),
Output(result_silu_motz.value, "result_silu_motz", parties[1]),
]
55 changes: 55 additions & 0 deletions tests/nada-tests/src/fxpmath_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from nada_dsl import *

import nada_numpy as na


def nada_main():

parties = na.parties(2)

# We use na.SecretRational to create a secret rational number for party 0
a = na.secret_rational("my_input_0", parties[0])

result_exp = (a * na.rational(2)).exp()
result_log = (a * na.rational(100)).log()
result_rec_NR = (a * na.rational(2)).reciprocal(method="NR")
result_rec_log = (a * na.rational(4)).reciprocal(method="log")
result_isqrt = (a * na.rational(210)).inv_sqrt()
result_sqrt = (a * na.rational(16)).sqrt()
result_sin = (a * na.rational(2.1)).sin()
result_cos = (a * na.rational(2.1)).cos()
result_tan = (a * na.rational(4.8)).tan()
result_tanh = (a * na.rational(1.3)).tanh()
result_tanh_che = (a * na.rational(0.3)).tanh(method="chebyshev")
result_tanh_motz = (a * na.rational(0.4)).tanh(method="motzkin")
result_sig = (a * na.rational(0.1)).sigmoid()
result_sig_che = (a * na.rational(-0.1)).sigmoid(method="chebyshev")
result_sig_motz = (a * na.rational(10)).sigmoid(method="motzkin")
result_gelu = (a * na.rational(-13)).gelu()
result_gelu_motz = (a * na.rational(-13)).gelu(method="motzkin")
result_silu = (a * na.rational(10)).silu()
result_silu_che = (a * na.rational(-10)).silu(method_sigmoid="chebyshev")
result_silu_motz = (a * na.rational(0)).silu(method_sigmoid="motzkin")

return [
Output(result_exp.value, "result_exp", parties[1]),
Output(result_log.value, "result_log", parties[1]),
Output(result_rec_NR.value, "result_rec_NR", parties[1]),
Output(result_rec_log.value, "result_rec_log", parties[1]),
Output(result_isqrt.value, "result_isqrt", parties[1]),
Output(result_sqrt.value, "result_sqrt", parties[1]),
Output(result_sin.value, "result_sin", parties[1]),
Output(result_cos.value, "result_cos", parties[1]),
Output(result_tan.value, "result_tan", parties[1]),
Output(result_tanh.value, "result_tanh", parties[1]),
Output(result_tanh_che.value, "result_tanh_che", parties[1]),
Output(result_tanh_motz.value, "result_tanh_motz", parties[1]),
Output(result_sig.value, "result_sig", parties[1]),
Output(result_sig_che.value, "result_sig_che", parties[1]),
Output(result_sig_motz.value, "result_sig_motz", parties[1]),
Output(result_gelu.value, "result_gelu", parties[1]),
Output(result_gelu_motz.value, "result_gelu_motz", parties[1]),
Output(result_silu.value, "result_silu", parties[1]),
Output(result_silu_che.value, "result_silu_che", parties[1]),
Output(result_silu_motz.value, "result_silu_motz", parties[1]),
]
95 changes: 95 additions & 0 deletions tests/nada-tests/tests/fxpmath_arrays.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
program: fxpmath_arrays
inputs:
my_input_0:
SecretInteger: "3"
expected_outputs:
result_sig_0:
SecretInteger: "32711"
result_sqrt_1:
Integer: "80265"
result_sig_motz_1:
Integer: "53477"
result_silu_0:
SecretInteger: "1"
result_gelu_0:
SecretInteger: "1"
result_tan_1:
Integer: "888645"
result_tanh_1:
Integer: "59434"
result_tan_0:
SecretInteger: "0"
result_exp_0:
SecretInteger: "65536"
result_sig_1:
Integer: "53644"
result_silu_motz_1:
Integer: "80215"
result_gelu_motz_1:
Integer: "91629"
result_sig_che_1:
Integer: "53580"
result_log_0:
SecretInteger: "-614828"
result_sqrt_0:
SecretInteger: "45"
result_silu_che_0:
SecretInteger: "1"
result_rec_NR_1:
Integer: "43692"
result_tanh_che_1:
Integer: "65536"
result_gelu_motz_0:
SecretInteger: "103"
result_exp_1:
Integer: "292119"
result_isqrt_1:
Integer: "53510"
result_cos_1:
Integer: "4846"
result_tanh_motz_1:
Integer: "59394"
result_cos_0:
SecretInteger: "65536"
result_sig_che_0:
SecretInteger: "32768"
result_rec_log_0:
SecretInteger: "65040192"
result_sig_motz_0:
SecretInteger: "32686"
result_log_1:
Integer: "26359"
result_tanh_0:
SecretInteger: "-114"
result_sin_1:
Integer: "65710"
result_gelu_1:
Integer: "91839"
result_silu_che_1:
Integer: "80370"
result_tanh_motz_0:
SecretInteger: "-160"
result_tanh_che_0:
SecretInteger: "2"
result_rec_NR_0:
SecretInteger: "451510139"
result_rec_log_1:
Integer: "34575"
result_silu_1:
Integer: "80466"
result_isqrt_0:
SecretInteger: "989647"
result_silu_motz_0:
SecretInteger: "1"
result_sin_0:
SecretInteger: "0"
result_sign_1:
Integer: "65536"
result_sign_0:
SecretInteger: "65536"
result_abs_1:
Integer: "98304"
result_abs_0:
SecretInteger: "3"

44 changes: 44 additions & 0 deletions tests/nada-tests/tests/fxpmath_funcs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
program: fxpmath_funcs
inputs:
my_input_0:
SecretInteger: "65536"
expected_outputs:
result_gelu:
SecretInteger: "-13"
result_cos:
SecretInteger: "-32566"
result_isqrt:
SecretInteger: "-2341"
result_gelu_motz:
SecretInteger: "0"
result_silu:
SecretInteger: "655340"
result_sin:
SecretInteger: "57328"
result_tanh_che:
SecretInteger: "19089"
result_tanh_motz:
SecretInteger: "24917"
result_silu_che:
SecretInteger: "0"
result_sig_motz:
SecretInteger: "65536"
result_tanh:
SecretInteger: "56624"
result_rec_log:
SecretInteger: "15552"
result_sig_che:
SecretInteger: "31130"
result_sig:
SecretInteger: "34401"
result_tan:
SecretInteger: "-922765"
result_sqrt:
SecretInteger: "262128"
result_log:
SecretInteger: "298843"
result_exp:
SecretInteger: "480249"
result_rec_NR:
SecretInteger: "32768"
44 changes: 44 additions & 0 deletions tests/nada-tests/tests/fxpmath_methods.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
program: fxpmath_methods
inputs:
my_input_0:
SecretInteger: "65536"
expected_outputs:
result_gelu:
SecretInteger: "-13"
result_cos:
SecretInteger: "-32566"
result_isqrt:
SecretInteger: "-2341"
result_gelu_motz:
SecretInteger: "0"
result_silu:
SecretInteger: "655340"
result_sin:
SecretInteger: "57328"
result_tanh_che:
SecretInteger: "19089"
result_tanh_motz:
SecretInteger: "24917"
result_silu_che:
SecretInteger: "0"
result_sig_motz:
SecretInteger: "65536"
result_tanh:
SecretInteger: "56624"
result_rec_log:
SecretInteger: "15552"
result_sig_che:
SecretInteger: "31130"
result_sig:
SecretInteger: "34401"
result_tan:
SecretInteger: "-922765"
result_sqrt:
SecretInteger: "262128"
result_log:
SecretInteger: "298843"
result_exp:
SecretInteger: "480249"
result_rec_NR:
SecretInteger: "32768"
5 changes: 4 additions & 1 deletion tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@
# Not supported yet
# "unsigned_matrix_inverse",
"private_inverse",
# "unsigned_matrix_inverse_2"
# "unsigned_matrix_inverse_2",
"fxpmath_funcs",
"fxpmath_arrays",
"fxpmath_methods"
]

EXAMPLES = [
Expand Down

0 comments on commit dd112a0

Please sign in to comment.