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
cb70da1b
Commit
cb70da1b
authored
6 years ago
by
Wei Chen
Committed by
Tianqi Chen
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Improve CanonicalSimplify to handle Min, Max(#2248) (#2261)
Also enable Mul caching for more cases
parent
1cb602f1
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/arithmetic/canonical.cc
+19
-1
19 additions, 1 deletion
src/arithmetic/canonical.cc
tests/cpp/ir_simplify_test.cc
+19
-0
19 additions, 0 deletions
tests/cpp/ir_simplify_test.cc
tests/python/unittest/test_arith_simplify.py
+17
-4
17 additions, 4 deletions
tests/python/unittest/test_arith_simplify.py
with
55 additions
and
5 deletions
src/arithmetic/canonical.cc
+
19
−
1
View file @
cb70da1b
...
...
@@ -236,6 +236,24 @@ class Canonical::Internal : public IRMutator {
bool
EnableOpt
(
Type
t
)
const
{
return
(
t
.
lanes
()
==
1
&&
(
t
.
is_int
()
||
t
.
is_uint
()));
}
// Max
Expr
Mutate_
(
const
Max
*
op
,
const
Expr
&
e
)
final
{
CacheEntry
a
=
Produce
(
op
->
a
);
CacheEntry
b
=
Produce
(
op
->
b
);
if
(
a
.
has_side_effect
||
b
.
has_side_effect
)
{
return
Binary_
(
op
,
e
,
a
.
value
,
b
.
value
);
}
return
Binary
(
op
,
e
);
}
// Min
Expr
Mutate_
(
const
Min
*
op
,
const
Expr
&
e
)
final
{
CacheEntry
a
=
Produce
(
op
->
a
);
CacheEntry
b
=
Produce
(
op
->
b
);
if
(
a
.
has_side_effect
||
b
.
has_side_effect
)
{
return
Binary_
(
op
,
e
,
a
.
value
,
b
.
value
);
}
return
Binary
(
op
,
e
);
}
// Add
Expr
Mutate_
(
const
Add
*
op
,
const
Expr
&
e
)
final
{
if
(
!
EnableOpt
(
op
->
type
))
{
...
...
@@ -277,7 +295,7 @@ class Canonical::Internal : public IRMutator {
}
else
if
(
is_const
(
b
.
value
))
{
return
SumMulConst
(
a
.
AsSum
(),
b
.
value
);
}
else
{
return
Binary
_
(
op
,
e
,
a
.
value
,
b
.
value
);
return
Binary
(
op
,
e
);
}
}
// Variable
...
...
This diff is collapsed.
Click to expand it.
tests/cpp/ir_simplify_test.cc
+
19
−
0
View file @
cb70da1b
#include
<dmlc/logging.h>
#include
<gtest/gtest.h>
#include
<tvm/ir_pass.h>
#include
<tvm/tvm.h>
#include
<arithmetic/Simplify.h>
...
...
@@ -8,6 +9,24 @@ TEST(IRSIMPLIFY, Basic) {
simplify_test
();
}
TEST
(
IRSIMPLIFY
,
MinMax
)
{
auto
x
=
tvm
::
var
(
"x"
);
auto
e1
=
(
tvm
::
max
(
x
,
1
)
-
tvm
::
max
(
x
,
1
))
;
auto
e1s
=
tvm
::
ir
::
CanonicalSimplify
(
e1
);
CHECK
(
is_zero
(
e1s
));
auto
e2
=
(
x
*
tvm
::
min
(
x
,
1
))
-
(
x
*
tvm
::
min
(
x
,
1
));
auto
e2s
=
tvm
::
ir
::
CanonicalSimplify
(
e2
);
CHECK
(
is_zero
(
e2s
));
}
TEST
(
IRSIMPLIFY
,
Mul
)
{
auto
x
=
tvm
::
var
(
"x"
);
auto
e
=
(
x
*
x
)
-
(
x
*
x
)
;
auto
es
=
tvm
::
ir
::
CanonicalSimplify
(
e
);
CHECK
(
is_zero
(
es
));
}
int
main
(
int
argc
,
char
**
argv
)
{
testing
::
InitGoogleTest
(
&
argc
,
argv
);
testing
::
FLAGS_gtest_death_test_style
=
"threadsafe"
;
...
...
This diff is collapsed.
Click to expand it.
tests/python/unittest/test_arith_simplify.py
+
17
−
4
View file @
cb70da1b
...
...
@@ -46,6 +46,21 @@ def test_simplify_mod():
(
j
+
n
*
32
)
%
16
,
{
j
:
tvm
.
Range
(
0
,
6
)})
assert
index
==
j
def
test_simplify_minmax
():
x
=
tvm
.
var
(
'
x
'
)
e1
=
tvm
.
max
(
x
,
1
)
-
tvm
.
max
(
x
,
1
)
e1s
=
tvm
.
ir_pass
.
CanonicalSimplify
(
e1
)
assert
e1s
.
value
==
0
e2
=
tvm
.
min
(
x
,
1
)
-
tvm
.
min
(
x
,
1
)
e2s
=
tvm
.
ir_pass
.
CanonicalSimplify
(
e2
)
assert
e2s
.
value
==
0
def
test_mul
():
x
=
tvm
.
var
(
'
x
'
)
e
=
x
*
x
-
x
*
x
es
=
tvm
.
ir_pass
.
CanonicalSimplify
(
e
)
assert
es
.
value
==
0
def
test_modular
():
rx
=
tvm
.
var
(
"
rx
"
)
...
...
@@ -62,11 +77,9 @@ def test_modular():
assert
tvm
.
ir_pass
.
CanonicalSimplify
(
z1
-
(
ry
+
y
)).
value
==
0
assert
tvm
.
ir_pass
.
CanonicalSimplify
(
z2
-
(
rx
+
x
)).
value
==
0
if
__name__
==
"
__main__
"
:
test_simplify_mod
()
test_modular
()
test_simplify
()
test_mul
()
test_simplify_minmax
()
\ No newline at end of file
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