Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
b7e97160
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看板
提交
b7e97160
编写于
9月 16, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(mge/plugin): add load_binary_tensor, copybara add comp_binary_iodump
GitOrigin-RevId: 8bb5d439d1eb2b024b1061fcb65b1c222bd86f1b
上级
fc74d5f2
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
159 addition
and
0 deletion
+159
-0
imperative/python/megengine/utils/compare_binary_iodump.py
imperative/python/megengine/utils/compare_binary_iodump.py
+96
-0
imperative/python/megengine/utils/deprecation.py
imperative/python/megengine/utils/deprecation.py
+7
-0
imperative/python/megengine/utils/plugin.py
imperative/python/megengine/utils/plugin.py
+56
-0
未找到文件。
imperative/python/megengine/utils/compare_binary_iodump.py
0 → 100755
浏览文件 @
b7e97160
# MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
#
# Copyright (c) 2014-2020 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.
import
argparse
import
os
import
textwrap
from
pathlib
import
Path
import
numpy
as
np
from
megengine.utils
import
plugin
def
check
(
v0
,
v1
,
name
,
max_err
):
v0
=
np
.
ascontiguousarray
(
v0
,
dtype
=
np
.
float32
)
v1
=
np
.
ascontiguousarray
(
v1
,
dtype
=
np
.
float32
)
assert
np
.
isfinite
(
v0
.
sum
())
and
np
.
isfinite
(
v1
.
sum
()
),
"{} not finite: sum={} vs sum={}"
.
format
(
name
,
v0
.
sum
(),
v1
.
sum
())
assert
v0
.
shape
==
v1
.
shape
,
"{} shape mismatch: {} vs {}"
.
format
(
name
,
v0
.
shape
,
v1
.
shape
)
vdiv
=
np
.
max
([
np
.
abs
(
v0
),
np
.
abs
(
v1
),
np
.
ones_like
(
v0
)],
axis
=
0
)
err
=
np
.
abs
(
v0
-
v1
)
/
vdiv
check
=
err
>
max_err
if
check
.
sum
():
idx
=
tuple
(
i
[
0
]
for
i
in
np
.
nonzero
(
check
))
raise
AssertionError
(
"{} not equal: "
"shape={} nonequal_idx={} v0={} v1={} err={}"
.
format
(
name
,
v0
.
shape
,
idx
,
v0
[
idx
],
v1
[
idx
],
err
[
idx
]
)
)
def
main
():
parser
=
argparse
.
ArgumentParser
(
description
=
(
"compare tensor dumps generated BinaryOprIODump plugin, "
"it can compare two dirs or two single files"
),
formatter_class
=
argparse
.
ArgumentDefaultsHelpFormatter
,
)
parser
.
add_argument
(
"input0"
,
help
=
"dirname or filename"
)
parser
.
add_argument
(
"input1"
,
help
=
"dirname or filename"
)
parser
.
add_argument
(
"-e"
,
"--max-err"
,
type
=
float
,
default
=
1e-3
,
help
=
"max allowed error"
)
parser
.
add_argument
(
"-s"
,
"--stop-on-error"
,
action
=
"store_true"
,
help
=
"do not compare "
)
args
=
parser
.
parse_args
()
files0
=
set
()
files1
=
set
()
if
os
.
path
.
isdir
(
args
.
input0
):
assert
os
.
path
.
isdir
(
args
.
input1
)
name0
=
set
()
name1
=
set
()
for
i
in
os
.
listdir
(
args
.
input0
):
files0
.
add
(
str
(
Path
(
args
.
input0
)
/
i
))
name0
.
add
(
i
)
for
i
in
os
.
listdir
(
args
.
input1
):
files1
.
add
(
str
(
Path
(
args
.
input1
)
/
i
))
name1
.
add
(
i
)
assert
name0
==
name1
,
"dir files mismatch: a-b={} b-a={}"
.
format
(
name0
-
name1
,
name1
-
name0
)
else
:
files0
.
add
(
args
.
input0
)
files1
.
add
(
args
.
input1
)
files0
=
sorted
(
files0
)
files1
=
sorted
(
files1
)
for
i
,
j
in
zip
(
files0
,
files1
):
val0
,
name0
=
plugin
.
load_tensor_binary
(
i
)
val1
,
name1
=
plugin
.
load_tensor_binary
(
j
)
name
=
"{}:
\n
{}
\n
{}
\n
"
.
format
(
i
,
"
\n
"
.
join
(
textwrap
.
wrap
(
name0
)),
"
\n
"
.
join
(
textwrap
.
wrap
(
name1
))
)
try
:
check
(
val0
,
val1
,
name
,
args
.
max_err
)
except
Exception
as
exc
:
if
args
.
stop_on_error
:
raise
exc
print
(
exc
)
if
__name__
==
"__main__"
:
main
()
imperative/python/megengine/utils/deprecation.py
浏览文件 @
b7e97160
# MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
#
# Copyright (c) 2014-2020 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.
from
deprecated.sphinx
import
deprecated
imperative/python/megengine/utils/plugin.py
0 → 100644
浏览文件 @
b7e97160
# -*- coding: utf-8 -*-
# MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
#
# Copyright (c) 2014-2020 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.
import
struct
import
numpy
as
np
def
load_tensor_binary
(
fobj
):
"""load a tensor dumped by the :class:`BinaryOprIODump` plugin; the actual
tensor value dump is implemented by ``mgb::debug::dump_tensor``.
Multiple values can be compared by ``tools/compare_binary_iodump.py``.
:param fobj: file object, or a string that contains the file name
:return: tuple ``(tensor_value, tensor_name)``
"""
if
isinstance
(
fobj
,
str
):
with
open
(
fobj
,
"rb"
)
as
fin
:
return
load_tensor_binary
(
fin
)
DTYPE_LIST
=
{
0
:
np
.
float32
,
1
:
np
.
uint8
,
2
:
np
.
int8
,
3
:
np
.
int16
,
4
:
np
.
int32
,
# 5: _mgb.intb1,
# 6: _mgb.intb2,
# 7: _mgb.intb4,
8
:
None
,
9
:
np
.
float16
,
# quantized dtype start from 100000
# see MEGDNN_PARAMETERIZED_DTYPE_ENUM_BASE in
# dnn/include/megdnn/dtype.h
100000
:
np
.
uint8
,
100001
:
np
.
int32
,
100002
:
np
.
int8
,
}
header_fmt
=
struct
.
Struct
(
"III"
)
name_len
,
dtype
,
max_ndim
=
header_fmt
.
unpack
(
fobj
.
read
(
header_fmt
.
size
))
assert
(
DTYPE_LIST
[
dtype
]
is
not
None
),
"Cannot load this tensor: dtype Byte is unsupported."
shape
=
list
(
struct
.
unpack
(
"I"
*
max_ndim
,
fobj
.
read
(
max_ndim
*
4
)))
while
shape
[
-
1
]
==
0
:
shape
.
pop
(
-
1
)
name
=
fobj
.
read
(
name_len
).
decode
(
"ascii"
)
return
np
.
fromfile
(
fobj
,
dtype
=
DTYPE_LIST
[
dtype
]).
reshape
(
shape
),
name
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录