repr_printer.cc 1.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/*!
 * Printer utilities
22
 * \file node/repr_printer.cc
23
 */
24
#include <tvm/runtime/registry.h>
25
#include <tvm/node/repr_printer.h>
26 27 28

namespace tvm {

29
void ReprPrinter::Print(const ObjectRef& node) {
30 31 32 33 34 35 36 37 38 39 40 41 42
  static const FType& f = vtable();
  if (!node.defined()) {
    stream << "(nullptr)";
  } else {
    if (f.can_dispatch(node)) {
      f(node, this);
    } else {
      // default value, output type key and addr.
      stream << node->GetTypeKey() << "(" << node.get() << ")";
    }
  }
}

43
void ReprPrinter::PrintIndent() {
44 45 46 47 48
  for (int i = 0; i < indent; ++i) {
    stream << ' ';
  }
}

49
ReprPrinter::FType& ReprPrinter::vtable() {
50 51 52
  static FType inst;
  return inst;
}
53

54
void Dump(const runtime::ObjectRef& n) {
55 56
  std::cerr << n << "\n";
}
57

58 59 60 61
void Dump(const runtime::Object* n) {
  Dump(runtime::GetRef<runtime::ObjectRef>(n));
}

62 63 64 65 66 67
TVM_REGISTER_GLOBAL("node.AsRepr")
.set_body_typed([](runtime::ObjectRef obj) {
  std::ostringstream os;
  os << obj;
  return os.str();
});
68
}  // namespace tvm