xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/tf2tensorrt/convert/timing_cache.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7 http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #if GOOGLE_CUDA && GOOGLE_TENSORRT
16 
17 #include "tensorflow/compiler/tf2tensorrt/convert/timing_cache.h"
18 
19 #include <unordered_map>
20 
21 #include "tensorflow/compiler/tf2tensorrt/common/utils.h"
22 #include "tensorflow/core/platform/errors.h"
23 #include "third_party/tensorrt/NvInfer.h"
24 
25 namespace tensorflow {
26 namespace tensorrt {
27 namespace convert {
28 
LookUp(const string & name,nvinfer1::IBuilderConfig * builder_config)29 StatusOr<TimingCacheRegistry::TimingCachePtr> TimingCacheRegistry::LookUp(
30     const string& name, nvinfer1::IBuilderConfig* builder_config) {
31 #if IS_TRT_VERSION_GE(8, 0, 0, 0)
32   TRT_ENSURE(builder_config != nullptr);
33   mutex_lock scoped_lock(mu_);
34   if (map_.find(name) != map_.end()) {
35     const std::vector<uint8_t>& data = map_[name];
36     return std::unique_ptr<nvinfer1::ITimingCache>(
37         builder_config->createTimingCache(data.data(), data.size()));
38   }
39 
40   // If no such timing cache exists, create a new timing cache.
41   return std::unique_ptr<nvinfer1::ITimingCache>(
42       builder_config->createTimingCache(nullptr, 0));
43 #endif  // IS_TRT_VERSION_GE(8, 0, 0, 0)
44   return errors::Unimplemented(
45       "serializable timing cache does not exist in TensorRT versions < 8.0");
46 }
47 
Upsert(const string & name,TimingCache * cache)48 void TimingCacheRegistry::Upsert(const string& name, TimingCache* cache) {
49 #if IS_TRT_VERSION_GE(8, 0, 0, 0)
50   nvinfer1::IHostMemory* memory = cache->serialize();
51   if (memory == nullptr) {
52     return;
53   }
54 
55   if (map_.find(name) == map_.end()) {
56     // If the timing cache with the given name does not exist, emplace the
57     // serialized buffer.
58     std::vector<uint8_t> mem(memory->size());
59     std::copy_n(static_cast<uint8_t*>(memory->data()), memory->size(),
60                 mem.begin());
61     {
62       mutex_lock scoped_lock(mu_);
63       map_.emplace(name, std::move(mem));
64     }
65   } else {
66     // If the timing cache does exist, use the existing buffer.
67     mutex_lock scoped_lock(mu_);
68     std::vector<uint8_t>& mem = map_[name];
69     mem.resize(memory->size());
70     std::copy_n(static_cast<uint8_t*>(memory->data()), memory->size(),
71                 mem.begin());
72   }
73   memory->destroy();
74 #endif  // IS_TRT_VERSION_GE(8, 0, 0, 0)
75 }
76 
GetTimingCacheRegistry()77 TimingCacheRegistry* GetTimingCacheRegistry() {
78   static TimingCacheRegistry* registry = new TimingCacheRegistry();
79   return registry;
80 }
81 
82 }  // namespace convert
83 }  // namespace tensorrt
84 }  // namespace tensorflow
85 
86 #endif  // GOOGLE_CUDA && GOOGLE_TENSORRT
87