-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR introduces `PrinterConfig`, a systematic way to configure TVMScript printer without having to set global flags. This PR enables more customization of printer behavior. More specifically, now any TVM’s object in python, as long as it inherits from `Scriptable`, it automatically gains two methods: - `.script(tir_prefix=...)` - `.show(...)`
- Loading branch information
Showing
41 changed files
with
602 additions
and
849 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* 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. | ||
*/ | ||
/*! | ||
* \file tvm/node/repr_printer.h | ||
* \brief Printer class to print repr string of each AST/IR nodes. | ||
*/ | ||
#ifndef TVM_NODE_SCRIPT_PRINTER_H_ | ||
#define TVM_NODE_SCRIPT_PRINTER_H_ | ||
|
||
#include <tvm/node/functor.h> | ||
#include <tvm/node/object_path.h> | ||
#include <tvm/node/reflection.h> | ||
#include <tvm/runtime/data_type.h> | ||
|
||
#include <iostream> | ||
#include <string> | ||
|
||
namespace tvm { | ||
|
||
class PrinterConfigNode : public Object { | ||
public: | ||
/*! \brief The prefix of IR nodes */ | ||
std::string ir_prefix = "I"; | ||
/*! \brief The prefix of TIR nodes */ | ||
std::string tir_prefix = "T"; | ||
/*! \brief The prefix of Relax nodes */ | ||
std::string relax_prefix = "R"; | ||
/*! \brief Default data type of TIR buffer */ | ||
DataType buffer_dtype = DataType::Float(32); | ||
/*! \brief Default data type of integer literals */ | ||
DataType int_dtype = DataType::Int(32); | ||
/*! | ||
* \brief Default data type of float literals. Right now we always print out the explicit type | ||
* of floating point values, so setting it to Void means we do not print without the | ||
* T.float32/T.float64 wrapper. | ||
*/ | ||
DataType float_dtype = DataType::Void(); | ||
/*! \brief Whether or not to verbose print expressions. */ | ||
bool verbose_expr = false; | ||
/* \brief Number of spaces used for indentation*/ | ||
int indent_spaces = 4; | ||
/* \brief Whether to print line numbers */ | ||
bool print_line_numbers = false; | ||
/* \brief Number of context lines to print around the underlined text */ | ||
int num_context_lines = -1; | ||
/* \brief Object path to be underlined */ | ||
Optional<ObjectPath> path_to_underline = NullOpt; | ||
|
||
void VisitAttrs(AttrVisitor* v) { | ||
v->Visit("ir_prefix", &ir_prefix); | ||
v->Visit("buffer_dtype", &buffer_dtype); | ||
v->Visit("int_dtype", &int_dtype); | ||
v->Visit("float_dtype", &float_dtype); | ||
v->Visit("verbose_expr", &verbose_expr); | ||
v->Visit("indent_spaces", &indent_spaces); | ||
v->Visit("print_line_numbers", &print_line_numbers); | ||
v->Visit("num_context_lines", &num_context_lines); | ||
v->Visit("path_to_underline", &path_to_underline); | ||
} | ||
|
||
static constexpr const char* _type_key = "node.PrinterConfig"; | ||
TVM_DECLARE_FINAL_OBJECT_INFO(PrinterConfigNode, Object); | ||
}; | ||
|
||
class PrinterConfig : public ObjectRef { | ||
public: | ||
explicit PrinterConfig(Map<String, ObjectRef> config_dict = Map<String, ObjectRef>()); | ||
|
||
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(PrinterConfig, runtime::ObjectRef, | ||
PrinterConfigNode); | ||
}; | ||
|
||
/*! \brief Legacy behavior of ReprPrinter. */ | ||
class TVMScriptPrinter { | ||
public: | ||
/* Convert the object to TVMScript format */ | ||
static std::string Script(const ObjectRef& node, const Optional<PrinterConfig>& cfg); | ||
// Allow registration to be printer. | ||
using FType = NodeFunctor<std::string(const ObjectRef&, const PrinterConfig&)>; | ||
TVM_DLL static FType& vtable(); | ||
}; | ||
|
||
#define TVM_OBJECT_ENABLE_SCRIPT_PRINTER() \ | ||
std::string Script(const Optional<PrinterConfig>& config = NullOpt) const { \ | ||
return TVMScriptPrinter::Script(GetRef<ObjectRef>(this), config.value_or(PrinterConfig())); \ | ||
} | ||
|
||
} // namespace tvm | ||
#endif // TVM_NODE_SCRIPT_PRINTER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.