Skip to content

Commit 47dc9d1

Browse files
LukeBoyertensorflower-gardener
authored andcommitted
Cleanup
* Make dyn loading wrapper return lrt status * Use lrt logging instead of cerr * Remove need for LogSeverity:: prefix * Unify names in QnnManager PiperOrigin-RevId: 682997204
1 parent 5b31d46 commit 47dc9d1

File tree

10 files changed

+110
-111
lines changed

10 files changed

+110
-111
lines changed

tensorflow/lite/experimental/lrt/core/BUILD

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ cc_library(
128128
hdrs = ["dynamic_loading.h"],
129129
linkopts = ["-ldl"],
130130
deps = [
131+
":logging",
131132
"//tensorflow/lite/experimental/lrt/c:lite_rt_c_api",
132133
"@com_google_absl//absl/strings:string_view",
133134
],
@@ -220,8 +221,5 @@ cc_library(
220221
hdrs = [
221222
"logging.h",
222223
],
223-
deps = [
224-
"//tensorflow/lite:minimal_logging",
225-
"//tensorflow/lite/core:framework_stable",
226-
],
224+
deps = ["//tensorflow/lite:minimal_logging"],
227225
)

tensorflow/lite/experimental/lrt/core/dynamic_loading.cc

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,31 @@
1818
#include <link.h>
1919

2020
#include <iostream>
21+
#include <sstream>
2122
#include <string>
2223

2324
#include "absl/strings/string_view.h"
25+
#include "tensorflow/lite/experimental/lrt/c/lite_rt_common.h"
26+
#include "tensorflow/lite/experimental/lrt/core/logging.h"
2427

2528
namespace lrt {
2629

27-
void* OpenLib(absl::string_view so_path) {
28-
void* lib_handle =
29-
::dlopen(so_path.data(), RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND);
30-
if (lib_handle == nullptr) {
31-
std::cerr << "Failed to load so at path: " << so_path
32-
<< " with err: " << ::dlerror() << "\n";
30+
// TODO make this return a status and have handle be output param.
31+
LrtStatus OpenLib(absl::string_view so_path, void** lib_handle) {
32+
void* res = ::dlopen(so_path.data(), RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND);
33+
if (res == nullptr) {
34+
LITE_RT_LOG(ERROR, "Failed to load .so at path: %s, with error: %s",
35+
so_path, ::dlerror());
36+
37+
return kLrtStatusDynamicLoadErr;
3338
}
34-
return lib_handle;
39+
*lib_handle = res;
40+
return kLrtStatusOk;
3541
}
3642

3743
void DumpLibInfo(void* lib_handle) {
38-
std::cerr << "--- Lib Info ---\n";
44+
std::stringstream dump;
45+
dump << "--- Lib Info ---\n";
3946

4047
Lmid_t dl_ns_idx;
4148
if (0 != ::dlinfo(lib_handle, RTLD_DI_LMID, &dl_ns_idx)) {
@@ -53,25 +60,27 @@ void DumpLibInfo(void* lib_handle) {
5360
return;
5461
}
5562

56-
std::cerr << "Lib Namespace: " << dl_ns_idx << "\n";
57-
std::cerr << "Lib Origin: " << dl_origin << "\n";
63+
dump << "Lib Namespace: " << dl_ns_idx << "\n";
64+
dump << "Lib Origin: " << dl_origin << "\n";
5865

59-
std::cerr << "loaded objects:\n";
66+
dump << "loaded objects:\n";
6067

6168
auto* forward = lm->l_next;
6269
auto* backward = lm->l_prev;
6370

6471
while (forward != nullptr) {
65-
std::cerr << " " << forward->l_name << "\n";
72+
dump << " " << forward->l_name << "\n";
6673
forward = forward->l_next;
6774
}
6875

69-
std::cerr << "***" << lm->l_name << "\n";
76+
dump << "***" << lm->l_name << "\n";
7077

7178
while (backward != nullptr) {
72-
std::cerr << " " << backward->l_name << "\n";
79+
dump << " " << backward->l_name << "\n";
7380
backward = backward->l_prev;
7481
}
82+
83+
LITE_RT_LOG(INFO, "%s", dump.str().c_str());
7584
}
7685

7786
} // namespace lrt

tensorflow/lite/experimental/lrt/core/dynamic_loading.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,31 @@
1818
#include <dlfcn.h>
1919
#include <stdlib.h>
2020

21-
#include <iostream>
22-
2321
#include "absl/strings/string_view.h"
22+
#include "tensorflow/lite/experimental/lrt/c/lite_rt_common.h"
23+
#include "tensorflow/lite/experimental/lrt/core/logging.h"
2424

2525
namespace lrt {
2626

27-
// Loads shared library at given path, returning handle on success
28-
// and nullptr on failure.
29-
void* OpenLib(absl::string_view so_path);
27+
// Loads shared library at given path.
28+
LrtStatus OpenLib(absl::string_view so_path, void** lib_handle);
3029

3130
// Dumps loading details of given lib handle.
3231
void DumpLibInfo(void* lib_handle);
3332

34-
// Resolves a named symbol from given lib handle of type Sym. Returns
35-
// nullptr on failure.
33+
// Resolves a named symbol from given lib handle of type Sym.
3634
template <class Sym>
37-
inline static Sym ResolveLibSymbol(void* lib_handle,
38-
absl::string_view sym_name) {
35+
inline static LrtStatus ResolveLibSymbol(void* lib_handle,
36+
absl::string_view sym_name,
37+
Sym* sym_handle) {
3938
Sym ptr = (Sym)::dlsym(lib_handle, sym_name.data());
4039
if (ptr == nullptr) {
41-
std::cerr << "Failed to resolve symbol: " << sym_name << " with err "
42-
<< ::dlerror() << "\n";
40+
LITE_RT_LOG(ERROR, "Faild to resolve symbol: %s, with err: %s\n", sym_name,
41+
::dlerror());
42+
return kLrtStatusDynamicLoadErr;
4343
}
44-
return ptr;
44+
*sym_handle = ptr;
45+
return kLrtStatusOk;
4546
}
4647

4748
} // namespace lrt

tensorflow/lite/experimental/lrt/core/lite_rt_event.cc

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
#include "tensorflow/lite/experimental/lrt/c/lite_rt_common.h"
2727
#include "tensorflow/lite/experimental/lrt/core/logging.h"
2828

29-
using lrt::internal::LogSeverity;
30-
3129
struct LrtEventT {
3230
#if LRT_HAS_SYNC_FENCE_SUPPORT
3331
int fd;
@@ -63,22 +61,20 @@ LrtStatus LrtEventWait(LrtEvent event, int64_t timeout_in_ms) {
6361
if (ret == 1) {
6462
break;
6563
} else if (ret == 0) {
66-
LITE_RT_LOG(LogSeverity::WARNING, "Timeout expired: %d", timeout_in_ms);
64+
LITE_RT_LOG(WARNING, "Timeout expired: %d", timeout_in_ms);
6765
return kLrtStatusErrorTimeoutExpired;
6866
}
6967
} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
7068

7169
if (ret < 0) {
72-
LITE_RT_LOG(LogSeverity::ERROR, "Error waiting for fence: %s",
73-
::strerror(errno));
70+
LITE_RT_LOG(ERROR, "Error waiting for fence: %s", ::strerror(errno));
7471
return kLrtStatusErrorRuntimeFailure;
7572
}
7673

7774
return kLrtStatusOk;
7875

7976
#else
80-
LITE_RT_LOG(LogSeverity::ERROR,
81-
"LrtEventWait not implemented for this platform");
77+
LITE_RT_LOG(ERROR, "LrtEventWait not implemented for this platform");
8278
return kLrtStatusErrorUnsupported;
8379
#endif
8480
}
@@ -97,8 +93,7 @@ LrtStatus LrtEventDestroy(LrtEvent event) {
9793
delete event;
9894
return kLrtStatusOk;
9995
#else
100-
LITE_RT_LOG(LogSeverity::ERROR,
101-
"LrtEventDestroy not implemented for this platform");
96+
LITE_RT_LOG(ERROR, "LrtEventDestroy not implemented for this platform");
10297
return kLrtStatusErrorUnsupported;
10398
#endif
10499
}

tensorflow/lite/experimental/lrt/core/lite_rt_tensor_buffer.cc

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
#include "tensorflow/lite/experimental/lrt/core/logging.h"
2727
#include "tensorflow/lite/experimental/lrt/core/tensor_buffer.h"
2828

29-
using lrt::internal::LogSeverity;
30-
3129
LrtStatus LrtCreateTensorBufferFromHostMemory(
3230
LrtRankedTensorType tensor_type, void* host_buffer_addr, size_t size,
3331
LrtHostMemoryDeallocator deallocator, LrtTensorBuffer* buffer) {
@@ -36,7 +34,7 @@ LrtStatus LrtCreateTensorBufferFromHostMemory(
3634
absl::MakeSpan(static_cast<uint8_t*>(host_buffer_addr), size),
3735
deallocator);
3836
if (!tensor_buffer.ok()) {
39-
LITE_RT_LOG(LogSeverity::ERROR, "%s", tensor_buffer.status().message());
37+
LITE_RT_LOG(ERROR, "%s", tensor_buffer.status().message());
4038
return kLrtStatusErrorRuntimeFailure;
4139
}
4240
*buffer = tensor_buffer->release();
@@ -55,7 +53,7 @@ LrtStatus LrtCreateTensorBufferFromAhwb(LrtRankedTensorType tensor_type,
5553
auto tensor_buffer = LrtTensorBufferT::CreateFromAhwb(
5654
tensor_type, ahwb, ahwb_offset, deallocator);
5755
if (!tensor_buffer.ok()) {
58-
LITE_RT_LOG(LogSeverity::ERROR, "%s", tensor_buffer.status().message());
56+
LITE_RT_LOG(ERROR, "%s", tensor_buffer.status().message());
5957
return kLrtStatusErrorRuntimeFailure;
6058
}
6159
*buffer = tensor_buffer->release();
@@ -70,7 +68,7 @@ LrtStatus LrtGetTensorBufferAhwb(LrtTensorBuffer buffer,
7068

7169
auto ahwb_buffer = buffer->GetAhwbBuffer();
7270
if (!ahwb_buffer.ok()) {
73-
LITE_RT_LOG(LogSeverity::ERROR, "%s", ahwb_buffer.status().message());
71+
LITE_RT_LOG(ERROR, "%s", ahwb_buffer.status().message());
7472
return kLrtStatusErrorRuntimeFailure;
7573
}
7674

@@ -91,7 +89,7 @@ LrtStatus LrtCreateTensorBufferFromIonBuffer(
9189
tensor_type, ion_buffer_addr, ion_buffer_fd, ion_buffer_size,
9290
ion_buffer_offset, deallocator);
9391
if (!tensor_buffer.ok()) {
94-
LITE_RT_LOG(LogSeverity::ERROR, "%s", tensor_buffer.status().message());
92+
LITE_RT_LOG(ERROR, "%s", tensor_buffer.status().message());
9593
return kLrtStatusErrorRuntimeFailure;
9694
}
9795
*buffer = tensor_buffer->release();
@@ -107,7 +105,7 @@ LrtStatus LrtGetTensorBufferIonBuffer(LrtTensorBuffer buffer,
107105

108106
auto ion_buffer = buffer->GetIonBuffer();
109107
if (!ion_buffer.ok()) {
110-
LITE_RT_LOG(LogSeverity::ERROR, "%s", ion_buffer.status().message());
108+
LITE_RT_LOG(ERROR, "%s", ion_buffer.status().message());
111109
return kLrtStatusErrorRuntimeFailure;
112110
}
113111

@@ -130,7 +128,7 @@ LrtStatus LrtCreateTensorBufferFromDmaBufBuffer(
130128
tensor_type, dmabuf_buffer_addr, dmabuf_buffer_fd, dmabuf_buffer_size,
131129
dmabuf_buffer_offset, deallocator);
132130
if (!tensor_buffer.ok()) {
133-
LITE_RT_LOG(LogSeverity::ERROR, "%s", tensor_buffer.status().message());
131+
LITE_RT_LOG(ERROR, "%s", tensor_buffer.status().message());
134132
return kLrtStatusErrorRuntimeFailure;
135133
}
136134
*buffer = tensor_buffer->release();
@@ -146,7 +144,7 @@ LrtStatus LrtGetTensorBufferDmaBufBuffer(LrtTensorBuffer buffer,
146144

147145
auto dmabuf_buffer = buffer->GetDmaBufBuffer();
148146
if (!dmabuf_buffer.ok()) {
149-
LITE_RT_LOG(LogSeverity::ERROR, "%s", dmabuf_buffer.status().message());
147+
LITE_RT_LOG(ERROR, "%s", dmabuf_buffer.status().message());
150148
return kLrtStatusErrorRuntimeFailure;
151149
}
152150

@@ -169,7 +167,7 @@ LrtStatus LrtCreateTensorBufferFromFastRpcBuffer(
169167
tensor_type, fastrpc_buffer_addr, fastrpc_buffer_fd, fastrpc_buffer_size,
170168
fastrpc_buffer_offset, deallocator);
171169
if (!tensor_buffer.ok()) {
172-
LITE_RT_LOG(LogSeverity::ERROR, "%s", tensor_buffer.status().message());
170+
LITE_RT_LOG(ERROR, "%s", tensor_buffer.status().message());
173171
return kLrtStatusErrorRuntimeFailure;
174172
}
175173
*buffer = tensor_buffer->release();
@@ -185,7 +183,7 @@ LrtStatus LrtGetTensorBufferFastRpcBuffer(LrtTensorBuffer buffer,
185183

186184
auto fastrpc_buffer = buffer->GetFastRpcBuffer();
187185
if (!fastrpc_buffer.ok()) {
188-
LITE_RT_LOG(LogSeverity::ERROR, "%s", fastrpc_buffer.status().message());
186+
LITE_RT_LOG(ERROR, "%s", fastrpc_buffer.status().message());
189187
return kLrtStatusErrorRuntimeFailure;
190188
}
191189

@@ -202,7 +200,7 @@ LrtStatus LrtCreateManagedTensorBuffer(LrtTensorBufferType buffer_type,
202200
auto tensor_buffer =
203201
LrtTensorBufferT::CreateManaged(buffer_type, tensor_type, buffer_size);
204202
if (!tensor_buffer.ok()) {
205-
LITE_RT_LOG(LogSeverity::ERROR, "%s", tensor_buffer.status().message());
203+
LITE_RT_LOG(ERROR, "%s", tensor_buffer.status().message());
206204
return kLrtStatusErrorRuntimeFailure;
207205
}
208206
*buffer = tensor_buffer->release();
@@ -252,7 +250,7 @@ LrtStatus LrtGetTensorBufferHostMemory(LrtTensorBuffer buffer,
252250

253251
auto host_buffer = buffer->GetHostBuffer();
254252
if (!host_buffer.ok()) {
255-
LITE_RT_LOG(LogSeverity::ERROR, "%s", host_buffer.status().message());
253+
LITE_RT_LOG(ERROR, "%s", host_buffer.status().message());
256254
return kLrtStatusErrorRuntimeFailure;
257255
}
258256

@@ -268,7 +266,7 @@ LrtStatus LrtLockTensorBuffer(LrtTensorBuffer buffer, void** host_mem_addr,
268266

269267
auto mapped_addr = buffer->Lock(event);
270268
if (!mapped_addr.ok()) {
271-
LITE_RT_LOG(LogSeverity::ERROR, "%s", mapped_addr.status().message());
269+
LITE_RT_LOG(ERROR, "%s", mapped_addr.status().message());
272270
return kLrtStatusErrorRuntimeFailure;
273271
}
274272

@@ -282,7 +280,7 @@ LrtStatus LrtUnlockTensorBuffer(LrtTensorBuffer buffer) {
282280
}
283281

284282
if (auto status = buffer->Unlock(); !status.ok()) {
285-
LITE_RT_LOG(LogSeverity::ERROR, "%s", status.message());
283+
LITE_RT_LOG(ERROR, "%s", status.message());
286284
return kLrtStatusErrorRuntimeFailure;
287285
}
288286

tensorflow/lite/experimental/lrt/core/logging.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ namespace lrt {
2121
namespace internal {
2222

2323
enum class LogSeverity {
24-
VERBOSE = 0,
25-
INFO = 1,
26-
WARNING = 2,
27-
ERROR = 3,
28-
SILENT = 4
24+
kVerbose = 0,
25+
kInfo = 1,
26+
kWarning = 2,
27+
kError = 3,
28+
kSilent = 4
2929
};
3030

31+
#define VERBOSE ::lrt::internal::LogSeverity::kVerbose
32+
#define INFO ::lrt::internal::LogSeverity::kInfo
33+
#define WARNING ::lrt::internal::LogSeverity::kWarning
34+
#define ERROR ::lrt::internal::LogSeverity::kError
35+
#define SILENT ::lrt::internal::LogSeverity::kSilent
36+
3137
class Logger {
3238
public:
3339
static void Log(LogSeverity severity, const char* format, ...);

tensorflow/lite/experimental/lrt/qnn/qnn_compose_graph.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ LrtStatus GraphMapper::LegalizeAndRegister(LrtTensor lrt_tensor,
182182
LRT_RETURN_STATUS_IF_NOT_OK(LegalizeTensor(lrt_tensor, qnn_tensor));
183183
LRT_RETURN_STATUS_IF_NOT_OK(AssignName(qnn_tensor));
184184
LRT_RETURN_STATUS_IF_QNN_NOT_OK(
185-
Qnn().API()->tensorCreateGraphTensor(QnnGraph(), &qnn_tensor));
185+
Qnn().Api()->tensorCreateGraphTensor(QnnGraph(), &qnn_tensor));
186186

187187
return kLrtStatusOk;
188188
}
@@ -221,14 +221,14 @@ LrtStatus GraphMapper::IsLrtSubgraphSupported() {
221221

222222
LrtStatus GraphMapper::InitQnnGraph(absl::string_view qnn_graph_name) {
223223
LRT_RETURN_STATUS_IF_QNN_NOT_OK(
224-
Qnn().API()->graphCreate(Qnn().ContextHandle(), qnn_graph_name.data(),
224+
Qnn().Api()->graphCreate(Qnn().ContextHandle(), qnn_graph_name.data(),
225225
GetDefaultGraphConfigs().data(), &QnnGraph()));
226226
return kLrtStatusOk;
227227
}
228228

229229
LrtStatus GraphMapper::Finalize() {
230230
LRT_RETURN_STATUS_IF_QNN_NOT_OK(
231-
Qnn().API()->graphFinalize(QnnGraph(), nullptr, nullptr));
231+
Qnn().Api()->graphFinalize(QnnGraph(), nullptr, nullptr));
232232
return kLrtStatusOk;
233233
}
234234

@@ -300,7 +300,7 @@ LrtStatus GraphMapper::MapGraph(QnnManager& qnn, LrtSubgraph subgraph,
300300
qnn_op.v1.numOfOutputs = op_outs.size();
301301
qnn_op.v1.outputTensors = qnn_op_outs;
302302

303-
LRT_RETURN_STATUS_IF_QNN_NOT_OK(graph_mapper.Qnn().API()->graphAddNode(
303+
LRT_RETURN_STATUS_IF_QNN_NOT_OK(graph_mapper.Qnn().Api()->graphAddNode(
304304
graph_mapper.QnnGraph(), qnn_op));
305305
}
306306

tensorflow/lite/experimental/lrt/qnn_sdk/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ ungrte_cc_library_with_qnn(
3939
# copybara:uncomment "//third_party/qairt:qnn_lib_headers",
4040
"//tensorflow/lite/experimental/lrt/core:api_internal",
4141
"//tensorflow/lite/experimental/lrt/core:dynamic_loading",
42+
"//tensorflow/lite/experimental/lrt/core:logging",
4243
],
4344
)
4445

0 commit comments

Comments
 (0)