FunctionTest.h 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/* 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 "paddle/math/Vector.h"
#include "paddle/math/tests/TensorCheck.h"

namespace paddle {

class FunctionCompare {
public:
  FunctionCompare(const std::string& name, const FuncConfig& config)
      : cpu(FunctionBase::funcRegistrar_.createByType(name + "-CPU")),
        gpu(FunctionBase::funcRegistrar_.createByType(name + "-GPU")) {
    cpu->init(config);
    gpu->init(config);
  }

30 31 32
  void cmpWithArg(const BufferArgs& inputs,
                  const BufferArgs& outputs,
                  const BufferArgs& inouts) {
33 34
    // init cpu and gpu arguments
    auto initArgs = [=](
35 36 37 38
        BufferArgs& cpuArgs, BufferArgs& gpuArgs, const BufferArgs& inArgs) {
      /// leave it empty to pass the compile of ContextProjectionTest
      /// Daoyuan is working on FunctionTest
      /// and I will further merge with it
39 40 41 42 43
    };
    initArgs(cpuInputs, gpuInputs, inputs);
    initArgs(cpuOutputs, gpuOutputs, outputs);

    // function calculate
44 45
    cpu->calc(cpuInputs, cpuOutputs);
    gpu->calc(gpuInputs, gpuOutputs);
46 47

    // check outputs and inouts
48 49
    auto checkArgs = [=](const BufferArgs& cpuArgs, const BufferArgs& gpuArgs) {
      /// leave it open
50 51 52 53
    };
    checkArgs(cpuOutputs, gpuOutputs);
  }

54 55 56 57
  std::shared_ptr<FunctionBase> getCpuFunction() const { return cpu; }

  std::shared_ptr<FunctionBase> getGpuFunction() const { return gpu; }

58 59 60 61 62
protected:
  std::shared_ptr<FunctionBase> cpu;
  std::shared_ptr<FunctionBase> gpu;
  std::vector<CpuMemHandlePtr> cpuMemory;
  std::vector<GpuMemHandlePtr> gpuMemory;
63 64 65 66 67 68
  BufferArgs cpuInputs;
  BufferArgs cpuOutputs;
  BufferArgs cpuInouts;
  BufferArgs gpuInputs;
  BufferArgs gpuOutputs;
  BufferArgs gpuInouts;
69 70 71
};

}  // namespace paddle