framework.h 8.4 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

#pragma once
#include <gtest/gtest.h>
#include <time.h>
#include <algorithm>
#include <chrono>  // NOLINT
20
#include <cmath>
Y
Yan Chunwei 已提交
21
#include <iomanip>
22
#include <map>
Y
Yan Chunwei 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "lite/core/op_registry.h"
#include "lite/core/program.h"
#include "lite/core/scope.h"
#include "lite/core/types.h"
#include "lite/model_parser/cpp/op_desc.h"

namespace paddle {
namespace lite {
namespace arena {

/*
 * Init data and prepare the op.
 */
class TestCase {
 public:
  explicit TestCase(const Place& place, const std::string& alias)
43 44 45 46
      : place_(place),
        alias_(alias),
        inst_scope_(new Scope),
        base_scope_(new Scope) {
Y
Yan Chunwei 已提交
47 48
    ctx_ = ContextScheduler::Global().NewContext(place_.target);
  }
49
  virtual ~TestCase();
Y
Yan Chunwei 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

  void Prepare() {
    PrepareData();
    op_desc_.reset(new cpp::OpDesc);
    PrepareOpDesc(op_desc_.get());

    PrepareOutputsForInstruction();
    CreateInstruction();
    PrepareInputsForInstruction();
  }

  /// Run the target instruction, that is run the test operator.
  void RunInstruction() { instruction_->Run(); }

  KernelContext* context() { return ctx_.get(); }

  /// The baseline should be implemented, which acts similar to an operator,
  /// that is take several tensors as input and output several tensors as
  /// output.
  virtual void RunBaseline(Scope* scope) = 0;

71
  // checkout the precision of the two tensors with type T. b_tensor is baseline
72 73 74 75 76
  template <typename T>
  bool CheckTensorPrecision(const Tensor* a_tensor,
                            const Tensor* b_tensor,
                            float abs_error);

77 78 79 80 81 82
  // checkout the precision of the two tensors. b_tensor is baseline
  bool CheckPrecision(const Tensor* a_tensor,
                      const Tensor* b_tensor,
                      float abs_error,
                      PrecisionType precision_type);

83
  /// Check the precision of the output variables. It will compare the same
84 85 86 87 88
  /// tensor (or all tensors of the tensor_array) in two scopes, one of the
  /// instruction execution, and the other for the baseline.
  bool CheckPrecision(const std::string& var_name,
                      float abs_error,
                      PrecisionType precision_type);
Y
Yan Chunwei 已提交
89 90 91 92 93 94 95

  const cpp::OpDesc& op_desc() { return *op_desc_; }

  // Check whether the output tensor is consistent with the output definition in
  // kernel registry.
  void CheckKernelConsistWithDefinition() {}

96 97
  Scope* baseline_scope() { return base_scope_.get(); }
  Scope* inst_scope() { return inst_scope_.get(); }
Y
Yan Chunwei 已提交
98 99 100 101 102

 protected:
  // Prepare inputs in scope() for Tester.
  virtual void PrepareData() = 0;

103 104
  /// Prepare a tensor in host. The tensors will be created both in base_scope_
  /// and inst_scope_.
Y
Yan Chunwei 已提交
105 106 107 108 109
  /// Need to specify the targets other than X86 or ARM.
  template <typename T>
  void SetCommonTensor(const std::string& var_name,
                       const DDim& ddim,
                       const T* data,
Z
zhupengyang 已提交
110 111
                       const LoD& lod = {},
                       bool is_persistable = false) {
112 113 114 115
    // Create and fill a input tensor with the given data for baseline
    auto* base_tensor = base_scope_->NewTensor(var_name);
    base_tensor->Resize(ddim);
    memcpy(base_tensor->mutable_data<T>(), data, ddim.production() * sizeof(T));
Y
Yan Chunwei 已提交
116 117

    // set lod
118
    if (!lod.empty()) *base_tensor->mutable_lod() = lod;
Z
zhupengyang 已提交
119
    // set persistable
120 121 122 123 124
    base_tensor->set_persistable(is_persistable);

    // Create a copy for instruction
    auto* inst_tensor = inst_scope_->NewTensor(var_name);
    inst_tensor->CopyDataFrom(*base_tensor);
Y
Yan Chunwei 已提交
125 126
  }

127 128 129 130
  /// Prepare a tensor_array in host. The tensors will be created in scope_.
  /// Need to specify the targets other than X86 or ARM.
  template <typename T>
  void SetCommonTensorList(const std::string& var_name,
131
                           const std::vector<DDim>& ddims,
132 133
                           const std::vector<std::vector<T>>& datas,
                           const std::vector<LoD>& lods = {}) {
134 135
    // Create a tensor array for baseline, and a copy for instruction
    CHECK_EQ(ddims.size(), datas.size());
136
    if (!lods.empty()) {
137
      CHECK_EQ(ddims.size(), lods.size());
138 139
    }

140 141 142 143 144 145
    auto* base_tensor_list = base_scope_->NewTensorList(var_name);
    auto* inst_tensor_list = inst_scope_->NewTensorList(var_name);
    for (int i = 0; i < ddims.size(); i++) {
      Tensor item;
      item.Resize(ddims[i]);
      memcpy(item.mutable_data<T>(),
146
             datas[i].data(),
147
             ddims[i].production() * sizeof(T));
148
      if (!lods.empty()) {
149
        item.set_lod(lods[i]);
150
      }
151 152
      base_tensor_list->push_back(item);
      inst_tensor_list->push_back(item);
153 154 155
    }
  }

Y
Yan Chunwei 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169
  // Prepare for the operator.
  virtual void PrepareOpDesc(cpp::OpDesc* op_desc) = 0;

 public:
  const Instruction& instruction() { return *instruction_; }

 private:
  std::unique_ptr<KernelContext> ctx_;
  void CreateInstruction();

  // Check shape
  // TODO(Superjomn) Move this method to utils or DDim?
  bool ShapeEquals(const DDim& a, const DDim& b) {
    if (a.size() != b.size()) return false;
170
    for (size_t i = 0; i < a.size(); i++) {
Y
Yan Chunwei 已提交
171 172 173 174 175
      if (a[i] != b[i]) return false;
    }
    return true;
  }

176
  // Copy the host tensors to the device tensors if needed by the instruction.
Y
Yan Chunwei 已提交
177 178 179 180 181
  void PrepareInputsForInstruction();

  // Create output tensors and variables.
  void PrepareOutputsForInstruction() {
    for (auto x : op_desc().output_vars()) {
182
      inst_scope_->Var(x);
Y
Yan Chunwei 已提交
183 184 185 186
    }
  }

 private:
187 188
  Place place_;
  std::string alias_;
Y
Yan Chunwei 已提交
189
  // The workspace for the Instruction.
190
  std::shared_ptr<Scope> inst_scope_;
Y
Yan Chunwei 已提交
191
  // The workspace for the baseline implementation.
192
  std::shared_ptr<Scope> base_scope_;
Y
Yan Chunwei 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205
  std::unique_ptr<cpp::OpDesc> op_desc_;
  std::unique_ptr<Instruction> instruction_;
};

class Arena {
 public:
  Arena(std::unique_ptr<TestCase>&& tester,
        const Place& place,
        float abs_error = 1e-5)
      : tester_(std::move(tester)), place_(place), abs_error_(abs_error) {
    tester_->Prepare();
  }

206
  bool TestPrecision(const std::vector<std::string>& exclude_outs = {}) {
Y
Yan Chunwei 已提交
207 208 209 210 211 212
    tester_->RunBaseline(tester_->baseline_scope());
    tester_->RunInstruction();

    bool success = true;
    for (auto& out : tester_->op_desc().OutputArgumentNames()) {
      for (auto& var : tester_->op_desc().Output(out)) {
213 214 215 216
        if (std::find(exclude_outs.begin(), exclude_outs.end(), var) !=
            exclude_outs.end()) {
          continue;
        }
Y
Yan Chunwei 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230
        success = success && CompareTensor(out, var);
      }
    }
    LOG(INFO) << "done";
    return success;
  }

  void TestPerformance(int times = 100) {
    auto timer = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < times; i++) {
      tester_->RunInstruction();
    }
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
        std::chrono::high_resolution_clock::now() - timer);
231 232 233 234 235 236 237 238 239 240 241

    timer = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < times; i++) {
      tester_->RunBaseline(tester_->baseline_scope());
    }
    auto duration_basic = std::chrono::duration_cast<std::chrono::milliseconds>(
        std::chrono::high_resolution_clock::now() - timer);
    LOG(INFO) << "average lite duration: " << duration.count() << " ms";
    LOG(INFO) << "average basic duration: " << duration_basic.count() << " ms";
    LOG(INFO) << "speed up ratio: lite_speed / basic_speed: "
              << static_cast<float>(duration_basic.count()) / duration.count();
Y
Yan Chunwei 已提交
242 243 244 245 246 247 248 249
  }

 private:
  // input_name: X
  bool CompareTensor(const std::string& arg_name, const std::string& var_name) {
    // get tensor type.
    const Type* type =
        tester_->instruction().kernel()->GetOutputDeclType(arg_name);
J
juncaipeng 已提交
250
    auto precision_type = type->precision();
251
    return tester_->CheckPrecision(var_name, abs_error_, precision_type);
Y
Yan Chunwei 已提交
252 253 254 255 256
  }

 private:
  std::unique_ptr<TestCase> tester_;
  Place place_;
257
  float abs_error_;
Y
Yan Chunwei 已提交
258 259 260 261 262
};

}  // namespace arena
}  // namespace lite
}  // namespace paddle