|
| 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_ |
0 commit comments