File: onnxifi_graph_info.cc

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (55 lines) | stat: -rw-r--r-- 1,743 bytes parent folder | download | duplicates (2)
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
#include "caffe2/onnx/onnxifi_graph_info.h"
#include "caffe2/core/logging.h"

namespace caffe2 {
namespace onnx {

SharedPtrBackendGraphInfo OnnxBackendGraphMap::lookup(const std::string& key) {
  std::lock_guard<std::mutex> guard(backend_graph_map_lock_);
  auto it = backend_graph_map_.find(key);
  if (it != backend_graph_map_.end()) {
    return it->second;
  }
  return nullptr;
}

SharedPtrBackendGraphInfo OnnxBackendGraphMap::insert(
    const std::string& key,
    std::function<SharedPtrBackendGraphInfo()> creator) {
  // First acquire lock.
  std::lock_guard<std::mutex> guard(backend_graph_map_lock_);
  // Then check if the backend_graph_info already exists in the map.
  if (backend_graph_map_.find(key) != backend_graph_map_.end()) {
    LOG(INFO) << "Reusing onnxifi backend for: " << key;
    // If it already exists, return it.
    // The onus is on the caller to release onnxGraph pointed by
    // backend_graph_info
    return backend_graph_map_[key];
  }
  LOG(INFO) << "Creating onnxifi backend for: " << key;
  const auto ret_pair = backend_graph_map_.emplace(key, creator());
  return ret_pair.first->second;
}

void OnnxBackendGraphMap::remove(const std::string& key) {
  SharedPtrBackendGraphInfo tmp;
  {
    std::lock_guard<std::mutex> guard(backend_graph_map_lock_);
    auto it = backend_graph_map_.find(key);
    if (it != backend_graph_map_.end()) {
      if (it->second.unique()) {
        LOG(INFO) << "Removing onnxifi backend for " << key;
        tmp = it->second;
        backend_graph_map_.erase(it);
      }
    }
  }
}

OnnxBackendGraphMap* getOnnxBackendGraphMap() {
  static OnnxBackendGraphMap onnx_backend_graph_map;
  return &onnx_backend_graph_map;
}

} // namespace onnx
} // namespace caffe2