driazati commented on code in PR #12107:
URL: https://github.com/apache/tvm/pull/12107#discussion_r922394353
##########
python/tvm/target/virtual_device.py:
##########
@@ -27,7 +27,9 @@ class VirtualDevice(Object):
"""A compile time representation for where data is to be stored at runtime,
and how to compile code to compute it."""
- def __init__(self, device, target=None, memory_scope="") -> None:
+ def __init__(self, device=None, target=None, memory_scope="") -> None:
+ if device is None:
+ device = tvm.device(-1, -1)
Review Comment:
does `device(-1, -1)` mean anything special or is it just a stand in for
`kInvalidDeviceType`?
##########
src/target/compilation_config.cc:
##########
@@ -98,19 +98,30 @@ Target CompilationConfigNode::CanonicalTarget(const Target&
target) const {
VirtualDevice CompilationConfigNode::CanonicalVirtualDevice(
const VirtualDevice& virtual_device) const {
- DLDeviceType device_type = virtual_device->device_type();
+ // Targets need special handling.
Target target = virtual_device->target;
if (target.defined()) {
- target = CanonicalTarget(target);
- } else {
- // Find the (unique) target matching the device's device type.
- // TODO(mbs): Proper diagnostics.
- CHECK(device_type != kInvalidDeviceType)
- << "VirtualDevice annotations must include at least a device_type";
- target = FindPrimitiveTargetForDeviceOrFail(device_type);
+ // It is possible the given target object was constructed by the user, but
was then
+ // rewritten on the way into the CompilationConfig. So 'canonicalize' it
by replacing
+ // the given target with one structurally equal to one already known in
the config if
+ // possible.
+ Target canon_target = CanonicalTarget(target);
+ if (canon_target != target) {
+ VLOG(1) << "Canonicalized target " << canon_target->ToDebugString();
+ }
+ target = canon_target;
+ } else if (virtual_device->device_type() != kInvalidDeviceType) {
+ // Since no target was given, choose one with a matching device type.
+ // This is the one place where we allow device types to imply targets.
+ target = FindPrimitiveTargetForDeviceOrFail(virtual_device->device_type());
+ VLOG(1) << "Defaulted to target " << target->ToDebugString();
}
- return virtual_device_cache_.Unique(VirtualDevice(device_type,
virtual_device->virtual_device_id,
- target,
virtual_device->memory_scope));
+ // else: the target will remain unknown.
Review Comment:
I don't know the code around these parts to well but just wondering with
this change is it possible to get a virtual device that has neither a memory
scope or a concrete device type?
--
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]