apache / tvm

Open deep learning compiler stack for cpu, gpu and specialized accelerators
https://tvm.apache.org/
Apache License 2.0
11.58k stars 3.43k forks source link

[Bug] different results when conducting the attribute and ifthenelse statement #14729

Open KuiliangL opened 1 year ago

KuiliangL commented 1 year ago

Hello, I’m studying TVM and its low-level operators recently. I tried to construct some test files but I was puzzled by the results of some of the files. When I built different statements, different execution results have occurred. The reason that TVM throws the exception is that TVM checks the uint’s value and finds the uint expression is negative. But I still have two questions:

  1. Why using different Attribute type key can lead to different execution results?(see Primfunc with body1 and body2)

  2. Why using IfThenElse statement and using different statements as body can lead to different execution results?

The attribute statement with pragma_auto_unroll_max_step will execute normally (see PrimFunc with body4). But the LetStmt statement throws the exception(see PrimFunc with body6). I don't know what causes this difference in execution and whether TVM checks the branch’s expression if the branch is not executed. I have noticed that this problem maybe occurs in the specific version. I’d be appreciated if anyone can help me.

Actual behavior

The PrimFunc with body1,body3 and body6 will throw the exception and the PrimFunc with body2,body4 and body5 will execute normally. The error messages are: image

Environment

TVM version 0.10.dev0, git commit ee319d9d23c80091da9c4fb764b1e6d49d462714

Steps to reproduce

import tvm
from tvm import tir

var1=tir.Var('v1','int32')
var2=tir.Var('v2','uint32')
a=tir.IntImm('uint32',73829502)
b=tir.IntImm('uint32',1726448906)
uint=tir.Sub(a,b)
itevars=tir.IterVar([-16, 0], var1, 4, 'vthread.z')
body1=tir.AttrStmt(itevars,'pragma_auto_unroll_max_step',uint,tir.Evaluate(1.6))
body2=tir.AttrStmt(itevars,'thread_extent',uint,tir.Evaluate(1.6))
body3=tir.LetStmt(var2,uint,tir.Evaluate(2.1))
body4 = tir.IfThenElse(tir.LT(3,1),body1,tir.Evaluate(0))
body5=  tir.IfThenElse(tir.LT(3,1),body2,tir.Evaluate(0))
body6= tir.IfThenElse(tir.LT(3,1),body3,tir.Evaluate(0))
func1=tir.PrimFunc([],body=body1)
func2=tir.PrimFunc([],body=body2)
func3=tir.PrimFunc([],body=body3)
func4=tir.PrimFunc([],body=body4)
func5=tir.PrimFunc([],body=body5)
func6=tir.PrimFunc([],body=body6)
tvm.build(func4)

Triage

jikechao commented 1 year ago

Hi @blufissure. I cannot reproduce this crash in the latest TVM (i.e., 0.13.dev0). I guess this bug has been fixed.

KuiliangL commented 1 year ago

Hi @blufissure. I cannot reproduce this crash in the latest TVM (i.e., 0.13.dev0). I guess this bug has been fixed.

In the latest version, TVM will not check the negative unsigned integer, so this problem will not occur in the latest version.

KuiliangL commented 1 year ago

Here is another example

import tvm
from tvm import tir
a=tir.IntImm('uint32',73829502)
zero1=tir.IntImm('uint32',0)
div1=tir.Div(a,zero1)
v1=tir.Var('v1','int32')
itevars=tir.IterVar([-16, 0], v1, 4, 'vthread.z')
body1 = tir.AttrStmt(itevars,'rolling_buffer_scope',div1,tir.Evaluate(1.6))
body2 = tir.IfThenElse(tir.LT(3,1),body1,tir.Evaluate(0))
func=tir.PrimFunc([],body=body2)
tvm.build(func)