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,40 @@
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("//tensorflow/lite:build_def.bzl", "tflite_copts")
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"],
default_visibility = ["benchmark"],
licenses = ["notice"],
)
package_group(
name = "benchmark",
packages = [
"//tensorflow/lite/tools/benchmark/...",
],
)
exports_files(
["benchmark_c_api.h"],
visibility = ["//tensorflow/lite/tools/benchmark/experimental/c:benchmark"],
)
cc_library(
name = "benchmark_c_api",
srcs = ["benchmark_c_api.cc"],
hdrs = [
"benchmark_c_api.h",
],
copts = tflite_copts(),
visibility = [
"benchmark",
],
deps = [
"//tensorflow/lite/c:c_api_types",
"//tensorflow/lite/core/c:c_api_types",
"//tensorflow/lite/tools/benchmark:benchmark_model_lib",
"//tensorflow/lite/tools/benchmark:benchmark_params",
"//tensorflow/lite/tools/benchmark:benchmark_tflite_model_lib",
"@xla//xla/tsl/util:stats_calculator_portable",
],
)
@@ -0,0 +1,187 @@
/* 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/tools/benchmark/experimental/c/benchmark_c_api.h"
#include <cstdint>
#include <memory>
#include <utility>
#include "xla/tsl/util/stats_calculator.h"
#include "tensorflow/lite/c/c_api_types.h"
#include "tensorflow/lite/tools/benchmark/benchmark_model.h"
#include "tensorflow/lite/tools/benchmark/benchmark_params.h"
#include "tensorflow/lite/tools/benchmark/benchmark_tflite_model.h"
extern "C" {
// -----------------------------------------------------------------------------
// C APIs corresponding to tflite::benchmark::BenchmarkResults type.
// -----------------------------------------------------------------------------
struct TfLiteBenchmarkResults {
const tflite::benchmark::BenchmarkResults* results;
};
// Converts the given int64_t stat into a TfLiteBenchmarkInt64Stat struct.
TfLiteBenchmarkInt64Stat ConvertStat(const tsl::Stat<int64_t>& stat) {
return {
stat.empty(), stat.first(), stat.newest(), stat.max(),
stat.min(), stat.count(), stat.sum(), stat.squared_sum(),
stat.all_same(), stat.avg(), stat.std_deviation(),
};
}
TfLiteBenchmarkInt64Stat TfLiteBenchmarkResultsGetInferenceTimeMicroseconds(
const TfLiteBenchmarkResults* results) {
return ConvertStat(results->results->inference_time_us());
}
TfLiteBenchmarkInt64Stat TfLiteBenchmarkResultsGetWarmupTimeMicroseconds(
const TfLiteBenchmarkResults* results) {
return ConvertStat(results->results->warmup_time_us());
}
int64_t TfLiteBenchmarkResultsGetStartupLatencyMicroseconds(
const TfLiteBenchmarkResults* results) {
return results->results->startup_latency_us();
}
uint64_t TfLiteBenchmarkResultsGetInputBytes(
const TfLiteBenchmarkResults* results) {
return results->results->input_bytes();
}
double TfLiteBenchmarkResultsGetThroughputMbPerSecond(
const TfLiteBenchmarkResults* results) {
return results->results->throughput_MB_per_second();
}
// -----------------------------------------------------------------------------
// C APIs corresponding to tflite::benchmark::BenchmarkListener type.
// -----------------------------------------------------------------------------
class BenchmarkListenerAdapter : public tflite::benchmark::BenchmarkListener {
public:
void OnBenchmarkStart(
const tflite::benchmark::BenchmarkParams& params) override {
if (on_benchmark_start_fn_ != nullptr) {
on_benchmark_start_fn_(user_data_);
}
}
void OnSingleRunStart(tflite::benchmark::RunType runType) override {
if (on_single_run_start_fn_ != nullptr) {
on_single_run_start_fn_(user_data_, runType == tflite::benchmark::WARMUP
? TfLiteBenchmarkWarmup
: TfLiteBenchmarkRegular);
}
}
void OnSingleRunEnd() override {
if (on_single_run_end_fn_ != nullptr) {
on_single_run_end_fn_(user_data_);
}
}
void OnBenchmarkEnd(
const tflite::benchmark::BenchmarkResults& results) override {
if (on_benchmark_end_fn_ != nullptr) {
TfLiteBenchmarkResults* wrapper = new TfLiteBenchmarkResults{&results};
on_benchmark_end_fn_(user_data_, wrapper);
delete wrapper;
}
}
// Keep the user_data pointer provided when setting the callbacks.
void* user_data_;
// Function pointers set by the TfLiteBenchmarkListenerSetCallbacks call.
// Only non-null callbacks will be actually called.
void (*on_benchmark_start_fn_)(void* user_data);
void (*on_single_run_start_fn_)(void* user_data,
TfLiteBenchmarkRunType runType);
void (*on_single_run_end_fn_)(void* user_data);
void (*on_benchmark_end_fn_)(void* user_data,
TfLiteBenchmarkResults* results);
};
struct TfLiteBenchmarkListener {
std::unique_ptr<BenchmarkListenerAdapter> adapter;
};
TfLiteBenchmarkListener* TfLiteBenchmarkListenerCreate() {
std::unique_ptr<BenchmarkListenerAdapter> adapter =
std::make_unique<BenchmarkListenerAdapter>();
return new TfLiteBenchmarkListener{std::move(adapter)};
}
void TfLiteBenchmarkListenerDelete(TfLiteBenchmarkListener* listener) {
delete listener;
}
void TfLiteBenchmarkListenerSetCallbacks(
TfLiteBenchmarkListener* listener, void* user_data,
void (*on_benchmark_start_fn)(void* user_data),
void (*on_single_run_start_fn)(void* user_data,
TfLiteBenchmarkRunType runType),
void (*on_single_run_end_fn)(void* user_data),
void (*on_benchmark_end_fn)(void* user_data,
TfLiteBenchmarkResults* results)) {
listener->adapter->user_data_ = user_data;
listener->adapter->on_benchmark_start_fn_ = on_benchmark_start_fn;
listener->adapter->on_single_run_start_fn_ = on_single_run_start_fn;
listener->adapter->on_single_run_end_fn_ = on_single_run_end_fn;
listener->adapter->on_benchmark_end_fn_ = on_benchmark_end_fn;
}
// -----------------------------------------------------------------------------
// C APIs corresponding to tflite::benchmark::BenchmarkTfLiteModel type.
// -----------------------------------------------------------------------------
struct TfLiteBenchmarkTfLiteModel {
std::unique_ptr<tflite::benchmark::BenchmarkTfLiteModel> benchmark_model;
};
TfLiteBenchmarkTfLiteModel* TfLiteBenchmarkTfLiteModelCreate() {
std::unique_ptr<tflite::benchmark::BenchmarkTfLiteModel> benchmark_model(
new tflite::benchmark::BenchmarkTfLiteModel());
return new TfLiteBenchmarkTfLiteModel{std::move(benchmark_model)};
}
void TfLiteBenchmarkTfLiteModelDelete(
TfLiteBenchmarkTfLiteModel* benchmark_model) {
delete benchmark_model;
}
TfLiteStatus TfLiteBenchmarkTfLiteModelInit(
TfLiteBenchmarkTfLiteModel* benchmark_model) {
return benchmark_model->benchmark_model->Init();
}
TfLiteStatus TfLiteBenchmarkTfLiteModelRun(
TfLiteBenchmarkTfLiteModel* benchmark_model) {
return benchmark_model->benchmark_model->Run();
}
TfLiteStatus TfLiteBenchmarkTfLiteModelRunWithArgs(
TfLiteBenchmarkTfLiteModel* benchmark_model, int argc, char** argv) {
return benchmark_model->benchmark_model->Run(argc, argv);
}
void TfLiteBenchmarkTfLiteModelAddListener(
TfLiteBenchmarkTfLiteModel* benchmark_model,
const TfLiteBenchmarkListener* listener) {
return benchmark_model->benchmark_model->AddListener(listener->adapter.get());
}
} // extern "C"
@@ -0,0 +1,137 @@
/* 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_TOOLS_BENCHMARK_EXPERIMENTAL_C_BENCHMARK_C_API_H_
#define TENSORFLOW_LITE_TOOLS_BENCHMARK_EXPERIMENTAL_C_BENCHMARK_C_API_H_
#include <cstdint>
#include "tensorflow/lite/core/c/c_api_types.h"
// -----------------------------------------------------------------------------
// Experimental C APIs for the benchmark tool, mainly intended to be used for
// building a standalone TensorFlow Lite benchmark framework for iOS. This
// header only has a minimal dependency to the C API types, which can be
// included in the framework itself.
// -----------------------------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
typedef enum {
TfLiteBenchmarkWarmup,
TfLiteBenchmarkRegular,
} TfLiteBenchmarkRunType;
// -----------------------------------------------------------------------------
// C APIs corresponding to tsl::Stat<int64_t> type.
// -----------------------------------------------------------------------------
typedef struct TfLiteBenchmarkInt64Stat {
bool empty;
int64_t first;
int64_t newest;
int64_t max;
int64_t min;
int64_t count;
int64_t sum;
double squared_sum;
bool all_same;
double avg;
int64_t std_deviation;
} TfLiteBenchmarkInt64Stat;
// -----------------------------------------------------------------------------
// C APIs corresponding to tflite::benchmark::BenchmarkResults type.
// -----------------------------------------------------------------------------
typedef struct TfLiteBenchmarkResults TfLiteBenchmarkResults;
extern TfLiteBenchmarkInt64Stat
TfLiteBenchmarkResultsGetInferenceTimeMicroseconds(
const TfLiteBenchmarkResults* results);
extern TfLiteBenchmarkInt64Stat TfLiteBenchmarkResultsGetWarmupTimeMicroseconds(
const TfLiteBenchmarkResults* results);
extern int64_t TfLiteBenchmarkResultsGetStartupLatencyMicroseconds(
const TfLiteBenchmarkResults* results);
extern uint64_t TfLiteBenchmarkResultsGetInputBytes(
const TfLiteBenchmarkResults* results);
extern double TfLiteBenchmarkResultsGetThroughputMbPerSecond(
const TfLiteBenchmarkResults* results);
// -----------------------------------------------------------------------------
// C APIs corresponding to tflite::benchmark::BenchmarkListener type.
// -----------------------------------------------------------------------------
typedef struct TfLiteBenchmarkListener TfLiteBenchmarkListener;
extern TfLiteBenchmarkListener* TfLiteBenchmarkListenerCreate();
extern void TfLiteBenchmarkListenerDelete(TfLiteBenchmarkListener* listener);
// Sets the listener callbacks. Only non-null callback functions will be called
// when the following events occur. The user_data pointer provided by the caller
// will also be forwarded as a parameter of each callback function.
//
// - on_benchmark_start: Called before the (outer) inference loop begins. Note
// that this is called *after* the interpreter has been initialized, but
// *before* any warmup runs have been executed.
// - on_single_run_start: Called before a single (inner) inference call starts.
// - on_single_run_end: Called before a single (inner) inference call ends.
// - on_benchmark_end: Called after the (outer) inference loop ends.
//
// In case of `on_benchmark_end` callback, the passed in `results` pointer is
// only valid during the callback function execution, and will be destroyed
// afterwards.
extern void TfLiteBenchmarkListenerSetCallbacks(
TfLiteBenchmarkListener* listener, void* user_data,
void (*on_benchmark_start_fn)(void* user_data),
void (*on_single_run_start_fn)(void* user_data,
TfLiteBenchmarkRunType runType),
void (*on_single_run_end_fn)(void* user_data),
void (*on_benchmark_end_fn)(void* user_data,
TfLiteBenchmarkResults* results));
// -----------------------------------------------------------------------------
// C APIs corresponding to tflite::benchmark::BenchmarkTfLiteModel type.
// -----------------------------------------------------------------------------
typedef struct TfLiteBenchmarkTfLiteModel TfLiteBenchmarkTfLiteModel;
// TODO(b/144321502): Support BenchmarkParams.
extern TfLiteBenchmarkTfLiteModel* TfLiteBenchmarkTfLiteModelCreate();
extern void TfLiteBenchmarkTfLiteModelDelete(
TfLiteBenchmarkTfLiteModel* benchmark_model);
extern TfLiteStatus TfLiteBenchmarkTfLiteModelInit(
TfLiteBenchmarkTfLiteModel* benchmark_model);
extern TfLiteStatus TfLiteBenchmarkTfLiteModelRun(
TfLiteBenchmarkTfLiteModel* benchmark_model);
extern TfLiteStatus TfLiteBenchmarkTfLiteModelRunWithArgs(
TfLiteBenchmarkTfLiteModel* benchmark_model, int argc, char** argv);
extern void TfLiteBenchmarkTfLiteModelAddListener(
TfLiteBenchmarkTfLiteModel* benchmark_model,
const TfLiteBenchmarkListener* listener);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#endif // TENSORFLOW_LITE_TOOLS_BENCHMARK_EXPERIMENTAL_C_BENCHMARK_C_API_H_