1# Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Wraps toco interface with python lazy loader.""" 16# We need to import pywrap_tensorflow prior to the toco wrapper. 17# pylint: disable=invalid-import-order,g-bad-import-order 18from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import 19from tensorflow.python import _pywrap_toco_api 20 21# TODO(b/137402359): Remove lazy loading wrapper 22 23 24def wrapped_toco_convert(model_flags_str, toco_flags_str, input_data_str, 25 debug_info_str, enable_mlir_converter): 26 """Wraps TocoConvert with lazy loader.""" 27 return _pywrap_toco_api.TocoConvert( 28 model_flags_str, 29 toco_flags_str, 30 input_data_str, 31 False, # extended_return 32 debug_info_str, 33 enable_mlir_converter) 34 35 36def wrapped_experimental_mlir_quantize(input_data_str, disable_per_channel, 37 fully_quantize, inference_type, 38 input_data_type, output_data_type, 39 enable_numeric_verify, 40 enable_whole_model_verify, 41 denylisted_ops, denylisted_nodes): 42 """Wraps experimental mlir quantize model.""" 43 return _pywrap_toco_api.ExperimentalMlirQuantizeModel( 44 input_data_str, disable_per_channel, fully_quantize, inference_type, 45 input_data_type, output_data_type, enable_numeric_verify, 46 enable_whole_model_verify, denylisted_ops, denylisted_nodes) 47 48 49def wrapped_experimental_mlir_sparsify(input_data_str): 50 """Wraps experimental mlir sparsify model.""" 51 return _pywrap_toco_api.ExperimentalMlirSparsifyModel(input_data_str) 52 53 54def wrapped_register_custom_opdefs(custom_opdefs_list): 55 """Wraps RegisterCustomOpdefs with lazy loader.""" 56 return _pywrap_toco_api.RegisterCustomOpdefs(custom_opdefs_list) 57 58 59def wrapped_retrieve_collected_errors(): 60 """Wraps RetrieveCollectedErrors with lazy loader.""" 61 return _pywrap_toco_api.RetrieveCollectedErrors() 62 63 64def wrapped_flat_buffer_file_to_mlir(model, input_is_filepath): 65 """Wraps FlatBufferFileToMlir with lazy loader.""" 66 return _pywrap_toco_api.FlatBufferToMlir(model, input_is_filepath) 67