Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
ad0e7a26
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
ad0e7a26
编写于
9月 08, 2023
作者:
傅
傅剑寒
提交者:
GitHub
9月 08, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
【CINN】Integate cast_simplify into ir_simplify (#56958)
* integate cast_simplify into ir_simplify * fix cast simplify testcase
上级
fc71459f
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
96 addition
and
162 deletion
+96
-162
paddle/cinn/common/cas.cc
paddle/cinn/common/cas.cc
+0
-1
paddle/cinn/common/ir_util.cc
paddle/cinn/common/ir_util.cc
+1
-2
paddle/cinn/optim/CMakeLists.txt
paddle/cinn/optim/CMakeLists.txt
+0
-1
paddle/cinn/optim/cast_simplify.cc
paddle/cinn/optim/cast_simplify.cc
+0
-117
paddle/cinn/optim/cast_simplify.h
paddle/cinn/optim/cast_simplify.h
+0
-31
paddle/cinn/optim/cast_simplify_test.cc
paddle/cinn/optim/cast_simplify_test.cc
+5
-7
paddle/cinn/optim/ir_simplify.cc
paddle/cinn/optim/ir_simplify.cc
+88
-2
paddle/cinn/optim/ir_simplify.h
paddle/cinn/optim/ir_simplify.h
+2
-0
paddle/cinn/optim/optimize.cc
paddle/cinn/optim/optimize.cc
+0
-1
未找到文件。
paddle/cinn/common/cas.cc
浏览文件 @
ad0e7a26
...
...
@@ -27,7 +27,6 @@
#include "paddle/cinn/ir/utils/ir_nodes_collector.h"
#include "paddle/cinn/ir/utils/ir_printer.h"
#include "paddle/cinn/ir/utils/ir_visitor.h"
#include "paddle/cinn/optim/cast_simplify.h"
#include "paddle/cinn/utils/string.h"
namespace
cinn
{
...
...
paddle/cinn/common/ir_util.cc
浏览文件 @
ad0e7a26
...
...
@@ -21,7 +21,6 @@
#include "paddle/cinn/ir/op/ir_operators.h"
#include "paddle/cinn/ir/utils/ir_mutator.h"
#include "paddle/cinn/ir/utils/ir_printer.h"
#include "paddle/cinn/optim/cast_simplify.h"
namespace
cinn
{
namespace
common
{
...
...
@@ -147,7 +146,7 @@ Expr IndiceToAbsOffset(const std::vector<Expr> &shape,
for
(
int
i
=
0
;
i
<
shape
.
size
();
i
++
)
{
CHECK_EQ
(
shape
[
i
].
type
(),
Int
(
32
));
Expr
indice_prod
=
indices
[
i
];
optim
::
CastSimplify
(
&
indice_prod
);
optim
::
SimplifyCast
(
&
indice_prod
);
for
(
int
j
=
i
+
1
;
j
<
shape
.
size
();
j
++
)
{
indice_prod
=
RampRelatedMul
(
indice_prod
,
shape
[
j
]);
}
...
...
paddle/cinn/optim/CMakeLists.txt
浏览文件 @
ad0e7a26
...
...
@@ -23,7 +23,6 @@ gather_srcs(
compute_inline_expand.cc
buffer_assign.cc
replace_const_param_to_integer.cc
cast_simplify.cc
lower_intrin.cc
cast_bool_to_int8.cc
collect_undefined_vars.cc
...
...
paddle/cinn/optim/cast_simplify.cc
已删除
100644 → 0
浏览文件 @
fc71459f
// Copyright (c) 2021 CINN Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "paddle/cinn/optim/cast_simplify.h"
#include "paddle/cinn/ir/utils/ir_mutator.h"
namespace
cinn
::
optim
{
using
cinn
::
common
::
bfloat16
;
using
cinn
::
common
::
float16
;
namespace
{
template
<
typename
CastType
,
typename
T
>
CastType
NormCastValue
(
T
value
)
{
if
(
type_of
<
CastType
>
().
is_uint
()
||
type_of
<
T
>
().
is_uint
())
{
// not support uint
return
static_cast
<
CastType
>
(
value
);
}
if
(
std
::
isinf
(
value
))
{
return
std
::
numeric_limits
<
CastType
>::
infinity
();
}
else
if
(
std
::
isnan
(
value
))
{
return
std
::
numeric_limits
<
CastType
>::
signaling_NaN
();
}
else
if
(
value
>=
static_cast
<
T
>
(
std
::
numeric_limits
<
CastType
>::
max
()))
{
return
std
::
numeric_limits
<
CastType
>::
max
();
}
else
if
(
value
<=
static_cast
<
T
>
(
std
::
numeric_limits
<
CastType
>::
lowest
()))
{
return
std
::
numeric_limits
<
CastType
>::
lowest
();
}
return
static_cast
<
CastType
>
(
value
);
}
struct
Mutator
:
ir
::
IRMutator
<>
{
using
ir
::
IRMutator
<>::
Visit
;
void
Visit
(
const
ir
::
Cast
*
op
,
Expr
*
expr
)
{
auto
*
node
=
expr
->
As
<
ir
::
Cast
>
();
Visit
(
&
node
->
v
(),
&
node
->
v
());
if
(
op
->
type
()
==
op
->
v
().
type
())
{
*
expr
=
op
->
v
();
return
;
}
#define __CAST_TO_TYPE(type__) \
if (auto* i = op->v().As<ir::IntImm>()) { \
*expr = Expr(static_cast<type__>(i->value)); \
} else if (auto* f = op->v().As<ir::FloatImm>()) { \
*expr = Expr(static_cast<type__>(NormCastValue<type__>(f->value))); \
} else if (auto* u = op->v().As<ir::UIntImm>()) { \
*expr = Expr(static_cast<type__>(u->value)); \
} else { \
CINN_NOT_IMPLEMENTED \
}
if
(
op
->
v
().
is_constant
())
{
if
(
op
->
type
()
==
type_of
<
int8_t
>
())
{
__CAST_TO_TYPE
(
int8_t
)
}
else
if
(
op
->
type
()
==
type_of
<
int16_t
>
())
{
__CAST_TO_TYPE
(
int16_t
)
}
else
if
(
op
->
type
()
==
type_of
<
int32_t
>
())
{
__CAST_TO_TYPE
(
int32_t
)
}
else
if
(
op
->
type
()
==
type_of
<
int64_t
>
())
{
__CAST_TO_TYPE
(
int64_t
)
}
else
if
(
op
->
type
()
==
type_of
<
uint8_t
>
())
{
__CAST_TO_TYPE
(
uint8_t
)
}
else
if
(
op
->
type
()
==
type_of
<
uint16_t
>
())
{
__CAST_TO_TYPE
(
uint16_t
)
}
else
if
(
op
->
type
()
==
type_of
<
uint32_t
>
())
{
__CAST_TO_TYPE
(
uint32_t
)
}
else
if
(
op
->
type
()
==
type_of
<
uint64_t
>
())
{
__CAST_TO_TYPE
(
uint64_t
)
}
else
if
(
op
->
type
()
==
type_of
<
float
>
())
{
__CAST_TO_TYPE
(
float
)
}
else
if
(
op
->
type
()
==
type_of
<
double
>
())
{
__CAST_TO_TYPE
(
double
)
}
else
if
(
op
->
type
()
==
type_of
<
bool
>
())
{
__CAST_TO_TYPE
(
bool
)
}
else
if
(
op
->
type
()
==
type_of
<
uint32_t
>
())
{
__CAST_TO_TYPE
(
uint32_t
)
}
else
if
(
op
->
type
()
==
type_of
<
uint64_t
>
())
{
__CAST_TO_TYPE
(
uint64_t
)
}
else
if
(
op
->
type
()
==
type_of
<
bfloat16
>
())
{
// Cannot simplify!!! pass
__CAST_TO_TYPE
(
bfloat16
)
}
else
if
(
op
->
type
()
==
type_of
<
float16
>
())
{
// Cannot simplify!!! pass
__CAST_TO_TYPE
(
float16
)
}
else
{
CINN_NOT_IMPLEMENTED
}
}
#undef __CAST_TO_TYPE
}
};
}
// namespace
void
CastSimplify
(
Expr
*
e
)
{
Mutator
mutator
;
mutator
.
Visit
(
e
,
e
);
}
}
// namespace cinn::optim
paddle/cinn/optim/cast_simplify.h
已删除
100644 → 0
浏览文件 @
fc71459f
// Copyright (c) 2021 CINN Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include "paddle/cinn/ir/ir.h"
namespace
cinn
::
optim
{
/**
* Simplify the Cast nodes.
*
* There are several patterns:
* 1. the source and target type are the same, drop the Cast node
* 2. for intermediate numbers, just replace the Cast node with a Node of the
* target type
*/
void
CastSimplify
(
Expr
*
e
);
}
// namespace cinn::optim
paddle/cinn/optim/cast_simplify_test.cc
浏览文件 @
ad0e7a26
...
...
@@ -12,13 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "paddle/cinn/optim/cast_simplify.h"
#include <gtest/gtest.h>
#include "paddle/cinn/ir/op/ir_operators.h"
#include "paddle/cinn/ir/utils/ir_printer.h"
#include "paddle/cinn/optim/ir_simplify.h"
namespace
cinn
::
optim
{
TEST
(
CastSimplify
,
same_type
)
{
...
...
@@ -26,7 +24,7 @@ TEST(CastSimplify, same_type) {
Expr
a
=
ir
::
Cast
::
Make
(
Int
(
32
),
n
);
LOG
(
INFO
)
<<
n
->
type
();
LOG
(
INFO
)
<<
a
;
CastSimplify
(
&
a
);
SimplifyCast
(
&
a
);
ASSERT_EQ
(
utils
::
GetStreamCnt
(
a
),
"n"
);
}
...
...
@@ -34,7 +32,7 @@ TEST(CastSimplify, Imm_int) {
Expr
a
=
ir
::
Cast
::
Make
(
Int
(
64
),
Expr
(
1
));
Expr
c
=
ir
::
Cast
::
Make
(
Int
(
32
),
a
);
LOG
(
INFO
)
<<
c
;
CastSimplify
(
&
c
);
SimplifyCast
(
&
c
);
LOG
(
INFO
)
<<
c
;
ASSERT_EQ
(
utils
::
GetStreamCnt
(
c
),
"1"
);
ASSERT_EQ
(
c
.
type
(),
Int
(
32
));
...
...
@@ -44,7 +42,7 @@ TEST(CastSimplify, Imm_double) {
Expr
a
=
ir
::
Cast
::
Make
(
Float
(
64
),
Expr
(
2.33
));
Expr
c
=
ir
::
Cast
::
Make
(
Int
(
32
),
a
);
LOG
(
INFO
)
<<
c
;
CastSimplify
(
&
c
);
SimplifyCast
(
&
c
);
LOG
(
INFO
)
<<
c
;
ASSERT_EQ
(
utils
::
GetStreamCnt
(
c
),
"2"
);
ASSERT_EQ
(
c
.
type
(),
Int
(
32
));
...
...
@@ -54,7 +52,7 @@ TEST(CastSimplify, Imm_uint) {
Expr
a
=
ir
::
Cast
::
Make
(
UInt
(
64
),
Expr
(
1
));
Expr
c
=
ir
::
Cast
::
Make
(
UInt
(
32
),
a
);
LOG
(
INFO
)
<<
c
;
CastSimplify
(
&
c
);
SimplifyCast
(
&
c
);
LOG
(
INFO
)
<<
c
;
ASSERT_EQ
(
utils
::
GetStreamCnt
(
c
),
"1"
);
ASSERT_EQ
(
c
.
type
(),
UInt
(
32
));
...
...
paddle/cinn/optim/ir_simplify.cc
浏览文件 @
ad0e7a26
...
...
@@ -29,13 +29,14 @@
#include "paddle/cinn/ir/utils/ir_mutator.h"
#include "paddle/cinn/ir/utils/ir_printer.h"
#include "paddle/cinn/ir/utils/ir_visitor.h"
#include "paddle/cinn/optim/cast_simplify.h"
#include "paddle/cinn/utils/string.h"
namespace
cinn
{
namespace
optim
{
using
namespace
ir
;
// NOLINT
using
common
::
bfloat16
;
using
common
::
ExprToGinacConverter
;
using
common
::
float16
;
using
utils
::
GetStreamCnt
;
using
utils
::
Replace
;
...
...
@@ -346,11 +347,95 @@ struct SimplifyForLoopsMutator : public ir::IRMutator<> {
}
};
template
<
typename
CastType
,
typename
T
>
CastType
NormCastValue
(
T
value
)
{
if
(
type_of
<
CastType
>
().
is_uint
()
||
type_of
<
T
>
().
is_uint
())
{
// not support uint
return
static_cast
<
CastType
>
(
value
);
}
if
(
std
::
isinf
(
value
))
{
return
std
::
numeric_limits
<
CastType
>::
infinity
();
}
else
if
(
std
::
isnan
(
value
))
{
return
std
::
numeric_limits
<
CastType
>::
signaling_NaN
();
}
else
if
(
value
>=
static_cast
<
T
>
(
std
::
numeric_limits
<
CastType
>::
max
()))
{
return
std
::
numeric_limits
<
CastType
>::
max
();
}
else
if
(
value
<=
static_cast
<
T
>
(
std
::
numeric_limits
<
CastType
>::
lowest
()))
{
return
std
::
numeric_limits
<
CastType
>::
lowest
();
}
return
static_cast
<
CastType
>
(
value
);
}
struct
SimplifyCastMutator
:
public
ir
::
IRMutator
<>
{
void
operator
()(
Expr
*
expr
)
{
ir
::
IRMutator
<
ir
::
Expr
*>::
Visit
(
expr
,
expr
);
}
void
Visit
(
const
ir
::
Cast
*
op
,
Expr
*
expr
)
{
auto
*
node
=
expr
->
As
<
ir
::
Cast
>
();
ir
::
IRMutator
<
ir
::
Expr
*>::
Visit
(
&
node
->
v
(),
&
node
->
v
());
if
(
op
->
type
()
==
op
->
v
().
type
())
{
*
expr
=
op
->
v
();
return
;
}
#define __CAST_TO_TYPE(type__) \
if (auto* i = op->v().As<ir::IntImm>()) { \
*expr = Expr(static_cast<type__>(i->value)); \
} else if (auto* f = op->v().As<ir::FloatImm>()) { \
*expr = Expr(static_cast<type__>(NormCastValue<type__>(f->value))); \
} else if (auto* u = op->v().As<ir::UIntImm>()) { \
*expr = Expr(static_cast<type__>(u->value)); \
} else { \
CINN_NOT_IMPLEMENTED \
}
if
(
op
->
v
().
is_constant
())
{
if
(
op
->
type
()
==
type_of
<
int8_t
>
())
{
__CAST_TO_TYPE
(
int8_t
)
}
else
if
(
op
->
type
()
==
type_of
<
int16_t
>
())
{
__CAST_TO_TYPE
(
int16_t
)
}
else
if
(
op
->
type
()
==
type_of
<
int32_t
>
())
{
__CAST_TO_TYPE
(
int32_t
)
}
else
if
(
op
->
type
()
==
type_of
<
int64_t
>
())
{
__CAST_TO_TYPE
(
int64_t
)
}
else
if
(
op
->
type
()
==
type_of
<
uint8_t
>
())
{
__CAST_TO_TYPE
(
uint8_t
)
}
else
if
(
op
->
type
()
==
type_of
<
uint16_t
>
())
{
__CAST_TO_TYPE
(
uint16_t
)
}
else
if
(
op
->
type
()
==
type_of
<
uint32_t
>
())
{
__CAST_TO_TYPE
(
uint32_t
)
}
else
if
(
op
->
type
()
==
type_of
<
uint64_t
>
())
{
__CAST_TO_TYPE
(
uint64_t
)
}
else
if
(
op
->
type
()
==
type_of
<
float
>
())
{
__CAST_TO_TYPE
(
float
)
}
else
if
(
op
->
type
()
==
type_of
<
double
>
())
{
__CAST_TO_TYPE
(
double
)
}
else
if
(
op
->
type
()
==
type_of
<
bool
>
())
{
__CAST_TO_TYPE
(
bool
)
}
else
if
(
op
->
type
()
==
type_of
<
uint32_t
>
())
{
__CAST_TO_TYPE
(
uint32_t
)
}
else
if
(
op
->
type
()
==
type_of
<
uint64_t
>
())
{
__CAST_TO_TYPE
(
uint64_t
)
}
else
if
(
op
->
type
()
==
type_of
<
bfloat16
>
())
{
// Cannot simplify!!! pass
__CAST_TO_TYPE
(
bfloat16
)
}
else
if
(
op
->
type
()
==
type_of
<
float16
>
())
{
// Cannot simplify!!! pass
__CAST_TO_TYPE
(
float16
)
}
else
{
CINN_NOT_IMPLEMENTED
}
}
#undef __CAST_TO_TYPE
}
};
}
// namespace
void
Simplify
(
Expr
*
expr
)
{
VLOG
(
3
)
<<
"Begin Simplify "
<<
*
expr
;
optim
::
CastSimplify
(
expr
);
SimplifyCastMutator
()
(
expr
);
SimplifyRampMutator
()(
expr
);
SimplifyLoadMutator
()(
expr
);
SimplifyStoreMutator
()(
expr
);
...
...
@@ -363,6 +448,7 @@ void Simplify(Expr* expr) {
ReplaceFracWithDivMutator
()(
expr
);
}
void
SimplifyCast
(
Expr
*
expr
)
{
SimplifyCastMutator
()(
expr
);
}
void
SimplifyForLoops
(
Expr
*
expr
)
{
SimplifyForLoopsMutator
()(
expr
);
}
void
SimplifyBlocks
(
Expr
*
expr
)
{
SimplifyBlocksMutator
()(
expr
);
}
...
...
paddle/cinn/optim/ir_simplify.h
浏览文件 @
ad0e7a26
...
...
@@ -30,6 +30,8 @@ namespace optim {
*/
void
Simplify
(
Expr
*
expr
);
void
SimplifyCast
(
Expr
*
expr
);
void
SimplifyForLoops
(
Expr
*
expr
);
void
SimplifyBlocks
(
Expr
*
expr
);
...
...
paddle/cinn/optim/optimize.cc
浏览文件 @
ad0e7a26
...
...
@@ -19,7 +19,6 @@
#include "paddle/cinn/ir/utils/ir_printer.h"
#include "paddle/cinn/optim/call_arg_list_to_pod_value.h"
#include "paddle/cinn/optim/cast_bool_to_int8.h"
#include "paddle/cinn/optim/cast_simplify.h"
#include "paddle/cinn/optim/eliminate_broadcast_in_forloop.h"
#include "paddle/cinn/optim/extern_call_process.h"
#include "paddle/cinn/optim/fold_cinn_call_arguments.h"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录