diff --git a/paddle/function/BufferArg.cpp b/paddle/function/BufferArg.cpp index 08031917b21e1e57950ffc6d75d9f4cf899f5fe5..65c6f303041d830812fb2d99503b2b2166145f4a 100644 --- a/paddle/function/BufferArg.cpp +++ b/paddle/function/BufferArg.cpp @@ -28,16 +28,4 @@ const SparseMatrixArg& BufferArg::sparse() const { return dynamic_cast(*this); } -void BufferArgs::addArg(const Matrix& arg, const TensorShape& shape) { - args_.push_back(std::make_shared(arg, shape)); -} - -void BufferArgs::addArg(const CpuSparseMatrix& arg) { - args_.push_back(std::make_shared(arg)); -} - -void BufferArgs::addArg(const GpuSparseMatrix& arg) { - args_.push_back(std::make_shared(arg)); -} - } // namespace paddle diff --git a/paddle/function/BufferArgTest.cpp b/paddle/function/BufferArgTest.cpp index 5d669b8137e1a951d32bca6f4388068f40d6ff60..a9ee3ab079e339b86a9db8602c41e419df9dc544 100644 --- a/paddle/function/BufferArgTest.cpp +++ b/paddle/function/BufferArgTest.cpp @@ -14,6 +14,7 @@ limitations under the License. */ #include "BufferArg.h" #include +#include "Function.h" #include "paddle/math/MemoryHandle.h" namespace paddle { @@ -86,43 +87,4 @@ TEST(BufferTest, asArgument) { function(argments); } -template -void FunctionApi(typename Tensor::Matrix& output, - const typename Tensor::Matrix& input); - -template <> -void FunctionApi(CpuMatrix& output, const CpuMatrix& input) { - EXPECT_EQ(output.getHeight(), 100); - EXPECT_EQ(output.getWidth(), 200); -} - -template <> -void FunctionApi(GpuMatrix& output, const GpuMatrix& input) { - EXPECT_EQ(output.getHeight(), 10); - EXPECT_EQ(output.getWidth(), 20); -} - -template -void Function(const BufferArgs& arguments) { - auto input = arguments[0].matrix(); - auto output = arguments[1].matrix(); - FunctionApi(output, input); -} - -TEST(BufferTest, Function) { - CpuMatrix cpuInput = CpuMatrix(100, 200); - CpuMatrix cpuOutput = CpuMatrix(100, 200); - BufferArgs cpuArgments; - cpuArgments.addArg(cpuInput); - cpuArgments.addArg(cpuOutput); - Function(cpuArgments); - - GpuMatrix gpuInput = GpuMatrix(10, 20); - GpuMatrix gpuOutput = GpuMatrix(10, 20); - BufferArgs gpuArgments; - gpuArgments.addArg(gpuInput); - gpuArgments.addArg(gpuOutput); - Function(gpuArgments); -} - } // namespace paddle diff --git a/paddle/function/CMakeLists.txt b/paddle/function/CMakeLists.txt index 37c011549eca9c8cb9e248051424ad2418770348..31c395c8484a35f9db06dd91626302856414958d 100644 --- a/paddle/function/CMakeLists.txt +++ b/paddle/function/CMakeLists.txt @@ -21,6 +21,7 @@ if(WITH_TESTING) add_simple_unittest(TensorShapeTest) add_simple_unittest(TensorTypeTest) add_simple_unittest(BufferArgTest) + add_simple_unittest(FunctionTest) # add_unittest(ContextProjectionOpTest # ContextProjectionOpTest.cpp # ../gserver/tests/TestUtil.cpp) diff --git a/paddle/function/Function.cpp b/paddle/function/Function.cpp index 6f82a8d053bc203eed44bd0d8d4c47d23a15268d..2f56cfc1b5492c23d596f4bfb5a7ae9f066bd10b 100644 --- a/paddle/function/Function.cpp +++ b/paddle/function/Function.cpp @@ -72,6 +72,18 @@ FuncConfig& FuncConfig::set(const std::string& key, bool v) { return *this; } +void BufferArgs::addArg(const Matrix& arg, const TensorShape& shape) { + args_.push_back(std::make_shared(arg, shape)); +} + +void BufferArgs::addArg(const CpuSparseMatrix& arg) { + args_.push_back(std::make_shared(arg)); +} + +void BufferArgs::addArg(const GpuSparseMatrix& arg) { + args_.push_back(std::make_shared(arg)); +} + ClassRegistrar FunctionBase::funcRegistrar_; } // namespace paddle diff --git a/paddle/function/FunctionTest.cpp b/paddle/function/FunctionTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c3d6684cded1db1801cb18d637bbd4a976298ca --- /dev/null +++ b/paddle/function/FunctionTest.cpp @@ -0,0 +1,59 @@ +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#include "Function.h" +#include + +namespace paddle { + +template +void FunctionApi(typename Tensor::Matrix& output, + const typename Tensor::Matrix& input); + +template <> +void FunctionApi(CpuMatrix& output, const CpuMatrix& input) { + EXPECT_EQ(output.getHeight(), 100); + EXPECT_EQ(output.getWidth(), 200); +} + +template <> +void FunctionApi(GpuMatrix& output, const GpuMatrix& input) { + EXPECT_EQ(output.getHeight(), 10); + EXPECT_EQ(output.getWidth(), 20); +} + +template +void Function(const BufferArgs& arguments) { + auto input = arguments[0].matrix(); + auto output = arguments[1].matrix(); + FunctionApi(output, input); +} + +TEST(Function, BufferArgs) { + CpuMatrix cpuInput = CpuMatrix(100, 200); + CpuMatrix cpuOutput = CpuMatrix(100, 200); + BufferArgs cpuArgments; + cpuArgments.addArg(cpuInput); + cpuArgments.addArg(cpuOutput); + Function(cpuArgments); + + GpuMatrix gpuInput = GpuMatrix(10, 20); + GpuMatrix gpuOutput = GpuMatrix(10, 20); + BufferArgs gpuArgments; + gpuArgments.addArg(gpuInput); + gpuArgments.addArg(gpuOutput); + Function(gpuArgments); +} + +} // namespace paddle