Skip to content

Commit d5fab9e

Browse files
authored
[TVMScript] Use environment variable TVM_BLACK_FORMAT for .show() (#15762)
Prior to this commit, the default behavior of the `black_format` argument in TVMScript printing has changed back and forth, based on conflicting user preferences. This commit allows the default to be specified by each using using the `TVM_BLACK_FORMAT` environment variable. If unspecified in a `obj.show()` method call, this environment variable is used to determine the default.
1 parent dfd525b commit d5fab9e

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

python/tvm/runtime/script_printer.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
"""Configuration of TVMScript printer"""
18+
import os
1819
from typing import Dict, List, Optional, Sequence
1920

2021
from tvm._ffi import get_global_func, register_object
@@ -199,7 +200,7 @@ def script(
199200
def show(
200201
self,
201202
style: Optional[str] = None,
202-
black_format: bool = False,
203+
black_format: Optional[bool] = None,
203204
*,
204205
name: Optional[str] = None,
205206
show_meta: bool = False,
@@ -226,8 +227,26 @@ def show(
226227
style : str, optional
227228
Pygmentize printing style, auto-detected if None. See
228229
`tvm.script.highlight.cprint` for more details.
229-
black_format: bool
230-
If true, use the formatter Black to format the TVMScript
230+
231+
black_format: Optional[bool]
232+
233+
If true, use the formatter Black to format the TVMScript.
234+
If false, do not apply the auto-formatter.
235+
236+
If None (default), determine the behavior based on the
237+
environment variable "TVM_BLACK_FORMAT". If this
238+
environment variable is unset, set to the empty string, or
239+
set to the integer zero, black auto-formatting will be
240+
disabled. If the environment variable is set to a
241+
non-zero integer, black auto-formatting will be enabled.
242+
243+
Note that the "TVM_BLACK_FORMAT" environment variable only
244+
applies to the `.show()` method, and not the underlying
245+
`.script()` method. The `.show()` method is intended for
246+
human-readable output based on individual user
247+
preferences, while the `.script()` method is intended to
248+
provided a consistent output regardless of environment.
249+
231250
name : Optional[str] = None
232251
The name of the object
233252
show_meta : bool = False
@@ -263,11 +282,16 @@ def show(
263282
Object to be underlined
264283
obj_to_annotate : Optional[Dict[Object, str]] = None
265284
Object to be annotated
285+
266286
"""
267287
from tvm.script.highlight import ( # pylint: disable=import-outside-toplevel
268288
cprint,
269289
)
270290

291+
if black_format is None:
292+
env = os.environ.get("TVM_BLACK_FORMAT")
293+
black_format = env and int(env)
294+
271295
cprint(
272296
self.script(
273297
name=name,

python/tvm/tir/schedule/schedule.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
"""The TensorIR schedule class"""
18+
import inspect
1819
from typing import Callable, Dict, List, Optional, Tuple, Union
1920

2021
from tvm._ffi import register_object as _register_object
@@ -268,26 +269,24 @@ def fork_seed(self) -> int:
268269
"""
269270
return _ffi_api.ScheduleForkSeed(self) # type: ignore # pylint: disable=no-member
270271

271-
def show(self, style: Optional[str] = None, black_format: bool = False) -> None:
272+
def show(self, *args, **kwargs) -> None:
272273
"""A sugar for print highlighted TVM script.
273274
274-
Parameters
275-
----------
276-
style : str, optional
277-
278-
Pygmentize printing style, auto-detected if None. See
279-
`tvm.script.highlight.cprint` for more details.
280-
281-
black_format: bool
282-
283-
If true, use the formatter Black to format the TVMScript
275+
All parameters are forwarded to the underlying `Module.show`
276+
and `Trace.show` methods.
284277
"""
285278
mod = self.mod
286279
if mod is not None:
287-
mod.show(style=style, black_format=black_format)
280+
mod.show(*args, **kwargs)
281+
288282
trace = self.trace
289283
if trace is not None:
290-
trace.show(style=style, black_format=black_format)
284+
# Trace.show only supports the style and black_format arguments
285+
param_binding = inspect.signature(mod.show).bind(*args, **kwargs)
286+
param_binding.apply_defaults()
287+
bound_args = param_binding.arguments
288+
289+
trace.show(style=bound_args["style"], black_format=bound_args["black_format"])
291290

292291
########## Lookup ##########
293292

python/tvm/tir/schedule/trace.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
"""An execution trace of a scheduling program"""
18+
import os
1819
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional
1920

2021
from tvm._ffi import register_object as _register_object
@@ -274,10 +275,16 @@ def show(self, style: Optional[str] = None, black_format: bool = False) -> None:
274275
275276
black_format: bool
276277
277-
If true, use the formatter Black to format the TVMScript
278+
If true, use the formatter Black to format the TVMScript.
279+
If None, determine based on the "TVM_BLACK_FORMAT" environment
280+
variable.
278281
"""
279282
from tvm.script.highlight import ( # pylint: disable=import-outside-toplevel
280283
cprint,
281284
)
282285

286+
if black_format is None:
287+
env = os.environ.get("TVM_BLACK_FORMAT")
288+
black_format = bool(env and int(env))
289+
283290
cprint(str(self), style=style, black_format=black_format)

0 commit comments

Comments
 (0)