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/*!
21 * Printer utilities
22 * \file node/repr_printer.cc
23 */
24#include <tvm/node/repr_printer.h>
25#include <tvm/runtime/registry.h>
26
27namespace tvm {
28
29void ReprPrinter::Print(const ObjectRef& node) {
30 static const FType& f = vtable();
31 if (!node.defined()) {
32 stream << "(nullptr)";
33 } else {
34 if (f.can_dispatch(node)) {
35 f(node, this);
36 } else {
37 // default value, output type key and addr.
38 stream << node->GetTypeKey() << "(" << node.get() << ")";
39 }
40 }
41}
42
43void ReprPrinter::PrintIndent() {
44 for (int i = 0; i < indent; ++i) {
45 stream << ' ';
46 }
47}
48
49ReprPrinter::FType& ReprPrinter::vtable() {
50 static FType inst;
51 return inst;
52}
53
54void ReprLegacyPrinter::Print(const ObjectRef& node) {
55 static const FType& f = vtable();
56 if (!node.defined()) {
57 stream << "(nullptr)";
58 } else if (f.can_dispatch(node)) {
59 f(node, this);
60 } else {
61 try {
62 stream << node; // Use ReprPrinter
63 } catch (const tvm::Error& e) {
64 LOG(WARNING) << "ReprPrinter fails";
65 stream << node->GetTypeKey() << '(' << node.get() << ')';
66 }
67 }
68}
69
70bool ReprLegacyPrinter::CanDispatch(const ObjectRef& node) {
71 static const FType& f = vtable();
72 return !node.defined() || f.can_dispatch(node);
73}
74
75void ReprLegacyPrinter::PrintIndent() {
76 for (int i = 0; i < indent; ++i) {
77 stream << ' ';
78 }
79}
80
81ReprLegacyPrinter::FType& ReprLegacyPrinter::vtable() {
82 static FType inst;
83 return inst;
84}
85
86void Dump(const runtime::ObjectRef& n) { std::cerr << n << "\n"; }
87
88void Dump(const runtime::Object* n) { Dump(runtime::GetRef<runtime::ObjectRef>(n)); }
89
90TVM_REGISTER_GLOBAL("node.AsRepr").set_body_typed([](runtime::ObjectRef obj) {
91 std::ostringstream os;
92 os << obj;
93 return os.str();
94});
95
96TVM_REGISTER_GLOBAL("node.AsLegacyRepr").set_body_typed(runtime::AsLegacyRepr);
97
98} // namespace tvm
99