Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
93ceb80a
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
403
Star
4705
Fork
582
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
MegEngine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
93ceb80a
编写于
2月 15, 2022
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(imperative): fix broadcast,reshape,reduce
GitOrigin-RevId: ee3dc1487ddfab276e0a8e39801412f19efeaa96
上级
d919aaeb
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
54 addition
and
0 deletion
+54
-0
imperative/src/impl/ops/broadcast.cpp
imperative/src/impl/ops/broadcast.cpp
+45
-0
imperative/src/impl/ops/reduce.cpp
imperative/src/impl/ops/reduce.cpp
+9
-0
未找到文件。
imperative/src/impl/ops/broadcast.cpp
浏览文件 @
93ceb80a
...
...
@@ -81,10 +81,33 @@ std::tuple<SmallVector<LogicalTensorDesc>, bool> infer_output_attrs_fallible(
return
{{{
TensorLayout
(
out_shape
,
src
.
layout
.
dtype
),
src
.
comp_node
}},
true
};
}
SmallVector
<
TensorPtr
>
apply_on_physical_tensor
(
const
OpDef
&
def
,
const
SmallVector
<
TensorPtr
>&
inputs
)
{
auto
&
input
=
inputs
[
0
];
TensorShape
target_shape
;
cg
::
copy_tensor_value_to_shape
(
target_shape
,
inputs
[
1
]
->
get_value
().
proxy_to_default_cpu
());
TensorPtr
output
=
Tensor
::
make
(
TensorLayout
(
target_shape
,
input
->
dtype
()),
input
->
comp_node
());
if
(
output
->
layout
().
is_empty
())
{
return
{
output
};
}
if
(
input
->
shape
().
eq_shape
(
output
->
shape
()))
{
mgb_assert
(
input
->
layout
().
eq_layout
(
output
->
layout
()));
output
->
dev_tensor
().
copy_from_fixlayout
(
input
->
dev_tensor
());
}
else
{
TensorLayout
input_layout
=
input
->
layout
().
broadcast
(
output
->
shape
());
output
->
dev_tensor
().
copy_from_fixlayout
(
input
->
dev_tensor
().
sub
(
SubTensorSpec
::
make_from_layout
(
input_layout
)));
}
return
{
output
};
}
OP_TRAIT_REG
(
Broadcast
,
Broadcast
,
opr
::
Broadcast
)
.
make_from_op_node
(
make_from_op_node
)
.
apply_on_var_node
(
apply_on_var_node
)
.
infer_output_attrs_fallible
(
infer_output_attrs_fallible
)
.
apply_on_physical_tensor
(
apply_on_physical_tensor
)
.
fallback
();
}
// namespace broadcast
...
...
@@ -147,9 +170,31 @@ std::tuple<SmallVector<LogicalTensorDesc>, bool> infer_output_attrs_fallible(
return
{{{
TensorLayout
(
out_shape
,
src
.
layout
.
dtype
),
src
.
comp_node
}},
true
};
}
SmallVector
<
TensorPtr
>
apply_on_physical_tensor
(
const
OpDef
&
def
,
const
SmallVector
<
TensorPtr
>&
inputs
)
{
auto
&&
op_def
=
def
.
cast_final_safe
<
Reshape
>
();
size_t
nr_inp
=
inputs
.
size
();
mgb_assert
(
nr_inp
==
2
,
"Reshape expects 2 inputs; got %lu actually"
,
nr_inp
);
auto
&&
src
=
inputs
[
0
];
auto
&&
tshp_nd
=
inputs
[
1
];
auto
slayout
=
src
->
layout
();
TensorShape
tshp
;
cg
::
copy_tensor_value_to_shape
(
tshp
,
tshp_nd
->
get_value
().
proxy_to_default_cpu
());
if
(
op_def
.
axis
!=
opr
::
Reshape
::
Param
::
INVALID_AXIS
)
{
mgb_assert
(
tshp
[
op_def
.
axis
]
==
-
1
);
tshp
[
op_def
.
axis
]
=
1
;
tshp
[
op_def
.
axis
]
=
src
->
layout
().
total_nr_elems
()
/
tshp
.
total_nr_elems
();
}
TensorLayout
tlayout
=
slayout
.
reshape
(
tshp
);
// memory forward
return
{
Tensor
::
make
(
src
->
blob
(),
0
,
tlayout
)};
}
OP_TRAIT_REG
(
Reshape
,
Reshape
)
.
apply_on_var_node
(
apply_on_var_node
)
.
infer_output_attrs_fallible
(
infer_output_attrs_fallible
)
.
apply_on_physical_tensor
(
apply_on_physical_tensor
)
.
fallback
();
}
// namespace reshape
...
...
imperative/src/impl/ops/reduce.cpp
浏览文件 @
93ceb80a
...
...
@@ -50,9 +50,18 @@ bool memory_forward_success(const OpDef& def, SmallVector<TensorPtr> inputs) {
return
false
;
}
SmallVector
<
TensorPtr
>
apply_on_physical_tensor
(
const
OpDef
&
def
,
const
SmallVector
<
TensorPtr
>&
inputs
)
{
if
(
memory_forward_success
(
def
,
inputs
))
{
return
{
Tensor
::
make
(
inputs
[
0
]
->
blob
(),
0
,
inputs
[
0
]
->
layout
())};
}
return
proxy_graph_detail
::
apply_on_physical_tensor
(
def
,
inputs
);
}
OP_TRAIT_REG
(
Reduce
,
Reduce
,
opr
::
Reduce
)
.
make_from_op_node
(
make_from_op_node
)
.
apply_on_var_node
(
apply_on_var_node
)
.
apply_on_physical_tensor
(
apply_on_physical_tensor
)
.
fallback
();
}
// namespace reduce
}
// namespace
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录