FunctionTest.h 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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"
16 17
#include "paddle/math/Matrix.h"
#include "paddle/math/SparseMatrix.h"
18
#include "paddle/math/tests/TensorCheck.h"
H
hedaoyuan 已提交
19
#include "paddle/testing/TestUtil.h"
20 21 22

namespace paddle {

H
hedaoyuan 已提交
23 24
typedef std::shared_ptr<BufferArg> BufferArgPtr;

H
hedaoyuan 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/**
 * \brief A class for comparing CPU and GPU implementations of Function.
 *
 *
 * Use case:
 *  // Initializes a test object, the corresponding cpu and gpu Function
 *  // are constructed according to FunctionName and FuncConfig.
 *  FunctionCompare test(FunctionName, FuncConfig);
 *  // Prepare inputs and outputs arguments.
 *  // Here the input and output can not contain real data,
 *  // only contains the argument type and shape.
 *  test.addInputs(input1);
 *  test.addInputs(input2);
 *  test.addOutputs(output1);
 *  test.addOutputs(output2);
 *  // Run.
 *  // Will according to the type and shape of arguments(inputs_/outputs_),
 *  // automatic initialization cpu and gpu function required arguments
 *  // (cpuInputs_/cpuOutputs_/gpuInputs_/gpuOutputs_).
 *  // Call the CPU and GPU Function calculation results.
 *  // Compares CPU and GPU calculation results for consistency.
 *  test.run();
 */
48 49 50
class FunctionCompare {
public:
  FunctionCompare(const std::string& name, const FuncConfig& config)
H
hedaoyuan 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
      : cpuFunc_(FunctionBase::funcRegistrar_.createByType(name + "-CPU")),
        gpuFunc_(FunctionBase::funcRegistrar_.createByType(name + "-GPU")) {
    cpuFunc_->init(config);
    gpuFunc_->init(config);
  }

  ~FunctionCompare() {}

  // input need only contains shape, do not contains data.
  void addInputs(const BufferArg& input) {
    size_t size =
        input.shape().getElements() * sizeOfValuType(input.valueType());
    cpuMemory_.emplace_back(std::make_shared<CpuMemoryHandle>(size));
    gpuMemory_.emplace_back(std::make_shared<GpuMemoryHandle>(size));

X
xutianbing 已提交
66 67 68 69
    cpuInputs_.emplace_back(std::make_shared<BufferArg>(
        cpuMemory_.back()->getBuf(), input.valueType(), input.shape()));
    gpuInputs_.emplace_back(std::make_shared<BufferArg>(
        gpuMemory_.back()->getBuf(), input.valueType(), input.shape()));
H
hedaoyuan 已提交
70 71
  }

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  // assume one copy of sequence is shared by different SequenceArgs
  void addSequence(const SequenceIdArg& input) {
    CHECK_EQ(input.shape().ndims(), 1UL);
    size_t batchSize = input.shape()[0];
    size_t numSeqs = batchSize / 10 + 1;
    size_t sizeId = (numSeqs + 1) * sizeOfValuType(VALUE_TYPE_INT32);
    cpuMemory_.emplace_back(std::make_shared<CpuMemoryHandle>(sizeId));
    gpuMemory_.emplace_back(std::make_shared<GpuMemoryHandle>(sizeId));
    cpuSeq_ = std::make_shared<SequenceIdArg>(cpuMemory_.back()->getBuf(),
                                              TensorShape{numSeqs + 1});
    gpuSeq_ = std::make_shared<SequenceIdArg>(gpuMemory_.back()->getBuf(),
                                              TensorShape{numSeqs + 1});
    /// init sequence Id
    initArg(*cpuSeq_, batchSize);

    // todo(tianbing), delete it
    CHECK_EQ(cpuSeq_->shape().getElements(), cpuSeq_->numSeqs() + 1);

    CpuIVector cpuSeq(cpuSeq_->shape().getElements(), (int*)cpuSeq_->data());
    GpuIVector gpuSeq(gpuSeq_->shape().getElements(), (int*)gpuSeq_->data());
    gpuSeq.copyFrom(cpuSeq);
  }

  void addInputs(const SequenceArg& input) {
    CHECK_EQ(input.shape().ndims(), 2UL);
    size_t batchSize = input.shape()[0];
    if (!cpuSeq_ || !gpuSeq_) {  // sequence not exist
      addSequence(SequenceIdArg(TensorShape{batchSize}));
    }

    size_t size =
        input.shape().getElements() * sizeOfValuType(input.valueType());
    cpuMemory_.emplace_back(std::make_shared<CpuMemoryHandle>(size));
    gpuMemory_.emplace_back(std::make_shared<GpuMemoryHandle>(size));

    /// SequenceArg
    cpuInputs_.emplace_back(
        std::make_shared<SequenceArg>(cpuMemory_.back()->getBuf(),
                                      input.valueType(),
                                      input.shape(),
                                      *cpuSeq_));
    gpuInputs_.emplace_back(
        std::make_shared<SequenceArg>(gpuMemory_.back()->getBuf(),
                                      input.valueType(),
                                      input.shape(),
                                      *gpuSeq_));
  }

H
hedaoyuan 已提交
120
  // output need only contains shape, do not contains data.
X
xutianbing 已提交
121
  void addOutputs(const BufferArg& output, ArgType argType = ASSIGN_TO) {
H
hedaoyuan 已提交
122 123 124 125 126
    size_t size =
        output.shape().getElements() * sizeOfValuType(output.valueType());
    cpuMemory_.emplace_back(std::make_shared<CpuMemoryHandle>(size));
    gpuMemory_.emplace_back(std::make_shared<GpuMemoryHandle>(size));

127 128 129 130 131 132 133 134 135 136
    cpuOutputs_.emplace_back(
        std::make_shared<BufferArg>(cpuMemory_.back()->getBuf(),
                                    output.valueType(),
                                    output.shape(),
                                    argType));
    gpuOutputs_.emplace_back(
        std::make_shared<BufferArg>(gpuMemory_.back()->getBuf(),
                                    output.valueType(),
                                    output.shape(),
                                    argType));
137 138
  }

139 140
  /// add and init output sparse matrix
  void addOutputs(const SparseMatrixArg& output, ArgType argType = ASSIGN_TO) {
141 142 143 144 145 146 147 148 149 150 151 152 153
    cpuSparse_ = std::make_shared<CpuSparseMatrix>(
        output.shape()[0],
        output.shape()[1],
        output.nnz(),
        static_cast<SparseValueType>(output.dataType()),
        static_cast<SparseFormat>(output.dataFormat()));

    gpuSparse_ = std::make_shared<GpuSparseMatrix>(
        output.shape()[0],
        output.shape()[1],
        output.nnz(),
        static_cast<SparseValueType>(output.dataType()),
        static_cast<SparseFormat>(output.dataFormat()));
154 155 156 157 158 159

    /// init sparse matrix
    hl_stream_t stream(HPPL_STREAM_1);
    cpuSparse_->randomizeUniform();
    gpuSparse_->copyFrom(*cpuSparse_, stream);
    hl_stream_synchronize(stream);
160

161 162 163
  void addInputs(const SequenceArg& input) {
    size_t batchSize = input.shape()[0];
    size_t numSeqs = batchSize / 10 + 1;
164 165 166 167 168 169 170

    cpuOutputs_.emplace_back(
        std::make_shared<SparseMatrixArg>(*cpuSparse_, argType));
    gpuOutputs_.emplace_back(
        std::make_shared<SparseMatrixArg>(*gpuSparse_, argType));
  }

171 172 173
  void addOutputs(const SequenceArg& output, ArgType argType = ASSIGN_TO) {
    CHECK_EQ(output.shape().ndims(), 2UL);
    size_t batchSize = output.shape()[0];
H
hedaoyuan 已提交
174

175 176 177
    if (!cpuSeq_ || !gpuSeq_) {  // sequence not exist
      addSequence(SequenceIdArg(TensorShape{batchSize}));
    }
H
hedaoyuan 已提交
178
    size_t size =
179
        output.shape().getElements() * sizeOfValuType(output.valueType());
H
hedaoyuan 已提交
180 181 182
    cpuMemory_.emplace_back(std::make_shared<CpuMemoryHandle>(size));
    gpuMemory_.emplace_back(std::make_shared<GpuMemoryHandle>(size));

183 184 185 186 187 188 189 190 191 192 193 194 195
    /// SequenceArg
    cpuOutputs_.emplace_back(
        std::make_shared<SequenceArg>(cpuMemory_.back()->getBuf(),
                                      output.valueType(),
                                      output.shape(),
                                      *cpuSeq_,
                                      argType));
    gpuOutputs_.emplace_back(
        std::make_shared<SequenceArg>(gpuMemory_.back()->getBuf(),
                                      output.valueType(),
                                      output.shape(),
                                      *gpuSeq_,
                                      argType));
H
hedaoyuan 已提交
196
  }
H
hedaoyuan 已提交
197

198
  void addInputs(const SparseMatrixArg& input) {
199 200 201 202 203 204 205 206 207 208 209 210 211
    cpuSparse_ = std::make_shared<CpuSparseMatrix>(
        input.shape()[0],
        input.shape()[1],
        input.nnz(),
        static_cast<SparseValueType>(input.dataType()),
        static_cast<SparseFormat>(input.dataFormat()));

    gpuSparse_ = std::make_shared<GpuSparseMatrix>(
        input.shape()[0],
        input.shape()[1],
        input.nnz(),
        static_cast<SparseValueType>(input.dataType()),
        static_cast<SparseFormat>(input.dataFormat()));
212 213 214 215 216 217 218 219 220 221 222

    /// init sparse matrix
    hl_stream_t stream(HPPL_STREAM_1);
    cpuSparse_->randomizeUniform();
    gpuSparse_->copyFrom(*cpuSparse_, stream);
    hl_stream_synchronize(stream);

    cpuInputs_.emplace_back(std::make_shared<SparseMatrixArg>(*cpuSparse_));
    gpuInputs_.emplace_back(std::make_shared<SparseMatrixArg>(*gpuSparse_));
  }

H
hedaoyuan 已提交
223 224
  void run() {
    // prepare cpu/gpu arguments
H
hedaoyuan 已提交
225
    initInputs();
H
hedaoyuan 已提交
226

227
    initOutputs();
H
hedaoyuan 已提交
228
    // function calculate
H
hedaoyuan 已提交
229 230 231 232 233 234 235
    auto callFunction = [](FunctionBase* function,
                           std::vector<BufferArgPtr>& inputs,
                           std::vector<BufferArgPtr>& outputs) {
      BufferArgs inArgs;
      BufferArgs outArgs;
      for (auto arg : inputs) {
        inArgs.addArg(*arg);
H
hedaoyuan 已提交
236
      }
H
hedaoyuan 已提交
237 238
      for (auto arg : outputs) {
        outArgs.addArg(*arg);
239
      }
H
hedaoyuan 已提交
240
      function->calc(inArgs, outArgs);
241 242
    };

H
hedaoyuan 已提交
243 244
    callFunction(cpuFunc_.get(), cpuInputs_, cpuOutputs_);
    callFunction(gpuFunc_.get(), gpuInputs_, gpuOutputs_);
245

246
    // check outputs
H
hedaoyuan 已提交
247
    compareOutputs();
248 249
  }

H
hedaoyuan 已提交
250
  std::shared_ptr<FunctionBase> getCpuFunction() const { return cpuFunc_; }
251

H
hedaoyuan 已提交
252
  std::shared_ptr<FunctionBase> getGpuFunction() const { return gpuFunc_; }
253

H
hedaoyuan 已提交
254
protected:
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  // only init cpu argument, gpu argument copy from cpu argument.
  void initArg(BufferArg& arg) {
    CpuVector vector(arg.shape().getElements(), (real*)arg.data());
    vector.uniform(0.001, 1);
  }

  void initArg(SequenceArg& arg) {
    /// init only matrix
    CpuVector vector(arg.shape().getElements(), (real*)arg.data());
    vector.uniform(0.001, 1);
  }

  void initArg(SequenceIdArg& arg, size_t batchSize) {
    size_t numSeqs = arg.numSeqs();
    int* buf = reinterpret_cast<int*>(arg.data());
    int pos = 0;
    size_t maxLen = 2 * batchSize / numSeqs;
    for (int i = 0; i < (int)numSeqs; ++i) {
      int len = 1 + uniformRandom(std::min<int64_t>(
                        maxLen, batchSize - pos - numSeqs + i));
      buf[i] = pos;
      pos += len;
      VLOG(1) << " len=" << len;
    }
    buf[numSeqs] = batchSize;
  }

H
hedaoyuan 已提交
282 283
  void initInputs() {
    for (size_t i = 0; i < cpuInputs_.size(); i++) {
284 285 286 287
      if (cpuInputs_[i]->isSparseArg()) {
        continue;  /// sparse matrix already init
      }

288 289 290 291 292
      if (cpuInputs_[i]->isSequenceArg()) {
        initArg(dynamic_cast<SequenceArg&>(*cpuInputs_[i]));
      } else {
        initArg(*cpuInputs_[i]);
      }
H
hedaoyuan 已提交
293 294 295 296 297
      // TODO: Need a BufferCopy used to copy from one BufferArg to another.
      CpuVector cpuVector(cpuInputs_[i]->shape().getElements(),
                          (real*)cpuInputs_[i]->data());
      GpuVector gpuVector(gpuInputs_[i]->shape().getElements(),
                          (real*)gpuInputs_[i]->data());
H
hedaoyuan 已提交
298

H
hedaoyuan 已提交
299 300
      gpuVector.copyFrom(cpuVector);
    }
H
hedaoyuan 已提交
301 302
  }

303
  void initOutputs() {
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    for (size_t i = 0; i < cpuOutputs_.size(); i++) {
      initArg(*cpuOutputs_[i]);

      // TODO: Need a BufferCopy used to copy from one BufferArg to another.
      CpuVector cpuVector(cpuOutputs_[i]->shape().getElements(),
                          (real*)cpuOutputs_[i]->data());
      GpuVector gpuVector(gpuOutputs_[i]->shape().getElements(),
                          (real*)gpuOutputs_[i]->data());

      gpuVector.copyFrom(cpuVector);
    }
  }

  void compareOutputs() {
>>>>>>> rewrite unit test using Daoyuan's new FunctionTest.
319 320
    for (size_t i = 0; i < cpuOutputs_.size(); i++) {
      if (cpuOutputs_[i]->isSparseArg()) {
321
        continue;  /// sparse matrix already init
322 323
      }

324 325 326 327 328
      if (cpuOutputs_[i]->isSequenceArg()) {
        initArg(dynamic_cast<SequenceArg&>(*cpuOutputs_[i]));
      } else {
        initArg(*cpuOutputs_[i]);
      }
329 330 331 332 333 334 335 336 337 338 339

      // TODO: Need a BufferCopy used to copy from one BufferArg to another.
      CpuVector cpuVector(cpuOutputs_[i]->shape().getElements(),
                          (real*)cpuOutputs_[i]->data());
      GpuVector gpuVector(gpuOutputs_[i]->shape().getElements(),
                          (real*)gpuOutputs_[i]->data());

      gpuVector.copyFrom(cpuVector);
    }
  }

H
hedaoyuan 已提交
340 341 342
  void compareOutputs() {
    for (size_t i = 0; i < cpuOutputs_.size(); i++) {
      // TODO, Need a BufferCheck used to compare the two buffers.
343 344 345 346 347
      const auto cpu = cpuOutputs_[i];
      const auto gpu = gpuOutputs_[i];
      CHECK_EQ(cpu->numElements(), gpu->numElements());
      CpuVector cpuVector(cpu->numElements(), (real*)cpu->data());
      GpuVector gpuVector(gpu->numElements(), (real*)gpu->data());
H
hedaoyuan 已提交
348 349
      autotest::TensorCheckErr(cpuVector, gpuVector);
    }
H
hedaoyuan 已提交
350 351
  }

352
protected:
H
hedaoyuan 已提交
353 354
  std::shared_ptr<FunctionBase> cpuFunc_;
  std::shared_ptr<FunctionBase> gpuFunc_;
H
hedaoyuan 已提交
355 356
  std::vector<CpuMemHandlePtr> cpuMemory_;
  std::vector<GpuMemHandlePtr> gpuMemory_;
H
hedaoyuan 已提交
357 358 359 360
  std::vector<BufferArgPtr> cpuInputs_;
  std::vector<BufferArgPtr> cpuOutputs_;
  std::vector<BufferArgPtr> gpuInputs_;
  std::vector<BufferArgPtr> gpuOutputs_;
361 362
  std::shared_ptr<CpuSparseMatrix> cpuSparse_;
  std::shared_ptr<GpuSparseMatrix> gpuSparse_;
363 364
  std::shared_ptr<SequenceIdArg> cpuSeq_;
  std::shared_ptr<SequenceIdArg> gpuSeq_;
365 366 367
};

}  // namespace paddle