|
| 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