Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
2f3bc2db
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看板
提交
2f3bc2db
编写于
3月 11, 2022
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
perf(mge/utils): move astensor1d into C++
GitOrigin-RevId: e7c6659020d9db5a4b17f2a35a40ea2a99f7330d
上级
fa62f6c0
变更
8
展开全部
隐藏空白更改
内联
并排
Showing
8 changed file
with
588 addition
and
563 deletion
+588
-563
imperative/python/megengine/core/_trace_option.py
imperative/python/megengine/core/_trace_option.py
+5
-0
imperative/python/megengine/core/tensor/array_method.py
imperative/python/megengine/core/tensor/array_method.py
+4
-4
imperative/python/megengine/core/tensor/indexing.py
imperative/python/megengine/core/tensor/indexing.py
+0
-28
imperative/python/megengine/core/tensor/utils.py
imperative/python/megengine/core/tensor/utils.py
+2
-36
imperative/python/src/tensor.cpp
imperative/python/src/tensor.cpp
+3
-4
imperative/python/src/tensor_utils.cpp
imperative/python/src/tensor_utils.cpp
+524
-458
imperative/python/src/tensor_utils.h
imperative/python/src/tensor_utils.h
+2
-0
imperative/python/test/unit/core/test_indexing_op.py
imperative/python/test/unit/core/test_indexing_op.py
+48
-33
未找到文件。
imperative/python/megengine/core/_trace_option.py
浏览文件 @
2f3bc2db
...
...
@@ -9,6 +9,8 @@
import
os
from
._imperative_rt.core2
import
set_cpp_use_symbolic_shape
_use_symbolic_shape
=
False
if
os
.
environ
.
get
(
"MEGENGINE_USE_SYMBOLIC_SHAPE"
):
_use_symbolic_shape
=
True
...
...
@@ -25,3 +27,6 @@ def set_symbolic_shape(option: bool):
_org
=
_use_symbolic_shape
_use_symbolic_shape
=
option
return
_org
set_cpp_use_symbolic_shape
(
use_symbolic_shape
)
imperative/python/megengine/core/tensor/array_method.py
浏览文件 @
2f3bc2db
...
...
@@ -22,12 +22,12 @@ from .._imperative_rt.core2 import (
astype_cpp
,
broadcast_cpp
,
dtype_promotion
,
getitem_cpp
,
)
from
.._imperative_rt.core2
import
reduce_to_scalar
as
_reduce_to_scalar
from
.._imperative_rt.core2
import
reshape_cpp
,
squeeze_cpp
,
transpose_cpp
from
.._imperative_rt.core2
import
reshape_cpp
,
s
etitem_cpp
,
s
queeze_cpp
,
transpose_cpp
from
..ops
import
builtin
from
.
import
amp
from
.indexing
import
getitem
,
setitem
from
.utils
import
_normalize_axis
,
astensor1d
,
cast_tensors
,
make_shape_tuple
,
subgraph
_ElwMod
=
builtin
.
Elemwise
.
Mode
...
...
@@ -544,11 +544,11 @@ class ArrayMethodMixin(abc.ABC):
yield
self
[
i
]
def
__getitem__
(
self
,
index
):
return
getitem
(
self
,
index
)
return
getitem
_cpp
(
self
,
index
)
def
__setitem__
(
self
,
index
,
value
):
if
index
is
not
Ellipsis
:
value
=
setitem
(
self
,
index
,
value
)
value
=
setitem
_cpp
(
self
,
index
,
value
)
self
.
_reset
(
value
)
__contains__
=
_todo
...
...
imperative/python/megengine/core/tensor/indexing.py
已删除
100644 → 0
浏览文件 @
fa62f6c0
# -*- 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.
from
.._imperative_rt.core2
import
(
getitem_cpp
,
set_cpp_astensor1d
,
set_cpp_use_symbolic_shape
,
setitem_cpp
,
)
from
.._trace_option
import
use_symbolic_shape
from
.utils
import
astensor1d
def
getitem
(
tensor
,
index
):
return
getitem_cpp
(
tensor
,
index
)
def
setitem
(
tensor
,
index
,
value
):
return
setitem_cpp
(
tensor
,
index
,
value
)
set_cpp_use_symbolic_shape
(
use_symbolic_shape
)
set_cpp_astensor1d
(
astensor1d
)
imperative/python/megengine/core/tensor/utils.py
浏览文件 @
2f3bc2db
...
...
@@ -20,6 +20,7 @@ from .._imperative_rt.core2 import (
_get_convert_inputs
,
_set_convert_inputs
,
apply
,
astensor1d_cpp
,
astype_cpp
,
convert_inputs_cpp
,
convert_single_value_cpp
,
...
...
@@ -50,14 +51,6 @@ def set_convert_inputs(flag):
return
_set_convert_inputs
(
flag
)
def
concatenate
(
inputs
,
axis
=
0
,
*
,
device
=
None
):
inputs
=
convert_inputs
(
*
inputs
)
if
device
is
None
:
device
=
get_device
(
inputs
)
(
result
,)
=
apply
(
builtin
.
Concat
(
axis
=
axis
,
comp_node
=
device
),
*
inputs
)
return
result
def
convert_single_value
(
v
,
*
,
dtype
=
None
,
device
=
None
):
return
convert_single_value_cpp
(
v
,
dtype
,
device
)
...
...
@@ -104,34 +97,7 @@ def astensor1d(x, *reference, dtype=None, device=None):
* numpy array
* tensor (returned as is, regardless of dtype and device)
"""
try
:
ndim
=
x
.
ndim
except
AttributeError
:
pass
except
ValueError
:
if
dtype
is
not
None
and
dtype
!=
x
.
dtype
:
x
=
astype_cpp
(
x
,
dtype
)
if
device
is
not
None
:
cn
=
as_device
(
device
).
to_c
()
(
x
,)
=
apply
(
builtin
.
Copy
(
comp_node
=
cn
),
x
)
return
x
else
:
if
ndim
!=
0
and
ndim
!=
1
:
raise
ValueError
(
"ndim != 1 or 0, get : %d"
%
ndim
)
if
not
isinstance
(
x
,
(
Tensor
,
SymbolVar
)):
x
=
Const
(
x
,
dtype
,
device
,
reference
)
return
x
if
not
isinstance
(
x
,
collections
.
abc
.
Sequence
):
raise
TypeError
if
any
(
isinstance
(
i
,
(
Tensor
,
SymbolVar
))
for
i
in
x
):
x
=
concatenate
(
x
,
device
=
device
)
if
len
(
x
)
>
1
else
x
[
0
]
if
dtype
is
not
None
:
x
=
astype_cpp
(
x
,
dtype
)
return
x
x
=
Const
(
x
,
dtype
,
device
,
reference
)
return
x
return
astensor1d_cpp
(
x
,
dtype
,
device
,
reference
)
def
_normalize_axis
(
...
...
imperative/python/src/tensor.cpp
浏览文件 @
2f3bc2db
...
...
@@ -104,13 +104,12 @@ struct SymbolVarContext {
interpreter
::
Interpreter
::
Channel
*
interpreter_for_py
=
nullptr
;
PyTypeObject
*
py_tensor_type
=
nullptr
;
PyObject
*
cpp_use_symbolic_shape
,
*
cpp_astensor1d
;
PyObject
*
cpp_use_symbolic_shape
;
#define REGISTE_APPLY_FUNC(mode) \
void set_##mode(py::object pyf) { mode = pyf.ptr(); }
REGISTE_APPLY_FUNC
(
cpp_use_symbolic_shape
)
REGISTE_APPLY_FUNC
(
cpp_astensor1d
)
#undef REGISTE_APPLY_FUNC
...
...
@@ -426,6 +425,7 @@ WRAP_FUNC_PY35(Const);
WRAP_FUNC_PY35
(
astype_cpp
);
WRAP_FUNC_PY35
(
convert_single_value_cpp
);
WRAP_FUNC_PY35
(
convert_inputs_cpp
);
WRAP_FUNC_PY35
(
astensor1d_cpp
);
#undef WRAP_FUNC_PY35
#define MGE_PY_INTERFACE(NAME, FUNC) \
{ #NAME, (PyCFunction)py35_##FUNC, METH_VARARGS, nullptr }
...
...
@@ -568,6 +568,7 @@ void init_tensor(py::module m) {
MGE_PY_INTERFACE
(
astype_cpp
,
astype_cpp
),
MGE_PY_INTERFACE
(
convert_single_value_cpp
,
convert_single_value_cpp
),
MGE_PY_INTERFACE
(
convert_inputs_cpp
,
convert_inputs_cpp
),
MGE_PY_INTERFACE
(
astensor1d_cpp
,
astensor1d_cpp
),
{
nullptr
,
nullptr
,
0
,
nullptr
}};
for
(
auto
&&
def
:
method_defs
)
{
if
(
def
.
ml_meth
!=
nullptr
)
{
...
...
@@ -957,8 +958,6 @@ void init_tensor(py::module m) {
m
.
def
(
"set_cpp_use_symbolic_shape"
,
&
set_cpp_use_symbolic_shape
);
m
.
def
(
"set_cpp_astensor1d"
,
&
set_cpp_astensor1d
);
m
.
def
(
"set_module_tracing"
,
[
=
]
{
get_module_trace
()
->
enable
();
});
m
.
def
(
"unset_module_tracing"
,
[
=
]
{
get_module_trace
()
->
disable
();
});
...
...
imperative/python/src/tensor_utils.cpp
浏览文件 @
2f3bc2db
此差异已折叠。
点击以展开。
imperative/python/src/tensor_utils.h
浏览文件 @
2f3bc2db
...
...
@@ -32,4 +32,6 @@ PyObject* convert_single_value_cpp(PyObject* self, PyObject* const* args, size_t
PyObject
*
convert_inputs_cpp
(
PyObject
*
self
,
PyObject
*
const
*
args
,
size_t
nargs
);
PyObject
*
astensor1d_cpp
(
PyObject
*
self
,
PyObject
*
const
*
args
,
size_t
nargs
);
}
// namespace mgb::imperative::python
\ No newline at end of file
imperative/python/test/unit/core/test_indexing_op.py
浏览文件 @
2f3bc2db
...
...
@@ -511,6 +511,20 @@ def test_advance_indexing_with_bool(test_varnode):
network
=
Network
()
else
:
network
=
None
a
=
np
.
array
([[
True
,
False
],
[
False
,
True
]])
b
=
np
.
array
([
1
])
aa
=
make_tensor
(
a
,
network
)
bb
=
make_tensor
(
b
,
network
)
np
.
testing
.
assert_equal
(
a
[
b
],
get_value
(
aa
[
bb
]))
b
=
np
.
array
([[
True
,
True
],
[
False
,
True
]])
bb
=
make_tensor
(
b
,
network
)
np
.
testing
.
assert_equal
(
a
[
b
],
get_value
(
aa
[
bb
]))
if
not
test_varnode
:
a
[
b
]
=
False
aa
[
bb
]
=
False
np
.
testing
.
assert_equal
(
a
,
get_value
(
aa
))
a
=
np
.
arange
(
9
).
reshape
(
3
,
3
).
astype
(
np
.
float32
)
b
=
np
.
array
([
1
,
2
,
3
])
c
=
np
.
array
([
1
,
2
,
3
])
...
...
@@ -525,67 +539,68 @@ def test_advance_indexing_with_bool(test_varnode):
a
=
np
.
arange
(
9
).
reshape
(
3
,
3
).
astype
(
np
.
float32
)
b
=
np
.
array
([
False
,
True
,
True
])
c
=
np
.
array
([
2
,
0
]).
astype
(
np
.
int32
)
aa
=
Tensor
(
a
)
bb
=
Tensor
(
b
)
cc
=
Tensor
(
c
)
np
.
testing
.
assert_equal
(
a
[
b
,
c
],
aa
[
bb
,
cc
].
numpy
(
))
aa
=
make_tensor
(
a
,
network
)
bb
=
make_tensor
(
b
,
network
)
cc
=
make_tensor
(
c
,
network
)
np
.
testing
.
assert_equal
(
a
[
b
,
c
],
get_value
(
aa
[
bb
,
cc
]
))
a
[
b
,
c
]
=
-
1.0
aa
[
bb
,
cc
]
=
-
1.0
np
.
testing
.
assert_equal
(
a
,
aa
.
numpy
(
))
np
.
testing
.
assert_equal
(
a
,
get_value
(
aa
))
d
=
np
.
array
([
-
1
,
-
2
],
dtype
=
np
.
float32
)
dd
=
Tensor
(
d
)
dd
=
make_tensor
(
d
,
network
)
a
[
b
,
c
]
=
d
aa
[
bb
,
cc
]
=
dd
np
.
testing
.
assert_equal
(
a
,
aa
.
numpy
(
))
np
.
testing
.
assert_equal
(
a
,
get_value
(
aa
))
a
=
np
.
ones
((
2
,
2
))
b
=
np
.
array
([[
True
,
False
],
[
False
,
True
]])
aa
=
Tensor
(
a
)
bb
=
Tensor
(
b
)
np
.
testing
.
assert_equal
(
a
[
b
],
aa
[
bb
].
numpy
(
))
aa
=
make_tensor
(
a
,
network
)
bb
=
make_tensor
(
b
,
network
)
np
.
testing
.
assert_equal
(
a
[
b
],
get_value
(
aa
[
bb
]
))
b
[:]
=
True
bb
[:]
=
True
np
.
testing
.
assert_equal
(
a
[
b
],
aa
[
bb
].
numpy
(
))
np
.
testing
.
assert_equal
(
a
[:,
[
True
,
False
]],
aa
[:,
[
True
,
False
]].
numpy
(
))
np
.
testing
.
assert_equal
(
a
[
b
],
get_value
(
aa
[
bb
]
))
np
.
testing
.
assert_equal
(
a
[:,
[
True
,
False
]],
get_value
(
aa
[:,
[
True
,
False
]]
))
a
=
np
.
array
([[
True
,
False
],
[
False
,
True
]])
b
=
np
.
array
([
1
])
aa
=
Tensor
(
a
)
bb
=
Tensor
(
b
)
np
.
testing
.
assert_equal
(
a
[
b
],
aa
[
bb
].
numpy
(
))
aa
=
make_tensor
(
a
,
network
)
bb
=
make_tensor
(
b
,
network
)
np
.
testing
.
assert_equal
(
a
[
b
],
get_value
(
aa
[
bb
]
))
b
=
np
.
array
([[
True
,
True
],
[
False
,
True
]])
bb
=
Tensor
(
b
)
np
.
testing
.
assert_equal
(
a
[
b
],
aa
[
bb
].
numpy
())
a
[
b
]
=
False
aa
[
bb
]
=
False
np
.
testing
.
assert_equal
(
a
,
aa
.
numpy
())
bb
=
make_tensor
(
b
,
network
)
np
.
testing
.
assert_equal
(
a
[
b
],
get_value
(
aa
[
bb
]))
if
not
test_varnode
:
a
[
b
]
=
False
aa
[
bb
]
=
False
np
.
testing
.
assert_equal
(
a
,
get_value
(
aa
))
a
=
np
.
ones
((
2
,
2
),
dtype
=
np
.
int32
)
b
=
np
.
array
([[
False
,
False
],
[
False
,
False
]])
aa
=
Tensor
(
a
)
bb
=
Tensor
(
b
)
np
.
testing
.
assert_equal
(
a
[
b
],
aa
[
b
].
numpy
(
))
np
.
testing
.
assert_equal
(
a
[
b
],
aa
[
bb
].
numpy
(
))
aa
=
make_tensor
(
a
,
network
)
bb
=
make_tensor
(
b
,
network
)
np
.
testing
.
assert_equal
(
a
[
b
],
get_value
(
aa
[
b
]
))
np
.
testing
.
assert_equal
(
a
[
b
],
get_value
(
aa
[
bb
]
))
b
=
np
.
array
([
False
,
False
])
bb
=
Tensor
(
b
)
np
.
testing
.
assert_equal
(
a
[
b
],
aa
[
bb
].
numpy
(
).
reshape
(
a
[
b
].
shape
))
bb
=
make_tensor
(
b
,
network
)
np
.
testing
.
assert_equal
(
a
[
b
],
get_value
(
aa
[
bb
]
).
reshape
(
a
[
b
].
shape
))
a
=
np
.
arange
(
576
).
reshape
(
2
,
3
,
4
,
3
,
4
,
2
).
astype
(
"int32"
)
aa
=
Tensor
(
a
)
aa
=
make_tensor
(
a
,
network
)
b
=
(
np
.
random
.
sample
((
2
,
3
,
4
))
>
0.5
).
astype
(
"bool"
)
bb
=
Tensor
(
b
)
np
.
testing
.
assert_equal
(
a
[
b
,
:,
0
:
4
:
2
],
aa
[
bb
,
:,
0
:
4
:
2
].
numpy
(
))
bb
=
make_tensor
(
b
,
network
)
np
.
testing
.
assert_equal
(
a
[
b
,
:,
0
:
4
:
2
],
get_value
(
aa
[
bb
,
:,
0
:
4
:
2
]
))
b
=
(
np
.
random
.
sample
((
4
,
3
,
4
))
>
0.5
).
astype
(
"bool"
)
bb
=
Tensor
(
b
)
np
.
testing
.
assert_equal
(
a
[...,
b
,
0
:
2
],
aa
[...,
bb
,
0
:
2
].
numpy
(
))
bb
=
make_tensor
(
b
,
network
)
np
.
testing
.
assert_equal
(
a
[...,
b
,
0
:
2
],
get_value
(
aa
[...,
bb
,
0
:
2
]
))
b
=
(
np
.
random
.
sample
((
3
,
4
,
3
))
>
0.5
).
astype
(
"bool"
)
bb
=
Tensor
(
b
)
bb
=
make_tensor
(
b
,
network
)
np
.
testing
.
assert_equal
(
a
[:,
b
,
0
:
2
,
[
True
,
False
]],
aa
[:,
bb
,
0
:
2
,
[
True
,
False
]].
numpy
(
)
a
[:,
b
,
0
:
2
,
[
True
,
False
]],
get_value
(
aa
[:,
bb
,
0
:
2
,
[
True
,
False
]]
)
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录