Skip to content

Commit ccf0b1b

Browse files
committed
add FunctionTest.cpp
1 parent f3fdfd9 commit ccf0b1b

File tree

5 files changed

+73
-51
lines changed

5 files changed

+73
-51
lines changed

paddle/function/BufferArg.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,4 @@ const SparseMatrixArg& BufferArg::sparse() const {
2828
return dynamic_cast<const SparseMatrixArg&>(*this);
2929
}
3030

31-
void BufferArgs::addArg(const Matrix& arg, const TensorShape& shape) {
32-
args_.push_back(std::make_shared<BufferArg>(arg, shape));
33-
}
34-
35-
void BufferArgs::addArg(const CpuSparseMatrix& arg) {
36-
args_.push_back(std::make_shared<SparseMatrixArg>(arg));
37-
}
38-
39-
void BufferArgs::addArg(const GpuSparseMatrix& arg) {
40-
args_.push_back(std::make_shared<SparseMatrixArg>(arg));
41-
}
42-
4331
} // namespace paddle

paddle/function/BufferArgTest.cpp

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ limitations under the License. */
1414

1515
#include "BufferArg.h"
1616
#include <gtest/gtest.h>
17+
#include "Function.h"
1718
#include "paddle/math/MemoryHandle.h"
1819

1920
namespace paddle {
@@ -86,43 +87,4 @@ TEST(BufferTest, asArgument) {
8687
function(argments);
8788
}
8889

89-
template <DeviceType DType>
90-
void FunctionApi(typename Tensor<real, DType>::Matrix& output,
91-
const typename Tensor<real, DType>::Matrix& input);
92-
93-
template <>
94-
void FunctionApi<DEVICE_TYPE_CPU>(CpuMatrix& output, const CpuMatrix& input) {
95-
EXPECT_EQ(output.getHeight(), 100);
96-
EXPECT_EQ(output.getWidth(), 200);
97-
}
98-
99-
template <>
100-
void FunctionApi<DEVICE_TYPE_GPU>(GpuMatrix& output, const GpuMatrix& input) {
101-
EXPECT_EQ(output.getHeight(), 10);
102-
EXPECT_EQ(output.getWidth(), 20);
103-
}
104-
105-
template <DeviceType DType>
106-
void Function(const BufferArgs& arguments) {
107-
auto input = arguments[0].matrix<DType>();
108-
auto output = arguments[1].matrix<DType>();
109-
FunctionApi<DType>(output, input);
110-
}
111-
112-
TEST(BufferTest, Function) {
113-
CpuMatrix cpuInput = CpuMatrix(100, 200);
114-
CpuMatrix cpuOutput = CpuMatrix(100, 200);
115-
BufferArgs cpuArgments;
116-
cpuArgments.addArg(cpuInput);
117-
cpuArgments.addArg(cpuOutput);
118-
Function<DEVICE_TYPE_CPU>(cpuArgments);
119-
120-
GpuMatrix gpuInput = GpuMatrix(10, 20);
121-
GpuMatrix gpuOutput = GpuMatrix(10, 20);
122-
BufferArgs gpuArgments;
123-
gpuArgments.addArg(gpuInput);
124-
gpuArgments.addArg(gpuOutput);
125-
Function<DEVICE_TYPE_GPU>(gpuArgments);
126-
}
127-
12890
} // namespace paddle

paddle/function/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ if(WITH_TESTING)
2121
add_simple_unittest(TensorShapeTest)
2222
add_simple_unittest(TensorTypeTest)
2323
add_simple_unittest(BufferArgTest)
24+
add_simple_unittest(FunctionTest)
2425
# add_unittest(ContextProjectionOpTest
2526
# ContextProjectionOpTest.cpp
2627
# ../gserver/tests/TestUtil.cpp)

paddle/function/Function.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ FuncConfig& FuncConfig::set<bool>(const std::string& key, bool v) {
7272
return *this;
7373
}
7474

75+
void BufferArgs::addArg(const Matrix& arg, const TensorShape& shape) {
76+
args_.push_back(std::make_shared<BufferArg>(arg, shape));
77+
}
78+
79+
void BufferArgs::addArg(const CpuSparseMatrix& arg) {
80+
args_.push_back(std::make_shared<SparseMatrixArg>(arg));
81+
}
82+
83+
void BufferArgs::addArg(const GpuSparseMatrix& arg) {
84+
args_.push_back(std::make_shared<SparseMatrixArg>(arg));
85+
}
86+
7587
ClassRegistrar<FunctionBase> FunctionBase::funcRegistrar_;
7688

7789
} // namespace paddle

paddle/function/FunctionTest.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "Function.h"
16+
#include <gtest/gtest.h>
17+
18+
namespace paddle {
19+
20+
template <DeviceType DType>
21+
void FunctionApi(typename Tensor<real, DType>::Matrix& output,
22+
const typename Tensor<real, DType>::Matrix& input);
23+
24+
template <>
25+
void FunctionApi<DEVICE_TYPE_CPU>(CpuMatrix& output, const CpuMatrix& input) {
26+
EXPECT_EQ(output.getHeight(), 100);
27+
EXPECT_EQ(output.getWidth(), 200);
28+
}
29+
30+
template <>
31+
void FunctionApi<DEVICE_TYPE_GPU>(GpuMatrix& output, const GpuMatrix& input) {
32+
EXPECT_EQ(output.getHeight(), 10);
33+
EXPECT_EQ(output.getWidth(), 20);
34+
}
35+
36+
template <DeviceType DType>
37+
void Function(const BufferArgs& arguments) {
38+
auto input = arguments[0].matrix<DType>();
39+
auto output = arguments[1].matrix<DType>();
40+
FunctionApi<DType>(output, input);
41+
}
42+
43+
TEST(Function, BufferArgs) {
44+
CpuMatrix cpuInput = CpuMatrix(100, 200);
45+
CpuMatrix cpuOutput = CpuMatrix(100, 200);
46+
BufferArgs cpuArgments;
47+
cpuArgments.addArg(cpuInput);
48+
cpuArgments.addArg(cpuOutput);
49+
Function<DEVICE_TYPE_CPU>(cpuArgments);
50+
51+
GpuMatrix gpuInput = GpuMatrix(10, 20);
52+
GpuMatrix gpuOutput = GpuMatrix(10, 20);
53+
BufferArgs gpuArgments;
54+
gpuArgments.addArg(gpuInput);
55+
gpuArgments.addArg(gpuOutput);
56+
Function<DEVICE_TYPE_GPU>(gpuArgments);
57+
}
58+
59+
} // namespace paddle

0 commit comments

Comments
 (0)