gen_base.h 2.6 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2018 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 <gflags/gflags.h>
T
tensor-tang 已提交
18
#include <memory>  // for unique_ptr
T
tensor-tang 已提交
19
#include <string>
20
#include <vector>
T
tensor-tang 已提交
21
#include "paddle/fluid/operators/jit/kernel_base.h"
T
tensor-tang 已提交
22 23 24 25 26

DECLARE_bool(dump_jitcode);

namespace paddle {
namespace operators {
T
tensor-tang 已提交
27
namespace jit {
T
tensor-tang 已提交
28

T
tensor-tang 已提交
29
class GenBase : public Kernel {
T
tensor-tang 已提交
30
 public:
T
tensor-tang 已提交
31
  virtual ~GenBase() = default;
T
tensor-tang 已提交
32
  virtual std::string name() const = 0;
T
tensor-tang 已提交
33
  virtual size_t getSize() const = 0;
34 35
  virtual const unsigned char* getCodeInternal() const = 0;
  const char* ImplType() const override { return "JitCode"; }
T
tensor-tang 已提交
36
  template <typename Func>
37
  Func getCode() const {
T
tensor-tang 已提交
38 39 40 41
    const unsigned char* code = this->getCodeInternal();
    if (FLAGS_dump_jitcode) {
      this->dumpCode(code);
    }
42 43
    // Note: failed to cast with reinterpret_cast<const Func> on Mac clang,
    // then workaround with const_cast. Any better idea is appreciated.
T
tensor-tang 已提交
44
    return reinterpret_cast<Func>(const_cast<unsigned char*>(code));
T
tensor-tang 已提交
45 46
  }

47 48 49 50 51
  void* operator new(size_t size);
  void operator delete(void* ptr);
  void* operator new[](size_t size) { return operator new(size); }
  void operator delete[](void* ptr) { operator delete(ptr); }

T
tensor-tang 已提交
52
 protected:
T
tensor-tang 已提交
53
  void dumpCode(const unsigned char* code) const;
T
tensor-tang 已提交
54 55
};

T
tensor-tang 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68
// Creator is used to creat the jitcode and save in pool.
// Every JitCode should have one creator.
class GenCreator {
 public:
  virtual ~GenCreator() = default;
};

template <typename Attr>
class JitCodeCreator : public GenCreator {
 public:
  virtual ~JitCodeCreator() = default;

  // condition when this jit code can be used.
69
  virtual bool CanBeUsed(const Attr& attr) const = 0;
T
tensor-tang 已提交
70 71 72 73 74 75 76

  // estimate this code size
  virtual size_t CodeSize(const Attr& attr) const = 0;

  // create this code
  virtual std::unique_ptr<GenBase> CreateJitCode(const Attr& attr) const = 0;
};
T
tensor-tang 已提交
77

78 79 80 81 82
// unify the method of packed groups
// output the packed groups which used in weights, the block size and rest size
std::vector<int> packed_groups(int n, int k, int* block = nullptr,
                               int* rest = nullptr);

T
tensor-tang 已提交
83
}  // namespace jit
T
tensor-tang 已提交
84 85
}  // namespace operators
}  // namespace paddle