Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
df976782
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
404
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看板
提交
df976782
编写于
1月 20, 2021
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(mge/functional): add matinv
GitOrigin-RevId: d4fa8a82778abec33d2791f0a867d12482fad51a
上级
3bda3347
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
90 addition
and
0 deletion
+90
-0
imperative/python/megengine/functional/nn.py
imperative/python/megengine/functional/nn.py
+33
-0
imperative/python/test/unit/functional/test_functional.py
imperative/python/test/unit/functional/test_functional.py
+19
-0
imperative/src/impl/ops/matrix_inverse.cpp
imperative/src/impl/ops/matrix_inverse.cpp
+36
-0
src/core/include/megbrain/ir/ops.td
src/core/include/megbrain/ir/ops.td
+2
-0
未找到文件。
imperative/python/megengine/functional/nn.py
浏览文件 @
df976782
...
...
@@ -52,6 +52,7 @@ __all__ = [
"logsigmoid"
,
"logsumexp"
,
"logsoftmax"
,
"matinv"
,
"matmul"
,
"max_pool2d"
,
"one_hot"
,
...
...
@@ -1002,6 +1003,38 @@ def remap(
return
result
def
matinv
(
inp
:
Tensor
)
->
Tensor
:
"""
Computes the inverse of a batch of matrices; input must has shape [..., n, n].
:param inp: input tensor.
:return: output tensor.
Examples:
.. testcode::
import numpy as np
from megengine import tensor
import megengine.functional as F
data = tensor([[1.0, 0.0], [1.0, 1.0]])
out = F.matinv(data)
print(out.numpy())
Outputs:
.. testoutput::
[[ 1. 0.]
[-1. 1.]]
"""
(
result
,)
=
apply
(
builtin
.
MatrixInverse
(),
inp
)
return
result
def
matmul
(
inp1
:
Tensor
,
inp2
:
Tensor
,
...
...
imperative/python/test/unit/functional/test_functional.py
浏览文件 @
df976782
...
...
@@ -60,6 +60,25 @@ def test_dropout():
assert
out
.
numpy
().
sum
()
>=
0.0
def
test_matinv
():
shape1
=
(
5
,
5
)
shape2
=
(
3
,
9
,
9
)
data1
=
np
.
random
.
random
(
shape1
).
astype
(
"float32"
)
data2
=
np
.
random
.
random
(
shape2
).
astype
(
"float32"
)
cases
=
[
{
"input"
:
data1
},
{
"input"
:
data2
},
]
opr_test
(
cases
,
F
.
matinv
,
compare_fn
=
lambda
x
,
y
:
np
.
testing
.
assert_allclose
(
x
.
numpy
(),
y
,
rtol
=
1e-5
),
ref_fn
=
np
.
linalg
.
inv
,
)
def
test_matmul
():
shape1
=
3
shape2
=
3
...
...
imperative/src/impl/ops/matrix_inverse.cpp
0 → 100644
浏览文件 @
df976782
/**
* \file imperative/src/impl/ops/matrix_inverse.cpp
* 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.
*/
#include "../op_trait.h"
#include "megbrain/imperative/ops/autogen.h"
#include "megbrain/opr/blas.h"
namespace
mgb
{
namespace
imperative
{
namespace
{
auto
apply_on_var_node
(
const
OpDef
&
def
,
const
VarNodeArray
&
inputs
)
{
mgb_assert
(
inputs
.
size
()
==
1
);
return
opr
::
MatrixInverse
::
make
(
inputs
[
0
]);
}
OP_TRAIT_REG
(
MatrixInverse
,
MatrixInverse
)
.
apply_on_var_node
(
apply_on_var_node
)
.
fallback
();
}
// anonymous namespace
}
// namespace imperative
}
// namespace mgb
// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}
src/core/include/megbrain/ir/ops.td
浏览文件 @
df976782
...
...
@@ -34,6 +34,8 @@ def TypeCvt: MgbHashableOp<"TypeCvt", [], [NoSideEffect]> {
let results = (outs AnyType);
}
def MatrixInverse: MgbHashableOp<"MatrixInverse", [EmptyParam]>;
def MatrixMul: MgbHashableOp<"MatrixMul", [MatrixMulParam, ExecutionPolicyParamBase<"policy">]>;
def BatchedMatrixMul: MgbHashableOp<"BatchedMatmul", [MatrixMulParam, ExecutionPolicyParamBase<"policy">]>;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录