Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
34c705fc
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看板
提交
34c705fc
编写于
12月 15, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(mge/imperative): move detach into C++
GitOrigin-RevId: 8c0d86cbbfdde275885b50417c80d28045871493
上级
147cef52
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
12 addition
and
11 deletion
+12
-11
imperative/python/megengine/tensor.py
imperative/python/megengine/tensor.py
+0
-7
imperative/python/src/tensor.cpp
imperative/python/src/tensor.cpp
+10
-3
imperative/python/src/tensor.h
imperative/python/src/tensor.h
+1
-0
imperative/python/test/unit/functional/test_functional.py
imperative/python/test/unit/functional/test_functional.py
+1
-1
未找到文件。
imperative/python/megengine/tensor.py
浏览文件 @
34c705fc
...
@@ -118,13 +118,6 @@ class Tensor(_Tensor, ArrayMethodMixin):
...
@@ -118,13 +118,6 @@ class Tensor(_Tensor, ArrayMethodMixin):
def
__setstate__
(
self
,
state
):
def
__setstate__
(
self
,
state
):
self
.
q_dict
=
state
.
pop
(
"qdict"
)
self
.
q_dict
=
state
.
pop
(
"qdict"
)
def
detach
(
self
):
r
"""
Returns a new tensor sharing the same data memory, which is treated as a constant
during backward gradient calcuation, i.e. its gradient is zero.
"""
Wrapper
=
type
(
self
)
return
Wrapper
(
self
)
tensor
=
Tensor
tensor
=
Tensor
...
...
imperative/python/src/tensor.cpp
浏览文件 @
34c705fc
...
@@ -68,9 +68,6 @@ PyObject* py_apply(PyObject* self, PyObject*const* args, size_t nargs/* , PyObje
...
@@ -68,9 +68,6 @@ PyObject* py_apply(PyObject* self, PyObject*const* args, size_t nargs/* , PyObje
return
nullptr
;
return
nullptr
;
}
}
auto
*
op
=
args
[
0
];
auto
*
op
=
args
[
0
];
if
(
!
strcmp
(
op
->
ob_type
->
tp_base
->
tp_name
,
"PodOpVisitor"
)
||
!
strcmp
(
op
->
ob_type
->
tp_base
->
tp_name
,
"IndexingOpBase"
)){
op
=
PyObject_CallMethod
(
op
,
"to_c"
,
""
);
}
PyTypeObject
*
pytype
=
args
[
1
]
->
ob_type
;
PyTypeObject
*
pytype
=
args
[
1
]
->
ob_type
;
++
args
;
++
args
;
...
@@ -195,6 +192,15 @@ void TensorWrapper::reset(PyObject* tensor) {
...
@@ -195,6 +192,15 @@ void TensorWrapper::reset(PyObject* tensor) {
m_tensor
=
t
->
m_tensor
;
m_tensor
=
t
->
m_tensor
;
}
}
PyObject
*
TensorWrapper
::
detach
()
{
PyObject
*
self
=
wrap_t
::
pycast
(
this
);
PyTypeObject
*
pytype
=
self
->
ob_type
;
auto
new_tensor
=
std
::
make_shared
<
Tensor
>
(
m_tensor
->
m_handle
);
auto
ret
=
TensorWrapper
::
make
(
pytype
,
std
::
move
(
new_tensor
));
return
ret
.
release
().
ptr
();
}
PyObject
*
TensorWrapper
::
isscalar
()
{
PyObject
*
TensorWrapper
::
isscalar
()
{
if
(
m_tensor
->
m_flags
&
Tensor
::
Flags
::
SCALAR
)
{
if
(
m_tensor
->
m_flags
&
Tensor
::
Flags
::
SCALAR
)
{
Py_RETURN_TRUE
;
Py_RETURN_TRUE
;
...
@@ -233,6 +239,7 @@ void init_tensor(py::module m) {
...
@@ -233,6 +239,7 @@ void init_tensor(py::module m) {
.
def
<&
TensorWrapper
::
reset
>
(
"_reset"
)
.
def
<&
TensorWrapper
::
reset
>
(
"_reset"
)
.
def
<&
TensorWrapper
::
isscalar
>
(
"isscalar"
)
.
def
<&
TensorWrapper
::
isscalar
>
(
"isscalar"
)
.
def
<&
TensorWrapper
::
setscalar
>
(
"setscalar"
)
.
def
<&
TensorWrapper
::
setscalar
>
(
"setscalar"
)
.
def
<&
TensorWrapper
::
detach
>
(
"detach"
)
.
finalize
();
.
finalize
();
if
(
!
tensor_type
)
throw
py
::
error_already_set
();
if
(
!
tensor_type
)
throw
py
::
error_already_set
();
py
::
setattr
(
m
,
"Tensor"
,
tensor_type
);
py
::
setattr
(
m
,
"Tensor"
,
tensor_type
);
...
...
imperative/python/src/tensor.h
浏览文件 @
34c705fc
...
@@ -128,6 +128,7 @@ struct TensorWrapper {
...
@@ -128,6 +128,7 @@ struct TensorWrapper {
PyObject
*
device
();
PyObject
*
device
();
PyObject
*
numpy
();
PyObject
*
numpy
();
void
reset
(
PyObject
*
);
void
reset
(
PyObject
*
);
PyObject
*
detach
();
PyObject
*
isscalar
();
PyObject
*
isscalar
();
void
setscalar
();
void
setscalar
();
};
};
...
...
imperative/python/test/unit/functional/test_functional.py
浏览文件 @
34c705fc
...
@@ -166,7 +166,7 @@ def test_interpolate():
...
@@ -166,7 +166,7 @@ def test_interpolate():
def
_save_to
(
self
,
name
=
"grad"
):
def
_save_to
(
self
,
name
=
"grad"
):
def
callback
(
tensor
,
grad
):
def
callback
(
grad
):
setattr
(
self
,
name
,
grad
)
setattr
(
self
,
name
,
grad
)
return
callback
return
callback
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录