Skip to content

Commit

Permalink
Update apply plugin tool to load and select from multiple shared lib …
Browse files Browse the repository at this point in the history
…files. Move apply plugin tool into tools/ and general cleanup.

PiperOrigin-RevId: 686729742
  • Loading branch information
LukeBoyer authored and tensorflower-gardener committed Oct 17, 2024
1 parent 32ee73e commit ec8f074
Show file tree
Hide file tree
Showing 34 changed files with 1,136 additions and 336 deletions.
16 changes: 0 additions & 16 deletions tensorflow/lite/experimental/lrt/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,3 @@ package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
default_visibility = ["//tensorflow/lite/experimental/lrt:__subpackages__"],
)

cc_binary(
name = "apply_plugin",
srcs = [
"apply_plugin.cc",
# TODO: b/366821557 - Support pre-compiled plugins as data dependencies.
"//tensorflow/lite/experimental/lrt/vendors/examples:example_plugin_so",
],
deps = [
"//tensorflow/lite/experimental/lrt/core:api_internal",
"//tensorflow/lite/experimental/lrt/core:lite_rt_model_init",
"//tensorflow/lite/experimental/lrt/core:model",
"//tensorflow/lite/experimental/lrt/core/compiler_plugin:algo",
"@llvm-project//llvm:Support",
],
)
204 changes: 0 additions & 204 deletions tensorflow/lite/experimental/lrt/apply_plugin.cc

This file was deleted.

17 changes: 10 additions & 7 deletions tensorflow/lite/experimental/lrt/c/lite_rt_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,20 @@ typedef enum {
kLrtStatusErrorTimeoutExpired = 7,

// File and loading related errors.
kLrtStatusBadFileOp = 500,
kLrtStatusFlatbufferFailedVerify = 501,
kLrtStatusDynamicLoadErr = 502,
kLrtStatusErrorFileIO = 500,
kLrtStatusErrorInvalidFlatbuffer = 501,
kLrtStatusErrorDynamicLoading = 502,
kLrtStatusSerializationErr = 503,
kLrtStatusCompilationError = 504,

// IR related errors.
kLrtStatusParamIndexOOB = 1000,
kLrtStatusBadTensorType = 1001,
kLrtStatusGraphInvariantError = 1002,
kLrtStatusErrorIndexOOB = 1000,
kLrtStatusErrorInvalidIrType = 1001,
kLrtStatusErrorInvalidGraphInvariant = 1002,
kLrtStatusErrorGraphModification = 1003,

// Tool related errors.
kLrtStatusToolBadConfig = 1500,
kLrtStatusErrorInvalidToolConfig = 1500,
} LrtStatus;

#ifdef __cplusplus
Expand Down
1 change: 0 additions & 1 deletion tensorflow/lite/experimental/lrt/cc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ cc_library(
"//tensorflow/compiler/mlir/lite/core:model_builder_base",
"//tensorflow/lite/c:c_api_types",
"//tensorflow/lite/experimental/lrt/c:lite_rt_c_api",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/types:span",
],
)
Expand Down
24 changes: 24 additions & 0 deletions tensorflow/lite/experimental/lrt/cc/lite_rt_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ class LrtResult {
} \
decl = result.Value();

#define _MOVE_OR_BLOCK(decl, expr, block, result) \
auto result = (expr); \
if (!result.HasValue()) { \
block; \
} \
decl = std::move(result.Value());

#define _MOVE_OR_RETURN_VAL(decl, expr, val, result) \
_MOVE_OR_BLOCK(decl, expr, _RETURN_VAL(val), result)

#define _ASSIGN_OR_RETURN_VAL(decl, expr, val, result) \
_ASSIGN_OR_BLOCK(decl, expr, _RETURN_VAL(val), result)

Expand All @@ -144,10 +154,17 @@ class LrtResult {
#define _ASSIGN_OR_RETURN_STATUS(decl, expr, result) \
_ASSIGN_OR_RETURN_VAL(decl, expr, _STATUS_FROM_RESULT(result), result)

#define _MOVE_OR_RETURN_STATUS(decl, expr, result) \
_MOVE_OR_RETURN_VAL(decl, expr, _STATUS_FROM_RESULT(result), result)

// Assign value behind result returned from expr. If not ok, return status.
#define LRT_ASSIGN_OR_RETURN_STATUS(decl, expr) \
_ASSIGN_OR_RETURN_STATUS(decl, expr, _CONCAT_NAME(_result, __COUNTER__))

// Assign value behind result returned from expr. If not ok, return status.
#define LRT_MOVE_OR_RETURN_STATUS(decl, expr) \
_MOVE_OR_RETURN_STATUS(decl, expr, _CONCAT_NAME(_result, __COUNTER__))

#define _FORWARD_RESULT(result, ty) LrtResult<ty>::FromStatus(result.Status());

#define _ASSIGN_OR_RETURN_RESULT(decl, expr, ty, result) \
Expand All @@ -157,6 +174,13 @@ class LrtResult {
#define LRT_ASSIGN_OR_RETURN_RESULT(decl, expr, ty) \
_ASSIGN_OR_RETURN_RESULT(decl, expr, ty, _CONCAT_NAME(_result, __COUNTER__))

#define _MOVE_OR_RETURN_RESULT(decl, expr, ty, result) \
_MOVE_OR_RETURN_VAL(decl, expr, _FORWARD_RESULT(result, ty), result)

// Move value behind result returned from expr. If not ok, return result.
#define LRT_MOVE_OR_RETURN_RESULT(decl, expr, ty) \
_MOVE_OR_RETURN_RESULT(decl, expr, ty, _CONCAT_NAME(_result, __COUNTER__))

#define LRT_ENSURE_SUPPORTED(cond, msg) \
if (!(cond)) { \
std::cerr << __FILE__ << ":" << __LINE__ << " " << msg << "\n"; \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ CompilerPlugin::ResultT CompilerPlugin::LoadPlugin(

if (OpenLib(lib_path, &plugin.lib_handle_) != kLrtStatusOk) {
LITE_RT_LOG(LRT_WARNING, "Failed to load plugin at: %s", lib_path.data());
return ResultT::FromStatus(kLrtStatusDynamicLoadErr);
return ResultT::FromStatus(kLrtStatusErrorDynamicLoading);
}

if (ResolvePluginApi(plugin.lib_handle_, plugin.plugin_api_) !=
kLrtStatusOk) {
LITE_RT_LOG(LRT_WARNING, "Failed to resolve plugin api at: %s",
lib_path.data());
return ResultT::FromStatus(kLrtStatusDynamicLoadErr);
return ResultT::FromStatus(kLrtStatusErrorDynamicLoading);
}

if (plugin.plugin_api_.init(&plugin.plugin_handle_) != kLrtStatusOk) {
Expand All @@ -155,7 +155,7 @@ CompilerPlugin::ResultT CompilerPlugin::LoadPlugin(
LITE_RT_LOG(LRT_WARNING, "Failed to close loaded library at: %s",
lib_path.data());
}
return ResultT::FromStatus(kLrtStatusDynamicLoadErr);
return ResultT::FromStatus(kLrtStatusErrorDynamicLoading);
}

// This should never change throughout the lifetime of the compiler
Expand Down Expand Up @@ -235,8 +235,7 @@ LrtResult<std::vector<LrtOp>> CompilerPlugin::PartitionModel(
LRT_RETURN_RESULT_IF_NOT_OK(
plugin_api_.partition_model(plugin_handle_, c_model, &ops),
std::vector<LrtOp>);

return LrtResult<std::vector<LrtOp>>::TakeValue(std::move(ops.ops));
return LrtResult<std::vector<LrtOp>>::TakeValue(ops.Vec());
}

LrtStatus CompilerPlugin::Compile(const absl::string_view soc_model,
Expand Down
4 changes: 2 additions & 2 deletions tensorflow/lite/experimental/lrt/core/dynamic_loading.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ LrtStatus OpenLib(absl::string_view so_path, void** lib_handle) {
"Failed to load .so at path: %s, with error:\n\t %s\n", so_path,
::dlerror());

return kLrtStatusDynamicLoadErr;
return kLrtStatusErrorDynamicLoading;
}
*lib_handle = res;
return kLrtStatusOk;
Expand All @@ -55,7 +55,7 @@ LrtStatus OpenLib(absl::string_view so_path, void** lib_handle) {
LrtStatus CloseLib(void* lib_handle) {
if (0 != ::dlclose(lib_handle)) {
LITE_RT_LOG(LRT_ERROR, "Failed to close .so with error: %s", ::dlerror());
return kLrtStatusDynamicLoadErr;
return kLrtStatusErrorDynamicLoading;
}
return kLrtStatusOk;
}
Expand Down
2 changes: 1 addition & 1 deletion tensorflow/lite/experimental/lrt/core/dynamic_loading.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ inline static LrtStatus ResolveLibSymbol(void* lib_handle,
if (ptr == nullptr) {
LITE_RT_LOG(LRT_ERROR, "Faild to resolve symbol: %s, with err: %s\n",
sym_name, ::dlerror());
return kLrtStatusDynamicLoadErr;
return kLrtStatusErrorDynamicLoading;
}
*sym_handle = ptr;
return kLrtStatusOk;
Expand Down
Loading

0 comments on commit ec8f074

Please sign in to comment.