/
edgetpu_c.cc
107 lines (86 loc) · 3.26 KB
/
edgetpu_c.cc
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
// Copyright 2019 Google LLC
//
// 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.
#include "tflite/public/edgetpu_c.h"
#include <cstring>
#include "port/logging.h"
#include "tflite/edgetpu_delegate_for_custom_op.h"
#include "tflite/public/edgetpu.h"
using edgetpu::DeviceType;
using edgetpu::EdgeTpuContext;
using edgetpu::EdgeTpuManager;
using DeviceOptions = EdgeTpuManager::DeviceOptions;
extern "C" {
struct edgetpu_device* edgetpu_list_devices(size_t* num_devices) {
CHECK(num_devices);
auto records = EdgeTpuManager::GetSingleton()->EnumerateEdgeTpu();
if (records.empty()) {
*num_devices = 0;
return nullptr;
}
const auto devs_size = sizeof(edgetpu_device) * records.size();
size_t size = devs_size;
for (const auto& record : records) size += record.path.size() + 1;
char* memory = new char[size];
edgetpu_device* devs = reinterpret_cast<edgetpu_device*>(memory);
char* paths = memory + devs_size;
int i = 0;
for (const auto& record : records) {
edgetpu_device* dev = &devs[i++];
dev->type = static_cast<edgetpu_device_type>(record.type);
dev->path = paths;
const auto len = record.path.size() + 1;
std::memcpy(paths, record.path.c_str(), len);
paths += len;
}
*num_devices = records.size();
return devs;
}
void edgetpu_free_devices(struct edgetpu_device* dev) {
delete[] reinterpret_cast<char*>(dev);
}
TfLiteDelegate* edgetpu_create_delegate(enum edgetpu_device_type type,
const char* name,
const struct edgetpu_option* options,
size_t num_options) {
auto* manager = EdgeTpuManager::GetSingleton();
const auto device_type = static_cast<DeviceType>(type);
std::shared_ptr<EdgeTpuContext> context;
if (num_options > 0) {
CHECK(options);
CHECK(name);
EdgeTpuManager::DeviceOptions device_options;
for (size_t i = 0; i < num_options; ++i) {
const edgetpu_option* option = &options[i];
device_options.insert({option->name, option->value});
}
context = manager->OpenDevice(device_type, name, device_options);
} else {
context = (name == nullptr) ? manager->OpenDevice(device_type)
: manager->OpenDevice(device_type, name);
}
if (!context) return nullptr;
return platforms::darwinn::tflite::CreateEdgeTpuDelegateForCustomOp(context);
}
void edgetpu_free_delegate(TfLiteDelegate* delegate) {
platforms::darwinn::tflite::FreeEdgeTpuDelegateForCustomOp(delegate);
}
void edgetpu_verbosity(int verbosity) {
EdgeTpuManager::GetSingleton()->SetVerbosity(verbosity);
}
const char* edgetpu_version() {
static auto* version =
new std::string(EdgeTpuManager::GetSingleton()->Version());
return version->c_str();
}
} // extern "C"