taichi-dev / taichi

Productive, portable, and performant GPU programming in Python.
https://taichi-lang.org
Apache License 2.0
25.36k stars 2.27k forks source link

'NoneType' object has no attribute 'num_active_indices' with basic from_numpy usage #1958

Open samuela opened 3 years ago

samuela commented 3 years ago

Describe the bug from_numpy fails in simple case where a vector is placed on ti.root.

To Reproduce

import numpy as np
import taichi as ti

real = ti.f32
ti.init(default_fp=real)
x = ti.Vector.field(2, dtype=real)

@ti.layout
def place():
    ti.root.place(x)

if __name__ == "__main__":
    x.from_numpy(np.zeros(2))

Log/Screenshots

(venv) samuela@n64:~/dev/research/julia/odecontrol$ python difftaichi/electric/difftaichi_place_bug.py 
[Taichi] mode=release
[Taichi] preparing sandbox at /tmp/taichi-sv7adn87
[Taichi] version 0.6.37, llvm 10.0.0, commit e0ac1d86, linux, python 3.6.9
[Taichi] Starting on arch=x64
[Taichi] materializing...
Traceback (most recent call last):
  File "difftaichi/electric/difftaichi_place_bug.py", line 13, in <module>
    x.from_numpy(np.zeros(2))
  File "/home/samuela/dev/difftaichi/venv/lib/python3.6/site-packages/taichi/lang/util.py", line 212, in wrapped
    return func(*args, **kwargs)
  File "/home/samuela/dev/difftaichi/venv/lib/python3.6/site-packages/taichi/lang/matrix.py", line 709, in from_numpy
    if len(ndarray.shape) == len(self.loop_range().shape) + 1:
  File "/home/samuela/dev/difftaichi/venv/lib/python3.6/site-packages/taichi/lang/expr.py", line 155, in shape
    return self.snode.shape
  File "/home/samuela/dev/difftaichi/venv/lib/python3.6/site-packages/taichi/lang/snode.py", line 82, in shape
    dim = self.ptr.num_active_indices()
AttributeError: 'NoneType' object has no attribute 'num_active_indices'

Additional comments

(venv) samuela@n64:~/dev/research/julia/odecontrol$ ti diagnose
[Taichi] mode=release
[Taichi] preparing sandbox at /tmp/taichi-j_p0m2qz
[Taichi] version 0.6.37, llvm 10.0.0, commit e0ac1d86, linux, python 3.6.9

*******************************************
**      Taichi Programming Language      **
*******************************************

Docs:   https://taichi.rtfd.io/en/stable
GitHub: https://github.com/taichi-dev/taichi
Forum:  https://forum.taichi.graphics

Taichi system diagnose:

python: 3.6.9 (default, Jul 17 2020, 12:50:27) 
[GCC 8.4.0]
system: linux
executable: /home/samuela/dev/difftaichi/venv/bin/python
platform: Linux-5.3.0-51-generic-x86_64-with-Ubuntu-18.04-bionic
architecture: 64bit ELF
uname: Linux n64 5.3.0-51-generic #44~18.04.2-Ubuntu SMP Thu Apr 23 14:27:18 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

locale: en_US.UTF-8
PATH: /home/samuela/dev/difftaichi/venv/bin:/home/samuela/.vscode-server/bin/2af051012b66169dde0c4dfae3f5ef48f787ff69/bin:/home/samuela/julia-1.5.1/bin:/home/samuela/.local/bin:/home/samuela/.vscode-server/bin/2af051012b66169dde0c4dfae3f5ef48f787ff69/bin:/home/samuela/julia-1.5.1/bin:/home/samuela/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:        18.04
Codename:       bionic

TAICHI_REPO_DIR=

import: <module 'taichi' from '/home/samuela/dev/difftaichi/venv/lib/python3.6/site-packages/taichi/__init__.py'>

cc: True
cpu: True
metal: False
opengl: False
cuda: True

`glewinfo` not available: [Errno 2] No such file or directory: 'glewinfo': 'glewinfo'

Wed Oct 14 14:32:00 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 435.21       Driver Version: 435.21       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Quadro K4000        Off  | 00000000:49:00.0 Off |                  N/A |
| 30%   29C    P8    10W /  87W |     26MiB /  3015MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      2717      G   /usr/lib/xorg/Xorg                             8MiB |
|    0      2749      G   /usr/bin/gnome-shell                           3MiB |
+-----------------------------------------------------------------------------+

[Taichi] mode=release
[Taichi] preparing sandbox at /tmp/taichi-9jgndeao
[Taichi] version 0.6.37, llvm 10.0.0, commit e0ac1d86, linux, python 3.6.9

[Taichi] mode=release
[Taichi] preparing sandbox at /tmp/taichi-eog1e6fq
[Taichi] version 0.6.37, llvm 10.0.0, commit e0ac1d86, linux, python 3.6.9
[Taichi] Starting on arch=x64

[W 10/14/20 14:32:00.748] [__init__.py:adaptive_arch_select@552] Arch=[Arch.opengl] is not supported, falling back to CPU
[Taichi] mode=release
[Taichi] preparing sandbox at /tmp/taichi-o5qg7ve5
[Taichi] version 0.6.37, llvm 10.0.0, commit e0ac1d86, linux, python 3.6.9
[Taichi] Starting on arch=x64

[Taichi] mode=release
[Taichi] preparing sandbox at /tmp/taichi-8dx1u6a6
[Taichi] version 0.6.37, llvm 10.0.0, commit e0ac1d86, linux, python 3.6.9
[Taichi] Starting on arch=cuda

[Taichi] mode=release
[Taichi] preparing sandbox at /tmp/taichi-d9rv8u3q
[Taichi] version 0.6.37, llvm 10.0.0, commit e0ac1d86, linux, python 3.6.9

*******************************************
**      Taichi Programming Language      **
*******************************************

Docs:   https://taichi.rtfd.io/en/stable
GitHub: https://github.com/taichi-dev/taichi
Forum:  https://forum.taichi.graphics

Running example minimal ...
[Taichi] Starting on arch=x64
[Taichi] materializing...
>>> Running time: 0.15s
42

Consider attaching this log when maintainers ask about system information.
>>> Running time: 2.98s
samuela commented 3 years ago

just confirmed on the latest at the time of writing:

[Taichi] version 0.6.40, llvm 10.0.0, commit 45f52a90, linux, python 3.6.9
samuela commented 3 years ago

Same thing using ti.Vector instead of ti.Vector.field. What's the difference between them btw?

import numpy as np
import taichi as ti

real = ti.f32
ti.init(default_fp=real)
x = ti.Vector(2, dt=real)

@ti.layout
def place():
    ti.root.place(x)

if __name__ == "__main__":
    x.from_numpy(np.zeros(2))
samuela commented 3 years ago

This also doesn't seem to work:

import numpy as np
import taichi as ti

real = ti.f32
ti.init(default_fp=real)
x = ti.Vector(2, dt=real)

@ti.layout
def place():
    ti.root.dense(ti.i, 3).place(x)

if __name__ == "__main__":
    x.from_numpy(np.zeros((3, 2), dtype=np.float32))
samuela commented 3 years ago

Ahhh, but this works:

import numpy as np
import taichi as ti

real = ti.f32
ti.init(default_fp=real)
x = ti.Vector.field(2, dtype=real)

@ti.layout
def place():
    ti.root.place(x)

@ti.kernel
def noop():
    pass

if __name__ == "__main__":
    noop()
    x.from_numpy(np.zeros((2, ), dtype=np.float32))
archibate commented 3 years ago

Hi, @ti.layout is deprecated now. We should directly invoke ti.root.place without that decorator in latest version:

import numpy as np
import taichi as ti

real = ti.f32
ti.init(default_fp=real)
x = ti.Vector.field(2, dtype=real)

ti.root.place(x)

if __name__ == "__main__":
    x.from_numpy(np.zeros(2))

Same thing using ti.Vector instead of ti.Vector.field. What's the difference between them btw?

The first is deprecated for #1500, you should get an warning when deprecated API like ti.Vector and ti.layout is used.

samuela commented 3 years ago

@archibate Ah, thanks for clarifying! I was working off of https://github.com/yuanming-hu/difftaichi/blob/master/examples/electric.py#L49-L61 but I guess that code is out of date now.

If I just want to place something on ti.root, is it necessary to place it at all? Ie, the "Hello world" code (https://taichi.readthedocs.io/en/stable/hello.html) doesn't seem to do any places at all.

archibate commented 3 years ago

If I just want to place something on ti.root, is it necessary to place it at all? Ie, the "Hello world" code (https://taichi.readthedocs.io/en/stable/hello.html) doesn't seem to do any places at all.

Yes, for dense layouts, we can place them by shape= argument:

x = ti.Vector.field(2, dtype=real, shape=())  # same as ti.root.place(x)
y = ti.Vector.field(2, dtype=real, shape=3)  # same as ti.root.dense(ti.i, 3).place(y)
z = ti.Vector.field(2, dtype=real, shape=(3, 4))  # same as ti.root.dense(ti.i, (3, 4)).place(z)