tvm
virtual_device.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
26 #ifndef TVM_TARGET_VIRTUAL_DEVICE_H_
27 #define TVM_TARGET_VIRTUAL_DEVICE_H_
28 
29 #include <tvm/ir/transform.h>
30 #include <tvm/target/target.h>
31 
32 #include <string>
33 #include <unordered_set>
34 #include <utility>
35 
36 namespace tvm {
37 
46 
166 class VirtualDeviceNode : public AttrsNode<VirtualDeviceNode> {
167  private:
179  int /* actually DLDeviceType */ device_type_int;
180 
181  public:
182  DLDeviceType device_type() const { return static_cast<DLDeviceType>(device_type_int); }
183 
191 
200 
207 
212  bool IsFullyUnconstrained() const {
213  return !target.defined() && device_type() == kInvalidDeviceType && virtual_device_id == -1 &&
214  memory_scope.empty();
215  }
216 
221  bool IsFullyConstrained() const {
222  return target.defined() && virtual_device_id != -1 && !memory_scope.empty();
223  }
224 
231  Device ToDevice() const {
232  ICHECK(device_type() != kInvalidDeviceType);
233  ICHECK(virtual_device_id != -1);
234  Device device;
235  device.device_type = device_type();
236  device.device_id = virtual_device_id;
237  return device;
238  }
239 
241  TVM_ATTR_FIELD(device_type_int)
242  .describe("The type of the virtual device.")
243  .set_default(kInvalidDeviceType);
244  TVM_ATTR_FIELD(virtual_device_id)
245  .describe("The device id of the virtual device.")
246  .set_default(-1);
247  TVM_ATTR_FIELD(target)
248  .describe("The target describing how to compile for the virtual device.")
249  .set_default(Target());
250  TVM_ATTR_FIELD(memory_scope)
251  .describe("The area of memory w.r.t. the virtual device where data is stored.")
252  .set_default("");
253  }
254 
255  friend class VirtualDevice;
256 };
257 
261 class VirtualDevice : public ObjectRef {
262  public:
274  explicit VirtualDevice(DLDeviceType device_type = kInvalidDeviceType, int virtual_device_id = -1,
276 
278  static VirtualDevice FullyUnconstrained();
279 
284  static VirtualDevice ForDeviceType(DLDeviceType device_type, int virtual_device_id = -1) {
285  ICHECK_GT(device_type, 0);
286  return VirtualDevice(device_type, virtual_device_id);
287  }
289  return ForDeviceType(static_cast<DLDeviceType>(device_type), virtual_device_id);
290  }
292  return ForDeviceType(static_cast<int>(device_type->value), virtual_device_id);
293  }
294 
296  static VirtualDevice ForDevice(const Device& device) {
297  return ForDeviceType(device.device_type, device.device_id);
298  }
299 
302  return VirtualDevice(device.device_type, device.device_id, std::move(target));
303  }
304 
307  DLDeviceType device_type = static_cast<DLDeviceType>(target->kind->device_type);
308  return VirtualDevice(device_type, /*virtual_device_id=*/0, std::move(target));
309  }
310 
312  static VirtualDevice ForMemoryScope(MemoryScope memory_scope) {
313  return VirtualDevice(kInvalidDeviceType, -1, {}, std::move(memory_scope));
314  }
315 
318  MemoryScope memory_scope) {
319  return VirtualDevice(device.device_type, device.device_id, std::move(target),
320  std::move(memory_scope));
321  }
322 
328  static Optional<VirtualDevice> Join(const VirtualDevice& lhs, const VirtualDevice& rhs);
329 
334  static VirtualDevice Default(const VirtualDevice& lhs, const VirtualDevice& rhs);
335 
337 
338  friend class VirtualDeviceCache; // Private implementation helper.
339 };
340 
350  public:
352  VirtualDevice Make(DLDeviceType device_type = kInvalidDeviceType, int virtual_device_id = -1,
354 
358  VirtualDevice Unique(const VirtualDevice& virtual_device);
359 
360  private:
362  std::unordered_set<VirtualDevice, StructuralHash, StructuralEqual> cache_;
363 };
364 
370 constexpr const char* kVirtualDevice = "virtual_device";
371 
372 } // namespace tvm
373 
374 #endif // TVM_TARGET_VIRTUAL_DEVICE_H_
static VirtualDevice ForDeviceType(DLDeviceType device_type, int virtual_device_id=-1)
Returns the VirtualDevice for device_type and (if not -1) virtual_device_id. The target and memory sc...
Definition: virtual_device.h:284
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
static VirtualDevice ForMemoryScope(MemoryScope memory_scope)
Returns the VirtualDevice for memory_scope alone.
Definition: virtual_device.h:312
friend class VirtualDevice
Definition: virtual_device.h:255
A cache of VirtualDevices. This can be used:
Definition: virtual_device.h:349
int virtual_device_id
The device identifier for the virtual device. This must be resolved to a physical device identifier e...
Definition: virtual_device.h:190
TVM_DECLARE_ATTRS(VirtualDeviceNode, "VirtualDevice")
Definition: virtual_device.h:240
static VirtualDevice ForDeviceTargetAndMemoryScope(const Device &device, Target target, MemoryScope memory_scope)
Returns the VirtualDevice for device, target and memory_scope.
Definition: virtual_device.h:317
Managed reference class to VirtualDeviceNode.
Definition: virtual_device.h:261
bool defined() const
Definition: object.h:544
bool IsFullyConstrained() const
Returns true if virtual device is &#39;fully constrained&#39;, ie target, device id and memory scope are all ...
Definition: virtual_device.h:221
static VirtualDevice ForDeviceType(const Integer &device_type, int virtual_device_id=-1)
Definition: virtual_device.h:291
MemoryScope memory_scope
The scope of memory w.r.t. the virtual device which holds data.
Definition: virtual_device.h:206
#define TVM_ATTR_FIELD(FieldName)
Declare an attribute field.
Definition: attrs.h:76
Reference to string objects.
Definition: string.h:124
DLDevice Device
Definition: ndarray.h:43
Describes at compile time the constraints on where data is to be stored at runtime down to the (virtu...
Definition: virtual_device.h:166
Managed reference class to TargetNode.
Definition: target.h:141
static VirtualDevice ForDeviceType(int device_type, int virtual_device_id=-1)
Definition: virtual_device.h:288
Base class of all object reference.
Definition: object.h:511
static VirtualDevice ForDevice(const Device &device)
Returns the VirtualDevice for device.
Definition: virtual_device.h:296
Target target
The Target describing how to compile for the virtual device.
Definition: virtual_device.h:199
The base class of the all the Use "curiously recurring template pattern".
Definition: attrs.h:834
constexpr const char * kVirtualDevice
Definition: virtual_device.h:370
Compilation target object.
DLDeviceType device_type() const
Definition: virtual_device.h:182
static VirtualDevice ForTarget(Target target)
Returns the VirtualDevice for target.
Definition: virtual_device.h:306
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
bool empty() const
Retun if the string is empty.
Definition: string.h:234
static VirtualDevice ForDeviceAndTarget(const Device &device, Target target)
Returns the VirtualDevice for device and target.
Definition: virtual_device.h:301
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:728
bool IsFullyUnconstrained() const
Returns true if virtual device is &#39;fully unconstrained&#39;, ie no target/device type, device id or memory scope is specified.
Definition: virtual_device.h:212
constexpr DLDeviceType kInvalidDeviceType
Definition: ndarray.h:51
Container of constant int that adds more constructors.
Definition: expr.h:404
Device ToDevice() const
Returns the (virtual) Device implied by this VirtualDevice. Both the device_type and virtual_device_m...
Definition: virtual_device.h:231