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
+193
View File
@@ -0,0 +1,193 @@
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@xla//third_party/rules_python/python:py_library.bzl", "py_library")
load(
"//tensorflow:tensorflow.bzl",
"tf_custom_op_library",
"tf_gen_op_wrapper_py",
"tf_opts_nortti_if_android",
)
load("//tensorflow:tensorflow.default.bzl", "pybind_extension", "tf_custom_op_py_strict_library")
load("//tensorflow/lite:build_def.bzl", "tf_to_tflite", "tflite_copts")
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"],
default_visibility = ["//tensorflow:internal"],
licenses = ["notice"],
)
exports_files(glob([
"*.pb",
"*.pbtxt",
]))
tf_to_tflite(
name = "permute_float",
src = "permute.pbtxt",
out = "permute_float.tflite",
options = [
"--input_arrays=input",
"--output_arrays=output",
],
)
tf_to_tflite(
name = "permute_uint8",
src = "permute.pbtxt",
out = "permute_uint8.tflite",
options = [
"--input_arrays=input",
"--output_arrays=output",
"--inference_type=QUANTIZED_UINT8",
"--std_dev_values=1",
"--mean_values=0",
"--default_ranges_min=0",
"--default_ranges_max=255",
],
)
tf_to_tflite(
name = "gather_string",
src = "gather.pbtxt",
out = "gather_string.tflite",
options = [
"--input_arrays=input,indices",
"--output_arrays=output",
],
)
tf_to_tflite(
name = "gather_string_0d",
src = "gather_0d.pbtxt",
out = "gather_string_0d.tflite",
options = [
"--input_arrays=input,indices",
"--output_arrays=output",
],
)
filegroup(
name = "interpreter_test_data",
srcs = [
"pc_conv.bin",
"two_signatures.tflite",
":gather_string",
":gather_string_0d",
":permute_float",
":permute_uint8",
],
visibility = ["//tensorflow:__subpackages__"],
)
cc_library(
name = "test_delegate",
testonly = 1,
srcs = ["test_delegate.cc"],
visibility = ["//visibility:private"],
deps = [
"//tensorflow/lite/core/c:common",
],
alwayslink = 1,
)
cc_binary(
name = "test_delegate.so",
testonly = 1,
linkshared = 1,
linkstatic = 1,
deps = [
":test_delegate",
],
)
cc_library(
name = "double_op_and_kernels",
testonly = 1,
srcs = ["double_op.cc"],
copts = tflite_copts() + tf_opts_nortti_if_android() + select({
"//tensorflow:android": ["-Wno-private-header"],
"//conditions:default": [],
}),
deps = select({
"//tensorflow:android": [
"//tensorflow/core:portable_tensorflow_lib_lite",
],
"//tensorflow:ios": [
"//tensorflow/core:portable_tensorflow_lib_lite",
],
"//conditions:default": [
"//tensorflow/core:framework",
"//tensorflow/core:lib",
],
}),
alwayslink = 1,
)
tf_custom_op_library(
name = "_double_op.so",
testonly = 1,
srcs = ["double_op.cc"],
)
tf_gen_op_wrapper_py(
name = "gen_double_op_wrapper",
testonly = 1,
out = "double_op_wrapper.py",
extra_py_deps = [
"//tensorflow/python:pywrap_tfe",
"//tensorflow/python/util:dispatch",
"//tensorflow/python/util:deprecation",
"//tensorflow/python/util:tf_export",
],
py_lib_rule = py_library,
deps = [":double_op_and_kernels"],
)
tf_custom_op_py_strict_library(
name = "double_op",
testonly = 1,
srcs = ["double_op.py"],
dso = [":_double_op.so"],
kernels = [":double_op_and_kernels"],
deps = [
":gen_double_op_wrapper",
"//tensorflow/python/framework:dtypes",
"//tensorflow/python/framework:load_library",
"//tensorflow/python/platform:resource_loader",
],
)
cc_library(
name = "test_registerer",
srcs = ["test_registerer.cc"],
hdrs = ["test_registerer.h"],
visibility = ["//visibility:private"],
deps = [
"//tensorflow/lite:framework",
"//tensorflow/lite/kernels:builtin_ops",
"//tensorflow/lite/kernels:kernel_util",
"//tensorflow/lite/kernels/internal:tensor",
],
alwayslink = 1,
)
pybind_extension(
name = "_pywrap_test_registerer",
srcs = [
"test_registerer_wrapper.cc",
],
hdrs = ["test_registerer.h"],
additional_exported_symbols = ["TF_TestRegisterer"],
enable_stub_generation = True,
link_in_framework = True,
pytype_srcs = [
"_pywrap_test_registerer.pyi",
],
deps = [
":test_registerer",
"//tensorflow/lite:framework",
"//tensorflow/lite/kernels:builtin_ops",
"@pybind11",
"@xla//third_party/python_runtime:headers",
],
)
@@ -0,0 +1,17 @@
# Copyright 2023 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.
# ==============================================================================
def TF_TestRegisterer(arg0: int) -> None: ...
def get_num_test_registerer_calls() -> int: ...
+64
View File
@@ -0,0 +1,64 @@
node {
name: "a"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
}
node {
name: "b"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
}
node {
name: "c"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
}
node {
name: "d"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
}
node {
name: "Merge"
op: "Merge"
input: "a"
input: "b"
input: "c"
input: "d"
attr {
key: "N"
value {
i: 4
}
}
attr {
key: "T"
value {
type: DT_FLOAT
}
}
}
versions {
producer: 27
}
@@ -0,0 +1,9 @@
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"],
default_visibility = ["//tensorflow:internal"],
licenses = ["notice"],
)
exports_files([
"saved_model.pb",
])
+60
View File
@@ -0,0 +1,60 @@
/* Copyright 2020 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/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/shape_inference.h"
namespace tensorflow {
REGISTER_OP("Double")
.Input("input: T")
.Output("doubled: T")
.Attr("T: {int32, float}")
.SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) {
c->set_output(0, c->input(0));
return absl::OkStatus();
});
template <typename T>
class DoubleOp : public OpKernel {
public:
explicit DoubleOp(OpKernelConstruction* context) : OpKernel(context) {}
void Compute(OpKernelContext* context) override {
// Grab the input tensor
const Tensor& input_tensor = context->input(0);
auto input_flat = input_tensor.flat<T>();
// Create an output tensor
Tensor* output_tensor = nullptr;
OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),
&output_tensor));
auto output_flat = output_tensor->flat<T>();
// Set all but the first element of the output tensor to 0.
const int N = input_flat.size();
for (int i = 0; i < N; i++) {
output_flat(i) = 2 * input_flat(i);
}
}
};
REGISTER_KERNEL_BUILDER(
Name("Double").Device(DEVICE_CPU).TypeConstraint<int32_t>("T"),
DoubleOp<int32_t>);
REGISTER_KERNEL_BUILDER(
Name("Double").Device(DEVICE_CPU).TypeConstraint<float>("T"),
DoubleOp<float>);
} // namespace tensorflow
+31
View File
@@ -0,0 +1,31 @@
# Copyright 2020 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.
# ==============================================================================
"""Double op is a user's defined op for testing purpose."""
from tensorflow.lite.python.testdata import double_op_wrapper
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import load_library
from tensorflow.python.platform import resource_loader
_double_op = load_library.load_op_library(
resource_loader.get_path_to_datafile('_double_op.so'))
def double(input_tensor):
"""Double op applies element-wise double to input data."""
if (input_tensor.dtype != dtypes.int32 and
input_tensor.dtype != dtypes.float32):
raise ValueError('Double op only accept int32 or float32 values.')
return double_op_wrapper.double(input_tensor)
+93
View File
@@ -0,0 +1,93 @@
node {
name: "input"
op: "Placeholder"
device: "/device:CPU:0"
attr {
key: "dtype"
value {
type: DT_STRING
}
}
attr {
key: "shape"
value {
shape {
dim {
size: 10
}
}
}
}
}
node {
name: "indices"
op: "Placeholder"
device: "/device:CPU:0"
attr {
key: "dtype"
value {
type: DT_INT64
}
}
attr {
key: "shape"
value {
shape {
dim {
size: 3
}
}
}
}
}
node {
name: "axis"
op: "Const"
device: "/device:CPU:0"
attr {
key: "dtype"
value {
type: DT_INT32
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_INT32
tensor_shape {
}
int_val: 0
}
}
}
}
node {
name: "output"
op: "GatherV2"
input: "input"
input: "indices"
input: "axis"
device: "/device:CPU:0"
attr {
key: "Taxis"
value {
type: DT_INT32
}
}
attr {
key: "Tindices"
value {
type: DT_INT64
}
}
attr {
key: "Tparams"
value {
type: DT_STRING
}
}
}
versions {
producer: 27
}
+108
View File
@@ -0,0 +1,108 @@
node {
name: "input"
op: "Placeholder"
device: "/device:CPU:0"
attr {
key: "dtype"
value {
type: DT_STRING
}
}
}
node {
name: "input_const"
op: "Const"
device: "/device:CPU:0"
attr {
key: "dtype"
value {
type: DT_STRING
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_STRING
tensor_shape {
dim {
size: 1
}
}
string_val: "abcd"
}
}
}
}
node {
name: "indices"
op: "Placeholder"
device: "/device:CPU:0"
attr {
key: "dtype"
value {
type: DT_INT64
}
}
attr {
key: "shape"
value {
shape {
dim {
size: 3
}
}
}
}
}
node {
name: "axis"
op: "Const"
device: "/device:CPU:0"
attr {
key: "dtype"
value {
type: DT_INT32
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_INT32
tensor_shape {
}
int_val: 0
}
}
}
}
node {
name: "output"
op: "GatherV2"
input: "input_const"
input: "indices"
input: "axis"
device: "/device:CPU:0"
attr {
key: "Taxis"
value {
type: DT_INT32
}
}
attr {
key: "Tindices"
value {
type: DT_INT64
}
}
attr {
key: "Tparams"
value {
type: DT_STRING
}
}
}
versions {
producer: 27
}
Binary file not shown.
+98
View File
@@ -0,0 +1,98 @@
node {
name: "input"
op: "Placeholder"
device: "/device:CPU:0"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "shape"
value {
shape {
dim {
size: 1
}
dim {
size: 4
}
}
}
}
}
node {
name: "Const"
op: "Const"
device: "/device:CPU:0"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
dim {
size: 4
}
dim {
size: 4
}
}
float_val: 0.0
float_val: 0.0
float_val: 0.0
float_val: 1.0
float_val: 0.0
float_val: 0.0
float_val: 1.0
float_val: 0.0
float_val: 0.0
float_val: 1.0
float_val: 0.0
float_val: 0.0
float_val: 1.0
float_val: 0.0
float_val: 0.0
float_val: 0.0
}
}
}
}
node {
name: "output"
op: "MatMul"
input: "input"
input: "Const"
device: "/device:CPU:0"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
attr {
key: "transpose_a"
value {
b: false
}
}
attr {
key: "transpose_b"
value {
b: false
}
}
}
versions {
producer: 27
}
+94
View File
@@ -0,0 +1,94 @@
/* Copyright 2019 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 <cstdio>
#include <cstdlib>
#include <cstring>
#include "tensorflow/lite/core/c/common.h"
namespace tflite {
namespace {
int num_delegates_created = 0;
int num_delegates_destroyed = 0;
int num_delegates_invoked = 0;
int options_counter = 0;
int (*destruction_callback)(const char* s) = nullptr;
typedef void (*ErrorHandler)(const char*);
} // namespace
extern "C" {
TfLiteDelegate* tflite_plugin_create_delegate(char** options_keys,
char** options_values,
size_t num_options,
ErrorHandler error_handler) {
num_delegates_created++;
for (int idx = 0; idx < num_options; idx++) {
if (std::strcmp("options_counter", options_keys[idx]) == 0) {
int int_value;
if (sscanf(options_values[idx], "%d", &int_value) == 1) {
options_counter += int_value;
}
} else if (std::strcmp("fail", options_keys[idx]) == 0) {
if (error_handler) error_handler("Fail argument sent.");
return nullptr;
}
}
TfLiteDelegate* ptr = new TfLiteDelegate;
*ptr = TfLiteDelegateCreate();
ptr->Prepare = [](TfLiteContext* context, TfLiteDelegate* delegate) {
num_delegates_invoked++;
return kTfLiteOk;
};
ptr->flags = kTfLiteDelegateFlagsNone;
return ptr;
}
void set_destroy_callback(int (*callback)(const char* s)) {
destruction_callback = callback;
}
void tflite_plugin_destroy_delegate(TfLiteDelegate* delegate) {
num_delegates_destroyed++;
delete delegate;
if (destruction_callback) {
destruction_callback("test_delegate");
// destruction_callback is a global variable,
// so it should be set to nullptr here to avoid crashes
destruction_callback = nullptr;
}
}
void initialize_counters() {
num_delegates_created = 0;
num_delegates_destroyed = 0;
num_delegates_invoked = 0;
options_counter = 0;
}
int get_num_delegates_created() { return num_delegates_created; }
int get_num_delegates_destroyed() { return num_delegates_destroyed; }
int get_num_delegates_invoked() { return num_delegates_invoked; }
int get_options_counter() { return options_counter; }
}
} // namespace tflite
+107
View File
@@ -0,0 +1,107 @@
/* Copyright 2019 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/python/testdata/test_registerer.h"
#include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
#include "tensorflow/lite/kernels/kernel_util.h"
namespace tflite {
namespace {
static int num_test_registerer_calls = 0;
TfLiteRegistration* GetFakeRegistration() {
static TfLiteRegistration fake_op;
return &fake_op;
}
namespace double_op {
constexpr int kInputTensor = 0;
constexpr int kOutputTensor = 0;
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
const TfLiteTensor* input;
TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputTensor, &input));
TfLiteTensor* output;
TF_LITE_ENSURE_OK(context,
GetOutputSafe(context, node, kOutputTensor, &output));
TfLiteIntArray* output_shape = TfLiteIntArrayCopy(input->dims);
TF_LITE_ENSURE_TYPES_EQ(context, output->type, input->type);
return context->ResizeTensor(context, output, output_shape);
}
TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
const TfLiteTensor* input;
TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputTensor, &input));
TfLiteTensor* output;
TF_LITE_ENSURE_OK(context,
GetOutputSafe(context, node, kOutputTensor, &output));
TF_LITE_ENSURE_TYPES_EQ(context, output->type, input->type);
const size_t size = GetTensorShape(input).FlatSize();
if (input->type == kTfLiteFloat32) {
const float* input_ptr = input->data.f;
float* output_ptr = output->data.f;
for (int i = 0; i < size; ++i) {
output_ptr[i] = input_ptr[i] + input_ptr[i];
}
} else if (input->type == kTfLiteInt32) {
const int32_t* input_ptr = input->data.i32;
int32_t* output_ptr = output->data.i32;
for (int i = 0; i < size; ++i) {
output_ptr[i] = input_ptr[i] + input_ptr[i];
}
} else {
return kTfLiteError;
}
return kTfLiteOk;
}
} // namespace double_op
TfLiteRegistration* GetDoubleRegistration() {
static TfLiteRegistration double_op = {nullptr, nullptr, double_op::Prepare,
double_op::Eval};
return &double_op;
}
} // namespace
// Dummy registerer function with the correct signature. Registers a fake custom
// op needed by test models. Increments the num_test_registerer_calls counter by
// one. The TF_ prefix is needed to get past the version script in the OSS
// build.
extern "C" void TF_TestRegisterer(tflite::MutableOpResolver *resolver) {
resolver->AddCustom("FakeOp", GetFakeRegistration());
resolver->AddCustom("Double", GetDoubleRegistration());
num_test_registerer_calls++;
}
// Returns the num_test_registerer_calls counter and re-sets it.
int get_num_test_registerer_calls() {
const int result = num_test_registerer_calls;
num_test_registerer_calls = 0;
return result;
}
} // namespace tflite
+32
View File
@@ -0,0 +1,32 @@
/* Copyright 2019 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_PYTHON_TESTDATA_TEST_REGISTERER_H_
#define TENSORFLOW_LITE_PYTHON_TESTDATA_TEST_REGISTERER_H_
#include "tensorflow/lite/mutable_op_resolver.h"
namespace tflite {
// Dummy registerer function with the correct signature. Ignores the resolver
// but increments the num_test_registerer_calls counter by one. The TF_ prefix
// is needed to get past the version script in the OSS build.
extern "C" void TF_TestRegisterer(tflite::MutableOpResolver *resolver);
// Returns the num_test_registerer_calls counter and re-sets it.
int get_num_test_registerer_calls();
} // namespace tflite
#endif // TENSORFLOW_LITE_PYTHON_TESTDATA_TEST_REGISTERER_H_
@@ -0,0 +1,36 @@
/* Copyright 2019 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 "pybind11/pybind11.h" // from @pybind11
#include "pybind11/pytypes.h" // from @pybind11
#include "tensorflow/lite/python/testdata/test_registerer.h"
PYBIND11_MODULE(_pywrap_test_registerer, m) {
m.doc() = R"pbdoc(
_pywrap_test_registerer
-----
)pbdoc";
m.def("get_num_test_registerer_calls", &tflite::get_num_test_registerer_calls,
R"pbdoc(
Returns the num_test_registerer_calls counter and re-sets it.
)pbdoc");
m.def(
"TF_TestRegisterer",
[](uintptr_t resolver) {
tflite::TF_TestRegisterer(
reinterpret_cast<tflite::MutableOpResolver*>(resolver));
},
R"pbdoc(
Dummy registerer function with the correct signature. Registers a fake
custom op needed by test models. Increments the
num_test_registerer_calls counter by one.
)pbdoc");
}