tvm
config.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
23 #ifndef TVM_SCRIPT_PRINTER_CONFIG_H_
24 #define TVM_SCRIPT_PRINTER_CONFIG_H_
25 
26 #include <tvm/ffi/any.h>
27 #include <tvm/ffi/container/array.h>
28 #include <tvm/ffi/container/map.h>
29 #include <tvm/ffi/reflection/access_path.h>
30 #include <tvm/ffi/reflection/registry.h>
31 #include <tvm/ffi/string.h>
32 #include <tvm/ir/cast.h>
33 #include <tvm/ir/node_functor.h>
34 #include <tvm/runtime/data_type.h>
35 
36 #include <string>
37 
38 namespace tvm {
39 
40 class PrinterConfigNode : public ffi::Object {
41  public:
43  ffi::Array<ffi::String> binding_names = {};
45  bool show_meta = false;
47  ffi::String ir_prefix = "I";
49  ffi::String tir_prefix = "T";
55  ffi::String tir_import_module = "tir";
57  ffi::String tirx_prefix = "Tx";
61  ffi::String relax_prefix = "R";
66  ffi::String module_alias = "cls";
76  bool verbose_expr = false;
78  int indent_spaces = 4;
80  bool print_line_numbers = false;
84  bool syntax_sugar = true;
86  bool show_object_address = false;
87 
88  /* \brief ffi::Object path to be underlined */
89  ffi::Array<ffi::reflection::AccessPath> path_to_underline;
91  ffi::Map<ffi::reflection::AccessPath, ffi::String> path_to_annotate;
93  ffi::Array<ffi::ObjectRef> obj_to_underline = ffi::Array<ffi::ObjectRef>();
95  ffi::Map<ffi::ObjectRef, ffi::String> obj_to_annotate = ffi::Map<ffi::ObjectRef, ffi::String>();
96 
108  ffi::Map<ffi::String, ffi::Any> extra_config;
109 
116  template <typename T>
117  T GetExtraConfig(const ffi::String& key, T fallback) const {
118  auto it = extra_config.find(key);
119  if (it == extra_config.end()) return fallback;
120  return Downcast<T>((*it).second);
121  }
122 
123  static void RegisterReflection() {
124  namespace refl = tvm::ffi::reflection;
125  refl::ObjectDef<PrinterConfigNode>()
126  .def_ro("binding_names", &PrinterConfigNode::binding_names)
127  .def_ro("show_meta", &PrinterConfigNode::show_meta)
128  .def_ro("ir_prefix", &PrinterConfigNode::ir_prefix)
129  .def_ro("module_alias", &PrinterConfigNode::module_alias)
130  .def_ro("int_dtype", &PrinterConfigNode::int_dtype)
131  .def_ro("float_dtype", &PrinterConfigNode::float_dtype)
132  .def_ro("verbose_expr", &PrinterConfigNode::verbose_expr)
133  .def_ro("indent_spaces", &PrinterConfigNode::indent_spaces)
134  .def_ro("print_line_numbers", &PrinterConfigNode::print_line_numbers)
135  .def_ro("num_context_lines", &PrinterConfigNode::num_context_lines)
136  .def_ro("syntax_sugar", &PrinterConfigNode::syntax_sugar)
137  .def_ro("show_object_address", &PrinterConfigNode::show_object_address)
138  .def_ro("path_to_underline", &PrinterConfigNode::path_to_underline)
139  .def_ro("path_to_annotate", &PrinterConfigNode::path_to_annotate)
140  .def_ro("obj_to_underline", &PrinterConfigNode::obj_to_underline)
141  .def_ro("obj_to_annotate", &PrinterConfigNode::obj_to_annotate)
142  .def_ro("extra_config", &PrinterConfigNode::extra_config);
143  }
144 
145  ffi::Array<ffi::String> GetBuiltinKeywords();
146 
147  static constexpr const bool _type_mutable = true;
148  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.PrinterConfig", PrinterConfigNode, ffi::Object);
149 };
150 
151 class TVM_DLL PrinterConfig : public ffi::ObjectRef {
152  public:
153  explicit PrinterConfig(
154  ffi::Map<ffi::String, ffi::Any> config_dict = ffi::Map<ffi::String, ffi::Any>());
155 
157 };
158 
161  public:
162  /* Convert the object to TVMScript format */
163  TVM_DLL static std::string Script(const ffi::ObjectRef& node,
164  const ffi::Optional<PrinterConfig>& cfg);
165  // Allow registration to be printer.
166  using FType = NodeFunctor<std::string(const ffi::ObjectRef&, const PrinterConfig&)>;
167  TVM_DLL static FType& vtable();
168 };
169 
170 #define TVM_OBJECT_ENABLE_SCRIPT_PRINTER() \
171  std::string Script(const ffi::Optional<PrinterConfig>& config = std::nullopt) const { \
172  return TVMScriptPrinter::Script(ffi::GetRef<ffi::ObjectRef>(this), \
173  config.value_or(PrinterConfig())); \
174  }
175 
183 TVM_DLL std::string RedirectedReprPrinterMethod(const ffi::ObjectRef& obj);
184 
192 #define TVM_REGISTER_SCRIPT_AS_REPR(ObjectType, Method) \
193  TVM_FFI_STATIC_INIT_BLOCK() { \
194  namespace refl = tvm::ffi::reflection; \
195  refl::TypeAttrDef<ObjectType>().def(refl::type_attr::kRepr, \
196  [](ffi::ObjectRef obj, ffi::Function) -> ffi::String { \
197  return RedirectedReprPrinterMethod(obj); \
198  }); \
199  } \
200  TVM_STATIC_IR_FUNCTOR(TVMScriptPrinter, vtable).set_dispatch<ObjectType>(Method)
201 
202 } // namespace tvm
203 #endif // TVM_SCRIPT_PRINTER_CONFIG_H_
Value casting helpers.
A dynamically dispatched functor on the type of the first argument.
Definition: node_functor.h:62
Definition: config.h:40
ffi::String ir_prefix
The prefix of IR nodes.
Definition: config.h:47
static constexpr const bool _type_mutable
Definition: config.h:147
ffi::Array< ffi::String > binding_names
A stack that tracks the names of the binding hierarchy.
Definition: config.h:43
ffi::Array< ffi::ObjectRef > obj_to_underline
ffi::Object to be underlined.
Definition: config.h:93
ffi::String tir_import_module
The TIR module name used in the printed import (e.g. "tir" or "tirx"). Used in the header comment: "f...
Definition: config.h:55
bool print_line_numbers
Whether to print line numbers.
Definition: config.h:80
ffi::String tirx_prefix
The prefix of TIRX nodes.
Definition: config.h:57
DataType int_dtype
Default data type of integer literals.
Definition: config.h:68
bool verbose_expr
Whether or not to verbose print expressions.
Definition: config.h:76
bool syntax_sugar
Whether to output with syntax sugar, set false for complete printing.
Definition: config.h:84
ffi::String tir_prefix
The prefix of TIR nodes.
Definition: config.h:49
ffi::Map< ffi::reflection::AccessPath, ffi::String > path_to_annotate
ffi::Object path to be annotated.
Definition: config.h:91
ffi::String relax_prefix
The prefix of Relax nodes.
Definition: config.h:61
ffi::Map< ffi::ObjectRef, ffi::String > obj_to_annotate
ffi::Object to be annotated.
Definition: config.h:95
bool show_object_address
Whether variable names should include the object's address.
Definition: config.h:86
int indent_spaces
Number of spaces used for indentation.
Definition: config.h:78
int num_context_lines
Number of context lines to print around the underlined text.
Definition: config.h:82
ffi::Map< ffi::String, ffi::Any > extra_config
Generic extension map for dialect-specific config knobs.
Definition: config.h:108
bool show_meta
Whether or not to show metadata.
Definition: config.h:45
ffi::Array< ffi::reflection::AccessPath > path_to_underline
Definition: config.h:89
ffi::Array< ffi::String > GetBuiltinKeywords()
T GetExtraConfig(const ffi::String &key, T fallback) const
Look up a value in extra_config with type cast and fallback.
Definition: config.h:117
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.PrinterConfig", PrinterConfigNode, ffi::Object)
DataType float_dtype
Default data type of float literals. Right now we always print out the explicit type of floating poin...
Definition: config.h:74
ffi::String module_alias
The alias of the current module at cross-function call.
Definition: config.h:66
static void RegisterReflection()
Definition: config.h:123
DataType buffer_dtype
Default buffer dtype.
Definition: config.h:59
Definition: config.h:151
PrinterConfig(ffi::Map< ffi::String, ffi::Any > config_dict=ffi::Map< ffi::String, ffi::Any >())
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(PrinterConfig, ffi::ObjectRef, PrinterConfigNode)
TVMScript-based printer for IR nodes.
Definition: config.h:160
static std::string Script(const ffi::ObjectRef &node, const ffi::Optional< PrinterConfig > &cfg)
static FType & vtable()
Runtime primitive data type.
Definition: data_type.h:45
static DataType Float(int bits, int lanes=1)
Construct an float type.
Definition: data_type.h:293
static DataType Int(int bits, int lanes=1)
Construct an int type.
Definition: data_type.h:276
static DataType Void()
Construct a Void type.
Definition: data_type.h:399
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
std::string RedirectedReprPrinterMethod(const ffi::ObjectRef &obj)
The fallback body used by TVM_REGISTER_SCRIPT_AS_REPR.
Defines the Functor data structures.