chore: import upstream snapshot with attribution
cffconvert / validate (push) Has been skipped
License Check / license-check (push) Failing after 2s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:14:16 +08:00
commit 8a852e4b4e
36502 changed files with 9277225 additions and 0 deletions
@@ -0,0 +1,50 @@
load("@rules_cc//cc:cc_library.bzl", "cc_library")
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"],
default_visibility = [
"//visibility:public",
],
licenses = ["notice"],
)
cc_library(
name = "online_helper_delegate",
srcs = ["online_helper_delegate.cc"],
hdrs = ["online_helper_delegate.h"],
deps = [
"//tensorflow/lite:kernel_api",
"//tensorflow/lite:minimal_logging",
"//tensorflow/lite/c:c_api_types",
"//tensorflow/lite/c:common",
"//tensorflow/lite/core/c:common",
"//tensorflow/lite/tools/delegates/compatibility/protos:compatibility_result_cc",
"@com_google_absl//absl/status",
],
)
cc_library(
name = "delegate_compatibility_checker_base",
srcs = ["delegate_compatibility_checker_base.cc"],
hdrs = [
"delegate_compatibility_checker_base.h",
],
deps = [
":delegate_compatibility_checker_util",
"//tensorflow/lite:framework_stable",
"//tensorflow/lite/core:framework_stable",
"//tensorflow/lite/core/c:common",
"//tensorflow/lite/kernels:builtin_ops",
"//tensorflow/lite/schema:schema_fbs",
"//tensorflow/lite/tools/delegates/compatibility/protos:compatibility_result_cc",
"//tensorflow/lite/tools/versioning:op_signature",
"@com_google_absl//absl/status",
],
)
cc_library(
name = "delegate_compatibility_checker_util",
hdrs = [
"delegate_compatibility_checker_util.h",
],
)
@@ -0,0 +1,62 @@
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
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 "tensorflow/lite/tools/delegates/compatibility/common/delegate_compatibility_checker_base.h"
#include <cstdlib>
#include "absl/status/status.h"
#include "tensorflow/lite/model_builder.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/tools/delegates/compatibility/common/delegate_compatibility_checker_util.h"
#include "tensorflow/lite/tools/delegates/compatibility/protos/compatibility_result.pb.h"
#include "tensorflow/lite/tools/versioning/op_signature.h"
namespace tflite {
namespace tools {
absl::Status DelegateCompatibilityCheckerBase::checkModelCompatibilityOffline(
tflite::FlatBufferModel* model_buffer, proto::CompatibilityResult* result) {
auto model = model_buffer->GetModel();
auto subgraphs = model->subgraphs();
for (int i = 0; i < subgraphs->Length(); ++i) {
const tflite::SubGraph* subgraph = subgraphs->Get(i);
for (int j = 0; j < subgraph->operators()->Length(); ++j) {
proto::OpCompatibilityResult* op_result =
result->add_compatibility_results();
op_result->set_subgraph_index_in_model(i);
op_result->set_operator_index_in_subgraph(j);
const tflite::Operator* op = subgraph->operators()->Get(j);
const tflite::OperatorCode* op_code =
model->operator_codes()->Get(op->opcode_index());
RETURN_IF_ERROR(
checkOpCompatibilityOffline(op_code, op, subgraph, model, op_result));
}
}
return absl::OkStatus();
}
absl::Status DelegateCompatibilityCheckerBase::checkOpCompatibilityOffline(
const tflite::OperatorCode* op_code, const tflite::Operator* op,
const tflite::SubGraph* subgraph, const tflite::Model* model,
proto::OpCompatibilityResult* op_result) {
OpSignature op_sig = tflite::GetOpSignature(op_code, op, subgraph, model);
auto status = checkOpSigCompatibility(op_sig, op_result);
free(op_sig.builtin_data);
return status;
}
} // namespace tools
} // namespace tflite
@@ -0,0 +1,101 @@
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
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.
==============================================================================*/
#ifndef TENSORFLOW_LITE_TOOLS_DELEGATES_COMPATIBILITY_COMMON_DELEGATE_COMPATIBILITY_CHECKER_BASE_H_
#define TENSORFLOW_LITE_TOOLS_DELEGATES_COMPATIBILITY_COMMON_DELEGATE_COMPATIBILITY_CHECKER_BASE_H_
#include <string>
#include <unordered_map>
#include "absl/status/status.h"
#include "tensorflow/lite/core/model_builder.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/tools/delegates/compatibility/protos/compatibility_result.pb.h"
#include "tensorflow/lite/tools/versioning/op_signature.h"
namespace tflite {
namespace tools {
// TODO(b/243489631): Remove online mode support.
// Base class for the delegate compatibility checker (DCC). Extracts the logic
// of iterating through the model, and lets each specific DCC to check if the
// operation of a node is compatible with that delegate. TfLiteNode and
// operator (in schema.fbs) are equivalent.
// There are two modes supported: online and offline. Online mode needs
// TfLiteContext while offline mode doesn't. Online mode is supported as an
// intermediate stage and it is recommended that delegates support offline
// mode in validation logic, if possible.
class DelegateCompatibilityCheckerBase {
public:
virtual ~DelegateCompatibilityCheckerBase() = default;
// Iterates over the subgraphs in the model, and for each operator, checks if
// it is compatible with the delegate.
// Stores the compatibility for each operator in the result structure.
absl::Status checkModelCompatibilityOffline(
tflite::FlatBufferModel* model_buffer,
tflite::proto::CompatibilityResult* result);
// This function is implemented differently by each specific DCC.
// Stores the compatibility for each operator in the result structure.
virtual absl::Status checkModelCompatibilityOnline(
tflite::FlatBufferModel* model_buffer,
tflite::proto::CompatibilityResult* result) = 0;
// This function gets the operation signature (OpSignature) from the
// op_code, op, subgraph and model, and then call to checkOpSigCompatibility()
// with the operation signature.
// Params:
// op_code: Used to get the built in code of the operator, in
// order to know which operator is being used (e.g. MUL,
// FULLY_CONNECTED…).
// op: Used to get the input and output tensors indexes, so that obtains the
// tensors with the subgraph.
// subgraph: Used to get the tensors to finally get the OpSignature.
// model: Used to get the buffer in order to check if the tensor is
// a constant tensor.
// op_result: Stores whether the operation is compatible or not and why.
// Returns: absl::OkStatus() if the function is completed without exceptions.
absl::Status checkOpCompatibilityOffline(
const tflite::OperatorCode* op_code, const tflite::Operator* op,
const tflite::SubGraph* subgraph, const tflite::Model* model,
tflite::proto::OpCompatibilityResult* op_result);
// Returns the dictionary with the initialized keys. This is an abstract
// function because each specific DCC needs different parameters, which will
// be the keys in the returned dictionary.
virtual std::unordered_map<std::string, std::string>
getDccConfigurations() = 0;
// Sets the parameters needed in the specific DCC. Also checks if the
// value types are correct.
virtual absl::Status setDccConfigurations(
const std::unordered_map<std::string, std::string>& dcc_configs) = 0;
private:
// This function is implemented differently by each specific DCC because
// they contain the logic for checking if the operation in op_sig is
// compatible for that specific DCC. op_result stores whether the operation
// is supported or not, and why. By using offline mode, only op_signature is
// used to perform the checks.
virtual absl::Status checkOpSigCompatibility(
const tflite::OpSignature& op_sig,
tflite::proto::OpCompatibilityResult* op_result) = 0;
};
} // namespace tools
} // namespace tflite
#endif // TENSORFLOW_LITE_TOOLS_DELEGATES_COMPATIBILITY_COMMON_DELEGATE_COMPATIBILITY_CHECKER_BASE_H_
@@ -0,0 +1,31 @@
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
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.
==============================================================================*/
#ifndef TENSORFLOW_LITE_TOOLS_DELEGATES_COMPATIBILITY_COMMON_DELEGATE_COMPATIBILITY_CHECKER_UTIL_H_
#define TENSORFLOW_LITE_TOOLS_DELEGATES_COMPATIBILITY_COMMON_DELEGATE_COMPATIBILITY_CHECKER_UTIL_H_
namespace tflite {
namespace tools {
#define RETURN_IF_ERROR(s) \
{ \
auto c = (s); \
if (!c.ok()) return c; \
}
} // namespace tools
} // namespace tflite
#endif // TENSORFLOW_LITE_TOOLS_DELEGATES_COMPATIBILITY_COMMON_DELEGATE_COMPATIBILITY_CHECKER_UTIL_H_
@@ -0,0 +1,61 @@
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
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 "tensorflow/lite/tools/delegates/compatibility/common/online_helper_delegate.h"
#include "tensorflow/lite/c/c_api_types.h"
#include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/context_util.h"
#include "tensorflow/lite/logger.h"
#include "tensorflow/lite/minimal_logging.h"
#include "tensorflow/lite/tools/delegates/compatibility/protos/compatibility_result.pb.h"
namespace tflite {
namespace tools {
TfLiteStatus OnlineHelperDelegate::DoPrepare(TfLiteContext* context,
TfLiteDelegate* delegate) {
auto self = reinterpret_cast<OnlineHelperDelegate*>(delegate);
// Gets execution plan.
TfLiteIntArray* execution_plan;
TF_LITE_ENSURE_STATUS(context->GetExecutionPlan(context, &execution_plan));
// Validates compatibility for each node.
for (auto node_index : TfLiteIntArrayView(execution_plan)) {
proto::OpCompatibilityResult* op_result =
self->result_->add_compatibility_results();
TfLiteNode* node;
TfLiteRegistration* registration;
TF_LITE_ENSURE_STATUS(context->GetNodeAndRegistration(
context, node_index, &node, &registration));
// Always subgraph #0 because in the interpreter (and TfLiteContext), the
// methods refer to the primary_subgraph, so the only subgraph available is
// the subgraph #0.
op_result->set_subgraph_index_in_model(0);
op_result->set_operator_index_in_subgraph(node_index);
auto status = self->check_op_func_ptr_(context, node, registration,
self->dcc_configs_, op_result);
if (!status.ok()) {
TFLITE_LOG_PROD(TFLITE_LOG_ERROR, "Error at node %d: %s", node_index,
status.message());
return kTfLiteError;
}
}
return kTfLiteOk;
}
} // namespace tools
} // namespace tflite
@@ -0,0 +1,97 @@
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
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.
==============================================================================*/
#ifndef TENSORFLOW_LITE_TOOLS_DELEGATES_COMPATIBILITY_COMMON_ONLINE_HELPER_DELEGATE_H_
#define TENSORFLOW_LITE_TOOLS_DELEGATES_COMPATIBILITY_COMMON_ONLINE_HELPER_DELEGATE_H_
#include <functional>
#include <string>
#include <unordered_map>
#include "absl/status/status.h"
#include "tensorflow/lite/core/c/common.h"
#include "tensorflow/lite/tools/delegates/compatibility/protos/compatibility_result.pb.h"
namespace tflite {
namespace tools {
// This utility delegate is required because some TfLiteContext related
// functions are forbidden if not calling in delegate. This class is only used
// for online mode.
// WARNING: This is an experimental class and subject to change.
class OnlineHelperDelegate : public TfLiteDelegate {
public:
OnlineHelperDelegate(
std::unordered_map<std::string, std::string>& dcc_configs,
std::function<absl::Status(TfLiteContext*, const TfLiteNode*,
const TfLiteRegistration*,
std::unordered_map<std::string, std::string>&,
proto::OpCompatibilityResult*)>
check_op_func_ptr,
proto::CompatibilityResult* result)
: TfLiteDelegate(TfLiteDelegateCreate()),
result_(result),
dcc_configs_(dcc_configs),
check_op_func_ptr_(check_op_func_ptr) {
Prepare = DoPrepare;
CopyFromBufferHandle = DoCopyFromBufferHandle;
CopyToBufferHandle = DoCopyToBufferHandle;
FreeBufferHandle = DoFreeBufferHandle;
data_ = &delegate_data_;
}
protected:
// This function uses a pointer to a method (implemented by each specific DCC)
// which contains the logic to check whether the primary subgraph can be
// delegated to the specific delegate.
static TfLiteStatus DoPrepare(TfLiteContext* context,
TfLiteDelegate* delegate);
// This function is not expected to be called in this delegate.
static TfLiteStatus DoCopyFromBufferHandle(TfLiteContext* context,
TfLiteDelegate* delegate,
TfLiteBufferHandle buffer_handle,
TfLiteTensor* tensor) {
return kTfLiteError;
}
// This function is not expected to be called in this delegate.
static TfLiteStatus DoCopyToBufferHandle(TfLiteContext* context,
TfLiteDelegate* delegate,
TfLiteBufferHandle buffer_handle,
TfLiteTensor* tensor) {
return kTfLiteError;
}
// There is no buffer handle in this delegate.
static void DoFreeBufferHandle(TfLiteContext* context,
TfLiteDelegate* delegate,
TfLiteBufferHandle* handle) {}
private:
int delegate_data_;
proto::CompatibilityResult* result_;
std::unordered_map<std::string, std::string> dcc_configs_;
std::function<absl::Status(TfLiteContext*, const TfLiteNode*,
const TfLiteRegistration*,
std::unordered_map<std::string, std::string>&,
proto::OpCompatibilityResult*)>
check_op_func_ptr_;
};
} // namespace tools
} // namespace tflite
#endif // TENSORFLOW_LITE_TOOLS_DELEGATES_COMPATIBILITY_COMMON_ONLINE_HELPER_DELEGATE_H_