Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
7fadc16d
MegEngine
项目概览
MegEngine 天元
/
MegEngine
大约 1 年 前同步成功
通知
399
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看板
提交
7fadc16d
编写于
8月 31, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(mge/functional): support tensor shape in interpolate and split
GitOrigin-RevId: 6430b64f010ea5d0ecb1caa59b6da0a1547552ae
上级
968f74ce
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
27 addition
and
24 deletion
+27
-24
imperative/python/megengine/core/tensor/utils.py
imperative/python/megengine/core/tensor/utils.py
+3
-1
imperative/python/megengine/functional/elemwise.py
imperative/python/megengine/functional/elemwise.py
+12
-5
imperative/python/megengine/functional/nn.py
imperative/python/megengine/functional/nn.py
+2
-5
imperative/python/megengine/functional/tensor.py
imperative/python/megengine/functional/tensor.py
+6
-4
imperative/python/test/unit/functional/test_elemwise.py
imperative/python/test/unit/functional/test_elemwise.py
+4
-4
imperative/python/test/unit/functional/test_functional.py
imperative/python/test/unit/functional/test_functional.py
+0
-3
imperative/python/test/unit/functional/test_tensor.py
imperative/python/test/unit/functional/test_tensor.py
+0
-2
未找到文件。
imperative/python/megengine/core/tensor/utils.py
浏览文件 @
7fadc16d
...
@@ -31,7 +31,9 @@ def dtype_promotion(raw_inputs):
...
@@ -31,7 +31,9 @@ def dtype_promotion(raw_inputs):
]
]
inputs
=
[
i
for
i
in
raw_inputs
if
hasattr
(
i
,
"dtype"
)]
inputs
=
[
i
for
i
in
raw_inputs
if
hasattr
(
i
,
"dtype"
)]
assert
len
(
scalar_inputs
+
inputs
)
>
0
assert
len
(
scalar_inputs
+
inputs
)
>
0
dtype
=
np
.
result_type
(
*
inputs
)
dtype
=
None
if
len
(
inputs
)
>
0
:
dtype
=
np
.
result_type
(
*
inputs
)
dtype_all
=
np
.
result_type
(
*
(
inputs
+
scalar_inputs
))
dtype_all
=
np
.
result_type
(
*
(
inputs
+
scalar_inputs
))
assert
(
assert
(
dtype
!=
np
.
float64
and
dtype
!=
np
.
int64
dtype
!=
np
.
float64
and
dtype
!=
np
.
int64
...
...
imperative/python/megengine/functional/elemwise.py
浏览文件 @
7fadc16d
...
@@ -10,8 +10,9 @@
...
@@ -10,8 +10,9 @@
import
functools
import
functools
from
..core.ops
import
builtin
from
..core.ops
import
builtin
from
..core.tensor
import
utils
from
..core.tensor
import
megbrain_graph
,
utils
from
..core.tensor.core
import
apply
from
..core.tensor.core
import
apply
from
..device
import
get_default_device
from
..tensor
import
Tensor
from
..tensor
import
Tensor
__all__
=
[
__all__
=
[
...
@@ -76,11 +77,17 @@ __all__ = [
...
@@ -76,11 +77,17 @@ __all__ = [
def
_elwise
(
*
args
,
mode
):
def
_elwise
(
*
args
,
mode
):
op
=
builtin
.
Elemwise
(
mode
=
mode
)
op
=
builtin
.
Elemwise
(
mode
=
mode
)
tensor_args
=
list
(
filter
(
lambda
x
:
isinstance
(
x
,
(
Tensor
,
megbrain_graph
.
VarNode
)),
args
)
)
if
len
(
tensor_args
)
==
0
:
dtype
=
utils
.
dtype_promotion
(
args
)
first_arg
=
Tensor
(
args
[
0
],
dtype
=
dtype
,
device
=
get_default_device
())
args
=
utils
.
convert_inputs
(
first_arg
,
*
args
[
1
:])
else
:
args
=
utils
.
convert_inputs
(
*
args
)
if
mode
in
(
"true_div"
,
"exp"
,
"pow"
,
"log"
,
"expm1"
,
"log1p"
):
if
mode
in
(
"true_div"
,
"exp"
,
"pow"
,
"log"
,
"expm1"
,
"log1p"
):
args
=
tuple
(
args
=
tuple
(
map
(
lambda
x
:
x
.
astype
(
"float32"
),
args
))
map
(
lambda
x
:
x
.
astype
(
"float32"
)
if
hasattr
(
x
,
"dtype"
)
else
x
,
args
)
)
args
=
utils
.
convert_inputs
(
*
args
)
(
result
,)
=
apply
(
op
,
*
args
)
(
result
,)
=
apply
(
op
,
*
args
)
return
result
return
result
...
...
imperative/python/megengine/functional/nn.py
浏览文件 @
7fadc16d
...
@@ -1126,11 +1126,8 @@ def interpolate(
...
@@ -1126,11 +1126,8 @@ def interpolate(
if
mode
==
"LINEAR"
:
if
mode
==
"LINEAR"
:
inp
=
add_axis
(
inp
,
3
)
inp
=
add_axis
(
inp
,
3
)
if
not
isinstance
(
inp
.
shape
,
inp
.
__class__
):
if
inp
.
ndim
!=
4
:
if
len
(
inp
.
shape
)
!=
4
:
raise
ValueError
(
"shape of input tensor must correspond to the operartion mode"
)
raise
ValueError
(
"shape of input tensor must correspond to the operartion mode"
)
if
size
is
None
:
if
size
is
None
:
if
scale_factor
is
None
:
if
scale_factor
is
None
:
...
...
imperative/python/megengine/functional/tensor.py
浏览文件 @
7fadc16d
...
@@ -317,7 +317,7 @@ def split(inp, nsplits_or_sections, axis=0):
...
@@ -317,7 +317,7 @@ def split(inp, nsplits_or_sections, axis=0):
def
swapaxis
(
inp
,
src
,
dst
):
def
swapaxis
(
inp
,
src
,
dst
):
if
src
==
dst
:
if
src
==
dst
:
return
inp
return
inp
shape
=
[
i
for
i
in
range
(
len
(
inp
.
shape
)
)]
shape
=
[
i
for
i
in
range
(
inp
.
ndim
)]
shape
[
src
]
=
dst
shape
[
src
]
=
dst
shape
[
dst
]
=
src
shape
[
dst
]
=
src
return
inp
.
transpose
(
shape
)
return
inp
.
transpose
(
shape
)
...
@@ -325,9 +325,11 @@ def split(inp, nsplits_or_sections, axis=0):
...
@@ -325,9 +325,11 @@ def split(inp, nsplits_or_sections, axis=0):
inp
=
swapaxis
(
inp
,
0
,
axis
)
inp
=
swapaxis
(
inp
,
0
,
axis
)
if
isinstance
(
nsplits_or_sections
,
int
):
if
isinstance
(
nsplits_or_sections
,
int
):
incr_step
=
math
.
ceil
(
inp
.
shape
[
0
]
/
nsplits_or_sections
)
incr_step
=
ceil
(
inp
.
shape
[
0
]
/
nsplits_or_sections
)
while
incr_step
<
inp
.
shape
[
0
]:
nsplits
=
nsplits_or_sections
sections
.
append
(
incr_step
)
while
nsplits
>
0
:
nsplits
-=
1
sections
.
append
(
incr_step
.
astype
(
"int32"
))
incr_step
+=
nsplits_or_sections
incr_step
+=
nsplits_or_sections
else
:
else
:
sections
=
nsplits_or_sections
sections
=
nsplits_or_sections
...
...
imperative/python/test/unit/functional/test_elemwise.py
浏览文件 @
7fadc16d
...
@@ -19,13 +19,13 @@ def test_abs():
...
@@ -19,13 +19,13 @@ def test_abs():
np
.
abs
(
np
.
array
([
-
3.0
,
-
4.0
,
-
5.0
],
dtype
=
np
.
float32
)),
np
.
abs
(
np
.
array
([
-
3.0
,
-
4.0
,
-
5.0
],
dtype
=
np
.
float32
)),
)
)
# assertTensorClose(F.abs(-3.0
), np.abs(np.float32(-3.0)))
assertTensorClose
(
F
.
abs
(
-
3.0
).
numpy
(
),
np
.
abs
(
np
.
float32
(
-
3.0
)))
def
test_multiply
():
def
test_multiply
():
#
assertTensorClose(
assertTensorClose
(
# F.mul(-3.0, -4.0
), np.multiply(np.float32(-3.0), np.float32(-4.0))
F
.
mul
(
-
3.0
,
-
4.0
).
numpy
(
),
np
.
multiply
(
np
.
float32
(
-
3.0
),
np
.
float32
(
-
4.0
))
#
)
)
assertTensorClose
(
assertTensorClose
(
F
.
mul
(
tensor
([
3.0
,
4.0
]),
4.0
).
numpy
(),
F
.
mul
(
tensor
([
3.0
,
4.0
]),
4.0
).
numpy
(),
...
...
imperative/python/test/unit/functional/test_functional.py
浏览文件 @
7fadc16d
...
@@ -194,9 +194,6 @@ def test_matmul():
...
@@ -194,9 +194,6 @@ def test_matmul():
def
test_interpolate
():
def
test_interpolate
():
if
use_tensor_shape
():
# XXX: please fix me
return
def
linear_interpolate
():
def
linear_interpolate
():
inp
=
tensor
(
np
.
arange
(
1
,
3
,
dtype
=
np
.
float32
).
reshape
(
1
,
1
,
2
))
inp
=
tensor
(
np
.
arange
(
1
,
3
,
dtype
=
np
.
float32
).
reshape
(
1
,
1
,
2
))
...
...
imperative/python/test/unit/functional/test_tensor.py
浏览文件 @
7fadc16d
...
@@ -125,8 +125,6 @@ def test_stack():
...
@@ -125,8 +125,6 @@ def test_stack():
def
test_split
():
def
test_split
():
if
use_tensor_shape
():
# XXX: please fix me
return
data
=
np
.
random
.
random
((
2
,
3
,
4
,
5
)).
astype
(
np
.
float32
)
data
=
np
.
random
.
random
((
2
,
3
,
4
,
5
)).
astype
(
np
.
float32
)
mge_out1
=
F
.
split
(
tensor
(
data
),
2
,
axis
=
3
)
mge_out1
=
F
.
split
(
tensor
(
data
),
2
,
axis
=
3
)
mge_out2
=
F
.
split
(
tensor
(
data
),
[
3
,
5
],
axis
=
3
)
mge_out2
=
F
.
split
(
tensor
(
data
),
[
3
,
5
],
axis
=
3
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录