[go: nahoru, domu]

Skip to content

Commit

Permalink
Retain model metadata for 'reduced_precision_support' when sparsifying.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 495775017
  • Loading branch information
arfaian authored and tensorflower-gardener committed Dec 16, 2022
1 parent 0d19180 commit cb23291
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 4 deletions.
22 changes: 22 additions & 0 deletions tensorflow/compiler/mlir/lite/sparsity/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//tensorflow/core/platform:rules_cc.bzl", "cc_library")
load("//tensorflow:tensorflow.bzl", "tf_cc_test")

package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
Expand Down Expand Up @@ -33,11 +34,32 @@ cc_library(
"//tensorflow/core:protos_all_cc",
"//tensorflow/lite:framework",
"//tensorflow/lite/core/api",
"//tensorflow/lite/core/c:private_c_api_types",
"//tensorflow/lite/schema:schema_fbs",
"//tensorflow/lite/tools/optimize:reduced_precision_support",
"@com_google_absl//absl/strings",
"@flatbuffers",
"@llvm-project//llvm:Support",
"@llvm-project//mlir:FuncDialect",
"@llvm-project//mlir:IR",
"@llvm-project//mlir:Pass",
],
)

tf_cc_test(
name = "sparsify_model_test",
srcs = ["sparsify_model_test.cc"],
data = [
"//tensorflow/lite:testdata/sparse_tensor.bin",
],
deps = [
":sparsify_model",
"//tensorflow/lite/core:model_builder",
"//tensorflow/lite/core/api:error_reporter",
"//tensorflow/lite/core/c:private_c_api_types",
"//tensorflow/lite/schema:schema_fbs",
"//tensorflow/lite/tools/optimize:reduced_precision_support",
"@com_google_googletest//:gtest_main",
"@flatbuffers",
],
)
13 changes: 13 additions & 0 deletions tensorflow/compiler/mlir/lite/sparsity/sparsify_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ limitations under the License.
#include "tensorflow/compiler/mlir/lite/utils/convert_type.h"
#include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h"
#include "tensorflow/core/framework/types.pb.h"
#include "tensorflow/lite/tools/optimize/reduced_precision_support.h"

namespace mlir {
namespace lite {
Expand Down Expand Up @@ -75,6 +76,18 @@ TfLiteStatus SparsifyModel(const tflite::ModelT& input_model,
options.toco_flags.set_force_select_tf_ops(false);
options.toco_flags.set_enable_select_tf_ops(true);
options.toco_flags.set_allow_custom_ops(true);

// Copy metadata for Reduced Precision Support from input model if it exists
for (const auto& metadata : input_model.metadata) {
if (metadata->name != tflite::optimize::kTfLiteReducedPrecisionKey) {
continue;
}

const auto& data = input_model.buffers[metadata->buffer]->data;
options.metadata[metadata->name] = std::string(data.begin(), data.end());
break;
}

if (!tflite::MlirToFlatBufferTranslateFunction(module.get(), options,
&result)) {
error_reporter->Report("Failed to export MLIR to flatbuffer.");
Expand Down
6 changes: 2 additions & 4 deletions tensorflow/compiler/mlir/lite/sparsity/sparsify_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ limitations under the License.
#ifndef TENSORFLOW_COMPILER_MLIR_LITE_SPARSITY_SPARSIFY_MODEL_H_
#define TENSORFLOW_COMPILER_MLIR_LITE_SPARSITY_SPARSIFY_MODEL_H_

#include <memory>
#include <unordered_set>

#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers
#include "tensorflow/lite/core/api/error_reporter.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/core/c/c_api_types.h"
#include "tensorflow/lite/schema/schema_generated.h"

namespace mlir {
Expand Down
87 changes: 87 additions & 0 deletions tensorflow/compiler/mlir/lite/sparsity/sparsify_model_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* 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/compiler/mlir/lite/sparsity/sparsify_model.h"

#include <stdint.h>

#include <cstdarg>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "flatbuffers/flatbuffer_builder.h" // from @flatbuffers
#include "tensorflow/lite/core/api/error_reporter.h"
#include "tensorflow/lite/core/c/c_api_types.h"
#include "tensorflow/lite/core/model_builder.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/tools/optimize/reduced_precision_support.h"

namespace mlir {
namespace lite {
namespace {

class NoopErrorReporter : public ::tflite::ErrorReporter {
public:
int Report(const char* format, std::va_list args) override { return 0; }
};

TEST(SparsifyModelTest, MetadataIsAddedToOutputModel) {
std::string expected_key = tflite::optimize::kTfLiteReducedPrecisionKey;
std::string expected_value = "test_data";

// Load input model
auto input_fbm = tflite::FlatBufferModel::BuildFromFile(
"tensorflow/lite/testdata/sparse_tensor.bin");
tflite::ModelT input_model;
input_fbm->GetModel()->UnPackTo(&input_model);

// Populate input metadata
auto model_metadata_buffer = std::make_unique<tflite::BufferT>();
model_metadata_buffer->data =
std::vector<uint8_t>(expected_value.begin(), expected_value.end());
input_model.buffers.push_back(std::move(model_metadata_buffer));
auto metadata_t = std::make_unique<tflite::MetadataT>();
metadata_t->name = tflite::optimize::kTfLiteReducedPrecisionKey;
metadata_t->buffer = input_model.buffers.size() - 1;
input_model.metadata.push_back(std::move(metadata_t));

// Sparsify and create output model
flatbuffers::FlatBufferBuilder output_builder;
NoopErrorReporter reporter;
ASSERT_EQ(SparsifyModel(input_model, &output_builder, &reporter), kTfLiteOk);
auto output_fbm = tflite::FlatBufferModel::BuildFromBuffer(
reinterpret_cast<const char*>(output_builder.GetCurrentBufferPointer()),
output_builder.GetSize());
tflite::ModelT output_model;
output_fbm->GetModel()->UnPackTo(&output_model);

// Extract output metadata
std::map<std::string, std::string> output_metadata;
for (const auto& metadata : output_model.metadata) {
const auto& data = output_model.buffers[metadata->buffer]->data;
output_metadata[metadata->name] = std::string(data.begin(), data.end());
}

EXPECT_THAT(output_metadata,
testing::Contains(testing::Pair(expected_key, expected_value)));
}

} // namespace
} // namespace lite
} // namespace mlir

0 comments on commit cb23291

Please sign in to comment.