Remove unused pybind for sparsification.

PiperOrigin-RevId: 308519086
Change-Id: I8d52624a39e3c013dd302a1978ccc3ed075829f1
This commit is contained in:
Yunlu Li 2020-04-26 12:49:34 -07:00 committed by TensorFlower Gardener
parent 5332c9b92b
commit 56000c5d70
6 changed files with 0 additions and 366 deletions

View File

@ -24,22 +24,6 @@ cc_library(
],
)
cc_library(
name = "sparsification_wrapper_lib",
srcs = ["sparsification_wrapper.cc"],
hdrs = ["sparsification_wrapper.h"],
deps = [
"//tensorflow/compiler/mlir/lite/sparsity:sparsify_model",
"//tensorflow/lite:framework",
"//tensorflow/lite/c:common",
"//tensorflow/lite/python/interpreter_wrapper:numpy",
"//tensorflow/lite/python/interpreter_wrapper:python_error_reporter",
"//tensorflow/lite/python/interpreter_wrapper:python_utils",
"//third_party/python_runtime:headers", # buildcleaner: keep
"@com_google_absl//absl/memory",
],
)
pybind_extension(
name = "_pywrap_tensorflow_lite_calibration_wrapper",
srcs = [
@ -57,22 +41,6 @@ pybind_extension(
],
)
pybind_extension(
name = "_pywrap_tensorflow_lite_sparsification_wrapper",
srcs = [
"sparsification_wrapper_pybind11.cc",
],
hdrs = ["sparsification_wrapper.h"],
link_in_framework = True,
module_name = "_pywrap_tensorflow_lite_sparsification_wrapper",
deps = [
":sparsification_wrapper_lib",
"//tensorflow/python:pybind11_lib",
"//third_party/python_runtime:headers",
"@pybind11",
],
)
py_library(
name = "calibrator",
srcs = [
@ -87,19 +55,6 @@ py_library(
],
)
py_library(
name = "sparsifier",
srcs = [
"sparsifier.py",
],
srcs_version = "PY2AND3",
visibility = ["//visibility:public"],
deps = [
"_pywrap_tensorflow_lite_sparsification_wrapper", # buildcleaner: keep
"//tensorflow/python:util",
],
)
py_test(
name = "calibrator_test",
srcs = ["calibrator_test.py"],
@ -121,20 +76,3 @@ py_test(
"@six_archive//:six",
],
)
py_test(
name = "sparsifier_test",
srcs = ["sparsifier_test.py"],
data = [
"//tensorflow/lite:testdata/multi_add.bin",
],
python_version = "PY3",
srcs_version = "PY2AND3",
tags = ["no_oss"],
deps = [
":sparsifier",
"//tensorflow/python:client_testlib",
"//tensorflow/python:framework_test_lib",
"//tensorflow/python:platform",
],
)

View File

@ -1,102 +0,0 @@
/* 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/lite/python/optimize/sparsification_wrapper.h"
#include <memory>
#include <sstream>
#include <string>
#include "absl/memory/memory.h"
#include "tensorflow/compiler/mlir/lite/sparsity/sparsify_model.h"
#include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/python/interpreter_wrapper/numpy.h"
#include "tensorflow/lite/python/interpreter_wrapper/python_error_reporter.h"
#include "tensorflow/lite/python/interpreter_wrapper/python_utils.h"
#define TFLITE_PY_CHECK(x) \
if ((x) != kTfLiteOk) { \
return error_reporter_->exception(); \
}
#define TFLITE_PY_ENSURE_VALID_INTERPRETER() \
if (!interpreter_) { \
PyErr_SetString(PyExc_ValueError, "Interpreter was not initialized."); \
return nullptr; \
}
namespace tflite {
namespace sparsification_wrapper {
namespace {
std::unique_ptr<tflite::ModelT> CreateMutableModel(const tflite::Model& model) {
auto copied_model = absl::make_unique<tflite::ModelT>();
model.UnPackTo(copied_model.get(), nullptr);
return copied_model;
}
} // namespace
SparsificationWrapper::SparsificationWrapper(
std::unique_ptr<tflite::FlatBufferModel> model,
std::unique_ptr<tflite::interpreter_wrapper::PythonErrorReporter>
error_reporter)
: model_(std::move(model)), error_reporter_(std::move(error_reporter)) {}
SparsificationWrapper::~SparsificationWrapper() {}
PyObject* SparsificationWrapper::SparsifyModel() {
auto tflite_model = CreateMutableModel(*model_->GetModel());
flatbuffers::FlatBufferBuilder builder;
auto status = kTfLiteOk;
status =
mlir::lite::SparsifyModel(*tflite_model, &builder, error_reporter_.get());
if (status != kTfLiteOk) {
error_reporter_->exception();
return nullptr;
}
return python_utils::ConvertToPyString(
reinterpret_cast<const char*>(builder.GetCurrentBufferPointer()),
builder.GetSize());
}
/*static*/ SparsificationWrapper*
SparsificationWrapper::CreateWrapperCPPFromBuffer(PyObject* data) {
using tflite::interpreter_wrapper::PythonErrorReporter;
char* buf = nullptr;
Py_ssize_t length;
std::unique_ptr<PythonErrorReporter> error_reporter(new PythonErrorReporter);
::tflite::python::ImportNumpy();
if (python_utils::ConvertFromPyString(data, &buf, &length) == -1) {
return nullptr;
}
std::unique_ptr<tflite::FlatBufferModel> model =
tflite::FlatBufferModel::BuildFromBuffer(buf, length,
error_reporter.get());
if (!model) {
PyErr_Format(PyExc_ValueError, "Invalid model");
return nullptr;
}
auto wrapper =
new SparsificationWrapper(std::move(model), std::move(error_reporter));
return wrapper;
}
} // namespace sparsification_wrapper
} // namespace tflite

View File

@ -1,63 +0,0 @@
/* 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.
==============================================================================*/
#ifndef TENSORFLOW_LITE_PYTHON_OPTIMIZE_SPARSIFICATION_WRAPPER_H_
#define TENSORFLOW_LITE_PYTHON_OPTIMIZE_SPARSIFICATION_WRAPPER_H_
#include <memory>
#include <string>
#include <vector>
// Place `<locale>` before <Python.h> to avoid build failures in macOS.
#include <locale>
// The empty line above is on purpose as otherwise clang-format will
// automatically move <Python.h> before <locale>.
#include <Python.h>
// We forward declare TFLite classes here to avoid exposing them to SWIG.
namespace tflite {
class FlatBufferModel;
namespace interpreter_wrapper {
class PythonErrorReporter;
} // namespace interpreter_wrapper
namespace sparsification_wrapper {
class SparsificationWrapper {
public:
// SWIG caller takes ownership of pointer.
static SparsificationWrapper* CreateWrapperCPPFromBuffer(PyObject* data);
~SparsificationWrapper();
PyObject* SparsifyModel();
private:
// SparsificationWrapper is not copyable or assignable. We avoid the use of
// SparsificationWrapper() = delete here for SWIG compatibility.
SparsificationWrapper(
std::unique_ptr<tflite::FlatBufferModel> model,
std::unique_ptr<tflite::interpreter_wrapper::PythonErrorReporter>
error_reporter);
std::unique_ptr<tflite::FlatBufferModel> model_;
std::unique_ptr<tflite::interpreter_wrapper::PythonErrorReporter>
error_reporter_;
};
} // namespace sparsification_wrapper
} // namespace tflite
#endif // TENSORFLOW_LITE_PYTHON_OPTIMIZE_SPARSIFICATION_WRAPPER_H_

View File

@ -1,35 +0,0 @@
/* 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 "pybind11/pybind11.h"
#include "pybind11/pytypes.h"
#include "tensorflow/lite/python/optimize/sparsification_wrapper.h"
#include "tensorflow/python/lib/core/pybind11_lib.h"
namespace py = pybind11;
using tflite::sparsification_wrapper::SparsificationWrapper;
PYBIND11_MODULE(_pywrap_tensorflow_lite_sparsification_wrapper, m) {
m.doc() = R"pbdoc(
_pywrap_tensorflow_lite_sparsification_wrapper
-----
)pbdoc";
py::class_<SparsificationWrapper>(m, "SparsificationWrapper")
.def(py::init([](py::handle& data) {
return ::SparsificationWrapper::CreateWrapperCPPFromBuffer(data.ptr());
}))
.def("SparsifyModel", [](SparsificationWrapper& self) {
return tensorflow::PyoOrThrow(self.SparsifyModel());
});
}

View File

@ -1,62 +0,0 @@
# 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.
# ==============================================================================
"""Python wrapper for convert models from dense to sparse format."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from tensorflow.python.util.lazy_loader import LazyLoader
# Lazy load since some of the performance benchmark skylark rules
# break dependencies. Must use double quotes to match code internal rewrite
# rule.
_sparsification_wrapper = LazyLoader(
"_sparsification_wrapper", globals(),
"tensorflow.lite.python.optimize."
"_pywrap_tensorflow_lite_sparsification_wrapper")
class Sparsifier(object):
"""Convert a model from dense to sparse format.
This is an internal class, not a public interface.
"""
def __init__(self, model_content):
"""Constructor.
Args:
model_content: Content of a TFLite Flatbuffer file.
Raises:
ValueError: If unable to open the model.
"""
if not model_content:
raise ValueError("`model_content` must be specified.")
try:
self._sparsifier = (
_sparsification_wrapper.SparsificationWrapper(model_content))
except Exception as e:
raise ValueError("Failed to parse the model: %s." % e)
if not self._sparsifier:
raise ValueError("Failed to parse the model.")
def sparsify(self):
"""Convert the model to sparse format.
Returns:
A sparse model.
"""
return self._sparsifier.SparsifyModel()

View File

@ -1,42 +0,0 @@
# Lint as: python2, python3
# # 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.
# ==============================================================================
"""Tests for tensorflow.lite.python.optimize.format_converter."""
# These 3 lines below are not necessary in a Python 3-only module
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from tensorflow.lite.python.optimize import sparsifier
from tensorflow.python.framework import test_util
from tensorflow.python.platform import resource_loader
from tensorflow.python.platform import test
class SparsifierTest(test_util.TensorFlowTestCase):
def test_simple(self):
model_path = resource_loader.get_path_to_datafile(
'../../testdata/multi_add.bin')
dense_model = open(model_path, 'rb').read()
converter = sparsifier.Sparsifier(dense_model)
sparse_model = converter.sparsify()
self.assertIsNotNone(sparse_model)
if __name__ == '__main__':
test.main()