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/node/functor.h>
27 #include <tvm/node/object_path.h>
28 #include <tvm/node/reflection.h>
29 #include <tvm/runtime/data_type.h>
30 
31 #include <iostream>
32 #include <string>
33 
34 namespace tvm {
35 
36 class PrinterConfigNode : public Object {
37  public:
41  bool show_meta = false;
43  std::string ir_prefix = "I";
45  std::string tir_prefix = "T";
47  std::string relax_prefix = "R";
52  std::string module_alias = "cls";
64  bool verbose_expr = false;
66  int indent_spaces = 4;
68  bool print_line_numbers = false;
72  bool syntax_sugar = true;
74  bool show_object_address = false;
75 
109  bool show_all_struct_info = true;
110 
111  /* \brief Object path to be underlined */
119 
121  v->Visit("binding_names", &binding_names);
122  v->Visit("show_meta", &show_meta);
123  v->Visit("ir_prefix", &ir_prefix);
124  v->Visit("tir_prefix", &tir_prefix);
125  v->Visit("relax_prefix", &relax_prefix);
126  v->Visit("module_alias", &module_alias);
127  v->Visit("buffer_dtype", &buffer_dtype);
128  v->Visit("int_dtype", &int_dtype);
129  v->Visit("float_dtype", &float_dtype);
130  v->Visit("verbose_expr", &verbose_expr);
131  v->Visit("indent_spaces", &indent_spaces);
132  v->Visit("print_line_numbers", &print_line_numbers);
133  v->Visit("num_context_lines", &num_context_lines);
134  v->Visit("syntax_sugar", &syntax_sugar);
135  v->Visit("show_object_address", &show_object_address);
136  v->Visit("show_all_struct_info", &show_all_struct_info);
137  v->Visit("path_to_underline", &path_to_underline);
138  v->Visit("path_to_annotate", &path_to_annotate);
139  v->Visit("obj_to_underline", &obj_to_underline);
140  v->Visit("obj_to_annotate", &obj_to_annotate);
141  }
142 
144 
145  static constexpr const char* _type_key = "node.PrinterConfig";
147 };
148 
149 class PrinterConfig : public ObjectRef {
150  public:
152 
155 };
156 
159  public:
160  /* Convert the object to TVMScript format */
161  static std::string Script(const ObjectRef& node, const Optional<PrinterConfig>& cfg);
162  // Allow registration to be printer.
163  using FType = NodeFunctor<std::string(const ObjectRef&, const PrinterConfig&)>;
164  TVM_DLL static FType& vtable();
165 };
166 
167 #define TVM_OBJECT_ENABLE_SCRIPT_PRINTER() \
168  std::string Script(const Optional<PrinterConfig>& config = NullOpt) const { \
169  return TVMScriptPrinter::Script(GetRef<ObjectRef>(this), config.value_or(PrinterConfig())); \
170  }
171 
172 } // namespace tvm
173 #endif // TVM_NODE_SCRIPT_PRINTER_H_
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
A dynamically dispatched functor on the type of the first argument.
Definition: functor.h:64
Definition: script_printer.h:36
static constexpr const char * _type_key
Definition: script_printer.h:145
std::string tir_prefix
The prefix of TIR nodes.
Definition: script_printer.h:45
bool print_line_numbers
Whether to print line numbers.
Definition: script_printer.h:68
Array< String > GetBuiltinKeywords()
DataType int_dtype
Default data type of integer literals.
Definition: script_printer.h:56
bool verbose_expr
Whether or not to verbose print expressions.
Definition: script_printer.h:64
bool syntax_sugar
Whether to output with syntax sugar, set false for complete printing.
Definition: script_printer.h:72
Array< ObjectRef > obj_to_underline
Object to be underlined.
Definition: script_printer.h:116
bool show_all_struct_info
In Relax, whether to show all StructInfo annotations.
Definition: script_printer.h:109
bool show_object_address
Whether variable names should include the object's address.
Definition: script_printer.h:74
int indent_spaces
Number of spaces used for indentation.
Definition: script_printer.h:66
int num_context_lines
Number of context lines to print around the underlined text.
Definition: script_printer.h:70
bool show_meta
Whether or not to show metadata.
Definition: script_printer.h:41
std::string relax_prefix
The prefix of Relax nodes.
Definition: script_printer.h:47
Map< ObjectRef, String > obj_to_annotate
Object to be annotated.
Definition: script_printer.h:118
Array< String > binding_names
A stack that tracks the names of the binding hierarchy.
Definition: script_printer.h:39
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:62
std::string module_alias
The alias of the current module at cross-function call.
Definition: script_printer.h:52
DataType buffer_dtype
Default data type of TIR buffer.
Definition: script_printer.h:54
TVM_DECLARE_FINAL_OBJECT_INFO(PrinterConfigNode, Object)
Map< ObjectPath, String > path_to_annotate
Object path to be annotated.
Definition: script_printer.h:114
void VisitAttrs(AttrVisitor *v)
Definition: script_printer.h:120
Array< ObjectPath > path_to_underline
Definition: script_printer.h:112
std::string ir_prefix
The prefix of IR nodes.
Definition: script_printer.h:43
Definition: script_printer.h:149
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(PrinterConfig, runtime::ObjectRef, PrinterConfigNode)
PrinterConfig(Map< String, ObjectRef > config_dict=Map< String, ObjectRef >())
Legacy behavior of ReprPrinter.
Definition: script_printer.h:158
static std::string Script(const ObjectRef &node, const Optional< PrinterConfig > &cfg)
static FType & vtable()
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Runtime primitive data type.
Definition: data_type.h:43
static DataType Float(int bits, int lanes=1)
Construct an float type.
Definition: data_type.h:236
static DataType Int(int bits, int lanes=1)
Construct an int type.
Definition: data_type.h:219
static DataType Void()
Construct a Void type.
Definition: data_type.h:276
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
Base class of all object reference.
Definition: object.h:519
base class of all object containers.
Definition: object.h:171
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Defines the Functor data structures.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Reflection and serialization of compiler IR/AST nodes.