-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathdnnl_constant_tensor_cache.hpp
108 lines (94 loc) · 3.96 KB
/
dnnl_constant_tensor_cache.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*******************************************************************************
* Copyright 2023-2024 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
#ifndef GRAPH_BACKEND_DNNL_DNNL_CONSTANT_TENSOR_CACHE_HPP
#define GRAPH_BACKEND_DNNL_DNNL_CONSTANT_TENSOR_CACHE_HPP
#include "graph/interface/constant_tensor_cache.hpp"
#include "graph/backend/dnnl/common.hpp"
#include "graph/backend/dnnl/dnnl_backend.hpp"
#include "oneapi/dnnl/dnnl.hpp"
namespace dnnl {
namespace impl {
namespace graph {
namespace dnnl_impl {
struct dnnl_constant_buffer_t : public graph::constant_buffer_t {
dnnl_constant_buffer_t(
size_t size, dnnl::engine &engine, graph::allocator_t *alc)
: graph::constant_buffer_t(
size, engine.get(), alc, malloc_func, free_func) {}
static void *malloc_func(
size_t size, impl::engine_t *eng, graph::allocator_t *alc) {
dnnl::engine engine;
engine.reset(eng, true); // not own
return dnnl_allocator_t::malloc(
size, engine, alc, allocator_t::mem_type_t::persistent);
}
static void free_func(
void *data, impl::engine_t *eng, graph::allocator_t *alc) {
dnnl::engine engine;
engine.reset(eng, true); // not own
if (eng->kind() == engine_kind::cpu) {
#if DNNL_CPU_RUNTIME == DNNL_RUNTIME_SYCL
dnnl_allocator_t::free(data, engine, alc, ::sycl::event());
#else
dnnl_allocator_t::free(data, engine, alc);
#endif
} else if (eng->kind() == engine_kind::gpu) {
#if DNNL_GPU_RUNTIME == DNNL_RUNTIME_SYCL
dnnl_allocator_t::free(data, engine, alc, ::sycl::event());
#elif DNNL_GPU_RUNTIME == DNNL_RUNTIME_OCL
dnnl_allocator_t::free(data, engine, alc, cl_event());
#endif
}
}
};
inline graph::constant_tensor_cache_t::value_t dnnl_constant_cache_get_or_add(
const dnnl::engine &eng, graph::constant_tensor_cache_t::key_t key,
size_t size, const graph::constant_tensor_cache_t::value_t &value) {
auto cache = graph::get_constant_tensor_cache(
eng.get()->kind(), eng.get()->index());
assertm(cache,
"no available constant cache for specified engine kind and index");
return cache->get_or_add(
dnnl_backend_t::get_singleton().get_id(), key, size, value);
}
inline void dnnl_constant_cache_remove_if_exist(
const dnnl::engine &eng, graph::constant_tensor_cache_t::key_t key) {
auto cache = graph::get_constant_tensor_cache(
eng.get()->kind(), eng.get()->index());
assertm(cache,
"no available constant cache for specified engine kind and index");
cache->remove_if_exist(dnnl_backend_t::get_singleton().get_id(), key);
}
inline bool is_constant_cache_enabled(const dnnl::engine &eng) {
auto cache = graph::get_constant_tensor_cache(
eng.get()->kind(), eng.get()->index());
return cache && cache->get_capacity() != 0;
}
inline void dnnl_constant_cache_retain(const dnnl::engine &eng) {
auto cache = graph::get_constant_tensor_cache(
eng.get()->kind(), eng.get()->index());
cache->retain();
}
inline void dnnl_constant_cache_release(const dnnl::engine &eng) {
auto cache = graph::get_constant_tensor_cache(
eng.get()->kind(), eng.get()->index());
cache->release();
}
} // namespace dnnl_impl
} // namespace graph
} // namespace impl
} // namespace dnnl
#endif