tvm
se_scope.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 
25 #ifndef TVM_TARGET_SE_SCOPE_H_
26 #define TVM_TARGET_SE_SCOPE_H_
27 
28 #include <tvm/ir/transform.h>
29 #include <tvm/target/target.h>
30 
31 #include <string>
32 #include <unordered_set>
33 #include <utility>
34 
35 namespace tvm {
36 
45 
161 class SEScopeNode : public AttrsNode<SEScopeNode> {
162  public:
174 
175  DLDeviceType device_type() const { return static_cast<DLDeviceType>(device_type_int); }
176 
184 
193 
200 
205  bool IsFullyUnconstrained() const {
206  return !target.defined() && device_type() == kInvalidDeviceType && virtual_device_id == -1 &&
207  memory_scope.empty();
208  }
209 
214  bool IsFullyConstrained() const {
215  return target.defined() && virtual_device_id != -1 && !memory_scope.empty();
216  }
217 
224  Device ToDevice() const {
225  ICHECK(device_type() != kInvalidDeviceType);
226  ICHECK(virtual_device_id != -1);
227  Device device;
228  device.device_type = device_type();
229  device.device_id = virtual_device_id;
230  return device;
231  }
232 
234  TVM_ATTR_FIELD(device_type_int)
235  .describe("The type of the virtual device.")
236  .set_default(kInvalidDeviceType);
237  TVM_ATTR_FIELD(virtual_device_id)
238  .describe("The device id of the virtual device.")
239  .set_default(-1);
240  TVM_ATTR_FIELD(target)
241  .describe("The target describing how to compile for the virtual device.")
242  .set_default(Target());
243  TVM_ATTR_FIELD(memory_scope)
244  .describe("The area of memory w.r.t. the virtual device where data is stored.")
245  .set_default("");
246  }
247 
248  friend class SEScope;
249 };
250 
256 class SEScope : public ObjectRef {
257  public:
269  explicit SEScope(DLDeviceType device_type = kInvalidDeviceType, int virtual_device_id = -1,
271 
273  static SEScope FullyUnconstrained();
274 
279  static SEScope ForDeviceType(DLDeviceType device_type, int virtual_device_id = -1) {
280  ICHECK_GT(device_type, 0);
281  return SEScope(device_type, virtual_device_id);
282  }
284  return ForDeviceType(static_cast<DLDeviceType>(device_type), virtual_device_id);
285  }
287  return ForDeviceType(static_cast<int>(device_type->value), virtual_device_id);
288  }
289 
291  static SEScope ForDevice(const Device& device) {
292  return ForDeviceType(device.device_type, device.device_id);
293  }
294 
296  static SEScope ForDeviceAndTarget(const Device& device, Target target) {
297  return SEScope(device.device_type, device.device_id, std::move(target));
298  }
299 
302  MemoryScope memory_scope) {
303  return SEScope(device.device_type, device.device_id, std::move(target),
304  std::move(memory_scope));
305  }
306 
312  static Optional<SEScope> Join(const SEScope& lhs, const SEScope& rhs);
313 
318  static SEScope Default(const SEScope& lhs, const SEScope& rhs);
319 
321 
322  friend class SEScopeCache; // Private implementation helper.
323 };
324 
334  public:
336  SEScope Make(DLDeviceType device_type = kInvalidDeviceType, int virtual_device_id = -1,
338 
340  SEScope Unique(const SEScope& scope);
341 
342  private:
344  std::unordered_set<SEScope, StructuralHash, StructuralEqual> cache_;
345 };
346 
347 } // namespace tvm
348 
349 #endif // TVM_TARGET_SE_SCOPE_H_
MemoryScope memory_scope
The scope of memory w.r.t. the virtual device which holds data.
Definition: se_scope.h:199
static SEScope ForDeviceType(int device_type, int virtual_device_id=-1)
Definition: se_scope.h:283
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
static SEScope ForDeviceType(DLDeviceType device_type, int virtual_device_id=-1)
Returns the SEScope for device_type and (if not -1) virtual_device_id. The target and memory scope wi...
Definition: se_scope.h:279
bool IsFullyConstrained() const
Returns true if scope is fully constrained, ie target, device id and memory scope are all specified...
Definition: se_scope.h:214
friend class SEScope
Definition: se_scope.h:248
TVM_DECLARE_ATTRS(SEScopeNode, "SEScope")
Definition: se_scope.h:233
Target target
The Target describing how to compile for the virtual device.
Definition: se_scope.h:192
static SEScope ForDeviceTargetAndMemoryScope(const Device &device, Target target, MemoryScope memory_scope)
Returns the SEScope for device, target and memory_scope.
Definition: se_scope.h:301
bool defined() const
Definition: object.h:537
Describes at compile time where data is to be stored down to the device and memory scope level...
Definition: se_scope.h:161
#define TVM_ATTR_FIELD(FieldName)
Declare an attribute field.
Definition: attrs.h:76
Reference to string objects.
Definition: string.h:129
DLDeviceType device_type() const
Definition: se_scope.h:175
A cache of SEScopes. This can be used:
Definition: se_scope.h:333
DLDevice Device
Definition: ndarray.h:43
static SEScope ForDeviceAndTarget(const Device &device, Target target)
Returns the SEScope for device and target.
Definition: se_scope.h:296
bool IsFullyUnconstrained() const
Returns true if scope is fully unconstrained, ie no target/device type, device id or memory scope is ...
Definition: se_scope.h:205
static SEScope ForDevice(const Device &device)
Returns the SEScope for device.
Definition: se_scope.h:291
Managed reference class to TargetNode.
Definition: target.h:132
int device_type_int
The DLDeviceType (represtented as an int) of the virtual device. If target is known then this will be...
Definition: se_scope.h:173
Base class of all object reference.
Definition: object.h:504
The base class of the all the Use "curiously recurring template pattern".
Definition: attrs.h:793
Managed reference class to SEScopeNode.
Definition: se_scope.h:256
Compilation target object.
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
static SEScope ForDeviceType(const Integer &device_type, int virtual_device_id=-1)
Definition: se_scope.h:286
int virtual_device_id
The device identifier for the virtual device. This must be resolved to a physical device identifier e...
Definition: se_scope.h:183
bool empty() const
Retun if the string is empty.
Definition: string.h:239
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:721
Device ToDevice() const
Returns the (virtual) Device implied by this SEScope. Both the device_type and virtual_device_must be...
Definition: se_scope.h:224
constexpr DLDeviceType kInvalidDeviceType
Definition: ndarray.h:51
Container of constant int that adds more constructors.
Definition: expr.h:356