ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 29 Apr 2020 13:02:05 +0200Problem: Jupyter (Sage) with Numba and pypyhttps://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/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/latest/user/5minguide.html )
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))Tue, 28 Apr 2020 12:39:37 +0200https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/Answer by Sébastien for <p>I install it by:</p>
<pre><code>pip3 install numba
</code></pre>
<p>How to use and import numba, pypy in sage.</p>
<p>I get:</p>
<pre><code>ModuleNotFoundError: No module named 'numba'
</code></pre>
<p>when I run the following code
(from <a href="https://numba.pydata.org/numba-doc/latest/user/5minguide.html">https://numba.pydata.org/numba-doc/la...</a> )</p>
<pre><code>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))
</code></pre>
https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?answer=51100#post-id-51100To 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.Tue, 28 Apr 2020 13:46:34 +0200https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?answer=51100#post-id-51100Comment by SYLA for <p>To have it available in Sage, you need to use the pip from sage:</p>
<pre><code>sage -pip install numba
</code></pre>
<p>EDIT:</p>
<p>It seems to be a bug in jit or in that script which can not add x of type <code>array(int64, 2d, C)</code> with trace which get transformed as an array of type <code>array(float64, 2d, C)</code>. Replacing the line</p>
<pre><code>x = np.arange(100).reshape(10, 10)
</code></pre>
<p>by</p>
<pre><code>x = np.arange(100, dtype='float64').reshape(10, 10)
</code></pre>
<p>fixes it on my side.</p>
https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?comment=51109#post-id-51109Now 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)
^Tue, 28 Apr 2020 16:40:16 +0200https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?comment=51109#post-id-51109Comment by Sébastien for <p>To have it available in Sage, you need to use the pip from sage:</p>
<pre><code>sage -pip install numba
</code></pre>
<p>EDIT:</p>
<p>It seems to be a bug in jit or in that script which can not add x of type <code>array(int64, 2d, C)</code> with trace which get transformed as an array of type <code>array(float64, 2d, C)</code>. Replacing the line</p>
<pre><code>x = np.arange(100).reshape(10, 10)
</code></pre>
<p>by</p>
<pre><code>x = np.arange(100, dtype='float64').reshape(10, 10)
</code></pre>
<p>fixes it on my side.</p>
https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?comment=51110#post-id-51110Try replacing the two occurrences of `0` by `0r` (to avoid Sage to preparse them).Tue, 28 Apr 2020 16:42:58 +0200https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?comment=51110#post-id-51110Comment by SYLA for <p>To have it available in Sage, you need to use the pip from sage:</p>
<pre><code>sage -pip install numba
</code></pre>
<p>EDIT:</p>
<p>It seems to be a bug in jit or in that script which can not add x of type <code>array(int64, 2d, C)</code> with trace which get transformed as an array of type <code>array(float64, 2d, C)</code>. Replacing the line</p>
<pre><code>x = np.arange(100).reshape(10, 10)
</code></pre>
<p>by</p>
<pre><code>x = np.arange(100, dtype='float64').reshape(10, 10)
</code></pre>
<p>fixes it on my side.</p>
https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?comment=51125#post-id-51125Does not work.Tue, 28 Apr 2020 22:21:46 +0200https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?comment=51125#post-id-51125Comment by Sébastien for <p>To have it available in Sage, you need to use the pip from sage:</p>
<pre><code>sage -pip install numba
</code></pre>
<p>EDIT:</p>
<p>It seems to be a bug in jit or in that script which can not add x of type <code>array(int64, 2d, C)</code> with trace which get transformed as an array of type <code>array(float64, 2d, C)</code>. Replacing the line</p>
<pre><code>x = np.arange(100).reshape(10, 10)
</code></pre>
<p>by</p>
<pre><code>x = np.arange(100, dtype='float64').reshape(10, 10)
</code></pre>
<p>fixes it on my side.</p>
https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?comment=51134#post-id-51134Please give more details or I can't help you.Wed, 29 Apr 2020 09:27:27 +0200https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?comment=51134#post-id-51134Comment by SYLA for <p>To have it available in Sage, you need to use the pip from sage:</p>
<pre><code>sage -pip install numba
</code></pre>
<p>EDIT:</p>
<p>It seems to be a bug in jit or in that script which can not add x of type <code>array(int64, 2d, C)</code> with trace which get transformed as an array of type <code>array(float64, 2d, C)</code>. Replacing the line</p>
<pre><code>x = np.arange(100).reshape(10, 10)
</code></pre>
<p>by</p>
<pre><code>x = np.arange(100, dtype='float64').reshape(10, 10)
</code></pre>
<p>fixes it on my side.</p>
https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?comment=51137#post-id-51137I 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
^Wed, 29 Apr 2020 11:38:26 +0200https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?comment=51137#post-id-51137Comment by SYLA for <p>To have it available in Sage, you need to use the pip from sage:</p>
<pre><code>sage -pip install numba
</code></pre>
<p>EDIT:</p>
<p>It seems to be a bug in jit or in that script which can not add x of type <code>array(int64, 2d, C)</code> with trace which get transformed as an array of type <code>array(float64, 2d, C)</code>. Replacing the line</p>
<pre><code>x = np.arange(100).reshape(10, 10)
</code></pre>
<p>by</p>
<pre><code>x = np.arange(100, dtype='float64').reshape(10, 10)
</code></pre>
<p>fixes it on my side.</p>
https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?comment=51138#post-id-51138What's about PyPy?Wed, 29 Apr 2020 11:39:31 +0200https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?comment=51138#post-id-51138Comment by Sébastien for <p>To have it available in Sage, you need to use the pip from sage:</p>
<pre><code>sage -pip install numba
</code></pre>
<p>EDIT:</p>
<p>It seems to be a bug in jit or in that script which can not add x of type <code>array(int64, 2d, C)</code> with trace which get transformed as an array of type <code>array(float64, 2d, C)</code>. Replacing the line</p>
<pre><code>x = np.arange(100).reshape(10, 10)
</code></pre>
<p>by</p>
<pre><code>x = np.arange(100, dtype='float64').reshape(10, 10)
</code></pre>
<p>fixes it on my side.</p>
https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?comment=51140#post-id-51140It seems to be a bug in jit 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)`. Try replacing the line
x = np.arange(100).reshape(10, 10)
by
x = np.arange(100, dtype='float64').reshape(10, 10)Wed, 29 Apr 2020 13:02:05 +0200https://ask.sagemath.org/question/51098/problem-jupyter-sage-with-numba-and-pypy/?comment=51140#post-id-51140