Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
a0d36566
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看板
提交
a0d36566
编写于
9月 26, 2022
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(mge/functional): fix memory leak when set tensor dtype to Quantized
GitOrigin-RevId: daa6b1912429754c79629eab69636782003797cc
上级
f01b1255
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
36 addition
and
18 deletion
+36
-18
imperative/python/src/helper.cpp
imperative/python/src/helper.cpp
+30
-15
imperative/python/src/tensor_utils.cpp
imperative/python/src/tensor_utils.cpp
+6
-3
未找到文件。
imperative/python/src/helper.cpp
浏览文件 @
a0d36566
...
...
@@ -192,12 +192,16 @@ std::unique_ptr<PyArray_Descr, PyArrayDescrDeleter> dtype_mgb2np_descr(DType dty
const
std
::
vector
<
std
::
pair
<
const
char
*
,
PyObject
*>>&
data
)
{
PyObject
*
metadata
=
PyDict_New
();
PyObject
*
mgb_dtype_metadata
=
PyDict_New
();
PyDict_SetItemString
(
mgb_dtype_metadata
,
"name"
,
PyUnicode_FromString
(
name
));
PyObject
*
py_name
=
PyUnicode_FromString
(
name
);
PyDict_SetItemString
(
mgb_dtype_metadata
,
"name"
,
py_name
);
Py_DECREF
(
py_name
);
for
(
const
auto
&
d
:
data
)
{
PyDict_SetItemString
(
mgb_dtype_metadata
,
d
.
first
,
d
.
second
);
}
PyDict_SetItemString
(
metadata
,
"mgb_dtype"
,
mgb_dtype_metadata
);
Py_DECREF
(
mgb_dtype_metadata
);
return
metadata
;
};
if
(
dtype
.
has_param
())
{
...
...
@@ -206,51 +210,62 @@ std::unique_ptr<PyArray_Descr, PyArrayDescrDeleter> dtype_mgb2np_descr(DType dty
case
DTypeEnum
::
QuantizedS1
:
{
auto
&
param
=
dtype
.
param
<
dtype
::
QuantizedS1
>
();
type_descr
=
PyArray_DescrNewFromType
(
NPY_INT8
);
PyObject
*
scale
=
PyFloat_FromDouble
(
param
.
scale
);
type_descr
->
metadata
=
build_mgb_dtype_dict
(
DTypeTrait
<
dtype
::
QuantizedS1
>::
name
,
{{
"scale"
,
PyFloat_FromDouble
(
param
.
scale
)}}
);
DTypeTrait
<
dtype
::
QuantizedS1
>::
name
,
{{
"scale"
,
scale
}});
Py_DECREF
(
scale
);
break
;
}
case
DTypeEnum
::
Quantized4Asymm
:
{
auto
&
param
=
dtype
.
param
<
dtype
::
Quantized4Asymm
>
();
type_descr
=
PyArray_DescrNewFromType
(
NPY_UINT8
);
PyObject
*
scale
=
PyFloat_FromDouble
(
param
.
scale
);
PyObject
*
zero_point
=
PyLong_FromLong
(
param
.
zero_point
);
type_descr
->
metadata
=
build_mgb_dtype_dict
(
DTypeTrait
<
dtype
::
Quantized4Asymm
>::
name
,
{{
"scale"
,
PyFloat_FromDouble
(
param
.
scale
)},
{
"zero_point"
,
PyLong_FromLong
(
param
.
zero_point
)}});
{{
"scale"
,
scale
},
{
"zero_point"
,
zero_point
}});
Py_DECREF
(
scale
);
Py_DECREF
(
zero_point
);
break
;
}
case
DTypeEnum
::
QuantizedS4
:
{
auto
&
param
=
dtype
.
param
<
dtype
::
QuantizedS4
>
();
type_descr
=
PyArray_DescrNewFromType
(
NPY_INT8
);
PyObject
*
scale
=
PyFloat_FromDouble
(
param
.
scale
);
type_descr
->
metadata
=
build_mgb_dtype_dict
(
DTypeTrait
<
dtype
::
QuantizedS4
>::
name
,
{{
"scale"
,
PyFloat_FromDouble
(
param
.
scale
)}}
);
DTypeTrait
<
dtype
::
QuantizedS4
>::
name
,
{{
"scale"
,
scale
}});
Py_DECREF
(
scale
);
break
;
}
case
DTypeEnum
::
Quantized8Asymm
:
{
auto
&
param
=
dtype
.
param
<
dtype
::
Quantized8Asymm
>
();
type_descr
=
PyArray_DescrNewFromType
(
NPY_UINT8
);
PyObject
*
scale
=
PyFloat_FromDouble
(
param
.
scale
);
PyObject
*
zero_point
=
PyLong_FromLong
(
param
.
zero_point
);
type_descr
->
metadata
=
build_mgb_dtype_dict
(
DTypeTrait
<
dtype
::
Quantized8Asymm
>::
name
,
{{
"scale"
,
PyFloat_FromDouble
(
param
.
scale
)},
{
"zero_point"
,
PyLong_FromLong
(
param
.
zero_point
)}});
{{
"scale"
,
scale
},
{
"zero_point"
,
zero_point
}});
Py_DECREF
(
scale
);
Py_DECREF
(
zero_point
);
break
;
}
case
DTypeEnum
::
QuantizedS8
:
{
auto
&
param
=
dtype
.
param
<
dtype
::
QuantizedS8
>
();
type_descr
=
PyArray_DescrNewFromType
(
NPY_INT8
);
type_descr
->
metadata
=
build_mgb_dtype_dict
(
DTypeTrait
<
dtype
::
QuantizedS8
>::
name
,
{{
"scale"
,
PyFloat_FromDouble
(
param
.
scale
)}});
PyObject
*
scale
=
PyFloat_FromDouble
(
param
.
scale
);
auto
metadata
=
build_mgb_dtype_dict
(
DTypeTrait
<
dtype
::
QuantizedS8
>::
name
,
{{
"scale"
,
scale
}});
type_descr
->
metadata
=
metadata
;
Py_DECREF
(
scale
);
break
;
}
case
DTypeEnum
::
QuantizedS32
:
{
auto
&
param
=
dtype
.
param
<
dtype
::
QuantizedS32
>
();
type_descr
=
PyArray_DescrNewFromType
(
NPY_INT32
);
PyObject
*
scale
=
PyFloat_FromDouble
(
param
.
scale
);
type_descr
->
metadata
=
build_mgb_dtype_dict
(
DTypeTrait
<
dtype
::
QuantizedS32
>::
name
,
{{
"scale"
,
PyFloat_FromDouble
(
param
.
scale
)}}
);
DTypeTrait
<
dtype
::
QuantizedS32
>::
name
,
{{
"scale"
,
scale
}});
Py_DECREF
(
scale
);
break
;
}
default:
...
...
imperative/python/src/tensor_utils.cpp
浏览文件 @
a0d36566
...
...
@@ -422,6 +422,7 @@ mgb::DType _get_dtype(py::handle tensor) {
py
::
object
_astype_cpp
(
py
::
handle
tensor
,
py
::
handle
dtype_hdl
)
{
PyArray_Descr
*
descr
;
py
::
object
ret
;
if
(
!
PyArray_DescrConverter
(
dtype_hdl
.
ptr
(),
&
descr
))
{
throw
py
::
value_error
(
ssprintf
(
"can not convert to numpy.dtype from %s"
,
...
...
@@ -432,11 +433,13 @@ py::object _astype_cpp(py::handle tensor, py::handle dtype_hdl) {
std
::
shared_ptr
<
OpDef
>
op
=
TypeCvt
::
make
(
npy
::
dtype_np2mgb_descr
(
descr
));
py
::
object
Op
=
py
::
cast
(
op
);
PyObject
*
p
[
2
]
=
{
Op
.
ptr
(),
tensor
.
ptr
()};
py
::
tuple
ret
=
py
::
reinterpret_steal
<
py
::
object
>
(
py_apply
(
NULL
,
p
,
2
));
ret
urn
ret
[
0
];
py
::
tuple
apply_res
=
py
::
reinterpret_steal
<
py
::
object
>
(
py_apply
(
NULL
,
p
,
2
));
ret
=
apply_res
[
0
];
}
else
{
ret
urn
py
::
reinterpret_borrow
<
py
::
object
>
(
tensor
);
ret
=
py
::
reinterpret_borrow
<
py
::
object
>
(
tensor
);
}
Py_DECREF
(
descr
);
return
ret
;
}
py
::
object
_convert_single_value_cpp
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录