Ask Your Question
1

Problem: Jupyter (Sage) with Numba and pypy

asked 2020-04-28 05:39:37 -0600

SYLA gravatar image

updated 2020-04-30 06:07:36 -0600

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 06:46:34 -0600

Sébastien gravatar image

updated 2020-04-30 06:05:43 -0600

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 09:40:16 -0600 )edit

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

Sébastien gravatar imageSébastien ( 2020-04-28 09:42:58 -0600 )edit

Does not work.

SYLA gravatar imageSYLA ( 2020-04-28 15:21:46 -0600 )edit

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

Sébastien gravatar imageSébastien ( 2020-04-29 02:27:27 -0600 )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 04:38:26 -0600 )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 05:39:37 -0600

Seen: 193 times

Last updated: Apr 30