Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

highlight tvm script with pygment on vscode dark theme #185

Merged
merged 12 commits into from
Jul 27, 2022
1 change: 1 addition & 0 deletions python/gen_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
(
"Base requirements needed to install tvm",
[
"Pygments",
"attrs",
"cloudpickle",
"decorator",
Expand Down
14 changes: 14 additions & 0 deletions python/tvm/ir/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,20 @@ def script(self, tir_prefix: str = "T", show_meta: bool = False) -> str:
self, tir_prefix, show_meta
) # type: ignore

def show(self, style: str = "light") -> None:
"""
A sugar for print highlighted TVM script.

Parameters
----------
style : str, optional
Pygments styles extended by "light" (default) and "dark", by default "light"
"""
from tvm.script.highlight import cprint # pylint: disable=import-outside-toplevel

# Use deferred import to avoid circular import while keeping cprint under tvm/script
cprint(self, style=style)

def get_attr(self, attr_key):
"""Get the IRModule attribute.

Expand Down
29 changes: 29 additions & 0 deletions python/tvm/relax/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,35 @@ def __call__(self, *args):
"""
return Call(self, args, None, None)

def script(self, show_meta: bool = False) -> str:
"""Print relax.Function into TVMScript

Parameters
----------
show_meta : bool
Whether to show meta information

Returns
-------
script : str
The TVM Script of the relax.Function
"""
return tvm._ffi.get_global_func("script.AsRelaxScript")(self, show_meta) # type: ignore

def show(self, style: str = "light") -> None:
"""
A sugar for print highlighted TVM script.

Parameters
----------
style : str, optional
Pygments styles extended by "light" (default) and "dark", by default "light"
"""
from tvm.script.highlight import cprint # pylint: disable=import-outside-toplevel

# Use deferred import to avoid circular import while keeping cprint under tvm/script
cprint(self, style=style)


@tvm._ffi.register_object("relax.expr.ExternFunc")
class ExternFunc(BaseFunc):
Expand Down
88 changes: 88 additions & 0 deletions python/tvm/script/highlight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# 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.
"""Highlight printed TVM script.
"""

from typing import Union

from pygments import highlight
from pygments.lexers import Python3Lexer
from pygments.formatters import Terminal256Formatter
from pygments.style import Style
from pygments.token import Keyword, Name, Comment, String, Number, Operator

from tvm.ir import IRModule
from tvm.tir import PrimFunc


class VSCDark(Style):
"""A VSCode-Dark-like Pygments style configuration"""

styles = {
Keyword: "bold #c586c0",
Keyword.Namespace: "#4ec9b0",
Keyword.Type: "#82aaff",
Name.Function: "bold #dcdcaa",
Name.Class: "bold #569cd6",
Name.Decorator: "italic #fe4ef3",
String: "#ce9178",
Number: "#b5cea8",
Operator: "#bbbbbb",
Operator.Word: "#569cd6",
Comment: "italic #6a9956",
}


class JupyterLight(Style):
"""A Jupyter-Notebook-like Pygments style configuration"""

styles = {
Keyword: "bold #008000",
Keyword.Type: "nobold #008000",
Name.Function: "#0000FF",
Name.Class: "bold #0000FF",
Name.Decorator: "#AA22FF",
String: "#BA2121",
Number: "#008000",
Operator: "bold #AA22FF",
Operator.Word: "bold #008000",
Comment: "italic #007979",
}


def cprint(printable: Union[IRModule, PrimFunc], style="light") -> None:
"""
Print highlighted TVM script string with Pygments

Parameters
----------
printable : Union[IRModule, PrimFunc]
The TVM script to be printed
style : str, optional
Style of the printed script

Notes
-----
The style parameter follows the Pygments style names or Style objects. Two
ganler marked this conversation as resolved.
Show resolved Hide resolved
built-in styles are extended: "light" (default) and "dark". Other styles
can be found in https://pygments.org/styles/
"""
if style == "light":
style = JupyterLight
elif style == "dark":
style = VSCDark
print(highlight(printable.script(), Python3Lexer(), Terminal256Formatter(style=style)))
14 changes: 14 additions & 0 deletions python/tvm/tir/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ def script(self, tir_prefix: str = "T", show_meta: bool = False) -> str:
self, tir_prefix, show_meta
) # type: ignore

def show(self, style: str = "light") -> None:
"""
A sugar for print highlighted TVM script.

Parameters
----------
style : str, optional
Pygments styles extended by "light" (default) and "dark", by default "light"
"""
from tvm.script.highlight import cprint # pylint: disable=import-outside-toplevel

# Use deferred import to avoid circular import while keeping cprint under tvm/script
cprint(self, style=style)


@tvm._ffi.register_object("tir.TensorIntrin")
class TensorIntrin(Object):
Expand Down