Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
5591292b
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
5591292b
编写于
11月 11, 2016
作者:
H
Haonan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modifications according to comments
上级
728defbe
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
87 addition
and
99 deletion
+87
-99
paddle/cuda/src/hl_cuda_matrix.cu
paddle/cuda/src/hl_cuda_matrix.cu
+4
-2
paddle/gserver/layers/CostLayer.cpp
paddle/gserver/layers/CostLayer.cpp
+2
-8
paddle/gserver/tests/test_LayerGrad.cpp
paddle/gserver/tests/test_LayerGrad.cpp
+17
-1
paddle/math/CpuSparseMatrix.cpp
paddle/math/CpuSparseMatrix.cpp
+0
-3
paddle/math/Matrix.cpp
paddle/math/Matrix.cpp
+21
-21
paddle/math/Vector.cpp
paddle/math/Vector.cpp
+26
-0
paddle/math/Vector.h
paddle/math/Vector.h
+8
-0
paddle/math/tests/test_matrixCompare.cpp
paddle/math/tests/test_matrixCompare.cpp
+9
-20
paddle/parameter/Argument.cpp
paddle/parameter/Argument.cpp
+0
-38
paddle/parameter/Argument.h
paddle/parameter/Argument.h
+0
-6
未找到文件。
paddle/cuda/src/hl_cuda_matrix.cu
浏览文件 @
5591292b
...
...
@@ -346,6 +346,7 @@ void hl_matrix_multi_binary_cross_entropy(real* output,
CHECK_NOTNULL
(
output
);
CHECK_NOTNULL
(
entropy
);
CHECK_NOTNULL
(
csr_mat
);
CHECK_EQ
(
csr_mat
->
format
,
HL_SPARSE_CSR
);
int
n_threads
=
1024
;
int
blocks
=
(
dimM
+
n_threads
-
1
)
/
n_threads
;
dim3
threads
(
n_threads
);
...
...
@@ -385,6 +386,7 @@ void hl_matrix_multi_binary_cross_entropy_bp(real* output,
CHECK_NOTNULL
(
output
);
CHECK_NOTNULL
(
grad
);
CHECK_NOTNULL
(
csr_mat
);
CHECK_EQ
(
csr_mat
->
format
,
HL_SPARSE_CSR
);
int
n_threads
=
1024
;
int
blocks
=
(
dimM
+
n_threads
-
1
)
/
n_threads
;
dim3
threads
(
n_threads
);
...
...
@@ -763,7 +765,7 @@ __global__ void KeMatrixAddSharedBias(real* A,
int
dim
=
N
/
channel
;
if
(
index
<
M
*
N
)
{
int
i
=
index
%
N
;
i
=
i
/
dim
;
i
=
i
/
dim
;
A
[
index
]
+=
scale
*
B
[
i
];
}
}
...
...
@@ -791,7 +793,7 @@ __global__ void KeMatrixCollectSharedBias(real *B,
const
int
dim
,
const
int
limit
,
real
scale
)
{
if
(
dim
<
limit
)
{
if
(
dim
<
limit
)
{
int
index
=
blockIdx
.
x
*
blockDim
.
x
+
threadIdx
.
x
;
if
(
index
<
channel
)
{
real
sum
=
0.0
;
...
...
paddle/gserver/layers/CostLayer.cpp
浏览文件 @
5591292b
...
...
@@ -465,10 +465,7 @@ void MultiBinaryLabelCrossEntropy::forwardImp(Matrix& output, Argument& label,
MatrixPtr
value
=
nullptr
;
if
(
label
.
ids
)
{
CHECK
(
!
label
.
value
);
value
=
Matrix
::
createSparseMatrix
(
label
.
ids
->
getSize
(),
output
.
getWidth
(),
label
.
ids
->
getSize
(),
NO_VALUE
,
SPARSE_CSR
,
false
,
useGpu_
);
label
.
idsToSparseMatrix
(
value
);
value
=
label
.
ids
->
toOneHotSparseMatrix
(
output
.
getWidth
(),
useGpu_
);
}
else
{
CHECK
(
label
.
value
);
value
=
label
.
value
;
...
...
@@ -491,10 +488,7 @@ void MultiBinaryLabelCrossEntropy::backwardImp(
MatrixPtr
value
=
nullptr
;
if
(
label
.
ids
)
{
CHECK
(
!
value
);
value
=
Matrix
::
createSparseMatrix
(
label
.
ids
->
getSize
(),
output
.
getWidth
(),
label
.
ids
->
getSize
(),
NO_VALUE
,
SPARSE_CSR
,
false
,
useGpu_
);
label
.
idsToSparseMatrix
(
value
);
value
=
label
.
ids
->
toOneHotSparseMatrix
(
output
.
getWidth
(),
useGpu_
);
}
else
{
CHECK
(
label
.
value
);
value
=
label
.
value
;
...
...
paddle/gserver/tests/test_LayerGrad.cpp
浏览文件 @
5591292b
...
...
@@ -528,7 +528,7 @@ TEST(Layer, multi_cross) {
}
}
TEST
(
Layer
,
multi_binary_label
)
{
TEST
(
Layer
,
multi_binary_label
_sparse_mat
)
{
TestConfig
config
;
config
.
layerConfig
.
set_type
(
"multi_binary_label_cross_entropy"
);
config
.
biasSize
=
0
;
...
...
@@ -544,6 +544,22 @@ TEST(Layer, multi_binary_label) {
}
}
TEST
(
layer
,
multi_binary_label_id
)
{
TestConfig
config
;
config
.
layerConfig
.
set_type
(
"multi_binary_label_cross_entropy"
);
config
.
biasSize
=
0
;
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
50
,
0
});
config
.
inputDefs
.
push_back
({
INPUT_LABEL
,
"layer_1"
,
10
,
0
});
config
.
layerConfig
.
add_inputs
();
config
.
layerConfig
.
add_inputs
();
for
(
auto
useGpu
:
{
false
,
true
})
{
testLayerGrad
(
config
,
"multi_binary_label_cross_entropy"
,
100
,
/* trans */
false
,
useGpu
);
}
}
TEST
(
Layer
,
multi_cross_with_selfnorm
)
{
TestConfig
config
;
config
.
layerConfig
.
set_type
(
"multi_class_cross_entropy_with_selfnorm"
);
...
...
paddle/math/CpuSparseMatrix.cpp
浏览文件 @
5591292b
...
...
@@ -409,9 +409,6 @@ void CpuSparseMatrix::setRow(size_t row, size_t colNum,
if
(
format_
==
SPARSE_CSR
)
{
CHECK_LT
(
row
,
height_
);
CHECK
(
NULL
!=
cols
);
for
(
size_t
i
=
row
;
i
<
height_
;
i
++
)
{
CHECK_EQ
(
rows_
[
i
+
1
],
rows_
[
i
]);
}
if
(
0
==
row
)
{
rows_
[
row
]
=
0
;
}
...
...
paddle/math/Matrix.cpp
浏览文件 @
5591292b
...
...
@@ -1269,37 +1269,37 @@ void GpuMatrix::bilinearBackward(const Matrix& out,
}
void
GpuMatrix
::
multiBinaryLabelCrossEntropy
(
Matrix
&
output
,
Matrix
&
label
)
{
GpuMatrix
*
output
_p
tr
=
dynamic_cast
<
GpuMatrix
*>
(
&
output
);
auto
label
_p
tr
=
dynamic_cast
<
GpuSparseMatrix
*>
(
&
label
);
CHECK
(
output
_ptr
&&
label_p
tr
)
<<
"Invalid argument pointer"
;
CHECK
(
label
_p
tr
->
format_
==
SPARSE_CSR
)
<<
"Matrix format not supported"
;
CHECK
(
height_
==
output
_p
tr
->
height_
&&
width_
==
1
&&
output
_ptr
->
width_
==
label_p
tr
->
getWidth
()
&&
output
_ptr
->
height_
==
label_p
tr
->
getHeight
())
GpuMatrix
*
output
P
tr
=
dynamic_cast
<
GpuMatrix
*>
(
&
output
);
auto
label
P
tr
=
dynamic_cast
<
GpuSparseMatrix
*>
(
&
label
);
CHECK
(
output
Ptr
&&
labelP
tr
)
<<
"Invalid argument pointer"
;
CHECK
(
label
P
tr
->
format_
==
SPARSE_CSR
)
<<
"Matrix format not supported"
;
CHECK
(
height_
==
output
P
tr
->
height_
&&
width_
==
1
&&
output
Ptr
->
width_
==
labelP
tr
->
getWidth
()
&&
output
Ptr
->
height_
==
labelP
tr
->
getHeight
())
<<
"Matrix dimensions are not equal"
;
real
*
output_d
=
output
_p
tr
->
data_
;
real
*
output_d
=
output
P
tr
->
data_
;
real
*
entropy_d
=
data_
;
hl_sparse_matrix_s
mat_d
=
label
_p
tr
->
sMatrix_
.
get
();
hl_sparse_matrix_s
mat_d
=
label
P
tr
->
sMatrix_
.
get
();
hl_matrix_multi_binary_cross_entropy
(
output_d
,
entropy_d
,
mat_d
,
height_
,
output
_p
tr
->
width_
);
output_d
,
entropy_d
,
mat_d
,
height_
,
output
P
tr
->
width_
);
}
void
GpuMatrix
::
multiBinaryLabelCrossEntropyBp
(
Matrix
&
output
,
Matrix
&
label
)
{
GpuMatrix
*
output
_p
tr
=
dynamic_cast
<
GpuMatrix
*>
(
&
output
);
auto
label
_p
tr
=
dynamic_cast
<
GpuSparseMatrix
*>
(
&
label
);
CHECK
(
output
_ptr
&&
label_p
tr
)
<<
"Invalid argument pointer"
;
CHECK
(
label
_p
tr
->
format_
==
SPARSE_CSR
)
<<
"Matrix format not supported"
;
CHECK
(
height_
==
output
_ptr
->
height_
&&
width_
==
output_p
tr
->
width_
&&
output
_ptr
->
width_
==
label_p
tr
->
getWidth
()
&&
output
_ptr
->
height_
==
label_p
tr
->
getHeight
())
GpuMatrix
*
output
P
tr
=
dynamic_cast
<
GpuMatrix
*>
(
&
output
);
auto
label
P
tr
=
dynamic_cast
<
GpuSparseMatrix
*>
(
&
label
);
CHECK
(
output
Ptr
&&
labelP
tr
)
<<
"Invalid argument pointer"
;
CHECK
(
label
P
tr
->
format_
==
SPARSE_CSR
)
<<
"Matrix format not supported"
;
CHECK
(
height_
==
output
Ptr
->
height_
&&
width_
==
outputP
tr
->
width_
&&
output
Ptr
->
width_
==
labelP
tr
->
getWidth
()
&&
output
Ptr
->
height_
==
labelP
tr
->
getHeight
())
<<
"Matrix dimensions are not equal"
;
real
*
output_d
=
output
_p
tr
->
data_
;
real
*
output_d
=
output
P
tr
->
data_
;
real
*
grad_d
=
data_
;
hl_sparse_matrix_s
mat_d
=
label
_p
tr
->
sMatrix_
.
get
();
hl_sparse_matrix_s
mat_d
=
label
P
tr
->
sMatrix_
.
get
();
hl_matrix_multi_binary_cross_entropy_bp
(
output_d
,
grad_d
,
mat_d
,
height_
,
width_
);
}
...
...
paddle/math/Vector.cpp
浏览文件 @
5591292b
...
...
@@ -21,6 +21,7 @@ limitations under the License. */
#include "paddle/utils/ThreadLocal.h"
#include "paddle/utils/Thread.h"
#include "paddle/utils/Flags.h"
#include "Matrix.h"
#include "hl_gpu.h"
#include "hl_table_apply.h"
...
...
@@ -73,6 +74,31 @@ std::shared_ptr<VectorT<T>> VectorT<T>::create(size_t size,
}
}
template
<
>
MatrixPtr
VectorT
<
real
>::
toOneHotSparseMatrix
(
size_t
idRange
,
bool
useGpu
)
{
LOG
(
FATAL
)
<<
"Wrong for real vector"
;
return
nullptr
;
}
template
<
>
MatrixPtr
VectorT
<
int
>::
toOneHotSparseMatrix
(
size_t
idRange
,
bool
useGpu
)
{
int
height
=
getSize
();
int
width
=
idRange
;
MatrixPtr
mat
=
Matrix
::
createSparseMatrix
(
height
,
idRange
,
height
,
NO_VALUE
,
SPARSE_CSR
,
false
,
useGpu
);
CpuIVector
cpuIds
(
height
);
cpuIds
.
copyFrom
(
*
this
);
int
*
idData
=
cpuIds
.
getData
();
for
(
int
i
=
0
;
i
<
height
;
i
++
)
{
const
unsigned
int
id
=
idData
[
i
];
CHECK_LT
(
id
,
width
);
mat
->
setRow
(
i
,
1
,
&
id
,
nullptr
);
}
return
mat
;
}
template
<
class
T
>
GpuVectorT
<
T
>::
GpuVectorT
(
size_t
size
)
:
VectorT
<
T
>
(
size
,
std
::
make_shared
<
GpuMemoryHandle
>
(
sizeof
(
T
)
*
size
),
...
...
paddle/math/Vector.h
浏览文件 @
5591292b
...
...
@@ -37,6 +37,8 @@ class BaseVector;
class
SyncThreadPool
;
class
Matrix
;
template
<
class
T
>
class
BaseVector
:
public
BaseMatrixT
<
T
>
{
public:
...
...
@@ -155,6 +157,12 @@ public:
subVecFrom
(
src
,
interval
.
first
,
interval
.
second
-
interval
.
first
);
}
/**
* convert the vector to a sparse one_hot matrix of width idRange
* only applies to IVector
*/
std
::
shared_ptr
<
Matrix
>
toOneHotSparseMatrix
(
size_t
idRange
,
bool
useGpu
);
/**
* This function will crash if the size of src and dest is different.
*/
...
...
paddle/math/tests/test_matrixCompare.cpp
浏览文件 @
5591292b
...
...
@@ -2232,26 +2232,15 @@ void testMultiBinaryLabelCrossEntropy(int numSamples, int dim) {
MatrixPtr
cpuGrad
=
std
::
make_shared
<
CpuMatrix
>
(
numSamples
,
dim
);
MatrixPtr
gpuGrad
=
std
::
make_shared
<
GpuMatrix
>
(
numSamples
,
dim
);
auto
cpuRows
=
IVector
::
create
(
numSamples
+
1
,
false
);
auto
cpuCols
=
IVector
::
create
(
numSamples
,
false
);
auto
gpuRows
=
IVector
::
create
(
numSamples
+
1
,
true
);
auto
gpuCols
=
IVector
::
create
(
numSamples
,
true
);
cpuRows
->
setElement
(
0
,
0
);
gpuRows
->
setElement
(
0
,
0
);
for
(
int
i
=
0
;
i
<
numSamples
;
i
++
)
{
int
id
=
rand
()
%
dim
;
// NOLINT
cpuRows
->
setElement
(
i
+
1
,
i
+
1
);
gpuRows
->
setElement
(
i
+
1
,
i
+
1
);
cpuCols
->
setElement
(
i
,
id
);
gpuCols
->
setElement
(
i
,
id
);
}
MatrixPtr
cpuLabel
=
std
::
make_shared
<
CpuSparseMatrix
>
(
nullptr
,
cpuRows
->
getData
(),
cpuCols
->
getData
(),
numSamples
,
dim
,
numSamples
,
NO_VALUE
,
SPARSE_CSR
,
false
);
(
numSamples
,
dim
,
numSamples
,
NO_VALUE
,
SPARSE_CSR
,
false
);
MatrixPtr
gpuLabel
=
std
::
make_shared
<
GpuSparseMatrix
>
(
nullptr
,
gpuRows
->
getData
(),
gpuCols
->
getData
(),
numSamples
,
dim
,
numSamples
,
NO_VALUE
,
SPARSE_CSR
,
false
);
(
numSamples
,
dim
,
numSamples
,
NO_VALUE
,
SPARSE_CSR
,
false
);
for
(
int
i
=
0
;
i
<
numSamples
;
i
++
)
{
const
unsigned
int
id
=
rand
()
%
dim
;
// NOLINT
cpuLabel
->
setRow
(
i
,
1
,
&
id
,
nullptr
);
gpuLabel
->
setRow
(
i
,
1
,
&
id
,
nullptr
);
}
output
->
randomizeUniform
();
cpuOutput
->
zeroMem
();
...
...
@@ -2278,8 +2267,8 @@ void testMultiBinaryLabelCrossEntropy(int numSamples, int dim) {
}
TEST
(
Matrix
,
multiBinaryCrossEntropy
)
{
for
(
auto
numSamples
:
{
1
,
100
,
5
00
})
{
for
(
auto
dim
:
{
100
0
,
10000
,
10
0000
})
{
for
(
auto
numSamples
:
{
1
00
,
1000
,
100
00
})
{
for
(
auto
dim
:
{
100
,
1000
,
1
0000
})
{
VLOG
(
3
)
<<
" numSamples="
<<
numSamples
<<
" dim="
<<
dim
;
testMultiBinaryLabelCrossEntropy
(
numSamples
,
dim
);
}
...
...
paddle/parameter/Argument.cpp
浏览文件 @
5591292b
...
...
@@ -572,42 +572,4 @@ void Argument::subArgFrom(const Argument& input, size_t offset, size_t height,
}
}
void
Argument
::
idsToSparseMatrix
(
MatrixPtr
sparse_mat
)
{
int
height
=
ids
->
getSize
();
int
width
=
sparse_mat
->
getWidth
();
CpuIVector
cpu_ids
(
height
);
cpu_ids
.
copyFrom
(
*
ids
);
int
*
id_data
=
cpu_ids
.
getData
();
int
*
rows
=
nullptr
;
int
*
cols
=
nullptr
;
if
(
sparse_mat
->
useGpu
())
{
auto
gpu_sparse_mat
=
dynamic_cast
<
GpuSparseMatrix
*>
(
sparse_mat
.
get
());
rows
=
gpu_sparse_mat
->
rows_
;
cols
=
gpu_sparse_mat
->
cols_
;
}
else
{
rows
=
sparse_mat
->
getRows
();
cols
=
sparse_mat
->
getCols
();
}
rows
[
0
]
=
0
;
for
(
int
i
=
0
;
i
<
height
;
i
++
)
{
int
id
=
id_data
[
i
];
CHECK_LT
(
id
,
width
);
rows
[
i
+
1
]
=
i
+
1
;
cols
[
i
]
=
id
;
}
if
(
sparse_mat
->
useGpu
())
{
auto
gpu_sparse_mat
=
dynamic_cast
<
GpuSparseMatrix
*>
(
sparse_mat
.
get
());
hl_memcpy_csr_matrix
(
gpu_sparse_mat
->
sMatrix_
.
get
(),
nullptr
,
rows
,
cols
,
HPPL_STREAM_DEFAULT
);
hl_stream_synchronize
(
HPPL_STREAM_DEFAULT
);
}
}
}
// namespace paddle
paddle/parameter/Argument.h
浏览文件 @
5591292b
...
...
@@ -286,12 +286,6 @@ struct Argument {
sequence has sub-sequence degrades to a sequence.
*/
void
degradeSequence
(
const
Argument
&
input
,
bool
useGpu
);
/*
@brief convert the ids vector to value as a sparse matrix
@param[out] the output sparse_mat (already allocated)
*/
void
idsToSparseMatrix
(
MatrixPtr
sparse_mat
);
};
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录