Skip to content

Commit 1ceebbc

Browse files
committed
[TVMScript] Introduce PrinterConfig
This PR introduces `PrinterConfig`, a systematic way to configure TVMScript printer without having to set global flags.
1 parent 1d89071 commit 1ceebbc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+602
-849
lines changed

include/tvm/ir/expr.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,7 @@ class PrimExprNode : public BaseExprNode {
100100
*/
101101
DataType dtype;
102102

103-
/*!
104-
* \brief Returns the TVMScript format
105-
* \param indent_spaces Number of spaces used for indentation
106-
* \param print_line_numbers Whether to print line numbers
107-
* \param num_context_lines Number of context lines to print around the underlined text
108-
* \param path_to_underline Object path to be underlined
109-
*/
110-
TVM_DLL std::string Script(int indent_spaces = 4, bool print_line_numbers = false,
111-
int num_context_lines = -1,
112-
Optional<ObjectPath> path_to_underline = NullOpt) const;
103+
TVM_OBJECT_ENABLE_SCRIPT_PRINTER();
113104

114105
static constexpr const char* _type_key = "PrimExpr";
115106
static constexpr const uint32_t _type_child_slots = 38;

include/tvm/ir/module.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,16 +328,7 @@ class IRModuleNode : public Object {
328328
*/
329329
TVM_DLL std::unordered_set<String> Imports() const;
330330

331-
/*!
332-
* \brief Returns the TVMScript format
333-
* \param indent_spaces Number of spaces used for indentation
334-
* \param print_line_numbers Whether to print line numbers
335-
* \param num_context_lines Number of context lines to print around the underlined text
336-
* \param path_to_underline Object path to be underlined
337-
*/
338-
TVM_DLL std::string Script(int indent_spaces = 4, bool print_line_numbers = false,
339-
int num_context_lines = -1,
340-
Optional<ObjectPath> path_to_underline = NullOpt) const;
331+
TVM_OBJECT_ENABLE_SCRIPT_PRINTER();
341332

342333
static constexpr const char* _type_key = "IRModule";
343334
static constexpr const bool _type_has_method_sequal_reduce = true;

include/tvm/node/repr_printer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#define TVM_NODE_REPR_PRINTER_H_
2525

2626
#include <tvm/node/functor.h>
27+
#include <tvm/node/script_printer.h>
2728

2829
#include <iostream>
2930
#include <string>

include/tvm/node/script_printer.h

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
34+
namespace tvm {
35+
36+
class PrinterConfigNode : public Object {
37+
public:
38+
/*! \brief The prefix of IR nodes */
39+
std::string ir_prefix = "I";
40+
/*! \brief The prefix of TIR nodes */
41+
std::string tir_prefix = "T";
42+
/*! \brief The prefix of Relax nodes */
43+
std::string relax_prefix = "R";
44+
/*! \brief Default data type of TIR buffer */
45+
DataType buffer_dtype = DataType::Float(32);
46+
/*! \brief Default data type of integer literals */
47+
DataType int_dtype = DataType::Int(32);
48+
/*!
49+
* \brief Default data type of float literals. Right now we always print out the explicit type
50+
* of floating point values, so setting it to Void means we do not print without the
51+
* T.float32/T.float64 wrapper.
52+
*/
53+
DataType float_dtype = DataType::Void();
54+
/*! \brief Whether or not to verbose print expressions. */
55+
bool verbose_expr = false;
56+
/* \brief Number of spaces used for indentation*/
57+
int indent_spaces = 4;
58+
/* \brief Whether to print line numbers */
59+
bool print_line_numbers = false;
60+
/* \brief Number of context lines to print around the underlined text */
61+
int num_context_lines = -1;
62+
/* \brief Object path to be underlined */
63+
Optional<ObjectPath> path_to_underline = NullOpt;
64+
65+
void VisitAttrs(AttrVisitor* v) {
66+
v->Visit("ir_prefix", &ir_prefix);
67+
v->Visit("buffer_dtype", &buffer_dtype);
68+
v->Visit("int_dtype", &int_dtype);
69+
v->Visit("float_dtype", &float_dtype);
70+
v->Visit("verbose_expr", &verbose_expr);
71+
v->Visit("indent_spaces", &indent_spaces);
72+
v->Visit("print_line_numbers", &print_line_numbers);
73+
v->Visit("num_context_lines", &num_context_lines);
74+
v->Visit("path_to_underline", &path_to_underline);
75+
}
76+
77+
static constexpr const char* _type_key = "node.PrinterConfig";
78+
TVM_DECLARE_FINAL_OBJECT_INFO(PrinterConfigNode, Object);
79+
};
80+
81+
class PrinterConfig : public ObjectRef {
82+
public:
83+
explicit PrinterConfig(Map<String, ObjectRef> config_dict = Map<String, ObjectRef>());
84+
85+
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(PrinterConfig, runtime::ObjectRef,
86+
PrinterConfigNode);
87+
};
88+
89+
/*! \brief Legacy behavior of ReprPrinter. */
90+
class TVMScriptPrinter {
91+
public:
92+
/* Convert the object to TVMScript format */
93+
static std::string Script(const ObjectRef& node, const Optional<PrinterConfig>& cfg);
94+
// Allow registration to be printer.
95+
using FType = NodeFunctor<std::string(const ObjectRef&, const PrinterConfig&)>;
96+
TVM_DLL static FType& vtable();
97+
};
98+
99+
#define TVM_OBJECT_ENABLE_SCRIPT_PRINTER() \
100+
std::string Script(const Optional<PrinterConfig>& config = NullOpt) const { \
101+
return TVMScriptPrinter::Script(GetRef<ObjectRef>(this), config.value_or(PrinterConfig())); \
102+
}
103+
104+
} // namespace tvm
105+
#endif // TVM_NODE_SCRIPT_PRINTER_H_

include/tvm/script/printer/doc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@ namespace tvm {
2929
namespace script {
3030
namespace printer {
3131

32+
// Forward declaration
3233
class Doc;
3334

35+
/*!
36+
* \brief Convert Doc into Python script.
37+
* \param doc Doc to be converted
38+
* \param cfg The configuration of the printer
39+
*/
40+
String DocToPythonScript(Doc doc, const PrinterConfig& cfg);
41+
3442
/*!
3543
* \brief The base class of all Doc.
3644
*

include/tvm/script/printer/ir_docsifier.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ class IRDocsifierNode : public Object {
126126
/*! \brief The name of the variable */
127127
Optional<String> name;
128128
};
129+
/*! \brief The configuration of the printer */
130+
PrinterConfig cfg{nullptr};
129131
/*!
130132
* \brief The stack of frames.
131133
* \sa FrameNode
@@ -232,7 +234,7 @@ class IRDocsifier : public ObjectRef {
232234
public:
233235
using FType = IRDocsifierFunctor<printer::Doc, ObjectPath, IRDocsifier>;
234236
/*! \brief Create a IRDocsifier. */
235-
IRDocsifier();
237+
explicit IRDocsifier(const PrinterConfig& cfg);
236238
/*! \brief The registration table for IRDocsifier. */
237239
TVM_DLL static FType& vtable();
238240

include/tvm/script/printer/printer.h

Lines changed: 0 additions & 76 deletions
This file was deleted.

include/tvm/tir/function.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,7 @@ class PrimFuncNode : public BaseFuncNode {
132132
*/
133133
TVM_DLL FuncType func_type_annotation() const;
134134

135-
/*!
136-
* \brief Returns the TVMScript format
137-
* \param indent_spaces Number of spaces used for indentation
138-
* \param print_line_numbers Whether to print line numbers
139-
* \param num_context_lines Number of context lines to print around the underlined text
140-
* \param path_to_underline Object path to be underlined
141-
*/
142-
std::string Script(int indent_spaces = 4, bool print_line_numbers = false,
143-
int num_context_lines = -1,
144-
Optional<ObjectPath> path_to_underline = NullOpt) const;
135+
TVM_OBJECT_ENABLE_SCRIPT_PRINTER();
145136

146137
static constexpr const char* _type_key = "tir.PrimFunc";
147138
TVM_DECLARE_FINAL_OBJECT_INFO(PrimFuncNode, BaseFuncNode);

include/tvm/tir/stmt.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,7 @@ class StmtNode : public Object {
4646
StmtNode() = default;
4747
explicit StmtNode(Span span) : span(span) {}
4848

49-
/*!
50-
* \brief Returns the TVMScript format
51-
* \param indent_spaces Number of spaces used for indentation
52-
* \param print_line_numbers Whether to print line numbers
53-
* \param num_context_lines Number of context lines to print around the underlined text
54-
* \param path_to_underline Object path to be underlined
55-
*/
56-
std::string Script(int indent_spaces = 4, bool print_line_numbers = false,
57-
int num_context_lines = -1,
58-
Optional<ObjectPath> path_to_underline = NullOpt) const;
49+
TVM_OBJECT_ENABLE_SCRIPT_PRINTER();
5950

6051
static constexpr const char* _type_key = "tir.Stmt";
6152
static constexpr const bool _type_has_method_sequal_reduce = true;

python/tvm/ir/expr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"""Common expressions data structures in the IR."""
1818
import tvm._ffi
1919

20-
from ..runtime import const, convert
20+
from ..runtime import Scriptable, const, convert
2121
from . import _ffi_api
2222
from .base import Node
2323

@@ -121,7 +121,7 @@ def astext(self, show_meta_data=True, annotate=None):
121121

122122

123123
@tvm._ffi.register_object
124-
class Range(Node):
124+
class Range(Node, Scriptable):
125125
"""Represent a range in TVM.
126126
127127
You do not need to create a Range explicitly.

0 commit comments

Comments
 (0)