-
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.
[TVMScript] Printer entry point (#12462)
This PR: - Adds an entry point for the TVMScript Unified Printer - Adds a helper object class `RootNodeContainer` to provide an injection point for the actual printer implementation to add specialized logic on the root node to print. Tracking issue: #11912
- Loading branch information
Showing
11 changed files
with
355 additions
and
3 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
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.