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