tvm
script_printer.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_NODE_SCRIPT_PRINTER_H_
24 #define TVM_NODE_SCRIPT_PRINTER_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/node/functor.h>
33 #include <tvm/runtime/data_type.h>
34 
35 #include <iostream>
36 #include <string>
37 
38 namespace tvm {
39 
40 class PrinterConfigNode : public ffi::Object {
41  public:
43  Array<String> binding_names = {};
45  bool show_meta = false;
47  std::string ir_prefix = "I";
49  std::string tir_prefix = "T";
51  std::string relax_prefix = "R";
56  std::string module_alias = "cls";
68  bool verbose_expr = false;
70  int indent_spaces = 4;
72  bool print_line_numbers = false;
76  bool syntax_sugar = true;
78  bool show_object_address = false;
79 
113  bool show_all_struct_info = true;
114 
115  /* \brief Object path to be underlined */
116  Array<ffi::reflection::AccessPath> path_to_underline;
118  Map<ffi::reflection::AccessPath, String> path_to_annotate;
120  Array<ObjectRef> obj_to_underline = Array<ObjectRef>();
122  Map<ObjectRef, String> obj_to_annotate = Map<ObjectRef, String>();
123 
124  static void RegisterReflection() {
125  namespace refl = tvm::ffi::reflection;
126  refl::ObjectDef<PrinterConfigNode>()
127  .def_ro("binding_names", &PrinterConfigNode::binding_names)
128  .def_ro("show_meta", &PrinterConfigNode::show_meta)
129  .def_ro("ir_prefix", &PrinterConfigNode::ir_prefix)
130  .def_ro("tir_prefix", &PrinterConfigNode::tir_prefix)
131  .def_ro("relax_prefix", &PrinterConfigNode::relax_prefix)
132  .def_ro("module_alias", &PrinterConfigNode::module_alias)
133  .def_ro("buffer_dtype", &PrinterConfigNode::buffer_dtype)
134  .def_ro("int_dtype", &PrinterConfigNode::int_dtype)
135  .def_ro("float_dtype", &PrinterConfigNode::float_dtype)
136  .def_ro("verbose_expr", &PrinterConfigNode::verbose_expr)
137  .def_ro("indent_spaces", &PrinterConfigNode::indent_spaces)
138  .def_ro("print_line_numbers", &PrinterConfigNode::print_line_numbers)
139  .def_ro("num_context_lines", &PrinterConfigNode::num_context_lines)
140  .def_ro("syntax_sugar", &PrinterConfigNode::syntax_sugar)
141  .def_ro("show_object_address", &PrinterConfigNode::show_object_address)
142  .def_ro("show_all_struct_info", &PrinterConfigNode::show_all_struct_info)
143  .def_ro("path_to_underline", &PrinterConfigNode::path_to_underline)
144  .def_ro("path_to_annotate", &PrinterConfigNode::path_to_annotate)
145  .def_ro("obj_to_underline", &PrinterConfigNode::obj_to_underline)
146  .def_ro("obj_to_annotate", &PrinterConfigNode::obj_to_annotate);
147  }
148 
149  Array<String> GetBuiltinKeywords();
150 
151  static constexpr const char* _type_key = "script.PrinterConfig";
153 };
154 
155 class PrinterConfig : public ObjectRef {
156  public:
157  explicit PrinterConfig(Map<String, ffi::Any> config_dict = Map<String, ffi::Any>());
158 
161 };
162 
165  public:
166  /* Convert the object to TVMScript format */
167  static std::string Script(const ObjectRef& node, const Optional<PrinterConfig>& cfg);
168  // Allow registration to be printer.
169  using FType = NodeFunctor<std::string(const ObjectRef&, const PrinterConfig&)>;
170  TVM_DLL static FType& vtable();
171 };
172 
173 #define TVM_OBJECT_ENABLE_SCRIPT_PRINTER() \
174  std::string Script(const Optional<PrinterConfig>& config = std::nullopt) const { \
175  return TVMScriptPrinter::Script(GetRef<ObjectRef>(this), config.value_or(PrinterConfig())); \
176  }
177 
178 } // namespace tvm
179 #endif // TVM_NODE_SCRIPT_PRINTER_H_
A dynamically dispatched functor on the type of the first argument.
Definition: functor.h:65
Definition: script_printer.h:40
Array< ffi::reflection::AccessPath > path_to_underline
Definition: script_printer.h:116
static constexpr const char * _type_key
Definition: script_printer.h:151
std::string tir_prefix
The prefix of TIR nodes.
Definition: script_printer.h:49
bool print_line_numbers
Whether to print line numbers.
Definition: script_printer.h:72
Array< String > GetBuiltinKeywords()
DataType int_dtype
Default data type of integer literals.
Definition: script_printer.h:60
bool verbose_expr
Whether or not to verbose print expressions.
Definition: script_printer.h:68
bool syntax_sugar
Whether to output with syntax sugar, set false for complete printing.
Definition: script_printer.h:76
Array< ObjectRef > obj_to_underline
Object to be underlined.
Definition: script_printer.h:120
bool show_all_struct_info
In Relax, whether to show all StructInfo annotations.
Definition: script_printer.h:113
bool show_object_address
Whether variable names should include the object's address.
Definition: script_printer.h:78
Map< ffi::reflection::AccessPath, String > path_to_annotate
Object path to be annotated.
Definition: script_printer.h:118
int indent_spaces
Number of spaces used for indentation.
Definition: script_printer.h:70
int num_context_lines
Number of context lines to print around the underlined text.
Definition: script_printer.h:74
bool show_meta
Whether or not to show metadata.
Definition: script_printer.h:45
std::string relax_prefix
The prefix of Relax nodes.
Definition: script_printer.h:51
Map< ObjectRef, String > obj_to_annotate
Object to be annotated.
Definition: script_printer.h:122
Array< String > binding_names
A stack that tracks the names of the binding hierarchy.
Definition: script_printer.h:43
DataType float_dtype
Default data type of float literals. Right now we always print out the explicit type of floating poin...
Definition: script_printer.h:66
std::string module_alias
The alias of the current module at cross-function call.
Definition: script_printer.h:56
static void RegisterReflection()
Definition: script_printer.h:124
DataType buffer_dtype
Default data type of TIR buffer.
Definition: script_printer.h:58
TVM_DECLARE_FINAL_OBJECT_INFO(PrinterConfigNode, Object)
std::string ir_prefix
The prefix of IR nodes.
Definition: script_printer.h:47
Definition: script_printer.h:155
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(PrinterConfig, runtime::ObjectRef, PrinterConfigNode)
PrinterConfig(Map< String, ffi::Any > config_dict=Map< String, ffi::Any >())
Legacy behavior of ReprPrinter.
Definition: script_printer.h:164
static std::string Script(const ObjectRef &node, const Optional< PrinterConfig > &cfg)
static FType & vtable()
Runtime primitive data type.
Definition: data_type.h:47
static DataType Float(int bits, int lanes=1)
Construct an float type.
Definition: data_type.h:291
static DataType Int(int bits, int lanes=1)
Construct an int type.
Definition: data_type.h:274
static DataType Void()
Construct a Void type.
Definition: data_type.h:397
Defines the Functor data structures.
Definition: repr_printer.h:91
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37