RuntimeError: [ir.cpp:get_attribute@293] Attribute dim not found

Hi,
I am new to Taichi programming and having some problems about using Taichi class with multiple image buffers. I created a short test case that has two image buffers in a Taichi class and I want to use the buffers programmatically but got some error, the code is:


import numpy as np
import taichi as ti
ti.init(arch=ti.cuda)   # use gpu

@ti.data_oriented
class test:
    def __init__(self, kin, sizeX, sizeY):
        self.k = kin
        self.imgA = ti.var(ti.f32)
        self.imgB = ti.var(ti.f32)
        ti.root.dense(ti.ij, (sizeX,sizeY)).place(self.imgA)
        ti.root.dense(ti.ij, (sizeX,sizeY)).place(self.imgB)
       
    @ti.func
    def runker(self,img:ti.f32, x:ti.i32, y:ti.i32):
        return self.k + img[x,y]
    
    @ti.kernel
    def convolve(self,imgSrc:ti.f32,imgDest:ti.f32,sx:ti.i32,sy:ti.i32):
        # the following line caused ti error:
        # RuntimeError: [expr.cpp:snode@81] is<GlobalVariableExpression>()
        for i,j in imgDest:
            imgDest[i,j] = self.runker(imgSrc,i,j)

nr = 0.2
sx = 1024
sy = 1024
testImg = nr * np.random.rand(sx, sy)

x = test(2,sx,sy)
x.convolve(x.imgA,x.imgB,sx,sy)

And the error is:

Traceback (most recent call last):

  File "/home/ling/proj-taichi/test/ti-bug2.py", line 58, in <module>
    x.convolve(x.imgA,x.imgB,sx,sy)

  File "/home/ling/.local/lib/python3.7/site-packages/taichi/lang/kernel.py", line 574, in __call__
    return self._primal(self._kernel_owner, *args, **kwargs)

  File "/home/ling/.local/lib/python3.7/site-packages/taichi/lang/kernel.py", line 469, in __call__
    self.materialize(key=key, args=args, arg_features=arg_features)

  File "/home/ling/.local/lib/python3.7/site-packages/taichi/lang/kernel.py", line 348, in materialize
    taichi_kernel = taichi_kernel.define(taichi_ast_generator)

  File "/home/ling/.local/lib/python3.7/site-packages/taichi/lang/kernel.py", line 345, in taichi_ast_generator
    compiled()

  File "/home/ling/proj-taichi/test/ti-bug2.py", line 40, in convolve
    # the following line caused ti error:

  File "/home/ling/.local/lib/python3.7/site-packages/taichi/lang/impl.py", line 63, in begin_frontend_struct_for
    if group.size() != len(loop_range.shape):

  File "/home/ling/.local/lib/python3.7/site-packages/taichi/lang/expr.py", line 136, in shape
    return self.snode().shape

  File "/home/ling/.local/lib/python3.7/site-packages/taichi/lang/expr.py", line 129, in snode
    return SNode(self.ptr.snode())

RuntimeError: [expr.cpp:snode@81] is<GlobalVariableExpression>()

I am using Taichi version 0.6.22, llvm 10.0.0, commit f5283775, python 3.7.8
Can someone help?

Thanks.

Try this:

def runker(self, img:ti.template(), x:ti.i32, y:ti.i32):
...
def convolve(self,imgSrc:ti.template(),imgDest:ti.f32,sx:ti.i32,sy:ti.i32):

since the img is not a 32-bit float number. It’s a tensor, use ti.template() for tensor.


Btw, I’m improving the error message in:


hope this would helps beginners.

Hi,
I changed my code using ti.template as:


import numpy as np
import taichi as ti
ti.init(arch=ti.cuda)   # use gpu

@ti.data_oriented
class test:
    def __init__(self, kin, sizeX, sizeY):
        self.k = kin
        self.sx = sizeX
        self.sy = sizeY
#        self.imgA = ti.var(ti.f32,shape=(sizeX,sizeY))
#        self.imgB = ti.var(ti.f32,shape=(sizeX,sizeY))
        self.imgA = ti.var(ti.f32)
        self.imgB = ti.var(ti.f32)
        ti.root.dense(ti.ij, (sizeX,sizeY)).place(self.imgA)
        ti.root.dense(ti.ij, (sizeX,sizeY)).place(self.imgB)
       
    @ti.func
    def runker(self, img:ti.template(), x:ti.i32, y:ti.i32):
        return self.k + img[x,y]
    
    @ti.kernel
    def convolve(self, imgSrc:ti.template(), imgDest:ti.f32, sx:ti.i32, sy:ti.i32):
        for i,j in imgDest:
            imgDest[i,j] = self.runker(imgSrc,i,j)
            
    @ti.func
    def runker2(self, pix:ti.var(ti.f32)):
        return self.k + pix
    
    @ti.kernel
    def convolve2(self, imgSrc:ti.template(), imgDest:ti.template()):
        for I in ti.grouped(imgSrc):
            imgDest[I] = self.runker2(imgSrc[I])
            
    def info(self):
        print("test.k = ", self.k)
        print("test.sx = ", self.sx)
        print("test.sy = ", self.sy)
        print("test.imgA shape = ", self.imgA.shape())
        print("test.imgB shape = ", self.imgB.shape)

nr = 0.2
sx = 1024
sy = 1024
testImg = nr * np.random.rand(sx, sy)

x = test(0.2,sx,sy)
x.info()

But this time the error is :

Python 3.7.8 (default, Jul 25 2020, 12:05:48) 
Type "copyright", "credits" or "license" for more information.

IPython 7.16.1 -- An enhanced Interactive Python.

In [1]: runfile('/home/ling/proj-taichi/test/ti-buf2.py', wdir='/home/ling/proj-taichi/test')
[Taichi] mode=release
[Taichi] version 0.6.23, llvm 10.0.0, commit 2bfbca88, python 3.7.8
[Taichi] Starting on arch=cuda
test.k =  0.2
test.sx =  1024
test.sy =  1024
Traceback (most recent call last):

  File "/home/ling/proj-taichi/test/ti-buf2.py", line 56, in <module>
    x.info()

  File "/home/ling/proj-taichi/test/ti-buf2.py", line 47, in info
    print("test.imgA shape = ", self.imgA.shape())

  File "/home/ling/.local/lib/python3.7/site-packages/taichi/lang/expr.py", line 136, in shape
    return self.snode().shape

  File "/home/ling/.local/lib/python3.7/site-packages/taichi/lang/snode.py", line 81, in shape
    impl.get_runtime().materialize()

  File "/home/ling/.local/lib/python3.7/site-packages/taichi/lang/impl.py", line 210, in materialize
    ) is not None, 'Some variable(s) are not placed'

AssertionError: Some variable(s) are not placed


Somehow test.imgA or test.imgB were never placed??

??? Maybe you mean:

def runker2(self, pix:ti.template()):

for passing a tensor?

Thanks, now it works.
The corrected code:


import numpy as np
import taichi as ti
ti.init(arch=ti.cuda)   # use gpu

@ti.data_oriented
class test:
    def __init__(self, kin, sizeX, sizeY):
        self.k = kin
        self.sx = sizeX
        self.sy = sizeY
#        self.imgA = ti.var(ti.f32,shape=(sizeX,sizeY))
#        self.imgB = ti.var(ti.f32,shape=(sizeX,sizeY))
        self.imgA = ti.var(ti.f32)
        self.imgB = ti.var(ti.f32)
        ti.root.dense(ti.ij, (sizeX,sizeY)).place(self.imgA)
        ti.root.dense(ti.ij, (sizeX,sizeY)).place(self.imgB)
       
    @ti.func
    def runker(self, img:ti.template(), x:ti.i32, y:ti.i32):
        return self.k + img[x,y]
    
    # NO GOOD: 
    # RuntimeError: [expr.cpp:taichi::lang::Expr::snode@81] is<GlobalVariableExpression>()
    # @ti.kernel
    # def convolveNoGood(self, imgSrc:ti.template(), imgDest:ti.f32):
    #     for i,j in imgDest:
    #         imgDest[i,j] = self.runker(imgSrc,i,j)
            
    @ti.kernel
    def convolve(self, imgSrc:ti.template(), imgDest:ti.template()):
        for i,j in imgDest:
            imgDest[i,j] = self.runker(imgSrc,i,j)
            
    # the following func cause error, but does not show untill x.info() is called
    # @ti.func
    # def runkerNoGood(self, pix:ti.var(ti.f32)):
    #     return self.k + pix
    @ti.func
    def runker2(self, pix:ti.template()):
        return self.k * pix
    
    @ti.kernel
    def convolve2(self, imgSrc:ti.template(), imgDest:ti.template()):
        for I in ti.grouped(imgSrc):
            imgDest[I] = self.runker2(imgSrc[I])
            
    def info(self):
        print("test.k = ", self.k)
        print("test.sx = ", self.sx)
        print("test.sy = ", self.sy)
        print("test.imgA shape = ", self.imgA.shape)
        print("test.imgB shape = ", self.imgB.shape)

nr = 0.8
sx = 512
sy = 480
testImg = nr * np.random.rand(sx, sy)

x = test(0.2,sx,sy)
x.info()
x.imgA.from_numpy(testImg)

gui = ti.GUI("Ti class test",x.imgA.shape)
gui.set_image(x.imgA)
gui.show()


x.convolve(x.imgA, x.imgB)
gui.set_image(x.imgB)
gui.show()

x.convolve2(x.imgA, x.imgB)
gui.set_image(x.imgB)
gui.show()