Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tvm
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cld
ml
tvm
Commits
f01cc0e6
Commit
f01cc0e6
authored
6 years ago
by
Sergei Grechanik
Committed by
Tianqi Chen
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[TVM] Eagerer const folding for logic ops (#1907)
parent
b4946e77
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/lang/ir_operator.cc
+14
-8
14 additions, 8 deletions
src/lang/ir_operator.cc
tests/python/unittest/test_lang_operator.py
+38
-0
38 additions, 0 deletions
tests/python/unittest/test_lang_operator.py
with
52 additions
and
8 deletions
src/lang/ir_operator.cc
+
14
−
8
View file @
f01cc0e6
...
...
@@ -310,20 +310,26 @@ Expr operator!=(Expr a, Expr b) {
Expr
operator
&&
(
Expr
a
,
Expr
b
)
{
using
ir
::
UIntImm
;
const
UIntImm
*
pa
=
a
.
as
<
UIntImm
>
();
const
UIntImm
*
pb
=
b
.
as
<
UIntImm
>
();
if
(
pa
&&
pb
)
{
return
UIntImm
::
make
(
UInt
(
1
),
pa
->
value
&&
pb
->
value
);
if
(
a
.
type
().
is_bool
()
&&
b
.
type
().
is_bool
())
{
const
UIntImm
*
pa
=
a
.
as
<
UIntImm
>
();
const
UIntImm
*
pb
=
b
.
as
<
UIntImm
>
();
if
(
pa
&&
pa
->
value
)
return
b
;
if
(
pa
&&
!
pa
->
value
)
return
a
;
if
(
pb
&&
pb
->
value
)
return
a
;
if
(
pb
&&
!
pb
->
value
)
return
b
;
}
return
ir
::
And
::
make
(
a
,
b
);
}
Expr
operator
||
(
Expr
a
,
Expr
b
)
{
using
ir
::
UIntImm
;
const
UIntImm
*
pa
=
a
.
as
<
UIntImm
>
();
const
UIntImm
*
pb
=
b
.
as
<
UIntImm
>
();
if
(
pa
&&
pb
)
{
return
UIntImm
::
make
(
UInt
(
1
),
pa
->
value
||
pb
->
value
);
if
(
a
.
type
().
is_bool
()
&&
b
.
type
().
is_bool
())
{
const
UIntImm
*
pa
=
a
.
as
<
UIntImm
>
();
const
UIntImm
*
pb
=
b
.
as
<
UIntImm
>
();
if
(
pa
&&
pa
->
value
)
return
a
;
if
(
pa
&&
!
pa
->
value
)
return
b
;
if
(
pb
&&
pb
->
value
)
return
b
;
if
(
pb
&&
!
pb
->
value
)
return
a
;
}
return
ir
::
Or
::
make
(
a
,
b
);
}
...
...
This diff is collapsed.
Click to expand it.
tests/python/unittest/test_lang_operator.py
+
38
−
0
View file @
f01cc0e6
...
...
@@ -30,6 +30,44 @@ def test_const_fold2():
assert
(
1
*
x
).
same_as
(
x
)
assert
isinstance
((
1
/
x
),
tvm
.
expr
.
Div
)
def
test_const_fold3
():
def
check_throws
(
f
):
try
:
f
()
except
tvm
.
TVMError
:
pass
else
:
raise
AssertionError
(
"
Should have raised an exception but didn
'
t.
"
)
# Test that using ints with logic operations is forbidden
x
=
tvm
.
var
(
"
x
"
)
for
val
in
[
0
,
1
]:
for
func
in
[
tvm
.
all
,
tvm
.
any
]:
check_throws
(
lambda
:
func
(
tvm
.
const
(
val
,
'
uint1
'
),
x
))
check_throws
(
lambda
:
func
(
x
,
tvm
.
const
(
val
,
'
uint1
'
)))
# Test const folding when both arguments are const
for
tvm_func
,
py_func
in
[(
tvm
.
all
,
lambda
a
,
b
:
a
and
b
),
(
tvm
.
any
,
lambda
a
,
b
:
a
or
b
)]:
for
v1
in
[
0
,
1
]:
for
v2
in
[
0
,
1
]:
assert
tvm
.
ir_pass
.
Equal
(
tvm_func
(
tvm
.
const
(
v1
,
'
uint1
'
),
tvm
.
const
(
v2
,
'
uint1
'
)),
tvm
.
const
(
py_func
(
v1
,
v2
),
'
uint1
'
))
x
=
tvm
.
var
(
"
x
"
,
'
uint1
'
)
true
=
tvm
.
const
(
1
,
'
uint1
'
)
false
=
tvm
.
const
(
0
,
'
uint1
'
)
assert
tvm
.
all
(
x
,
true
).
same_as
(
x
)
assert
tvm
.
all
(
true
,
x
).
same_as
(
x
)
assert
tvm
.
any
(
x
,
false
).
same_as
(
x
)
assert
tvm
.
any
(
false
,
x
).
same_as
(
x
)
assert
tvm
.
all
(
x
,
false
).
same_as
(
false
)
assert
tvm
.
all
(
false
,
x
).
same_as
(
false
)
assert
tvm
.
any
(
x
,
true
).
same_as
(
true
)
assert
tvm
.
any
(
true
,
x
).
same_as
(
true
)
if
__name__
==
"
__main__
"
:
test_const_fold
()
test_const_fold2
()
test_const_fold3
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment