1# Copyright 2022 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"""Test configs for tensor_list_set_item.""" 16import functools 17 18import tensorflow.compat.v1 as tf 19from tensorflow.lite.testing.zip_test_utils import create_tensor_data 20from tensorflow.lite.testing.zip_test_utils import make_zip_of_tests 21from tensorflow.lite.testing.zip_test_utils import register_make_test_function 22from tensorflow.python.ops import list_ops 23 24 25def _tflite_convert_verify_op(tflite_convert_function, *args, **kwargs): 26 """Verifies that the result of the conversion contains DynamicUpdateSlice op.""" 27 result = tflite_convert_function(*args, **kwargs) 28 tflite_model_binary = result[0] 29 if not result[0]: 30 tf.compat.v1.logging.error(result[1]) # stderr from running tflite_convert. 31 raise RuntimeError("Failed to build model: \n\n" + result[1]) 32 interpreter = tf.lite.Interpreter(model_content=tflite_model_binary) 33 interpreter.allocate_tensors() 34 for op in interpreter._get_ops_details(): # pylint: disable=protected-access 35 if op["op_name"] == "DYNAMIC_UPDATE_SLICE": 36 return result 37 raise RuntimeError( 38 "Expected to generate DYNAMIC_UPDATE_SLICE op node in graph.") 39 40 41@register_make_test_function() 42def make_dynamic_update_slice_tests(options): 43 """Make a set of tests to do TensorListSetItem.""" 44 45 test_parameters = [ 46 { 47 "element_dtype": [tf.float32, tf.int32, tf.bool], 48 "num_elements": [4, 5, 6], 49 "element_shape": [[], [5], [3, 3]], 50 "index": [0, 1, 2, 3], 51 }, 52 ] 53 54 def build_graph(parameters): 55 """Build the TensorListSetItem op testing graph.""" 56 data = tf.placeholder( 57 dtype=parameters["element_dtype"], 58 shape=[parameters["num_elements"]] + parameters["element_shape"]) 59 item = tf.placeholder( 60 dtype=parameters["element_dtype"], shape=parameters["element_shape"]) 61 tensor_list = list_ops.tensor_list_from_tensor(data, 62 parameters["element_shape"]) 63 tensor_list = list_ops.tensor_list_set_item(tensor_list, 64 parameters["index"], item) 65 out = list_ops.tensor_list_stack( 66 tensor_list, 67 num_elements=parameters["num_elements"], 68 element_dtype=parameters["element_dtype"]) 69 return [data, item], [out] 70 71 def build_inputs(parameters, sess, inputs, outputs): 72 data = create_tensor_data(parameters["element_dtype"], 73 [parameters["num_elements"]] + 74 parameters["element_shape"]) 75 item = create_tensor_data(parameters["element_dtype"], 76 parameters["element_shape"]) 77 return [data, item], sess.run( 78 outputs, feed_dict=dict(zip(inputs, [data, item]))) 79 80 options.enable_dynamic_update_slice = True 81 options.tflite_convert_function = functools.partial( 82 _tflite_convert_verify_op, options.tflite_convert_function) 83 make_zip_of_tests(options, test_parameters, build_graph, build_inputs) 84