Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
b64fca6e
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
b64fca6e
编写于
7月 16, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
7月 16, 2020
浏览文件
操作
浏览文件
下载
差异文件
!3091 GPU add fusion: replace batchnormgrad cast
Merge pull request !3091 from VectorSL/batchnorm_grad
上级
0c781152
4cf7faea
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
287 addition
and
0 deletion
+287
-0
mindspore/ccsrc/backend/optimizer/gpu/replace_bn_grad_cast2_fusion.cc
...src/backend/optimizer/gpu/replace_bn_grad_cast2_fusion.cc
+88
-0
mindspore/ccsrc/backend/optimizer/gpu/replace_bn_grad_cast2_fusion.h
...csrc/backend/optimizer/gpu/replace_bn_grad_cast2_fusion.h
+54
-0
mindspore/ccsrc/backend/optimizer/gpu/replace_bn_grad_cast_fusion.cc
...csrc/backend/optimizer/gpu/replace_bn_grad_cast_fusion.cc
+91
-0
mindspore/ccsrc/backend/optimizer/gpu/replace_bn_grad_cast_fusion.h
...ccsrc/backend/optimizer/gpu/replace_bn_grad_cast_fusion.h
+54
-0
未找到文件。
mindspore/ccsrc/backend/optimizer/gpu/replace_bn_grad_cast2_fusion.cc
0 → 100644
浏览文件 @
b64fca6e
/**
* Copyright 2020 Huawei Technologies Co., Ltd
*
* 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 "backend/optimizer/gpu/replace_bn_grad_cast2_fusion.h"
#include <memory>
#include <vector>
#include <string>
#include "backend/session/anf_runtime_algorithm.h"
#include "ir/primitive.h"
#include "utils/utils.h"
#include "backend/optimizer/common/helper.h"
namespace
mindspore
{
namespace
opt
{
const
BaseRef
ReplaceBNGradCast2Fusion
::
DefinePattern
()
const
{
VectorRef
fbn2g
=
VectorRef
({
prim
::
kPrimFusedBatchNormGrad
,
dy_
,
x_
,
scale_
,
mean_
,
var_
});
VectorRef
tupleget
=
VectorRef
({
prim
::
kPrimTupleGetItem
,
fbn2g
,
index_
});
VectorRef
out_cast
=
VectorRef
({
prim
::
kPrimCast
,
tupleget
});
return
out_cast
;
}
const
AnfNodePtr
ReplaceBNGradCast2Fusion
::
Process
(
const
FuncGraphPtr
&
graph
,
const
AnfNodePtr
&
node
,
const
EquivPtr
&
equiv
)
const
{
MS_EXCEPTION_IF_NULL
(
graph
);
MS_EXCEPTION_IF_NULL
(
node
);
MS_EXCEPTION_IF_NULL
(
equiv
);
auto
tuple
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
node
),
0
);
auto
index_node
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
tuple
),
1
);
MS_EXCEPTION_IF_NULL
(
index_node
);
auto
value_node
=
index_node
->
cast
<
ValueNodePtr
>
();
MS_EXCEPTION_IF_NULL
(
value_node
);
int
item_idx
=
GetValue
<
int
>
(
value_node
->
value
());
if
(
item_idx
!=
0
)
{
return
nullptr
;
}
auto
fbn2g
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
tuple
),
0
);
auto
dy_
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
fbn2g
),
0
);
auto
x_
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
fbn2g
),
1
);
auto
scale
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
fbn2g
),
2
);
auto
mean
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
fbn2g
),
3
);
auto
var
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
fbn2g
),
4
);
MS_EXCEPTION_IF_NULL
(
fbn2g
);
MS_EXCEPTION_IF_NULL
(
dy_
);
MS_EXCEPTION_IF_NULL
(
scale
);
MS_EXCEPTION_IF_NULL
(
x_
);
MS_EXCEPTION_IF_NULL
(
mean
);
MS_EXCEPTION_IF_NULL
(
var
);
auto
manager
=
graph
->
manager
();
MS_EXCEPTION_IF_NULL
(
manager
);
manager
->
Replace
(
utils
::
cast
<
CNodePtr
>
(
node
),
utils
::
cast
<
CNodePtr
>
(
tuple
));
std
::
vector
<
TypeId
>
outputs_type
;
std
::
vector
<
std
::
vector
<
size_t
>>
outputs_shape
;
auto
output_num
=
AnfAlgo
::
GetOutputTensorNum
(
fbn2g
);
for
(
size_t
i
=
0
;
i
<
output_num
;
i
++
)
{
outputs_type
.
push_back
(
AnfAlgo
::
GetOutputInferDataType
(
fbn2g
,
i
));
outputs_shape
.
push_back
(
AnfAlgo
::
GetOutputInferShape
(
fbn2g
,
i
));
}
outputs_type
[
0
]
=
AnfAlgo
::
GetPrevNodeOutputInferDataType
(
fbn2g
,
0
);
AnfAlgo
::
SetOutputInferTypeAndShape
(
outputs_type
,
outputs_shape
,
fbn2g
.
get
());
outputs_type
.
clear
();
outputs_shape
.
clear
();
outputs_type
.
push_back
(
AnfAlgo
::
GetPrevNodeOutputInferDataType
(
fbn2g
,
0
));
outputs_shape
.
push_back
(
AnfAlgo
::
GetOutputInferShape
(
tuple
,
0
));
AnfAlgo
::
SetOutputInferTypeAndShape
(
outputs_type
,
outputs_shape
,
tuple
.
get
());
return
tuple
;
}
}
// namespace opt
}
// namespace mindspore
mindspore/ccsrc/backend/optimizer/gpu/replace_bn_grad_cast2_fusion.h
0 → 100644
浏览文件 @
b64fca6e
/**
* Copyright 2020 Huawei Technologies Co., Ltd
*
* 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.
*/
#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GPU_REPLACE_BN_GRAD_CAST2_FUSION_H_
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GPU_REPLACE_BN_GRAD_CAST2_FUSION_H_
#include <memory>
#include "backend/optimizer/common/optimizer.h"
namespace
mindspore
{
namespace
opt
{
class
ReplaceBNGradCast2Fusion
:
public
PatternProcessPass
{
public:
explicit
ReplaceBNGradCast2Fusion
(
bool
multigraph
=
true
)
:
PatternProcessPass
(
"replace_grad_cast2"
,
multigraph
)
{
dy_
=
std
::
make_shared
<
Var
>
();
x_
=
std
::
make_shared
<
Var
>
();
scale_
=
std
::
make_shared
<
Var
>
();
mean_
=
std
::
make_shared
<
Var
>
();
var_
=
std
::
make_shared
<
Var
>
();
dx_
=
std
::
make_shared
<
Var
>
();
bn_scale_
=
std
::
make_shared
<
Var
>
();
bn_bias_
=
std
::
make_shared
<
Var
>
();
index_
=
std
::
make_shared
<
Var
>
();
}
~
ReplaceBNGradCast2Fusion
()
override
=
default
;
const
BaseRef
DefinePattern
()
const
override
;
const
AnfNodePtr
Process
(
const
FuncGraphPtr
&
,
const
AnfNodePtr
&
,
const
EquivPtr
&
)
const
override
;
private:
VarPtr
dy_
;
VarPtr
x_
;
VarPtr
scale_
;
VarPtr
mean_
;
VarPtr
var_
;
VarPtr
dx_
;
VarPtr
bn_scale_
;
VarPtr
bn_bias_
;
VarPtr
index_
;
};
}
// namespace opt
}
// namespace mindspore
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GPU_REPLACE_BN_GRAD_CAST2_FUSION_H_
mindspore/ccsrc/backend/optimizer/gpu/replace_bn_grad_cast_fusion.cc
0 → 100644
浏览文件 @
b64fca6e
/**
* Copyright 2020 Huawei Technologies Co., Ltd
*
* 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 "backend/optimizer/gpu/replace_bn_grad_cast_fusion.h"
#include <memory>
#include <vector>
#include <string>
#include "backend/session/anf_runtime_algorithm.h"
#include "ir/primitive.h"
#include "utils/utils.h"
#include "backend/optimizer/common/helper.h"
namespace
mindspore
{
namespace
opt
{
const
BaseRef
ReplaceBNGradCastFusion
::
DefinePattern
()
const
{
VectorRef
dy_cast
=
VectorRef
({
prim
::
kPrimCast
,
dy_
});
VectorRef
fbn2g
=
VectorRef
({
prim
::
kPrimFusedBatchNormGrad
,
dy_cast
,
x_
,
scale_
,
mean_
,
var_
});
VectorRef
tupleget
=
VectorRef
({
prim
::
kPrimTupleGetItem
,
fbn2g
,
index_
});
VectorRef
out_cast
=
VectorRef
({
prim
::
kPrimCast
,
tupleget
});
return
out_cast
;
}
const
AnfNodePtr
ReplaceBNGradCastFusion
::
Process
(
const
FuncGraphPtr
&
graph
,
const
AnfNodePtr
&
node
,
const
EquivPtr
&
equiv
)
const
{
MS_EXCEPTION_IF_NULL
(
graph
);
MS_EXCEPTION_IF_NULL
(
node
);
MS_EXCEPTION_IF_NULL
(
equiv
);
auto
tuple
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
node
),
0
);
auto
index_node
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
tuple
),
1
);
MS_EXCEPTION_IF_NULL
(
index_node
);
auto
value_node
=
index_node
->
cast
<
ValueNodePtr
>
();
MS_EXCEPTION_IF_NULL
(
value_node
);
int
item_idx
=
GetValue
<
int
>
(
value_node
->
value
());
if
(
item_idx
!=
0
)
{
return
nullptr
;
}
auto
fbn2g
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
tuple
),
0
);
auto
dy_after
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
fbn2g
),
0
);
auto
dy_before
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
dy_after
),
0
);
auto
x_
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
fbn2g
),
1
);
auto
scale
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
fbn2g
),
2
);
auto
mean
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
fbn2g
),
3
);
auto
var
=
AnfAlgo
::
GetInputNode
(
utils
::
cast
<
CNodePtr
>
(
fbn2g
),
4
);
MS_EXCEPTION_IF_NULL
(
fbn2g
);
MS_EXCEPTION_IF_NULL
(
dy_after
);
MS_EXCEPTION_IF_NULL
(
dy_before
);
MS_EXCEPTION_IF_NULL
(
scale
);
MS_EXCEPTION_IF_NULL
(
x_
);
MS_EXCEPTION_IF_NULL
(
mean
);
MS_EXCEPTION_IF_NULL
(
var
);
auto
manager
=
graph
->
manager
();
MS_EXCEPTION_IF_NULL
(
manager
);
manager
->
Replace
(
utils
::
cast
<
CNodePtr
>
(
dy_after
),
utils
::
cast
<
CNodePtr
>
(
dy_before
));
manager
->
Replace
(
utils
::
cast
<
CNodePtr
>
(
node
),
utils
::
cast
<
CNodePtr
>
(
tuple
));
std
::
vector
<
TypeId
>
outputs_type
;
std
::
vector
<
std
::
vector
<
size_t
>>
outputs_shape
;
auto
output_num
=
AnfAlgo
::
GetOutputTensorNum
(
fbn2g
);
for
(
size_t
i
=
0
;
i
<
output_num
;
i
++
)
{
outputs_type
.
push_back
(
AnfAlgo
::
GetOutputInferDataType
(
fbn2g
,
i
));
outputs_shape
.
push_back
(
AnfAlgo
::
GetOutputInferShape
(
fbn2g
,
i
));
}
outputs_type
[
0
]
=
kNumberTypeFloat16
;
AnfAlgo
::
SetOutputInferTypeAndShape
(
outputs_type
,
outputs_shape
,
fbn2g
.
get
());
outputs_type
.
clear
();
outputs_shape
.
clear
();
outputs_type
.
push_back
(
kNumberTypeFloat16
);
outputs_shape
.
push_back
(
AnfAlgo
::
GetOutputInferShape
(
tuple
,
0
));
AnfAlgo
::
SetOutputInferTypeAndShape
(
outputs_type
,
outputs_shape
,
tuple
.
get
());
return
tuple
;
}
}
// namespace opt
}
// namespace mindspore
mindspore/ccsrc/backend/optimizer/gpu/replace_bn_grad_cast_fusion.h
0 → 100644
浏览文件 @
b64fca6e
/**
* Copyright 2020 Huawei Technologies Co., Ltd
*
* 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.
*/
#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GPU_REPLACE_BN_GRAD_CAST_FUSION_H_
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GPU_REPLACE_BN_GRAD_CAST_FUSION_H_
#include <memory>
#include "backend/optimizer/common/optimizer.h"
namespace
mindspore
{
namespace
opt
{
class
ReplaceBNGradCastFusion
:
public
PatternProcessPass
{
public:
explicit
ReplaceBNGradCastFusion
(
bool
multigraph
=
true
)
:
PatternProcessPass
(
"replace_bn_grad_cast"
,
multigraph
)
{
dy_
=
std
::
make_shared
<
Var
>
();
x_
=
std
::
make_shared
<
Var
>
();
scale_
=
std
::
make_shared
<
Var
>
();
mean_
=
std
::
make_shared
<
Var
>
();
var_
=
std
::
make_shared
<
Var
>
();
dx_
=
std
::
make_shared
<
Var
>
();
bn_scale_
=
std
::
make_shared
<
Var
>
();
bn_bias_
=
std
::
make_shared
<
Var
>
();
index_
=
std
::
make_shared
<
Var
>
();
}
~
ReplaceBNGradCastFusion
()
override
=
default
;
const
BaseRef
DefinePattern
()
const
override
;
const
AnfNodePtr
Process
(
const
FuncGraphPtr
&
,
const
AnfNodePtr
&
,
const
EquivPtr
&
)
const
override
;
private:
VarPtr
dy_
;
VarPtr
x_
;
VarPtr
scale_
;
VarPtr
mean_
;
VarPtr
var_
;
VarPtr
dx_
;
VarPtr
bn_scale_
;
VarPtr
bn_bias_
;
VarPtr
index_
;
};
}
// namespace opt
}
// namespace mindspore
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GPU_REPLACE_BN_GRAD_CAST_FUSION_H_
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录