YongseopKim / ONE

On-device Neural Engine
Other
0 stars 0 forks source link

Investigate tflite v2.3.0 #21

Closed YongseopKim closed 4 years ago

YongseopKim commented 4 years ago

-DTFLITE_WITH_RUY_GEMV 의 의미

YongseopKim commented 4 years ago

tensorflow/tensorflow/lite/build_def.bzl

def tflite_linkopts_unstripped():
    """Defines linker flags to reduce size of TFLite binary.

       These are useful when trying to investigate the relative size of the
       symbols in TFLite.

    Returns:
       a select object with proper linkopts
    """

    # In case you wonder why there's no --icf is because the gains were
    # negligible, and created potential compatibility problems.
    return select({
        clean_dep("//tensorflow:android"): [
            "-Wl,--no-export-dynamic",  # Only inc syms referenced by dynamic obj.
            "-Wl,--gc-sections",  # Eliminate unused code and data.
            "-Wl,--as-needed",  # Don't link unused libs.
        ],
        "//conditions:default": [],
    })

def tflite_jni_linkopts_unstripped():
    """Defines linker flags to reduce size of TFLite binary with JNI.

       These are useful when trying to investigate the relative size of the
       symbols in TFLite.

    Returns:
       a select object with proper linkopts
    """

    # In case you wonder why there's no --icf is because the gains were
    # negligible, and created potential compatibility problems.
    return select({
        clean_dep("//tensorflow:android"): [
            "-Wl,--gc-sections",  # Eliminate unused code and data.
            "-Wl,--as-needed",  # Don't link unused libs.
        ],
        "//conditions:default": [],
    })

def gen_selected_ops(name, model, namespace = "", **kwargs):
    """Generate the library that includes only used ops.

    Args:
      name: Name of the generated library.
      model: TFLite models to interpret, expect a list in case of multiple models.
      namespace: Namespace in which to put RegisterSelectedOps.
      **kwargs: Additional kwargs to pass to genrule.
    """
    out = name + "_registration.cc"
    tool = clean_dep("//tensorflow/lite/tools:generate_op_registrations")
    tflite_path = "//tensorflow/lite"

    # isinstance is not supported in skylark.
    if type(model) != type([]):
        model = [model]

    input_models_args = " --input_models=%s" % ",".join(
        ["$(location %s)" % f for f in model],
    )

    native.genrule(
        name = name,
        srcs = model,
        outs = [out],
        cmd = ("$(location %s) --namespace=%s --output_registration=$(location %s) --tflite_path=%s %s") %
              (tool, namespace, out, tflite_path[2:], input_models_args),
        tools = [tool],
        **kwargs
    )
YongseopKim commented 4 years ago

lite/tools/make/Makefile

# Benchmark sources
BENCHMARK_SRCS_DIR := tensorflow/lite/tools/benchmark
DELEGATE_PROVIDER_SRCS_DIR := tensorflow/lite/tools/delegates
EVALUATION_UTILS_SRCS := \
  tensorflow/lite/tools/evaluation/utils.cc
BENCHMARK_ALL_SRCS := \
    $(wildcard $(BENCHMARK_SRCS_DIR)/*.cc) \
    $(PROFILE_SUMMARIZER_SRCS) \
    $(CMD_LINE_TOOLS_SRCS) \
    $(EVALUATION_UTILS_SRCS)

BENCHMARK_MAIN_SRC := $(BENCHMARK_SRCS_DIR)/benchmark_main.cc
BENCHMARK_PERF_OPTIONS_SRC := \
    $(BENCHMARK_SRCS_DIR)/benchmark_tflite_performance_options_main.cc
BENCHMARK_LIB_SRCS := $(filter-out \
    $(wildcard $(BENCHMARK_SRCS_DIR)/*_test.cc) \
    $(BENCHMARK_MAIN_SRC) \
    $(BENCHMARK_PERF_OPTIONS_SRC) \
    $(BENCHMARK_SRCS_DIR)/benchmark_plus_flex_main.cc \
    $(DELEGATE_PROVIDER_SRCS_DIR)/default_execution_provider.cc \
    $(DELEGATE_PROVIDER_SRCS_DIR)/external_delegate_provider.cc \
    $(DELEGATE_PROVIDER_SRCS_DIR)/gpu_delegate_provider.cc \
    $(DELEGATE_PROVIDER_SRCS_DIR)/hexagon_delegate_provider.cc \
    $(DELEGATE_PROVIDER_SRCS_DIR)/nnapi_delegate_provider.cc \
    $(DELEGATE_PROVIDER_SRCS_DIR)/xnnpack_delegate_provider.cc, \
    $(BENCHMARK_ALL_SRCS))
dragon@loki:~/Works/github/tensorflow/tensorflow/lite$ ls tools/delegates/ -1
BUILD
coreml_delegate_provider.cc
default_execution_provider.cc
delegate_provider.h
external_delegate_provider.cc
gpu_delegate_provider.cc
hexagon_delegate_provider.cc
nnapi_delegate_provider.cc
README.md
xnnpack_delegate_provider.cc
YongseopKim commented 4 years ago

lite/tools/delegates/gpu_delegate_provider.cc

#if defined(__ANDROID__)
#include "tensorflow/lite/delegates/gpu/delegate.h"

class GpuDelegateProvider : public DelegateProvider {
 public:
  GpuDelegateProvider() {
    default_params_.AddParam("use_gpu", ToolParam::Create<bool>(false));
#if defined(__ANDROID__) || defined(REAL_IPHONE_DEVICE)
    default_params_.AddParam("gpu_precision_loss_allowed",
                             ToolParam::Create<bool>(true));
    default_params_.AddParam("gpu_experimental_enable_quant",
                             ToolParam::Create<bool>(true));
#endif
#if defined(__ANDROID__)
    default_params_.AddParam("gpu_backend", ToolParam::Create<std::string>(""));
#endif
YongseopKim commented 4 years ago

lite/delegates/gpu/delegate.cc

// Represent the execution of a subset of nodes on GPU.
class DelegateKernel {
 public:
  explicit DelegateKernel(Delegate* delegate) : delegate_(delegate) {
    ++delegate_->num_delegate_kernels_;
  }
  ~DelegateKernel() { --delegate_->num_delegate_kernels_; }

  absl::Status Prepare(TfLiteContext* context,
                       const TfLiteDelegateParams* delegate_params) {
    // ...

    const int experimental_flags = delegate_->options().experimental_flags;
    if (experimental_flags & TFLITE_GPU_EXPERIMENTAL_FLAGS_CL_ONLY) {
      RETURN_IF_ERROR(
          InitializeOpenClApi(&graph, &builder, &graph_is_destroyed));
    } else if (experimental_flags & TFLITE_GPU_EXPERIMENTAL_FLAGS_GL_ONLY) {
      RETURN_IF_ERROR(InitializeOpenGlApi(&graph, &builder));
    } else {
      // By default, we try CL first & fall back to GL if that fails.
      absl::Status status =
          InitializeOpenClApi(&graph, &builder, &graph_is_destroyed);
      if (!status.ok()) {
        TF_LITE_KERNEL_LOG(context, std::string(status.message()).c_str());
        TF_LITE_KERNEL_LOG(context, "Falling back to OpenGL");

        // Graph needs to be re-created because it is moved above.
        GraphFloat32 graph2;
        if (graph_is_destroyed) {
          RETURN_IF_ERROR(InitializeGraph(context, delegate_params, &graph2,
                                          &input_refs, &output_refs));
        }
        RETURN_IF_ERROR(InitializeOpenGlApi(
            graph_is_destroyed ? &graph2 : &graph, &builder));
      }
    }

    // ...
YongseopKim commented 4 years ago

minimal_logging.h

// Convenience macro for basic internal logging in production builds.
// Note: This should never be used for debug-type logs, as it will *not* be
// stripped in release optimized builds. In general, prefer the error reporting
// APIs for developer-facing errors, and only use this for diagnostic output
// that should always be logged in user builds.
#define TFLITE_LOG_PROD(severity, format, ...) \
  tflite::logging_internal::MinimalLogger::Log(severity, format, ##__VA_ARGS__);

// Convenience macro for logging a statement *once* for a given process lifetime
// in production builds.
#define TFLITE_LOG_PROD_ONCE(severity, format, ...)    \
  do {                                                 \
    static const bool s_logged = [&] {                 \
      TFLITE_LOG_PROD(severity, format, ##__VA_ARGS__) \
      return true;                                     \
    }();                                               \
    (void)s_logged;                                    \
  } while (false);

#ifndef NDEBUG
// In debug builds, always log.
#define TFLITE_LOG TFLITE_LOG_PROD
#define TFLITE_LOG_ONCE TFLITE_LOG_PROD_ONCE
#else
// In prod builds, never log, but ensure the code is well-formed and compiles.
#define TFLITE_LOG(severity, format, ...)             \
  while (false) {                                     \
    TFLITE_LOG_PROD(severity, format, ##__VA_ARGS__); \
  }
#define TFLITE_LOG_ONCE TFLITE_LOG
#endif