Skip to content

Commit

Permalink
[Option] Add option to set class Var id attribute to 0 by default (#393)
Browse files Browse the repository at this point in the history
By default Var instances will have an id of 0.

This is to ensure that each entire FlowGraph is entirely deterministic
given the same interpreter+input.
  • Loading branch information
destefy committed Dec 15, 2023
1 parent 669d97f commit e7f2efd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 3 additions & 0 deletions python/hidet/ir/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import string
import operator
import numpy as np
import hidet.option
from .node import Node
from .type import BaseType, TensorType, DataType, TensorPointerType, PointerType, FuncType, StringType, ArrayType
from .type import tensor_pointer_type, string_type, tensor_type, data_type
Expand Down Expand Up @@ -565,6 +566,8 @@ def __init__(self, hint: Optional[str], type: BaseType, name: Optional[str] = No

@staticmethod
def new_id():
if not hidet.option.get_option('debug_enable_var_id'):
return 0
Var.id_clock += 1
return Var.id_clock

Expand Down
28 changes: 27 additions & 1 deletion python/hidet/option.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# limitations under the License.
from __future__ import annotations
from typing import Dict, Any, List, Optional, Callable, Iterable, Tuple, Union
import warnings
import os
import tomlkit

Expand Down Expand Up @@ -190,11 +191,19 @@ def register_hidet_options():
description='Whether to cache the generated kernels during tuning.',
choices=[True, False],
)
register_option(
name='debug_enable_var_id',
type_hint='bool',
default_value=False,
description='Assign a variable id to each variable in the IR. If set to false, all variable IDs will be 0',
choices=[True, False],
)
register_option(
name='debug_show_var_id',
type_hint='bool',
default_value=False,
description='Whether to show the variable id in the IR.',
description='Whether to show the variable id in the IR.\
Hint: all variable ids will be 0 unless the debug_enable_var_id option is set to True.',
choices=[True, False],
)
register_option(
Expand Down Expand Up @@ -701,6 +710,21 @@ def debug_cache_tuning(enabled: bool = True):
OptionContext.current().set_option('debug_cache_tuning', enabled)


def debug_enable_var_id(enable: bool = True):
"""
Whether to enable var id in the IR.
When this option is enabled, each variable (i.e., hidet.ir.Var) will have a unique id.
Otherwise, each variable's ID will be 0.
Parameters
----------
enable: bool
Whether to enable var id in the IR.
"""
OptionContext.current().set_option('debug_enable_var_id', enable)


def debug_show_var_id(enable: bool = True):
"""
Whether to show the var id in the IR.
Expand All @@ -713,6 +737,8 @@ def debug_show_var_id(enable: bool = True):
enable: bool
Whether to show the var id in the IR.
"""
if not OptionContext.current().get_option('debug_enable_var_id'):
warnings.warn("Please use `hidet.option.debug_enable_var_id()` to enable the id first")
OptionContext.current().set_option('debug_show_var_id', enable)


Expand Down

0 comments on commit e7f2efd

Please sign in to comment.