From 56000c5d70dfc514c3331dea10123963a404c0d3 Mon Sep 17 00:00:00 2001 From: Yunlu Li Date: Sun, 26 Apr 2020 12:49:34 -0700 Subject: [PATCH] Remove unused pybind for sparsification. PiperOrigin-RevId: 308519086 Change-Id: I8d52624a39e3c013dd302a1978ccc3ed075829f1 --- tensorflow/lite/python/optimize/BUILD | 62 ----------- .../python/optimize/sparsification_wrapper.cc | 102 ------------------ .../python/optimize/sparsification_wrapper.h | 63 ----------- .../sparsification_wrapper_pybind11.cc | 35 ------ tensorflow/lite/python/optimize/sparsifier.py | 62 ----------- .../lite/python/optimize/sparsifier_test.py | 42 -------- 6 files changed, 366 deletions(-) delete mode 100644 tensorflow/lite/python/optimize/sparsification_wrapper.cc delete mode 100644 tensorflow/lite/python/optimize/sparsification_wrapper.h delete mode 100644 tensorflow/lite/python/optimize/sparsification_wrapper_pybind11.cc delete mode 100644 tensorflow/lite/python/optimize/sparsifier.py delete mode 100644 tensorflow/lite/python/optimize/sparsifier_test.py diff --git a/tensorflow/lite/python/optimize/BUILD b/tensorflow/lite/python/optimize/BUILD index 53ebba2fcb2..1a0d3db3b73 100644 --- a/tensorflow/lite/python/optimize/BUILD +++ b/tensorflow/lite/python/optimize/BUILD @@ -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", - ], -) diff --git a/tensorflow/lite/python/optimize/sparsification_wrapper.cc b/tensorflow/lite/python/optimize/sparsification_wrapper.cc deleted file mode 100644 index 3526ac0b129..00000000000 --- a/tensorflow/lite/python/optimize/sparsification_wrapper.cc +++ /dev/null @@ -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 -#include -#include - -#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 CreateMutableModel(const tflite::Model& model) { - auto copied_model = absl::make_unique(); - model.UnPackTo(copied_model.get(), nullptr); - return copied_model; -} - -} // namespace - -SparsificationWrapper::SparsificationWrapper( - std::unique_ptr model, - std::unique_ptr - 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(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 error_reporter(new PythonErrorReporter); - ::tflite::python::ImportNumpy(); - - if (python_utils::ConvertFromPyString(data, &buf, &length) == -1) { - return nullptr; - } - std::unique_ptr 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 diff --git a/tensorflow/lite/python/optimize/sparsification_wrapper.h b/tensorflow/lite/python/optimize/sparsification_wrapper.h deleted file mode 100644 index b6c5ae7147e..00000000000 --- a/tensorflow/lite/python/optimize/sparsification_wrapper.h +++ /dev/null @@ -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 -#include -#include - -// Place `` before to avoid build failures in macOS. -#include - -// The empty line above is on purpose as otherwise clang-format will -// automatically move before . -#include - -// 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 model, - std::unique_ptr - error_reporter); - std::unique_ptr model_; - std::unique_ptr - error_reporter_; -}; - -} // namespace sparsification_wrapper -} // namespace tflite - -#endif // TENSORFLOW_LITE_PYTHON_OPTIMIZE_SPARSIFICATION_WRAPPER_H_ diff --git a/tensorflow/lite/python/optimize/sparsification_wrapper_pybind11.cc b/tensorflow/lite/python/optimize/sparsification_wrapper_pybind11.cc deleted file mode 100644 index 641c208c2fb..00000000000 --- a/tensorflow/lite/python/optimize/sparsification_wrapper_pybind11.cc +++ /dev/null @@ -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_(m, "SparsificationWrapper") - .def(py::init([](py::handle& data) { - return ::SparsificationWrapper::CreateWrapperCPPFromBuffer(data.ptr()); - })) - .def("SparsifyModel", [](SparsificationWrapper& self) { - return tensorflow::PyoOrThrow(self.SparsifyModel()); - }); -} diff --git a/tensorflow/lite/python/optimize/sparsifier.py b/tensorflow/lite/python/optimize/sparsifier.py deleted file mode 100644 index a91d78be1fb..00000000000 --- a/tensorflow/lite/python/optimize/sparsifier.py +++ /dev/null @@ -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() diff --git a/tensorflow/lite/python/optimize/sparsifier_test.py b/tensorflow/lite/python/optimize/sparsifier_test.py deleted file mode 100644 index 31904545d77..00000000000 --- a/tensorflow/lite/python/optimize/sparsifier_test.py +++ /dev/null @@ -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()