Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
39ac606b
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
404
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看板
提交
39ac606b
编写于
1月 14, 2022
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(dispatch): implement eval
GitOrigin-RevId: 32563e0a27b27776f2e85fd6fcb835eab32195e8
上级
42759dc7
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
202 addition
and
0 deletion
+202
-0
imperative/src/impl/transformations/eval.cpp
imperative/src/impl/transformations/eval.cpp
+107
-0
imperative/src/include/megbrain/imperative/transformations/eval.h
...ve/src/include/megbrain/imperative/transformations/eval.h
+95
-0
未找到文件。
imperative/src/impl/transformations/eval.cpp
0 → 100644
浏览文件 @
39ac606b
/**
* \file imperative/src/impl/transformations/trace.cpp
* MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
*
* Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
#include "megbrain/imperative/transformations/eval.h"
#include "megbrain/imperative/transformations/grad.h"
namespace
mgb
{
namespace
imperative
{
std
::
vector
<
ValueRef
>
InterpreterTransformation
::
apply_transformation
(
const
Operator
&
op
,
Span
<
ValueRef
>
inputs
)
{
if
(
auto
*
op_val
=
op
.
as
<
ApplyOp
>
())
{
if
(
op_val
->
op
().
same_type
<
FastpathCopy
>
())
{
return
{
inputs
[
0
]};
}
SmallVector
<
Handle
>
input_handles
;
SmallVector
<
Handle
>
output_handles
;
CleanupGuard
_
{[
&
]
{
for
(
auto
handle
:
output_handles
)
{
if
(
handle
)
{
m_channel
->
del
(
handle
);
}
}
}};
for
(
auto
input
:
inputs
)
{
input_handles
.
push_back
(
*
input
.
cast
<
InterpreterValue
>
().
handle
());
}
output_handles
=
m_channel
->
apply_op
(
op_val
->
op
().
shared_from_this
(),
input_handles
);
std
::
vector
<
ValueRef
>
outputs
;
for
(
auto
&
handle
:
output_handles
)
{
outputs
.
push_back
(
InterpreterValue
::
make
(
share_handle
(
handle
)));
handle
=
nullptr
;
}
return
outputs
;
}
else
if
(
auto
*
get_attr
=
op
.
as
<
GetAttr
>
())
{
Handle
handle
=
*
inputs
[
0
].
cast
<
InterpreterValue
>
().
handle
();
ValueRef
output
;
switch
(
get_attr
->
attr
())
{
case
GetAttr
::
DType
:
output
=
DTypeValue
::
make
(
m_channel
->
get_dtype
(
handle
));
break
;
case
GetAttr
::
Shape
:
output
=
ShapeValue
::
make
(
ValueShape
::
from
(
m_channel
->
get_shape
(
handle
)));
break
;
case
GetAttr
::
Device
:
output
=
CompNodeValue
::
make
(
m_channel
->
get_device
(
handle
));
break
;
case
GetAttr
::
Value
:
output
=
HostValue
::
make
(
m_channel
->
get_value
(
handle
));
break
;
case
GetAttr
::
Data
:
output
=
DeviceValue
::
make
(
m_channel
->
get_dev_tensor
(
handle
));
break
;
default:
mgb_throw
(
MegBrainError
,
"Interpreter: malformed GetAttr: %s"
,
op
.
to_string
().
c_str
());
}
return
{
output
};
}
else
if
(
auto
*
create_tensor
=
op
.
as
<
CreateTensor
>
())
{
auto
args
=
create_tensor
->
parse
(
inputs
);
if
(
!
args
.
device
)
{
// implies H2D
mgb_assert
(
args
.
host
,
"neither host and device value is valid"
);
return
{
InterpreterValue
::
make
(
share_handle
(
m_channel
->
put
(
*
args
.
host
,
args
.
kind
==
CreateTensor
::
Unique
)))};
}
else
{
return
{
InterpreterValue
::
make
(
share_handle
(
m_channel
->
put
(
*
args
.
device
,
args
.
host
?
*
args
.
host
:
HostTensorND
())))};
}
}
else
if
(
auto
*
dtr_command
=
op
.
as
<
DTRCommand
>
())
{
auto
handle
=
*
inputs
[
0
].
cast
<
InterpreterValue
>
().
handle
();
switch
(
dtr_command
->
kind
())
{
case
DTRCommand
::
Drop
:
m_channel
->
drop
(
handle
);
break
;
default:
mgb_throw
(
AssertionError
,
"unknown DTRCommand %d"
,
dtr_command
->
kind
());
}
return
{};
}
else
if
(
auto
*
rename_value
=
op
.
as
<
RenameValue
>
())
{
auto
&
input
=
inputs
[
0
].
cast
<
InterpreterValue
>
();
return
{
InterpreterValue
::
make
(
input
.
handle
(),
rename_value
->
name
())};
}
else
if
(
op
.
is
<
GetName
>
())
{
auto
name
=
inputs
[
0
].
cast
<
InterpreterValue
>
().
name
();
if
(
!
name
.
empty
())
{
return
{
StringValue
::
make
(
name
)};
}
else
{
return
{
ValueRef
()};
}
}
else
{
return
imperative
::
apply
(
op
,
inputs
);
}
}
}
// namespace imperative
}
// namespace mgb
imperative/src/include/megbrain/imperative/transformations/eval.h
0 → 100644
浏览文件 @
39ac606b
/**
* \file imperative/src/include/megbrain/imperative/eval.h
* MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
*
* Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#include "megbrain/imperative/dispatch.h"
#include "megbrain/imperative/interpreter.h"
#include "megbrain/imperative/ops/autogen.h"
#include "megbrain/imperative/utils/helper.h"
namespace
mgb
::
imperative
{
struct
InterpreterInfo
{
public:
using
Handle
=
interpreter
::
Interpreter
::
Handle
;
using
Channel
=
interpreter
::
Interpreter
::
Channel
;
private:
std
::
shared_ptr
<
Handle
>
m_handle
=
nullptr
;
std
::
string
m_name
;
public:
InterpreterInfo
()
=
default
;
InterpreterInfo
(
std
::
shared_ptr
<
Handle
>
handle
,
std
::
string
name
=
{})
:
m_handle
(
handle
),
m_name
(
name
)
{}
std
::
shared_ptr
<
Handle
>
handle
()
const
{
return
m_handle
;
}
std
::
string
name
()
const
{
return
m_name
;
}
};
class
InterpreterValue
final
:
public
MixinValueImpl
<
InterpreterValue
,
InterpreterInfo
>
{
public:
using
MixinValueImpl
::
MixinValueImpl
;
std
::
string
to_string
()
const
override
{
return
ssprintf
(
"Handle{ptr=%p, name=%s}"
,
handle
().
get
(),
imperative
::
quoted
(
name
()).
c_str
());
}
};
/**
* \brief interpret operations with interpreter
*
* This is the most basic and simplest transformation. It read operation requests and
* forwards them to interpreter. Not all tensor requests would be handled by it,
* some were resolved by CompiledTransformation or LazyEvalTransformation.
*/
class
InterpreterTransformation
final
:
public
Transformation
{
public:
using
Interpreter
=
interpreter
::
Interpreter
;
using
Handle
=
Interpreter
::
Handle
;
using
Channel
=
Interpreter
::
Channel
;
private:
std
::
unique_ptr
<
Channel
>
m_channel
;
public:
explicit
InterpreterTransformation
(
std
::
unique_ptr
<
Channel
>
channel
)
:
m_channel
{
std
::
move
(
channel
)}
{}
Channel
*
channel
()
{
return
m_channel
.
get
();
}
std
::
vector
<
ValueRef
>
apply_transformation
(
const
Operator
&
op
,
Span
<
ValueRef
>
inputs
)
override
;
ValueRef
unwrap
(
ValueRef
value
)
override
{
mgb_assert
(
!
value
.
is
<
InterpreterValue
>
());
return
value
;
}
std
::
string
name
()
const
override
{
return
"InterpreterTransformation"
;
}
std
::
shared_ptr
<
Handle
>
share_handle
(
Handle
handle
)
{
return
std
::
shared_ptr
<
Handle
>
(
new
Handle
(
handle
),
[
channel
=
m_channel
.
get
()](
Handle
*
ptr
)
{
if
(
ptr
)
{
channel
->
del
(
*
ptr
);
delete
ptr
;
}
});
}
};
}
// namespace mgb::imperative
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录