op_registry.h 22.0 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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 <list>
18
#include <map>
Y
Yan Chunwei 已提交
19 20 21
#include <memory>
#include <set>
#include <string>
22
#include <tuple>
Y
Yan Chunwei 已提交
23 24 25 26 27 28 29 30
#include <unordered_map>
#include <utility>
#include <vector>
#include "lite/api/paddle_lite_factory_helper.h"
#include "lite/core/kernel.h"
#include "lite/core/op_lite.h"
#include "lite/core/target_wrapper.h"
#include "lite/utils/all.h"
31
#include "lite/utils/macros.h"
Y
Yan Chunwei 已提交
32 33 34

using LiteType = paddle::lite::Type;

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
class OpKernelInfoCollector {
 public:
  static OpKernelInfoCollector &Global() {
    static auto *x = new OpKernelInfoCollector;
    return *x;
  }
  void AddOp2path(const std::string &op_name, const std::string &op_path) {
    size_t index = op_path.find_last_of('/');
    if (index != std::string::npos) {
      op2path_.insert(std::pair<std::string, std::string>(
          op_name, op_path.substr(index + 1)));
    }
  }
  void AddKernel2path(const std::string &kernel_name,
                      const std::string &kernel_path) {
    size_t index = kernel_path.find_last_of('/');
    if (index != std::string::npos) {
      kernel2path_.insert(std::pair<std::string, std::string>(
          kernel_name, kernel_path.substr(index + 1)));
    }
  }
  void SetKernel2path(
      const std::map<std::string, std::string> &kernel2path_map) {
    kernel2path_ = kernel2path_map;
  }
  const std::map<std::string, std::string> &GetOp2PathDict() {
    return op2path_;
  }
  const std::map<std::string, std::string> &GetKernel2PathDict() {
    return kernel2path_;
  }

 private:
  std::map<std::string, std::string> op2path_;
  std::map<std::string, std::string> kernel2path_;
};

Y
Yan Chunwei 已提交
72 73 74
namespace paddle {
namespace lite {

75 76
const std::map<std::string, std::string> &GetOp2PathDict();

Y
Yan Chunwei 已提交
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
using KernelFunc = std::function<void()>;
using KernelFuncCreator = std::function<std::unique_ptr<KernelFunc>()>;
class LiteOpRegistry final : public Factory<OpLite, std::shared_ptr<OpLite>> {
 public:
  static LiteOpRegistry &Global() {
    static auto *x = new LiteOpRegistry;
    return *x;
  }

 private:
  LiteOpRegistry() = default;
};

template <typename OpClass>
class OpLiteRegistor : public Registor<OpClass> {
 public:
  explicit OpLiteRegistor(const std::string &op_type)
      : Registor<OpClass>([&] {
          LiteOpRegistry::Global().Register(
              op_type, [op_type]() -> std::unique_ptr<OpLite> {
                return std::unique_ptr<OpLite>(new OpClass(op_type));
              });
        }) {}
};
template <TargetType Target, PrecisionType Precision, DataLayoutType Layout>
using KernelRegistryForTarget =
    Factory<KernelLite<Target, Precision, Layout>, std::unique_ptr<KernelBase>>;

class KernelRegistry final {
 public:
  using any_kernel_registor_t =
      variant<KernelRegistryForTarget<TARGET(kCUDA),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNCHW)> *,  //
111 112 113
              KernelRegistryForTarget<TARGET(kCUDA),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNHWC)> *,  //
Y
Yan Chunwei 已提交
114 115 116
              KernelRegistryForTarget<TARGET(kCUDA),
                                      PRECISION(kInt8),
                                      DATALAYOUT(kNCHW)> *,  //
117 118 119
              KernelRegistryForTarget<TARGET(kCUDA),
                                      PRECISION(kInt8),
                                      DATALAYOUT(kNHWC)> *,  //
Y
Yan Chunwei 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
              KernelRegistryForTarget<TARGET(kX86),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kX86),
                                      PRECISION(kInt8),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kHost),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kHost),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNHWC)> *,  //
              KernelRegistryForTarget<TARGET(kHost),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kAny)> *,  //
              KernelRegistryForTarget<TARGET(kHost),
                                      PRECISION(kAny),
                                      DATALAYOUT(kAny)> *,  //
138 139 140 141 142 143
              KernelRegistryForTarget<TARGET(kHost),
                                      PRECISION(kInt32),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kHost),
                                      PRECISION(kInt64),
                                      DATALAYOUT(kNCHW)> *,  //
Y
Yan Chunwei 已提交
144 145 146 147 148 149 150 151 152 153 154 155
              KernelRegistryForTarget<TARGET(kCUDA),
                                      PRECISION(kAny),
                                      DATALAYOUT(kAny)> *,  //
              KernelRegistryForTarget<TARGET(kARM),
                                      PRECISION(kAny),
                                      DATALAYOUT(kAny)> *,  //
              KernelRegistryForTarget<TARGET(kARM),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kARM),
                                      PRECISION(kInt8),
                                      DATALAYOUT(kNCHW)> *,  //
X
xiaogang 已提交
156 157 158
              KernelRegistryForTarget<TARGET(kARM),
                                      PRECISION(kInt64),
                                      DATALAYOUT(kNCHW)> *,  //
J
juncaipeng 已提交
159 160 161
              KernelRegistryForTarget<TARGET(kARM),
                                      PRECISION(kInt32),
                                      DATALAYOUT(kNCHW)> *,  //
162 163 164 165 166 167
              KernelRegistryForTarget<TARGET(kARM),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNHWC)> *,  //
              KernelRegistryForTarget<TARGET(kARM),
                                      PRECISION(kInt8),
                                      DATALAYOUT(kNHWC)> *,  //
168

Y
Yan Chunwei 已提交
169 170 171
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNCHW)> *,  //
172 173 174 175 176 177 178 179 180 181 182 183
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNHWC)> *,  //
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kAny),
                                      DATALAYOUT(kNHWC)> *,  //
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kAny),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kAny)> *,  //
Y
Yan Chunwei 已提交
184 185 186
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kInt8),
                                      DATALAYOUT(kNCHW)> *,  //
187 188 189
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kAny),
                                      DATALAYOUT(kAny)> *,  //
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kNHWC)> *,  //
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kImageDefault)> *,  //
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kImageFolder)> *,  //
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kImageNW)> *,  //
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kImageDefault)> *,  //
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kImageFolder)> *,  //
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kImageNW)> *,  //
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kAny),
                                      DATALAYOUT(kImageDefault)> *,  //
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kAny),
                                      DATALAYOUT(kImageFolder)> *,  //
              KernelRegistryForTarget<TARGET(kOpenCL),
                                      PRECISION(kAny),
                                      DATALAYOUT(kImageNW)> *,  //
223

Y
Yan Chunwei 已提交
224 225 226 227 228 229 230 231 232
              KernelRegistryForTarget<TARGET(kNPU),
                                      PRECISION(kAny),
                                      DATALAYOUT(kAny)> *,  //
              KernelRegistryForTarget<TARGET(kNPU),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kNPU),
                                      PRECISION(kInt8),
                                      DATALAYOUT(kNCHW)> *,  //
233

H
hong19860320 已提交
234 235 236
              KernelRegistryForTarget<TARGET(kAPU),
                                      PRECISION(kInt8),
                                      DATALAYOUT(kNCHW)> *,  //
237 238 239 240 241 242 243 244 245 246
              KernelRegistryForTarget<TARGET(kXPU),
                                      PRECISION(kAny),
                                      DATALAYOUT(kAny)> *,  //
              KernelRegistryForTarget<TARGET(kXPU),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kXPU),
                                      PRECISION(kInt8),
                                      DATALAYOUT(kNCHW)> *,  //

247 248 249 250 251 252 253 254 255 256
              KernelRegistryForTarget<TARGET(kBM),
                                      PRECISION(kAny),
                                      DATALAYOUT(kAny)> *,  //
              KernelRegistryForTarget<TARGET(kBM),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kBM),
                                      PRECISION(kInt8),
                                      DATALAYOUT(kNCHW)> *,  //

257 258 259 260 261 262 263 264 265 266
              KernelRegistryForTarget<TARGET(kRKNPU),
                                      PRECISION(kAny),
                                      DATALAYOUT(kAny)> *,  //
              KernelRegistryForTarget<TARGET(kRKNPU),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kRKNPU),
                                      PRECISION(kInt8),
                                      DATALAYOUT(kNCHW)> *,  //

Y
Yan Chunwei 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
              KernelRegistryForTarget<TARGET(kFPGA),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kFPGA),
                                      PRECISION(kAny),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kFPGA),
                                      PRECISION(kAny),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kFPGA),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNHWC)> *,  //
              KernelRegistryForTarget<TARGET(kFPGA),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kFPGA),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kNHWC)> *,  //
              KernelRegistryForTarget<TARGET(kFPGA),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kAny)> *,  //
              KernelRegistryForTarget<TARGET(kFPGA),
                                      PRECISION(kAny),
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
                                      DATALAYOUT(kAny)> *,  //

              KernelRegistryForTarget<TARGET(kMLU),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNHWC)> *,  //
              KernelRegistryForTarget<TARGET(kMLU),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kMLU),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kNHWC)> *,  //
              KernelRegistryForTarget<TARGET(kMLU),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kMLU),
                                      PRECISION(kInt8),
                                      DATALAYOUT(kNHWC)> *,  //
              KernelRegistryForTarget<TARGET(kMLU),
                                      PRECISION(kInt8),
                                      DATALAYOUT(kNCHW)> *,  //
              KernelRegistryForTarget<TARGET(kMLU),
                                      PRECISION(kInt16),
                                      DATALAYOUT(kNHWC)> *,  //
              KernelRegistryForTarget<TARGET(kMLU),
                                      PRECISION(kInt16),
                                      DATALAYOUT(kNCHW)> *  //
Y
Yan Chunwei 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
              >;

  KernelRegistry();

  static KernelRegistry &Global();

  template <TargetType Target, PrecisionType Precision, DataLayoutType Layout>
  void Register(
      const std::string &name,
      typename KernelRegistryForTarget<Target, Precision, Layout>::creator_t
          &&creator) {
    using kernel_registor_t =
        KernelRegistryForTarget<Target, Precision, Layout>;
    auto &varient = registries_[GetKernelOffset<Target, Precision, Layout>()];
    auto *reg = varient.template get<kernel_registor_t *>();
    CHECK(reg) << "Can not be empty of " << name;
    reg->Register(name, std::move(creator));
333 334 335 336
#ifdef LITE_ON_MODEL_OPTIMIZE_TOOL
    kernel_info_map_[name].push_back(
        std::make_tuple(Target, Precision, Layout));
#endif  // LITE_ON_MODEL_OPTIMIZE_TOOL
Y
Yan Chunwei 已提交
337 338 339 340 341 342 343 344
  }

  template <TargetType Target,
            PrecisionType Precision = PRECISION(kFloat),
            DataLayoutType Layout = DATALAYOUT(kNCHW)>
  std::list<std::unique_ptr<KernelBase>> Create(const std::string &op_type) {
    using kernel_registor_t =
        KernelRegistryForTarget<Target, Precision, Layout>;
345 346 347 348 349 350 351
    std::list<std::unique_ptr<KernelBase>> kernel_list;
    if (registries_[GetKernelOffset<Target, Precision, Layout>()].valid()) {
      kernel_list = registries_[GetKernelOffset<Target, Precision, Layout>()]
                        .template get<kernel_registor_t *>()
                        ->Creates(op_type);
    }
    return kernel_list;
Y
Yan Chunwei 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
  }

  std::list<std::unique_ptr<KernelBase>> Create(const std::string &op_type,
                                                TargetType target,
                                                PrecisionType precision,
                                                DataLayoutType layout);

  // Get a kernel registry offset in all the registries.
  template <TargetType Target, PrecisionType Precision, DataLayoutType Layout>
  static int GetKernelOffset() {
    CHECK_LT(static_cast<int>(Target), static_cast<int>(TARGET(NUM)));
    CHECK_LT(static_cast<int>(Precision), static_cast<int>(PRECISION(NUM)));
    CHECK_LT(static_cast<int>(Layout), static_cast<int>(DATALAYOUT(NUM)));
    return static_cast<int>(Target) * static_cast<int>(PRECISION(NUM)) *
               static_cast<int>(DATALAYOUT(NUM)) +                            //
           static_cast<int>(Precision) * static_cast<int>(DATALAYOUT(NUM)) +  //
           static_cast<int>(Layout);
  }

  std::string DebugString() const {
372 373 374
#ifndef LITE_ON_MODEL_OPTIMIZE_TOOL
    return "No more debug info";
#else   // LITE_ON_MODEL_OPTIMIZE_TOOL
Y
Yan Chunwei 已提交
375
    STL::stringstream ss;
376 377 378 379
    ss << "\n";
    ss << "Count of kernel kinds: ";
    int count = 0;
    for (auto &item : kernel_info_map_) {
380
      count += item.second.size();
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
    }
    ss << count << "\n";

    ss << "Count of registered kernels: " << kernel_info_map_.size() << "\n";
    for (auto &item : kernel_info_map_) {
      ss << "op: " << item.first << "\n";
      for (auto &kernel : item.second) {
        ss << "   - (" << TargetToStr(std::get<0>(kernel)) << ",";
        ss << PrecisionToStr(std::get<1>(kernel)) << ",";
        ss << DataLayoutToStr(std::get<2>(kernel));
        ss << ")";
        ss << "\n";
      }
    }

Y
Yan Chunwei 已提交
396
    return ss.str();
397
#endif  // LITE_ON_MODEL_OPTIMIZE_TOOL
Y
Yan Chunwei 已提交
398 399 400 401
  }

 private:
  mutable std::vector<any_kernel_registor_t> registries_;
402 403 404 405 406 407
#ifndef LITE_ON_TINY_PUBLISH
  mutable std::map<
      std::string,
      std::vector<std::tuple<TargetType, PrecisionType, DataLayoutType>>>
      kernel_info_map_;
#endif
Y
Yan Chunwei 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
};

template <TargetType target,
          PrecisionType precision,
          DataLayoutType layout,
          typename KernelType>
class KernelRegistor : public lite::Registor<KernelType> {
 public:
  KernelRegistor(const std::string &op_type, const std::string &alias)
      : Registor<KernelType>([=] {
          KernelRegistry::Global().Register<target, precision, layout>(
              op_type, [=]() -> std::unique_ptr<KernelType> {
                std::unique_ptr<KernelType> x(new KernelType);
                x->set_op_type(op_type);
                x->set_alias(alias);
                return x;
              });
        }) {}
};

}  // namespace lite
}  // namespace paddle

// Operator registry
#define LITE_OP_REGISTER_INSTANCE(op_type__) op_type__##__registry__instance__
#define REGISTER_LITE_OP(op_type__, OpClass)                              \
  static paddle::lite::OpLiteRegistor<OpClass> LITE_OP_REGISTER_INSTANCE( \
      op_type__)(#op_type__);                                             \
  int touch_op_##op_type__() {                                            \
437
    OpKernelInfoCollector::Global().AddOp2path(#op_type__, __FILE__);     \
Y
Yan Chunwei 已提交
438 439 440 441 442 443 444 445
    return LITE_OP_REGISTER_INSTANCE(op_type__).Touch();                  \
  }

// Kernel registry
#define LITE_KERNEL_REGISTER(op_type__, target__, precision__) \
  op_type__##__##target__##__##precision__##__registor__
#define LITE_KERNEL_REGISTER_INSTANCE(                   \
    op_type__, target__, precision__, layout__, alias__) \
446 447
  op_type__##__##target__##__##precision__##__##layout__##registor__instance__##alias__  // NOLINT

Y
Yan Chunwei 已提交
448 449 450
#define LITE_KERNEL_REGISTER_FAKE(op_type__, target__, precision__, alias__) \
  LITE_KERNEL_REGISTER_INSTANCE(op_type__, target__, precision__, alias__)

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
#define REGISTER_LITE_KERNEL(                                                 \
    op_type__, target__, precision__, layout__, KernelClass, alias__)         \
  static paddle::lite::KernelRegistor<TARGET(target__),                       \
                                      PRECISION(precision__),                 \
                                      DATALAYOUT(layout__),                   \
                                      KernelClass>                            \
      LITE_KERNEL_REGISTER_INSTANCE(                                          \
          op_type__, target__, precision__, layout__, alias__)(#op_type__,    \
                                                               #alias__);     \
  static KernelClass LITE_KERNEL_INSTANCE(                                    \
      op_type__, target__, precision__, layout__, alias__);                   \
  int touch_##op_type__##target__##precision__##layout__##alias__() {         \
    OpKernelInfoCollector::Global().AddKernel2path(                           \
        #op_type__ "," #target__ "," #precision__ "," #layout__ "," #alias__, \
        __FILE__);                                                            \
    LITE_KERNEL_INSTANCE(op_type__, target__, precision__, layout__, alias__) \
        .Touch();                                                             \
    return 0;                                                                 \
  }                                                                           \
  static bool LITE_KERNEL_PARAM_INSTANCE(                                     \
      op_type__, target__, precision__, layout__, alias__) UNUSED =           \
      paddle::lite::ParamTypeRegistry::NewInstance<TARGET(target__),          \
                                                   PRECISION(precision__),    \
                                                   DATALAYOUT(layout__)>(     \
          #op_type__ "/" #alias__)
Y
Yan Chunwei 已提交
476 477 478 479 480 481 482

#define LITE_KERNEL_INSTANCE(                            \
    op_type__, target__, precision__, layout__, alias__) \
  op_type__##target__##precision__##layout__##alias__
#define LITE_KERNEL_PARAM_INSTANCE(                      \
    op_type__, target__, precision__, layout__, alias__) \
  op_type__##target__##precision__##layout__##alias__##param_register