Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
274bd253
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看板
提交
274bd253
编写于
6月 29, 2020
作者:
W
wuyongkang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optimize parser
上级
9ef744db
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
24 addition
and
7 deletion
+24
-7
mindspore/_extends/parse/parser.py
mindspore/_extends/parse/parser.py
+23
-6
mindspore/ccsrc/pipeline/parse/resolve.cc
mindspore/ccsrc/pipeline/parse/resolve.cc
+1
-1
未找到文件。
mindspore/_extends/parse/parser.py
浏览文件 @
274bd253
...
...
@@ -19,6 +19,7 @@
import
ast
import
types
import
inspect
import
hashlib
from
textwrap
import
dedent
from
dataclasses
import
is_dataclass
import
asttokens
...
...
@@ -319,7 +320,6 @@ def get_dataclass_methods(cls):
if
isinstance
(
getattr
(
cls
,
name
),
(
types
.
FunctionType
,))}
return
methods
class
Parser
:
"""
Parser python code to ast tree.
...
...
@@ -327,7 +327,10 @@ class Parser:
Args:
fn(FunctionType/MethodType): Need parse object instance.
parse_method(ExtendInfoOfParseObj): Extend information for parse the function.
ast_cache: Dictionary for caching ast tree.
"""
ast_cache
=
{}
def
__init__
(
self
,
fn
:
(
types
.
FunctionType
,
types
.
MethodType
),
parse_method
=
None
)
->
None
:
self
.
fn
=
fn
self
.
parse_method
=
parse_method
...
...
@@ -342,17 +345,31 @@ class Parser:
self
.
function_name
=
fn
.
__name__
self
.
col_offset
=
0
@
classmethod
def
get_cache
(
cls
,
key
):
"""Get the value of the ast_cache dictionary"""
return
cls
.
ast_cache
.
get
(
key
)
@
classmethod
def
insert_cache
(
cls
,
key
,
value
):
"""Insert elements to the ast_cache dictionary"""
cls
.
ast_cache
[
key
]
=
value
def
parse
(
self
):
"""Parse the function or method."""
logger
.
debug
(
"fn = %r"
,
self
.
fn
)
tree
=
None
if
isinstance
(
self
.
fn
,
(
types
.
FunctionType
,
types
.
MethodType
)):
original_src
=
inspect
.
getsource
(
self
.
fn
)
src
=
dedent
(
original_src
)
self
.
col_offset
=
\
len
(
original_src
.
split
(
'
\n
'
)[
0
])
-
len
(
src
.
split
(
'
\n
'
)[
0
])
logger
.
debug
(
"get source = %s"
,
src
)
tree
=
asttokens
.
ASTTokens
(
src
,
parse
=
True
).
tree
hexstr
=
hashlib
.
sha256
(
original_src
.
encode
()).
hexdigest
()
tree
=
Parser
.
get_cache
(
hexstr
)
if
not
tree
:
src
=
dedent
(
original_src
)
self
.
col_offset
=
\
len
(
original_src
.
split
(
'
\n
'
)[
0
])
-
len
(
src
.
split
(
'
\n
'
)[
0
])
logger
.
debug
(
"get source = %s"
,
src
)
tree
=
asttokens
.
ASTTokens
(
src
,
parse
=
True
).
tree
Parser
.
insert_cache
(
hexstr
,
tree
)
else
:
logger
.
error
(
"Fn type is invalid"
)
return
tree
...
...
mindspore/ccsrc/pipeline/parse/resolve.cc
浏览文件 @
274bd253
...
...
@@ -94,7 +94,7 @@ AnfNodePtr ResolveParameterObj(const FuncGraphPtr &func_graph, const py::object
auto
top_graph
=
Parser
::
GetTopFuncGraph
();
// if the parameter node has been created , return it
AnfNodePtr
para_node
=
nullptr
;
for
(
auto
param
:
top_graph
->
parameters
())
{
for
(
auto
const
&
param
:
top_graph
->
parameters
())
{
auto
param_node
=
dyn_cast
<
Parameter
>
(
param
);
if
(
param_node
!=
nullptr
&&
param_node
->
name
()
==
param_name
)
{
para_node
=
param
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录