Skip to content

Commit 00b0bee

Browse files
authored
[Target][TVMC][UX] Avoid creation of dummy Target objects in TVMC (apache#9662)
* [Target][TVMC][UX] Avoid creation of dummy Target objects in TVMC * nit fix
1 parent cb132e2 commit 00b0bee

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

python/tvm/driver/tvmc/target.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"""
2020

2121
from tvm.driver import tvmc
22-
from tvm.target import Target
22+
from tvm.target import Target, TargetKind
2323

2424
# We can't tell the type inside an Array but all current options are strings so
2525
# it can default to that. Bool is used alongside Integer but aren't distinguished
@@ -33,14 +33,14 @@ def _valid_target_kinds():
3333
return filter(lambda target: target not in codegen_names, Target.list_kinds())
3434

3535

36-
def _generate_target_kind_args(parser, kind):
37-
target_group = parser.add_argument_group(f"target {kind.name}")
38-
for target_option, target_type in kind.options.items():
36+
def _generate_target_kind_args(parser, kind_name):
37+
target_group = parser.add_argument_group(f"target {kind_name}")
38+
for target_option, target_type in TargetKind.options_from_name(kind_name).items():
3939
if target_type in INTERNAL_TO_NATIVE_TYPE:
4040
target_group.add_argument(
41-
f"--target-{kind.name}-{target_option}",
41+
f"--target-{kind_name}-{target_option}",
4242
type=INTERNAL_TO_NATIVE_TYPE[target_type],
43-
help=f"target {kind.name} {target_option}{INTERNAL_TO_HELP[target_type]}",
43+
help=f"target {kind_name} {target_option}{INTERNAL_TO_HELP[target_type]}",
4444
)
4545

4646

@@ -52,15 +52,14 @@ def generate_target_args(parser):
5252
required=True,
5353
)
5454
for target_kind in _valid_target_kinds():
55-
target = Target(target_kind)
56-
_generate_target_kind_args(parser, target.kind)
55+
_generate_target_kind_args(parser, target_kind)
5756

5857

59-
def _reconstruct_target_kind_args(args, kind):
58+
def _reconstruct_target_kind_args(args, kind_name):
6059
kind_options = {}
61-
for target_option, target_type in kind.options.items():
60+
for target_option, target_type in TargetKind.options_from_name(kind_name).items():
6261
if target_type in INTERNAL_TO_NATIVE_TYPE:
63-
var_name = f"target_{kind.name.replace('-', '_')}_{target_option.replace('-', '_')}"
62+
var_name = f"target_{kind_name.replace('-', '_')}_{target_option.replace('-', '_')}"
6463
option_value = getattr(args, var_name)
6564
if option_value is not None:
6665
kind_options[target_option] = getattr(args, var_name)
@@ -71,8 +70,7 @@ def reconstruct_target_args(args):
7170
"""Reconstructs the target options from the arguments"""
7271
reconstructed = {}
7372
for target_kind in _valid_target_kinds():
74-
target = Target(target_kind)
75-
kind_options = _reconstruct_target_kind_args(args, target.kind)
73+
kind_options = _reconstruct_target_kind_args(args, target_kind)
7674
if kind_options:
77-
reconstructed[target.kind.name] = kind_options
75+
reconstructed[target_kind] = kind_options
7876
return reconstructed

python/tvm/target/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
We can use :py:func:`tvm.target.Target` to create a tvm.target.Target from the target string.
5959
We can also use other specific function in this module to create specific targets.
6060
"""
61-
from .target import Target, create
61+
from .target import Target, create, TargetKind
6262
from .target import (
6363
cuda,
6464
rocm,

python/tvm/target/target.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ def options(self):
3636
"""Returns the dict of available option names and types"""
3737
return dict(_ffi_api.ListTargetKindOptions(self))
3838

39+
@staticmethod
40+
def options_from_name(kind_name: str):
41+
"""Returns the dict of available option names and types from a name of TargetKind"""
42+
return dict(_ffi_api.ListTargetKindOptionsFromName(kind_name))
43+
3944

4045
@tvm._ffi.register_object
4146
class Target(Object):

src/target/target_kind.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,15 @@ TVM_REGISTER_TARGET_KIND("hybrid", kDLCPU) // line break
402402

403403
TVM_REGISTER_TARGET_KIND("composite", kDLCPU).add_attr_option<Array<Target>>("devices");
404404

405-
406405
/********** Registry **********/
407406

408407
TVM_REGISTER_GLOBAL("target.ListTargetKinds").set_body_typed(TargetKindRegEntry::ListTargetKinds);
409408
TVM_REGISTER_GLOBAL("target.ListTargetKindOptions")
410409
.set_body_typed(TargetKindRegEntry::ListTargetKindOptions);
410+
TVM_REGISTER_GLOBAL("target.ListTargetKindOptionsFromName")
411+
.set_body_typed([](String target_kind_name) {
412+
TargetKind kind = TargetKind::Get(target_kind_name).value();
413+
return TargetKindRegEntry::ListTargetKindOptions(kind);
414+
});
411415

412416
} // namespace tvm

0 commit comments

Comments
 (0)