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 */
19/*!
20 * \file tvm/node/repr_printer.h
21 * \brief Printer class to print repr string of each AST/IR nodes.
22 */
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
34namespace tvm {
35
36class PrinterConfigNode : public Object {
37 public:
38 /*! \brief A stack that tracks the names of the binding hierarchy */
39 Array<String> binding_names = {};
40 /*! \brief Whether or not to show metadata. */
41 bool show_meta = false;
42 /*! \brief The prefix of IR nodes */
43 std::string ir_prefix = "I";
44 /*! \brief The prefix of TIR nodes */
45 std::string tir_prefix = "T";
46 /*! \brief The prefix of Relax nodes */
47 std::string relax_prefix = "R";
48 /*! \brief Default data type of TIR buffer */
49 DataType buffer_dtype = DataType::Float(32);
50 /*! \brief Default data type of integer literals */
51 DataType int_dtype = DataType::Int(32);
52 /*!
53 * \brief Default data type of float literals. Right now we always print out the explicit type
54 * of floating point values, so setting it to Void means we do not print without the
55 * T.float32/T.float64 wrapper.
56 */
57 DataType float_dtype = DataType::Void();
58 /*! \brief Whether or not to verbose print expressions. */
59 bool verbose_expr = false;
60 /*! \brief Number of spaces used for indentation*/
61 int indent_spaces = 4;
62 /*! \brief Whether to print line numbers */
63 bool print_line_numbers = false;
64 /*! \brief Number of context lines to print around the underlined text */
65 int num_context_lines = -1;
66 /*! \brief Whether to output with syntax sugar, set false for complete printing. */
67 bool syntax_sugar = true;
68 /* \brief Object path to be underlined */
69 Array<ObjectPath> path_to_underline = Array<ObjectPath>();
70 /*! \brief Object path to be annotated. */
71 Map<ObjectPath, String> path_to_annotate = Map<ObjectPath, String>();
72 /*! \brief Object to be underlined. */
73 Array<ObjectRef> obj_to_underline = Array<ObjectRef>();
74 /*! \brief Object to be annotated. */
75 Map<ObjectRef, String> obj_to_annotate = Map<ObjectRef, String>();
76
77 void VisitAttrs(AttrVisitor* v) {
78 v->Visit("binding_names", &binding_names);
79 v->Visit("show_meta", &show_meta);
80 v->Visit("ir_prefix", &ir_prefix);
81 v->Visit("buffer_dtype", &buffer_dtype);
82 v->Visit("int_dtype", &int_dtype);
83 v->Visit("float_dtype", &float_dtype);
84 v->Visit("verbose_expr", &verbose_expr);
85 v->Visit("indent_spaces", &indent_spaces);
86 v->Visit("print_line_numbers", &print_line_numbers);
87 v->Visit("num_context_lines", &num_context_lines);
88 v->Visit("syntax_sugar", &syntax_sugar);
89 v->Visit("path_to_underline", &path_to_underline);
90 v->Visit("path_to_annotate", &path_to_annotate);
91 v->Visit("obj_to_underline", &obj_to_underline);
92 v->Visit("obj_to_annotate", &obj_to_annotate);
93 }
94
95 static constexpr const char* _type_key = "node.PrinterConfig";
96 TVM_DECLARE_FINAL_OBJECT_INFO(PrinterConfigNode, Object);
97};
98
99class PrinterConfig : public ObjectRef {
100 public:
101 explicit PrinterConfig(Map<String, ObjectRef> config_dict = Map<String, ObjectRef>());
102
103 TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(PrinterConfig, runtime::ObjectRef,
104 PrinterConfigNode);
105};
106
107/*! \brief Legacy behavior of ReprPrinter. */
108class TVMScriptPrinter {
109 public:
110 /* Convert the object to TVMScript format */
111 static std::string Script(const ObjectRef& node, const Optional<PrinterConfig>& cfg);
112 // Allow registration to be printer.
113 using FType = NodeFunctor<std::string(const ObjectRef&, const PrinterConfig&)>;
114 TVM_DLL static FType& vtable();
115};
116
117#define TVM_OBJECT_ENABLE_SCRIPT_PRINTER() \
118 std::string Script(const Optional<PrinterConfig>& config = NullOpt) const { \
119 return TVMScriptPrinter::Script(GetRef<ObjectRef>(this), config.value_or(PrinterConfig())); \
120 }
121
122} // namespace tvm
123#endif // TVM_NODE_SCRIPT_PRINTER_H_
124