Skip to content

Implement tanh and exp in Numpy_intrinsic library #1064

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

Merged
merged 5 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ RUN(NAME elemental_03 LABELS cpython llvm)
RUN(NAME elemental_04 LABELS cpython llvm)
RUN(NAME elemental_05 LABELS cpython llvm)
RUN(NAME elemental_06 LABELS cpython llvm)
RUN(NAME elemental_07 LABELS cpython llvm)
RUN(NAME elemental_08 LABELS cpython llvm)
RUN(NAME test_random LABELS cpython llvm)
RUN(NAME test_os LABELS cpython llvm)
RUN(NAME test_builtin LABELS cpython llvm)
Expand Down
50 changes: 50 additions & 0 deletions integration_tests/elemental_07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from ltypes import i32, f64, f32
from numpy import empty, tanh, reshape, int32, float64, sin, log10

def verify1d(array: f32[:], result: f32[:], size: i32):
i: i32
eps: f32 = 1e-6

for i in range(size):
assert abs(tanh(sin(array[i])) - result[i]) <= eps

def verifynd(array: f64[:, :, :, :], result: f64[:, :, :, :], size1: i32, size2: i32, size3: i32, size4: i32):
i: i32; size: i32;
eps: f64 = 1e-12
shape: i32[1] = empty((1,), dtype=int32)
size = size1 * size2 * size3 * size4
shape[0] = size
array1d: f64[1024] = reshape(array, shape)
result1d: f64[1024] = reshape(result, shape)

for i in range(size):
assert abs((tanh(sin(array1d[i])) + 2)/2 - result1d[i]) <= eps


def elemental_tanh():
i: i32; j: i32; k: i32; l: i32; size: i32;

array1d: f32[80] = empty(80)
tanh1d: f32[80] = empty(80)

for i in range(80):
array1d[i] = i / 10.0

tanh1d = tanh(sin(array1d))
verify1d(array1d, tanh1d, 10)

arraynd: f64[16, 8, 4, 2] = empty((16, 8, 4, 2))
tanhnd: f64[16, 8, 4, 2] = empty((16, 8, 4, 2))
size = 16 * 8 * 4 * 2

for i in range(16):
for j in range(8):
for k in range(4):
for l in range(2):
arraynd[i, j, k, l] = float(i + 2*j + 3*k + 4*k)/size

tanhnd = (tanh(sin(arraynd)) + 2)/2

verifynd(arraynd, tanhnd, 16, 8, 4, 2)

elemental_tanh()
49 changes: 49 additions & 0 deletions integration_tests/elemental_08.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from ltypes import i32, f64, f32
from numpy import empty, reshape, int32, exp

def verify1d(array: f32[:], result: f32[:], size: i32):
i: i32
eps: f32 = 1e-6

for i in range(size):
assert abs(exp(array[i]) - result[i]) <= eps

def verifynd(array: f64[:, :, :, :], result: f64[:, :, :, :], size1: i32, size2: i32, size3: i32, size4: i32):
i: i32; size: i32;
eps: f64 = 1e-12
shape: i32[1] = empty((1,), dtype=int32)
size = size1 * size2 * size3 * size4
shape[0] = size
array1d: f64[1024] = reshape(array, shape)
result1d: f64[1024] = reshape(result, shape)

for i in range(size):
assert abs(( exp(array1d[i]) + exp(array1d[i] / 3) ) / 2 - result1d[i]) <= eps

def elemental_exp():
i: i32; j: i32; k: i32; l: i32; size: i32;

array1d: f32[80] = empty(80)
exp1d: f32[80] = empty(80)

for i in range(80):
array1d[i] = i / 50.0

exp1d = exp(array1d)
verify1d(array1d, exp1d, 80)

arraynd: f64[16, 8, 4, 2] = empty((16, 8, 4, 2))
expnd: f64[16, 8, 4, 2] = empty((16, 8, 4, 2))
size = 32

for i in range(16):
for j in range(8):
for k in range(4):
for l in range(2):
arraynd[i, j, k, l] = float( i / 8 + j / 4 + k / 2 + l ) / size

expnd = (exp(arraynd) + exp(arraynd / 3)) / 2

verifynd(arraynd, expnd, 16, 8, 4, 2)

elemental_exp()
41 changes: 41 additions & 0 deletions src/runtime/lpython_intrinsic_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,44 @@ def _lfortran_sacos(x: f32) -> f32:
@vectorize
def arccos(x: f32) -> f32:
return _lfortran_sacos(x)

########## tanh ##########

@ccall
def _lfortran_dtanh(x: f64) -> f64:
pass

@overload
@vectorize
def tanh(x: f64) -> f64:
return _lfortran_dtanh(x)

@ccall
def _lfortran_stanh(x: f32) -> f32:
pass

@overload
@vectorize
def tanh(x: f32) -> f32:
return _lfortran_stanh(x)

########## exp ##########

@ccall
def _lfortran_dexp(x: f64) -> f64:
pass

@overload
@vectorize
def exp(x: f64) -> f64:
return _lfortran_dexp(x)

@ccall
def _lfortran_sexp(x: f32) -> f32:
pass

@overload
@vectorize
def exp(x: f32) -> f32:
return _lfortran_sexp(x)

2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-39cf894.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_01_decl-39cf894.stdout",
"stdout_hash": "5ecf6039e91c9a83a28aeb6a43fdc4d72d9561a3d8f0542ccdf019a3",
"stdout_hash": "581cac2fa1b5469cf1d1ccb3a91fb97ce22413a9b7a3bcb25b5e634c",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-39cf894.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-array_02_decl-e8f6874.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_02_decl-e8f6874.stdout",
"stdout_hash": "4a38b14377eb0d1bb08af1a3ac8298d87507a01528f564d6c2c77ed0",
"stdout_hash": "8086f9d4d75d8ac5b7819ddc6fa3f7066865eadffe0ad0a77bb3472b",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_02_decl-e8f6874.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-elemental_01-b58df26.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-elemental_01-b58df26.stdout",
"stdout_hash": "90ca8c75d86ea830fb2083b6b5a6813f385c634015b3e890784d6b2e",
"stdout_hash": "572b8546d87ba877caf44a4126b22b93a28cee853c0d5a56e89b8720",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-elemental_01-b58df26.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-test_numpy_03-e600a49.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-test_numpy_03-e600a49.stdout",
"stdout_hash": "47bb384882382f1812820016fd01fa30b8f04c52dd50c731c27b741c",
"stdout_hash": "1db5a5bf604257b691bc633d1e56383142498365050933be5df2a1a5",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-test_numpy_03-e600a49.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-test_numpy_04-ecbb614.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-test_numpy_04-ecbb614.stdout",
"stdout_hash": "52f53b0d09f443f7fcceb3db59b0bcf943f525a8746783bebb6980bb",
"stdout_hash": "92ccd61ff99f3f3187821f5adfaf4e84e3264ea14aa6ad7618e41aa3",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-test_numpy_04-ecbb614.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-vec_01-66ac423.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-vec_01-66ac423.stdout",
"stdout_hash": "27e2d720253a51fcc8522c2c43f572c4fc1d55adf4b351eb407867fe",
"stdout_hash": "4210bc49a30dcc6f81bce09c613c5a6515f2d9bcd311ccba2d8cc895",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-vec_01-66ac423.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/pass_loop_vectorise-vec_01-be9985e.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "pass_loop_vectorise-vec_01-be9985e.stdout",
"stdout_hash": "5e63d66337232a414c2dcc035de82b554cd346fe7d579429deddb397",
"stdout_hash": "90114a0b77cfbb8d9d18385e27a6393ca56659a19440dacf7d0cddca",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/pass_loop_vectorise-vec_01-be9985e.stdout

Large diffs are not rendered by default.