Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
e6b06914
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看板
提交
e6b06914
编写于
3月 12, 2021
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(mge/tensor): support non-Tensor value in `_reset` and remove depreciated tests
GitOrigin-RevId: faf6c78aa8f6d7c43c95dc174261cc5c5d9edac1
上级
5a38ad39
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
19 addition
and
101 deletion
+19
-101
imperative/python/megengine/core/tensor/array_method.py
imperative/python/megengine/core/tensor/array_method.py
+9
-10
imperative/python/megengine/tensor.py
imperative/python/megengine/tensor.py
+2
-2
imperative/python/test/unit/core/test_tensor_wrapper.py
imperative/python/test/unit/core/test_tensor_wrapper.py
+8
-0
imperative/python/test/unit/module/test_module_tensor.py
imperative/python/test/unit/module/test_module_tensor.py
+0
-89
未找到文件。
imperative/python/megengine/core/tensor/array_method.py
浏览文件 @
e6b06914
...
...
@@ -16,7 +16,6 @@ from .._imperative_rt.common import CompNode
from
.._imperative_rt.core2
import
Tensor
,
apply
from
..ops
import
builtin
from
..ops.builtin
import
Elemwise
,
GetVarShape
from
..ops.special
import
Const
from
.
import
utils
from
.indexing
import
getitem
as
_getitem
from
.indexing
import
setitem
as
_setitem
...
...
@@ -373,7 +372,7 @@ class ArrayMethodMixin(abc.ABC):
@
property
def
size
(
self
):
r
"""
Returns the size of the self :class:`~.Tensor`.
Returns the size of the self :class:`~.Tensor`.
The returned value is a subclass of :class:`tuple`.
"""
shape
=
self
.
shape
...
...
@@ -390,7 +389,7 @@ class ArrayMethodMixin(abc.ABC):
def
item
(
self
,
*
args
):
r
"""
Returns the value of this :class:`~.Tensor` as a standard Python :class:`numbers.Number`.
Returns the value of this :class:`~.Tensor` as a standard Python :class:`numbers.Number`.
This only works for tensors with one element. For other cases, see :meth:`~.tolist`.
"""
if
not
args
:
...
...
@@ -401,8 +400,8 @@ class ArrayMethodMixin(abc.ABC):
def
tolist
(
self
):
r
"""
Returns the tensor as a (nested) list.
For scalars, a standard Python number is returned, just like with :meth:`~.item`.
Returns the tensor as a (nested) list.
For scalars, a standard Python number is returned, just like with :meth:`~.item`.
Tensors are automatically moved to the CPU first if necessary.
This operation is not differentiable.
...
...
@@ -450,7 +449,7 @@ class ArrayMethodMixin(abc.ABC):
def
sum
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
r
"""
Returns the sum of each row of the input tensor in the given dimension ``axis``.
If ``axis`` is a list of axises, reduce over all of them.
If ``keepdims`` is ``True``, the shape of output tensor is the same as the input tensor,
except in the dimension(s) ``axis`` where it is of size 1.
...
...
@@ -483,7 +482,7 @@ class ArrayMethodMixin(abc.ABC):
def
prod
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
r
"""
Returns the product of each row of the input tensor in the given dimension ``axis``.
If ``axis`` is a list of axises, reduce over all of them.
If ``keepdims`` is ``True``, the shape of output tensor is the same as the input tensor,
except in the dimension(s) ``axis`` where it is of size 1.
...
...
@@ -516,7 +515,7 @@ class ArrayMethodMixin(abc.ABC):
def
min
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
r
"""
Returns the min value of each row of the input tensor in the given dimension ``axis``.
If ``axis`` is a list of axises, reduce over all of them.
If ``keepdims`` is ``True``, the shape of output tensor is the same as the input tensor,
except in the dimension(s) ``axis`` where it is of size 1.
...
...
@@ -549,7 +548,7 @@ class ArrayMethodMixin(abc.ABC):
def
max
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
r
"""
Returns the max value of each row of the input tensor in the given dimension ``axis``.
If ``axis`` is a list of axises, reduce over all of them.
If ``keepdims`` is ``True``, the shape of output tensor is the same as the input tensor,
except in the dimension(s) ``axis`` where it is of size 1.
...
...
@@ -582,7 +581,7 @@ class ArrayMethodMixin(abc.ABC):
def
mean
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
r
"""
Returns the mean value of each row of the input tensor in the given dimension ``axis``.
If ``axis`` is a list of axises, reduce over all of them.
If ``keepdims`` is ``True``, the shape of output tensor is the same as the input tensor,
except in the dimension(s) ``axis`` where it is of size 1.
...
...
imperative/python/megengine/tensor.py
浏览文件 @
e6b06914
...
...
@@ -119,6 +119,8 @@ class Tensor(_Tensor, ArrayMethodMixin):
return
super
().
detach
()
def
_reset
(
self
,
other
):
if
not
isinstance
(
other
,
_Tensor
):
other
=
Tensor
(
other
,
dtype
=
self
.
dtype
,
device
=
self
.
device
)
super
().
_reset
(
other
)
def
__repr__
(
self
):
...
...
@@ -141,8 +143,6 @@ class Tensor(_Tensor, ArrayMethodMixin):
@
deprecated
(
version
=
"1.0"
,
reason
=
"no need to reuse an existing tensor since 1.0"
)
def
set_value
(
self
,
value
):
if
not
isinstance
(
value
,
_Tensor
):
value
=
Tensor
(
value
,
dtype
=
self
.
dtype
,
device
=
self
.
device
)
self
.
_reset
(
value
)
@
deprecated
(
version
=
"1.0"
,
reason
=
"use *= 0 instead"
)
...
...
imperative/python/test/unit/core/test_tensor_wrapper.py
浏览文件 @
e6b06914
...
...
@@ -50,6 +50,14 @@ def test_reduce():
test_x
(
np
.
array
([
True
,
False
,
True
]))
def
test_set_value
():
v0
=
np
.
random
.
random
((
2
,
3
)).
astype
(
np
.
float32
)
param
=
Tensor
(
v0
)
v1
=
np
.
random
.
random
((
2
,
3
)).
astype
(
np
.
float32
)
param
[...]
=
v1
np
.
testing
.
assert_allclose
(
param
.
numpy
(),
v1
,
atol
=
5e-6
)
def
test_set_subtensor
():
x
=
Tensor
([
1
,
2
,
3
])
x
[:]
=
[
1
,
1
,
1
]
...
...
imperative/python/test/unit/module/test_module_tensor.py
已删除
100644 → 0
浏览文件 @
5a38ad39
# -*- coding: utf-8 -*-
# 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.
import
copy
import
numpy
as
np
import
pytest
import
megengine
as
mge
import
megengine.functional
as
F
from
megengine
import
Parameter
,
Tensor
from
megengine.module
import
Conv2d
# TODO: delete this test after deleting set_value
def
test_set_value
():
v0
=
np
.
random
.
random
((
2
,
3
)).
astype
(
np
.
float32
)
param
=
Parameter
(
v0
)
v1
=
np
.
random
.
random
((
2
,
3
)).
astype
(
np
.
float32
)
param
.
set_value
(
v1
)
np
.
testing
.
assert_allclose
(
param
.
numpy
(),
v1
,
atol
=
5e-6
)
v2
=
np
.
random
.
random
((
3
,
3
)).
astype
(
np
.
float32
)
# TODO: add this
# with pytest.raises(ValueError):
# param.set_value(v2)
np
.
testing
.
assert_allclose
(
param
.
numpy
(),
v1
,
atol
=
5e-6
)
@
pytest
.
mark
.
skip
(
reason
=
"fill unsupported"
)
def
test_fill
():
a
=
Tensor
(
np
.
zeros
((
2
,
3
),
dtype
=
np
.
float32
))
a
.
fill
(
3
)
np
.
testing
.
assert_allclose
(
a
.
numpy
(),
np
.
full
((
2
,
3
),
3
,
dtype
=
np
.
float32
))
a
.
fill
(
124.568
)
np
.
testing
.
assert_allclose
(
a
.
numpy
(),
np
.
full
((
2
,
3
),
124.568
,
dtype
=
np
.
float32
))
# TODO: remove or rewrite following test
# def test_attach():
# p_ = np.random.random((2, 3)).astype(np.float32)
# with Graph() as g:
# g.set_option('eager_evaluation', False)
# p = Parameter(p_)
# v = p * 2
# f = compile(v, None)
# out, = f()
# np.testing.assert_allclose(out, p_ * 2)
# F.add_update(p, p)
# out, = f()
# np.testing.assert_allclose(out, p_ * 4)
# TODO: remove or rewrite following test
# def test_module_attach():
# v = np.random.random((1, 3, 64, 64)).astype(np.float32)
# net = Conv2d(3, 16, 3)
# with Graph() as g:
# g.set_option('eager_evaluation', False)
# data0 = Input("data")
# f = compile(net(data0), None)
# out0, = f(data=v)
# data1 = Input("data", value=v)
# out1 = net(data1)
# np.testing.assert_allclose(out0, out1.numpy())
# def test_shape_warning():
# with Graph() as cg:
# cg.set_option("eager_evaluation", False)
# b = Tensor(np.ones((2, 3)).astype(np.float32))
# with pytest.warns(None) as record:
# print(b.shape)
# if len(record) != 0:
# raise ValueError(
# "Getting the shape of a constant Tensor should throw no Warning"
# )
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录