-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TVMScript] Printer entry point #12462
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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. | ||
*/ | ||
#ifndef TVM_SCRIPT_PRINTER_H_ | ||
#define TVM_SCRIPT_PRINTER_H_ | ||
|
||
#include <tvm/node/node.h> | ||
#include <tvm/node/object_path.h> | ||
|
||
namespace tvm { | ||
namespace script { | ||
namespace printer { | ||
|
||
/*! | ||
* \brief Print IR graph as TVMScript code | ||
* | ||
* \param root_node The root node to print. | ||
* \param ir_name The dispatch token of the target IR, e.g., "tir", "relax". | ||
* \param ir_prefix The symbol name for TVMScript IR namespaces. For example, {"tir": "T"}. | ||
* \param indent_spaces Number of spaces used for indentation | ||
* \param print_line_numbers Whether to print line numbers | ||
* \param num_context_lines Number of context lines to print around the underlined text | ||
* \param path_to_underline Object path to be underlined | ||
* | ||
* \return the TVMScript code as string. | ||
*/ | ||
String Script( // | ||
const ObjectRef& root_node, // | ||
String ir_name, // | ||
Map<String, String> ir_prefix, // | ||
int indent_spaces = 4, // | ||
bool print_line_numbers = false, // | ||
int num_context_lines = -1, // | ||
Optional<ObjectPath> path_to_underline = NullOpt // | ||
); | ||
|
||
} // namespace printer | ||
} // namespace script | ||
} // namespace tvm | ||
|
||
#endif // TVM_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 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 |
---|---|---|
|
@@ -24,3 +24,4 @@ | |
""" | ||
|
||
from . import _ffi_api | ||
from .entry import script |
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,71 @@ | ||
# 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. | ||
""" | ||
This file contains the entry point of TVMScript Unified Printer. | ||
""" | ||
|
||
from typing import Dict, Optional | ||
|
||
from tvm.runtime import Object, ObjectPath | ||
|
||
from . import _ffi_api | ||
|
||
|
||
def script( # pylint: disable=too-many-arguments | ||
root_node: Object, | ||
ir_name: str, | ||
ir_prefix: Dict[str, str], | ||
indent_spaces: int = 4, | ||
print_line_numbers: bool = False, | ||
num_context_lines: int = -1, | ||
path_to_underline: Optional[ObjectPath] = None, | ||
) -> str: | ||
""" | ||
Print IR graph as TVMScript code | ||
|
||
Parameters | ||
---------- | ||
root_node : Object | ||
The root node to print. | ||
ir_name : str | ||
The dispatch token of the target IR, e.g., "tir", "relax". | ||
ir_prefix : Dict[str, str] | ||
The symbol name for TVMScript IR namespaces. For example, | ||
{"tir": "T"}. | ||
indent_spaces : int | ||
The number of indent spaces to use in the output | ||
print_line_numbers: bool | ||
Whether to print line numbers | ||
num_context_lines : Optional[int] | ||
Number of context lines to print around the underlined text | ||
path_to_underline : Optional[ObjectPath] | ||
Object path to be underlined | ||
|
||
Returns | ||
------- | ||
script : str | ||
The TVMScript code of the root_node | ||
""" | ||
return _ffi_api.Script( # type: ignore # pylint: disable=no-member | ||
root_node, | ||
ir_name, | ||
ir_prefix, | ||
indent_spaces, | ||
print_line_numbers, | ||
num_context_lines, | ||
path_to_underline, | ||
) |
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,54 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#include <tvm/runtime/registry.h> | ||
#include <tvm/script/printer.h> | ||
#include <tvm/script/printer/doc.h> | ||
#include <tvm/script/printer/doc_printer.h> | ||
#include <tvm/script/printer/frame.h> | ||
#include <tvm/script/printer/ir_docsifier.h> | ||
|
||
namespace tvm { | ||
namespace script { | ||
namespace printer { | ||
|
||
String Script( // | ||
const ObjectRef& root_node, // | ||
String ir_name, // | ||
Map<String, String> ir_prefix, // | ||
int indent_spaces, // | ||
bool print_line_numbers, // | ||
int num_context_lines, // | ||
Optional<ObjectPath> path_to_underline // | ||
) { | ||
IRDocsifier ir_docsifier(ir_prefix); | ||
|
||
auto dispatch_ctx = ir_docsifier->WithDispatchToken(ir_name); | ||
|
||
Doc doc = ir_docsifier->AsDoc<Doc>(MakeTraced(RootNodeContainer(root_node))); | ||
|
||
return DocToPythonScript(doc, indent_spaces, print_line_numbers, num_context_lines, | ||
path_to_underline); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("script.printer.Script").set_body_typed(&Script); | ||
|
||
} // namespace printer | ||
} // namespace script | ||
} // namespace tvm |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, what do you think works better,
tvm::script::printer
ortvm::script
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the function name is
Script
, I thinktvm::script::printer
is a better choice. Otherwise the purpose of this function is less clear, because it's a free function rather than a method on IR node.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved it to the
tvm::script::printer