mbs-octoml commented on code in PR #11382:
URL: https://github.com/apache/tvm/pull/11382#discussion_r878877868


##########
python/tvm/target/target.py:
##########
@@ -233,86 +233,95 @@ def canonicalize_target(target):
         return Target(target)
 
     @staticmethod
-    def canonicalize_multi_targets(multi_targets):
-        """Given a single or collection of target-like objects, returns a TVM 
Array of Target
-        objects representing then. Can convert from:
+    def canon_target_and_host(target, target_host=None):
+        """Returns a TVM Target capturing target and target_host. Also returns 
the host in
+        canonical form. The given target can be in any form recognized by
+        Target.canon_target. If given, target_host can be in any form 
recognized by
+        Target.canon_target. If target_host is given it will be set as the 
'host' in the
+        result Target object (and a warning given).
+
+        Note that this method does not support heterogeneous compilation 
targets.
+        """
+        target = Target.canon_target(target)
+        target_host = Target.canon_target(target_host)
+        if target is None:
+            assert target_host is None, "Target host is not empty when target 
is empty."
+        if target_host is not None:
+            warnings.warn(
+                "target_host parameter is going to be deprecated. "
+                "Please pass in tvm.target.Target(target, host=target_host) 
instead."
+            )
+            target = Target(target, target_host)
+        if target is not None:
+            target_host = target.host
+        return target, target_host
+
+    @staticmethod
+    def canon_multi_target(multi_targets):
+        """Given a single target-like object, or a collection-like object of 
target-like objects,
+        returns a TVM Array of TVM Target objects representing then. Can 
convert from:
         - None (to None).
-        - A single target-like object in a form recognized by 
canonicalize_target.
+        - A single target-like object in a form recognized by canon_target.
         - A Python list or TVM Array of target-like objects in a form 
recognized by
-        canonicalize_target.
+        canon_target.
         - A Python dict or TVM Map from TVM IntImm objects representing device 
types to
-        a target-like object in a form recognized by canonicalize_target.
+        a target-like object in a form recognized by canon_target. (This is a 
legacy
+        method to represent heterogeneous targets. The keys are ignored.)
         """
         if multi_targets is None:
             return None
         if isinstance(multi_targets, (dict, Map)) and "kind" not in 
multi_targets:
             # Convert legacy heterogeneous map representation to ordinary list 
of targets.
-            return Target.canonicalize_multi_targets([t for _, t in 
multi_targets.items()])
+            return Target.canon_multi_target([t for _, t in 
multi_targets.items()])
         if isinstance(multi_targets, (list, Array)):
             # Multiple Target results.
-            return convert([Target.canonicalize_target(t) for t in 
multi_targets])
+            return convert([Target.canon_target(t) for t in multi_targets])
         # Single Target result.
-        return convert([Target.canonicalize_target(multi_targets)])
+        return convert([Target.canon_target(multi_targets)])
 
     @staticmethod
-    def canonicalize_target_and_host(target, target_host=None):
+    def canon_multi_target_and_host(target, target_host=None):
         """Returns a TVM Array<Target> capturing target and target_host. The 
given target can be in
-        any form recognized by Target.canonicalize_target or 
Target.canonicalize_multi_targets. If
-        given target_host can be in any form recognized by 
Target.canonicalize_target. If
-        target_host is given it will be set as the 'host' in each result 
Target object (and a
-        warning given).
+        any form recognized by Target.canon_multi_target. If given, 
target_host can be in
+        any form recognized by Target.canon_target. If target_host is given it 
will be set
+        as the 'host' in each result Target object (and a warning given).
         """
         # Convert target to Array<Target>, but not yet accounting for any host.
-        raw_targets = Target.canonicalize_multi_targets(target)
+        raw_targets = Target.canon_multi_target(target)
         assert raw_targets is not None
         # Convert host to Target, if given.
-        target_host = Target.canonicalize_target(target_host)
-        if target_host is None:
-            return raw_targets
-        warnings.warn(
-            "target_host parameter is going to be deprecated. "
-            "Please pass in tvm.target.Target(target, host=target_host) 
instead."
-        )
-        # Make sure the (canonical) host is captured in all the (canonical) 
targets.
-        return convert([Target(t, target_host) for t in raw_targets])
+        target_host = Target.canon_target(target_host)
+        if target_host is not None:
+            warnings.warn(
+                "target_host parameter is going to be deprecated. "
+                "Please pass in tvm.target.Target(target, host=target_host) 
instead."
+            )
+            # Make sure the (canonical) host is captured in all the 
(canonical) targets.
+            raw_targets = convert([Target(t, target_host) for t in 
raw_targets])
+        return raw_targets
 
     @staticmethod
-    def check_and_update_host_consist(target, host=None, 
target_is_dict_key=True):
-        """A helper function that merges a legacy "target, target_host" pair, 
then returns
-        the merged target and its host field. The function is for legacy 
target and target
-        host pair only, and should not be used in the new target system.
-
-        Parameters
-        ----------
-        target : Union[str, Dict[str, Any], Target]
-            The target or heterogeneous target
-        host : Union[str, Dict[str, Any], Target, None]
-            The target host
-        target_is_dict_key : Bool
-            When the type of target is dict, whether Target is the key 
(Otherwise the value)
-        """
-        if isinstance(target, (dict, str)):
-            target = convert(target)
-        if isinstance(host, (dict, str)):
-            host = convert(host)
-        if target is None:
-            assert host is None, "Target host is not empty when target is 
empty."
-            return target, host
-        if isinstance(target, Map) and "kind" not in target:
-            new_target = {}
-            for tgt, mod in target.items():
-                if not target_is_dict_key:
-                    tgt, mod = mod, tgt
-                if isinstance(tgt, (Map, String, Target)):
-                    tgt, host = Target.check_and_update_host_consist(tgt, host)
-                if not target_is_dict_key:
-                    tgt, mod = mod, tgt
-                new_target[tgt] = mod
-            target = new_target
-        else:
-            target = Target(target, host)
-            host = target.host
-        return target, host
+    def canon_target_map_and_host(target_map, target_host=None):
+        """Returns target_map as a map from TVM Target's in canonical form to 
IRModules. The keys
+        of the input target_map can be in any form recognized by 
Target.canon_target.
+        Similarly, if given, target_host can be in any form recognized by
+        Target.canon_target. The final target_map keys will capture the 
target_host in
+        canonical form. Also returns the target_host in canonical form."""
+        new_target_map = {}
+        if target_host is not None:
+            warnings.warn(
+                "target_host parameter is going to be deprecated. "
+                "Please pass in tvm.target.Target(target, host=target_host) 
instead."
+            )
+            target_host = Target.canon_target(target_host)
+        for tgt, mod in target_map.items():
+            tgt = Target.canon_target(tgt)
+            assert tgt is not None
+            if target_host is not None:
+                tgt = Target(tgt, target_host)
+            target_host = tgt.host

Review Comment:
   Actually I miss-spoke. The original code was also trying to handle the case 
when the first target already has a host, in which case it is copied into the 
rest. I'll preserve that behavior, fragile as it may be. Similarly, 
canon_target_and_host will return the host target even when it has already been 
provided in the target argument.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to