Ask Your Question
1

Problem: Jupyter (Sage) with Numba and pypy

asked 2020-04-28 12:39:37 +0200

SYLA gravatar image

updated 2020-04-30 13:07:36 +0200

Sébastien gravatar image

I install it by:

pip3 install numba

How to use and import numba, pypy in sage.

I get:

ModuleNotFoundError: No module named 'numba'

when I run the following code (from https://numba.pydata.org/numba-doc/la... )

from numba import jit 
import numpy as np 
import time

x = np.arange(100).reshape(10, 10)

@jit(nopython=True) 
def go_fast(a): # Function is compiled and runs in machine code
    trace = 0
    for i in range(a.shape[0]):
        trace += np.tanh(a[i, i])
    return a + trace

start = time.time() 
go_fast(x) 
end = time.time() 
print("Elapsed (with compilation) = %s" % (end - start))

start = time.time()
go_fast(x)
end = time.time()
print("Elapsed (after compilation) = %s" % (end - start))
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
3

answered 2020-04-28 13:46:34 +0200

Sébastien gravatar image

updated 2020-04-30 13:05:43 +0200

To have it available in Sage, you need to use the pip from sage:

sage -pip install numba

EDIT:

It seems to be a bug in jit or in that script which can not add x of type array(int64, 2d, C) with trace which get transformed as an array of type array(float64, 2d, C). Replacing the line

x = np.arange(100).reshape(10, 10)

by

x = np.arange(100, dtype='float64').reshape(10, 10)

fixes it on my side.

edit flag offensive delete link more

Comments

Now I get:

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Untyped global name 'Integer': cannot determine Numba type of <class 'sage.misc.inherit_comparison.InheritComparisonMetaclass'>

File "<ipython-input-2-450bc39e89d0>", line 9:
def go_fast(a): # Function is compiled and runs in machine code
    trace = Integer(0)
    ^
SYLA gravatar imageSYLA ( 2020-04-28 16:40:16 +0200 )edit

Try replacing the two occurrences of 0 by 0r (to avoid Sage to preparse them).

Sébastien gravatar imageSébastien ( 2020-04-28 16:42:58 +0200 )edit

Does not work.

SYLA gravatar imageSYLA ( 2020-04-28 22:21:46 +0200 )edit

Please give more details or I can't help you.

Sébastien gravatar imageSébastien ( 2020-04-29 09:27:27 +0200 )edit

I now have (after the substitution):

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Cannot unify array(int64, 2d, C) and array(float64, 2d, C) for '$56binary_add.2', defined at <ipython-input-2-d69b3052de9b> (12)

File "<ipython-input-2-d69b3052de9b>", line 12:
def go_fast(a): # Function is compiled and runs in machine code
    <source elided>
        trace += np.tanh(a[i, i])
    return a + trace
    ^

[1] During: typing of intrinsic-call at <ipython-input-2-d69b3052de9b> (12)

File "<ipython-input-2-d69b3052de9b>", line 12:
def go_fast(a): # Function is compiled and runs in machine code
    <source elided>
        trace += np.tanh(a[i, i])
    return a + trace
    ^
SYLA gravatar imageSYLA ( 2020-04-29 11:38:26 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-04-28 12:39:37 +0200

Seen: 1,004 times

Last updated: Apr 30 '20