treelite
compiler.h
Go to the documentation of this file.
1 
7 #ifndef TREELITE_COMPILER_H_
8 #define TREELITE_COMPILER_H_
9 
10 #include <unordered_map>
11 #include <functional>
12 #include <memory>
13 #include <string>
14 #include <vector>
15 #include <utility>
16 #include <dmlc/registry.h>
17 #include <treelite/common.h>
18 
19 namespace treelite {
20 
21 struct Model; // forward declaration
22 
23 namespace compiler {
24 
25 struct CompilerParam; // forward declaration
26 
27 struct CompiledModel {
28  struct FileEntry {
29  std::string content;
30  std::vector<char> content_binary;
31  bool is_binary;
32  FileEntry() : is_binary(false) {}
33  // Passing std::vector<char> indicates binary data
34  // Passing std::string indicates text data
35  // Use move constructor and assignment exclusively to save memory
36  explicit FileEntry(const std::string& content) = delete;
37  explicit FileEntry(std::string&& content)
38  : content(std::move(content)), is_binary(false) {}
39  explicit FileEntry(const std::vector<char>&) = delete;
40  explicit FileEntry(std::vector<char>&& content)
41  : content_binary(std::move(content)), is_binary(true) {}
42  FileEntry(const FileEntry& other) = delete;
43  FileEntry(FileEntry&& other) = default;
44  FileEntry& operator=(const FileEntry& other) = delete;
45  FileEntry& operator=(FileEntry&& other) = default;
46  };
47  std::string backend;
48  std::unordered_map<std::string, FileEntry> files;
49  std::string file_prefix;
50 };
51 
52 } // namespace compiler
53 
55 class Compiler {
56  public:
58  virtual ~Compiler() = default;
63  virtual compiler::CompiledModel Compile(const Model& model) = 0;
69  static Compiler* Create(const std::string& name,
70  const compiler::CompilerParam& param);
71 };
72 
77  : public dmlc::FunctionRegEntryBase<CompilerReg,
78  std::function<Compiler* (const compiler::CompilerParam&)> > {
79 };
80 
93 #define TREELITE_REGISTER_COMPILER(UniqueId, Name) \
94  static DMLC_ATTRIBUTE_UNUSED ::treelite::CompilerReg & \
95  __make_ ## CompilerReg ## _ ## UniqueId ## __ = \
96  ::dmlc::Registry< ::treelite::CompilerReg>::Get()->__REGISTER__(Name)
97 
98 } // namespace treelite
99 
100 #endif // TREELITE_COMPILER_H_
thin wrapper for tree ensemble model
Definition: tree.h:428
parameters for tree compiler
Definition: param.h:18
interface of compiler
Definition: compiler.h:55
Definition: compiler.h:28
Registry entry for compiler.
Definition: compiler.h:76