Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
d01f6269
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
d01f6269
编写于
9月 28, 2020
作者:
F
furnace
提交者:
GitHub
9月 28, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update mv op according PR#27024 (#27474)
上级
9d783aed
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
69 addition
and
54 deletion
+69
-54
paddle/fluid/operators/mv_op.cc
paddle/fluid/operators/mv_op.cc
+8
-8
paddle/fluid/operators/mv_op.cu
paddle/fluid/operators/mv_op.cu
+15
-16
paddle/fluid/operators/mv_op.h
paddle/fluid/operators/mv_op.h
+15
-15
python/paddle/fluid/tests/unittests/test_mv_op.py
python/paddle/fluid/tests/unittests/test_mv_op.py
+31
-15
未找到文件。
paddle/fluid/operators/mv_op.cc
浏览文件 @
d01f6269
...
@@ -42,21 +42,21 @@ class MVOp : public framework::OperatorWithKernel {
...
@@ -42,21 +42,21 @@ class MVOp : public framework::OperatorWithKernel {
OP_INOUT_CHECK
(
context
->
HasOutput
(
"Out"
),
"Output"
,
"Out"
,
"mv"
);
OP_INOUT_CHECK
(
context
->
HasOutput
(
"Out"
),
"Output"
,
"Out"
,
"mv"
);
auto
dim_x
=
context
->
GetInputDim
(
"X"
);
auto
dim_x
=
context
->
GetInputDim
(
"X"
);
auto
dim_
y
=
context
->
GetInputDim
(
"Vec"
);
auto
dim_
vec
=
context
->
GetInputDim
(
"Vec"
);
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
dim_x
.
size
(),
2
,
dim_x
.
size
(),
2
,
platform
::
errors
::
InvalidArgument
(
platform
::
errors
::
InvalidArgument
(
"The rank of input X should be 2, but is %d"
,
dim_x
.
size
()));
"The rank of input X should be 2, but is %d"
,
dim_x
.
size
()));
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
dim_
y
.
size
(),
1
,
dim_
vec
.
size
(),
1
,
platform
::
errors
::
InvalidArgument
(
platform
::
errors
::
InvalidArgument
(
"The rank of input Vec should be 1, but is %d"
,
dim_
y
.
size
()));
"The rank of input Vec should be 1, but is %d"
,
dim_
vec
.
size
()));
PADDLE_ENFORCE_EQ
(
dim_x
[
1
]
==
dim_y
[
0
],
true
,
PADDLE_ENFORCE_EQ
(
dim_x
[
1
]
,
dim_vec
[
0
]
,
platform
::
errors
::
InvalidArgument
(
platform
::
errors
::
InvalidArgument
(
"
The length of input X' second dim should equal the
"
"
X's second dimension is expected to be equal to
"
"
length of input Vec,
"
"
Vec's first dimension
"
"
but X[%d, %d], Vec[%d
]"
,
"
but recieved X'shape = [%s], Vec's shape = [%s
]"
,
dim_x
[
0
],
dim_x
[
1
],
dim_y
[
0
]
));
dim_x
,
dim_vec
));
framework
::
DDim
dim_out
=
framework
::
make_ddim
({
dim_x
[
0
]});
framework
::
DDim
dim_out
=
framework
::
make_ddim
({
dim_x
[
0
]});
...
...
paddle/fluid/operators/mv_op.cu
浏览文件 @
d01f6269
...
@@ -19,8 +19,8 @@ namespace paddle {
...
@@ -19,8 +19,8 @@ namespace paddle {
namespace
operators
{
namespace
operators
{
template
<
typename
T
>
template
<
typename
T
>
__global__
void
MVGradCUDAKernel
(
const
int
m
,
const
int
n
,
const
T
*
dout
,
__global__
void
MVGrad
Dx
CUDAKernel
(
const
int
m
,
const
int
n
,
const
T
*
dout
,
const
T
*
vec
,
T
*
dx
)
{
const
T
*
vec
,
T
*
dx
)
{
int
idx
=
blockDim
.
x
*
blockIdx
.
x
+
threadIdx
.
x
;
int
idx
=
blockDim
.
x
*
blockIdx
.
x
+
threadIdx
.
x
;
for
(;
idx
<
m
*
n
;
idx
+=
blockDim
.
x
*
gridDim
.
x
)
{
for
(;
idx
<
m
*
n
;
idx
+=
blockDim
.
x
*
gridDim
.
x
)
{
int
i
=
idx
/
n
;
int
i
=
idx
/
n
;
...
@@ -52,32 +52,31 @@ class MVGradKernel<platform::CUDADeviceContext, T>
...
@@ -52,32 +52,31 @@ class MVGradKernel<platform::CUDADeviceContext, T>
int
m
=
dim_x
[
0
];
int
m
=
dim_x
[
0
];
int
n
=
dim_x
[
1
];
int
n
=
dim_x
[
1
];
dx
->
Resize
(
framework
::
make_ddim
({
m
*
n
}));
// get data ptr
// get data ptr
const
T
*
x_data
=
x
->
data
<
T
>
();
const
T
*
x_data
=
x
->
data
<
T
>
();
const
T
*
vec_data
=
vec
->
data
<
T
>
();
const
T
*
vec_data
=
vec
->
data
<
T
>
();
const
T
*
dout_data
=
dout
->
data
<
T
>
();
const
T
*
dout_data
=
dout
->
data
<
T
>
();
T
*
dx_data
=
dx
->
mutable_data
<
T
>
(
context
.
GetPlace
());
T
*
dvec_data
=
dvec
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
&
dev_ctx
=
auto
&
dev_ctx
=
context
.
template
device_context
<
platform
::
CUDADeviceContext
>();
context
.
template
device_context
<
platform
::
CUDADeviceContext
>();
auto
blas
=
math
::
GetBlas
<
platform
::
CUDADeviceContext
,
T
>
(
dev_ctx
);
auto
blas
=
math
::
GetBlas
<
platform
::
CUDADeviceContext
,
T
>
(
dev_ctx
);
// calculate dx
auto
stream
=
context
.
cuda_device_context
().
stream
();
auto
stream
=
context
.
cuda_device_context
().
stream
();
auto
config
=
GetGpuLaunchConfig1D
(
dev_ctx
,
m
*
n
);
auto
config
=
GetGpuLaunchConfig1D
(
dev_ctx
,
m
*
n
);
MVGradCUDAKernel
<
T
><<<
config
.
block_per_grid
.
x
,
config
.
thread_per_block
.
x
,
0
,
stream
>>>
(
m
,
n
,
dout_data
,
vec_data
,
dx_data
);
dx
->
Resize
(
framework
::
make_ddim
({
m
,
n
}));
if
(
dx
)
{
T
*
dx_data
=
dx
->
mutable_data
<
T
>
(
context
.
GetPlace
());
MVGradDxCUDAKernel
<
T
><<<
config
.
block_per_grid
.
x
,
config
.
thread_per_block
.
x
,
0
,
stream
>>>
(
m
,
n
,
dout_data
,
vec_data
,
dx_data
);
}
if
(
dvec
)
{
T
*
dvec_data
=
dvec
->
mutable_data
<
T
>
(
context
.
GetPlace
());
// calculate dvec
blas
.
GEMV
(
true
,
dim_x
[
0
],
dim_x
[
1
],
static_cast
<
T
>
(
1
),
x_data
,
dout_data
,
blas
.
GEMV
(
true
,
dim_x
[
0
],
dim_x
[
1
],
static_cast
<
T
>
(
1
),
x_data
,
dout_data
,
static_cast
<
T
>
(
0
),
dvec_data
);
static_cast
<
T
>
(
0
),
dvec_data
);
}
}
}
};
};
...
...
paddle/fluid/operators/mv_op.h
浏览文件 @
d01f6269
...
@@ -74,30 +74,30 @@ class MVGradKernel : public framework::OpKernel<T> {
...
@@ -74,30 +74,30 @@ class MVGradKernel : public framework::OpKernel<T> {
int
m
=
dim_x
[
0
];
int
m
=
dim_x
[
0
];
int
n
=
dim_x
[
1
];
int
n
=
dim_x
[
1
];
dx
->
Resize
(
framework
::
make_ddim
({
m
*
n
}));
// get data ptr
// get data ptr
const
T
*
x_data
=
x
->
data
<
T
>
();
const
T
*
x_data
=
x
->
data
<
T
>
();
const
T
*
vec_data
=
vec
->
data
<
T
>
();
const
T
*
vec_data
=
vec
->
data
<
T
>
();
const
T
*
dout_data
=
dout
->
data
<
T
>
();
const
T
*
dout_data
=
dout
->
data
<
T
>
();
T
*
dx_data
=
dx
->
mutable_data
<
T
>
(
context
.
GetPlace
());
if
(
dx
)
{
T
*
dvec_data
=
dvec
->
mutable_data
<
T
>
(
context
.
GetPlace
());
T
*
dx_data
=
dx
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
&
dev_ctx
=
context
.
template
device_context
<
DeviceContext
>();
auto
blas
=
math
::
GetBlas
<
DeviceContext
,
T
>
(
dev_ctx
);
// calculate dx
for
(
int
i
=
0
;
i
<
m
;
++
i
)
{
for
(
int
i
=
0
;
i
<
m
;
++
i
)
{
for
(
int
j
=
0
;
j
<
n
;
++
j
)
{
for
(
int
j
=
0
;
j
<
n
;
++
j
)
dx_data
[
i
*
n
+
j
]
=
dout_data
[
i
]
*
vec_data
[
j
];
dx_data
[
i
*
n
+
j
]
=
dout_data
[
i
]
*
vec_data
[
j
];
}
}
}
}
dx
->
Resize
(
framework
::
make_ddim
({
m
,
n
}));
if
(
dvec
)
{
T
*
dvec_data
=
dvec
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
&
dev_ctx
=
context
.
template
device_context
<
DeviceContext
>();
auto
blas
=
math
::
GetBlas
<
DeviceContext
,
T
>
(
dev_ctx
);
// calculate dvec
blas
.
GEMV
(
true
,
dim_x
[
0
],
dim_x
[
1
],
static_cast
<
T
>
(
1
),
x_data
,
dout_data
,
blas
.
GEMV
(
true
,
dim_x
[
0
],
dim_x
[
1
],
static_cast
<
T
>
(
1
),
x_data
,
dout_data
,
static_cast
<
T
>
(
0
),
dvec_data
);
static_cast
<
T
>
(
0
),
dvec_data
);
}
}
}
};
};
...
...
python/paddle/fluid/tests/unittests/test_mv_op.py
浏览文件 @
d01f6269
...
@@ -20,6 +20,7 @@ import paddle
...
@@ -20,6 +20,7 @@ import paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
import
paddle.fluid.layers
as
layers
import
paddle.fluid.layers
as
layers
import
paddle.fluid.core
as
core
import
paddle.fluid.core
as
core
from
paddle.static
import
program_guard
,
Program
from
op_test
import
OpTest
from
op_test
import
OpTest
...
@@ -37,7 +38,7 @@ class TestMVOp(OpTest):
...
@@ -37,7 +38,7 @@ class TestMVOp(OpTest):
self
.
check_grad
([
'X'
,
'Vec'
],
'Out'
)
self
.
check_grad
([
'X'
,
'Vec'
],
'Out'
)
def
init_config
(
self
):
def
init_config
(
self
):
self
.
x
=
np
.
random
.
random
((
5
,
100
)).
astype
(
"float64"
)
self
.
x
=
np
.
random
.
random
((
2
,
100
)).
astype
(
"float64"
)
self
.
vec
=
np
.
random
.
random
((
100
)).
astype
(
"float64"
)
self
.
vec
=
np
.
random
.
random
((
100
)).
astype
(
"float64"
)
...
@@ -57,21 +58,36 @@ class TestMVAPI(unittest.TestCase):
...
@@ -57,21 +58,36 @@ class TestMVAPI(unittest.TestCase):
paddle
.
enable_static
()
paddle
.
enable_static
()
def
test_static_graph
(
self
):
def
test_static_graph
(
self
):
paddle
.
enable_static
()
for
x_stop_gradient
in
[
False
,
True
]:
for
vec_stop_gradient
in
[
False
,
True
]:
paddle
.
enable_static
()
train_program
=
Program
()
startup_program
=
Program
()
self
.
input_x
=
np
.
random
.
rand
(
5
,
100
).
astype
(
"float64"
)
self
.
input_vec
=
np
.
random
.
rand
(
100
).
astype
(
"float64"
)
with
program_guard
(
train_program
,
startup_program
):
data_x
=
paddle
.
static
.
data
(
"x"
,
shape
=
[
5
,
100
],
dtype
=
"float64"
)
data_vec
=
paddle
.
static
.
data
(
"vec"
,
shape
=
[
100
],
dtype
=
"float64"
)
data_x
.
stop_gradient
=
x_stop_gradient
data_vec
.
stop_gradient
=
vec_stop_gradient
result_vec
=
paddle
.
mv
(
data_x
,
data_vec
)
self
.
input_x
=
np
.
random
.
rand
(
5
,
100
).
astype
(
"float64"
)
self
.
place
=
paddle
.
CPUPlace
()
self
.
input_vec
=
np
.
random
.
rand
(
100
).
astype
(
"float64"
)
exe
=
paddle
.
static
.
Executor
(
self
.
place
)
res
,
=
exe
.
run
(
data_x
=
paddle
.
static
.
data
(
"x"
,
shape
=
[
5
,
100
],
dtype
=
"float64"
)
feed
=
{
"x"
:
self
.
input_x
,
data_vec
=
paddle
.
static
.
data
(
"vec"
,
shape
=
[
100
],
dtype
=
"float64"
)
"vec"
:
self
.
input_vec
},
result_vec
=
paddle
.
mv
(
data_x
,
data_vec
)
fetch_list
=
[
result_vec
])
self
.
place
=
paddle
.
CPUPlace
()
z_expected
=
np
.
array
(
np
.
dot
(
self
.
input_x
,
self
.
input_vec
))
exe
=
paddle
.
static
.
Executor
(
self
.
place
)
self
.
assertTrue
(
np
.
allclose
(
res
,
z_expected
))
res
,
=
exe
.
run
(
feed
=
{
"x"
:
self
.
input_x
,
"vec"
:
self
.
input_vec
},
fetch_list
=
[
result_vec
])
z_expected
=
np
.
array
(
np
.
dot
(
self
.
input_x
,
self
.
input_vec
))
self
.
assertTrue
(
np
.
allclose
(
res
,
z_expected
))
class
TestMVError
(
unittest
.
TestCase
):
class
TestMVError
(
unittest
.
TestCase
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录