tvm
repr_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_REPR_PRINTER_H_
24 #define TVM_NODE_REPR_PRINTER_H_
25 
26 #include <tvm/node/functor.h>
28 
29 #include <iostream>
30 #include <string>
31 
32 namespace tvm {
34 class ReprPrinter {
35  public:
37  std::ostream& stream;
39  int indent{0};
40 
41  explicit ReprPrinter(std::ostream& stream) // NOLINT(*)
42  : stream(stream) {}
43 
45  TVM_DLL void Print(const ObjectRef& node);
47  TVM_DLL void PrintIndent();
48  // Allow registration to be printer.
49  using FType = NodeFunctor<void(const ObjectRef&, ReprPrinter*)>;
50  TVM_DLL static FType& vtable();
51 };
52 
55  public:
57  int indent{0};
58 
59  explicit ReprLegacyPrinter(std::ostream& stream) // NOLINT(*)
60  : stream(stream) {}
61 
63  TVM_DLL void Print(const ObjectRef& node);
65  TVM_DLL void PrintIndent();
67  TVM_DLL static bool CanDispatch(const ObjectRef& node);
69  TVM_DLL std::ostream& Stream() const;
70  // Allow registration to be printer.
71  using FType = NodeFunctor<void(const ObjectRef&, ReprLegacyPrinter*)>;
72  TVM_DLL static FType& vtable();
73 
74  private:
76  std::ostream& stream;
77 };
78 
83 TVM_DLL void Dump(const runtime::ObjectRef& node);
84 
89 TVM_DLL void Dump(const runtime::Object* node);
90 
91 } // namespace tvm
92 
93 namespace tvm {
94 namespace runtime {
95 // default print function for all objects
96 // provide in the runtime namespace as this is where objectref originally comes from.
97 inline std::ostream& operator<<(std::ostream& os, const ObjectRef& n) { // NOLINT(*)
98  ReprPrinter(os).Print(n);
99  return os;
100 }
101 
102 inline std::string AsLegacyRepr(const ObjectRef& n) {
103  std::ostringstream os;
104  ReprLegacyPrinter(os).Print(n);
105  return os.str();
106 }
107 } // namespace runtime
109 } // namespace tvm
110 #endif // TVM_NODE_REPR_PRINTER_H_
A dynamically dispatched functor on the type of the first argument.
Definition: functor.h:64
Legacy behavior of ReprPrinter.
Definition: repr_printer.h:54
void Print(const ObjectRef &node)
The node to be printed.
static bool CanDispatch(const ObjectRef &node)
Could the LegacyPrinter dispatch the node.
std::ostream & Stream() const
Return the ostream it maintains.
ReprLegacyPrinter(std::ostream &stream)
Definition: repr_printer.h:59
int indent
The indentation level.
Definition: repr_printer.h:57
void PrintIndent()
Print indent to the stream.
static FType & vtable()
A printer class to print the AST/IR nodes.
Definition: repr_printer.h:34
void Print(const ObjectRef &node)
The node to be printed.
std::ostream & stream
The output stream.
Definition: repr_printer.h:37
ReprPrinter(std::ostream &stream)
Definition: repr_printer.h:41
void PrintIndent()
Print indent to the stream.
static FType & vtable()
int indent
The indentation level.
Definition: repr_printer.h:39
Base class of all object reference.
Definition: object.h:519
base class of all object containers.
Definition: object.h:171
Defines the Functor data structures.
std::string AsLegacyRepr(const ObjectRef &n)
Definition: repr_printer.h:102
std::ostream & operator<<(std::ostream &os, const ObjectRef &n)
Definition: repr_printer.h:97
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
void Dump(const runtime::ObjectRef &node)
Dump the node to stderr, used for debug purposes.