Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
824b1f71
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看板
提交
824b1f71
编写于
7月 07, 2023
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
docs(functional/complex): add basic docstring for complex apis
GitOrigin-RevId: dc93eacdaf8461edf279ec026b4e73c169a8d3f1
上级
76119020
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
81 addition
and
3 deletion
+81
-3
imperative/python/megengine/functional/tensor.py
imperative/python/megengine/functional/tensor.py
+69
-0
imperative/src/impl/basic_operators.cpp
imperative/src/impl/basic_operators.cpp
+2
-1
imperative/src/include/megbrain/imperative/transformations/complex.h
...src/include/megbrain/imperative/transformations/complex.h
+10
-2
未找到文件。
imperative/python/megengine/functional/tensor.py
浏览文件 @
824b1f71
...
@@ -425,10 +425,45 @@ def ones_like(inp: Tensor) -> Tensor:
...
@@ -425,10 +425,45 @@ def ones_like(inp: Tensor) -> Tensor:
def
polar
(
abs
:
Tensor
,
angle
:
Tensor
)
->
Tensor
:
def
polar
(
abs
:
Tensor
,
angle
:
Tensor
)
->
Tensor
:
r
"""Constructs a complex tensor whose elements are Cartesian coordinates
corresponding to the polar coordinates with absolute value abs and angle angle.
Args:
abs(Tensor): the absolute value the complex tensor. Must be float.
angle(Tensor): the angle of the complex tensor. Must be float.
Returns:
the complex tensor
Examples:
>>> abs = Tensor([1, 2], dtype=np.float32)
>>> angle = Tensor([np.pi / 2, 5 * np.pi / 4], dtype=np.float32)
>>> z = F.polar(abs, angle)
>>> z
Tensor([-4.3711e-08+1.j -1.4142e+00-1.4142j], dtype=complex64, device=xpux:0)
"""
return
create_complex
(
abs
*
cos
(
angle
),
abs
*
sin
(
angle
))
return
create_complex
(
abs
*
cos
(
angle
),
abs
*
sin
(
angle
))
def
complex
(
real
:
Tensor
,
imag
:
Tensor
)
->
Tensor
:
def
complex
(
real
:
Tensor
,
imag
:
Tensor
)
->
Tensor
:
r
"""Constructs a complex tensor with its real part equal to real and its imaginary part equal to imag.
Args:
real(Tensor): the real part of the complex tensor. Must be float.
imag(Tensor): the imaginary part of the complex tensor. Must be float.
Returns:
the complex tensor
Examples:
>>> real = Tensor([1, 2], dtype=np.float32)
>>> imag = Tensor([3, 4], dtype=np.float32)
>>> z = F.complex(real, imag)
>>> z
Tensor([1.+3.j 2.+4.j], dtype=complex64, device=xpux:0)
>>> z.dtype
dtype('complex64')
"""
if
not
isinstance
(
real
,
Tensor
):
if
not
isinstance
(
real
,
Tensor
):
real
=
Tensor
(
real
)
real
=
Tensor
(
real
)
if
not
isinstance
(
imag
,
Tensor
):
if
not
isinstance
(
imag
,
Tensor
):
...
@@ -437,10 +472,44 @@ def complex(real: Tensor, imag: Tensor) -> Tensor:
...
@@ -437,10 +472,44 @@ def complex(real: Tensor, imag: Tensor) -> Tensor:
def
real
(
complex
:
Tensor
)
->
Tensor
:
def
real
(
complex
:
Tensor
)
->
Tensor
:
r
"""Returns a new tensor containing real values of the complex tensor.
Args:
complex(Tensor) the complex tensor
Returns:
the real part of the complex tensor
Examples:
>>> x=Tensor([0.3100+0.3553j, -0.5445-0.7896j, -1.6492-0.0633j, -0.0638-0.8119j], dtype=np.complex64)
>>> F.real(x)
Tensor([[ 0.31 ]
[-0.5445]
[-1.6492]
[-0.0638]], device=xpux:0)
"""
return
get_real
(
complex
)
return
get_real
(
complex
)
def
imag
(
complex
:
Tensor
)
->
Tensor
:
def
imag
(
complex
:
Tensor
)
->
Tensor
:
r
"""Returns a new tensor containing imaginary values of the complex tensor.
Args:
complex(Tensor) the complex tensor
Returns:
the imaginary part of the complex tensor
Examples:
>>> x=Tensor([0.3100+0.3553j, -0.5445-0.7896j, -1.6492-0.0633j, -0.0638-0.8119j], dtype=np.complex64)
>>> F.imag(x)
Tensor([[ 0.3553]
[-0.7896]
[-0.0633]
[-0.8119]], device=xpux:0)
"""
return
get_imag
(
complex
)
return
get_imag
(
complex
)
...
...
imperative/src/impl/basic_operators.cpp
浏览文件 @
824b1f71
...
@@ -48,7 +48,8 @@ CreateTensor::CreateTensor(Kind kind, CompNode device, TensorLayout layout)
...
@@ -48,7 +48,8 @@ CreateTensor::CreateTensor(Kind kind, CompNode device, TensorLayout layout)
m_shape
(
ValueShape
::
from
(
layout
)),
m_shape
(
ValueShape
::
from
(
layout
)),
m_format
(
Format
::
Type
::
DEFAULT
)
{
m_format
(
Format
::
Type
::
DEFAULT
)
{
mgb_assert
(
mgb_assert
(
layout
.
is_contiguous
()
||
layout
.
is_empty
(),
"layout should be contiguous"
);
layout
.
is_contiguous
()
||
layout
.
is_empty
(),
"layout should be contiguous, got %s"
,
layout
.
to_string
().
c_str
());
}
}
auto
CreateTensor
::
parse
(
Span
<
ValueRef
>
inputs
)
const
->
Args
{
auto
CreateTensor
::
parse
(
Span
<
ValueRef
>
inputs
)
const
->
Args
{
...
...
imperative/src/include/megbrain/imperative/transformations/complex.h
浏览文件 @
824b1f71
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
#include "megbrain/imperative/utils/helper.h"
#include "megbrain/imperative/utils/helper.h"
#include "megbrain/imperative/utils/span.h"
#include "megbrain/imperative/utils/span.h"
#include "megbrain/imperative/value.h"
#include "megbrain/imperative/value.h"
#include "megbrain/tensor.h"
#include "megdnn/thin/small_vector.h"
#include "megdnn/thin/small_vector.h"
namespace
mgb
{
namespace
mgb
{
...
@@ -184,17 +185,24 @@ public:
...
@@ -184,17 +185,24 @@ public:
f32_host
.
sub
(
SubTensorSpec
::
make_from_layout
(
real_layout
));
f32_host
.
sub
(
SubTensorSpec
::
make_from_layout
(
real_layout
));
auto
imag_host
=
f32_host
.
sub
(
auto
imag_host
=
f32_host
.
sub
(
SubTensorSpec
::
make_from_offset_elem
(
imag_layout
,
1
));
SubTensorSpec
::
make_from_offset_elem
(
imag_layout
,
1
));
// copy into continuous
real_layout
.
init_contiguous_stride
();
imag_layout
.
init_contiguous_stride
();
auto
real_value
=
HostTensorND
{
create_tensor
->
device
(),
real_layout
};
real_value
.
copy_from_fixlayout
(
real_host
);
auto
imag_value
=
HostTensorND
{
create_tensor
->
device
(),
imag_layout
};
imag_value
.
copy_from_fixlayout
(
imag_host
);
// create real and imag
// create real and imag
auto
real
=
imperative
::
apply
(
auto
real
=
imperative
::
apply
(
CreateTensor
(
CreateTensor
(
create_tensor
->
kind
(),
create_tensor
->
device
(),
create_tensor
->
kind
(),
create_tensor
->
device
(),
real_layout
),
real_layout
),
HostStorage
::
make
(
real_
host
.
storage
()))[
0
];
HostStorage
::
make
(
real_
value
.
storage
()))[
0
];
auto
imag
=
imperative
::
apply
(
auto
imag
=
imperative
::
apply
(
CreateTensor
(
CreateTensor
(
create_tensor
->
kind
(),
create_tensor
->
device
(),
create_tensor
->
kind
(),
create_tensor
->
device
(),
imag_layout
),
imag_layout
),
HostStorage
::
make
(
imag_
host
.
storage
()))[
0
];
HostStorage
::
make
(
imag_
value
.
storage
()))[
0
];
return
{
m_complex_type
.
make
(
real
,
imag
)};
return
{
m_complex_type
.
make
(
real
,
imag
)};
}
else
{
}
else
{
return
imperative
::
apply
(
op
,
inputs
);
return
imperative
::
apply
(
op
,
inputs
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录