tvm
Loading...
Searching...
No Matches
memory_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 */
23#ifndef TVM_RUNTIME_MEMORY_SCOPE_H_
24#define TVM_RUNTIME_MEMORY_SCOPE_H_
25
26#include <tvm/ffi/enum.h>
27#include <tvm/ffi/reflection/enum_def.h>
28#include <tvm/runtime/base.h>
29
30#include <ostream>
31#include <string_view>
32
33namespace tvm {
34namespace runtime {
35namespace detail {
36TVM_RUNTIME_DLL void RegisterBuiltinMemoryScopes();
37} // namespace detail
38
40enum class MemoryAllocProperty : int {
41 kNormal = 0,
44};
45
46namespace attr {
48inline constexpr ffi::reflection::AttrKey<MemoryAllocProperty> kMemoryAllocProperty{
49 "runtime.memory_alloc_property"};
50} // namespace attr
51
53class MemoryScopeObj : public ffi::EnumObj {
54 public:
55 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("runtime.MemoryScope", MemoryScopeObj, ffi::EnumObj);
56};
57
65class MemoryScope : public ffi::Enum {
66 public:
68 MemoryScope() : ffi::Enum(Global()) {}
70 explicit MemoryScope(const ffi::String& name) : ffi::Enum(Get(name)) {}
72 const ffi::String& name() const { return get()->_str_index; }
74 static bool IsDefined(const ffi::String& name) {
75 return ffi::reflection::EnumDef<MemoryScopeObj>::IsDefined(name);
76 }
78 TVM_RUNTIME_DLL static MemoryScope Get(const ffi::String& name);
80 static MemoryScope Register(const ffi::String& name) {
81 namespace refl = ffi::reflection;
82 detail::RegisterBuiltinMemoryScopes();
83 TVM_FFI_CHECK(!name.empty(), ValueError) << "Memory scope name must not be empty";
84 TVM_FFI_CHECK(std::string_view(name.data(), name.size()).find('\0') == std::string_view::npos,
85 ValueError)
86 << "Memory scope name must not contain a null character";
87 refl::EnumDef<MemoryScopeObj>(name.c_str());
88 return Get(name);
89 }
92 bool operator==(const MemoryScope& other) const { return same_as(other); }
93 bool operator!=(const MemoryScope& other) const { return !same_as(other); }
94
96};
97
98inline std::ostream& operator<<(std::ostream& os, const MemoryScope& scope) {
99 return os << scope.name();
100}
101
102} // namespace runtime
103namespace ffi {
104template <>
105inline constexpr bool use_default_type_traits_v<runtime::MemoryScope> = false;
106template <>
107struct TypeTraits<runtime::MemoryScope>
108 : public ObjectRefWithFallbackTraitsBase<runtime::MemoryScope, ffi::String> {
110 return runtime::MemoryScope::Get(name);
111 }
112};
113} // namespace ffi
114} // namespace tvm
115#endif // TVM_RUNTIME_MEMORY_SCOPE_H_
#define TVM_RUNTIME_DLL
Definition base.h:73
Canonical identity of an extensible memory space.
Definition memory_scope.h:53
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("runtime.MemoryScope", MemoryScopeObj, ffi::EnumObj)
A registered memory space, independent of backend allocation policy.
Definition memory_scope.h:65
static bool IsDefined(const ffi::String &name)
Test whether a name is registered, without initializing memory scopes.
Definition memory_scope.h:74
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(MemoryScope, ffi::Enum, MemoryScopeObj)
static MemoryScope Register(const ffi::String &name)
Register a new name. Duplicate names raise ValueError.
Definition memory_scope.h:80
MemoryScope(const ffi::String &name)
Look up a registered name; never implicitly register it.
Definition memory_scope.h:70
static TVM_RUNTIME_DLL const MemoryScope & Global()
Return the canonical global singleton.
const ffi::String & name() const
Return the canonical spelling.
Definition memory_scope.h:72
static TVM_RUNTIME_DLL MemoryScope Get(const ffi::String &name)
Look up a registered name. Unknown names raise ValueError.
bool operator!=(const MemoryScope &other) const
Definition memory_scope.h:93
bool operator==(const MemoryScope &other) const
Definition memory_scope.h:92
MemoryScope()
Default to the global memory space.
Definition memory_scope.h:68
constexpr ffi::reflection::AttrKey< MemoryAllocProperty > kMemoryAllocProperty
Integer-valued MemoryAllocProperty; absence denotes kNormal.
Definition memory_scope.h:48
MemoryAllocProperty
Allocation behavior associated with a memory scope.
Definition memory_scope.h:40
std::ostream & operator<<(std::ostream &os, DLDevice dev)
Definition device_api.h:322
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
static runtime::MemoryScope ConvertFallbackValue(ffi::String name)
Definition memory_scope.h:109