@@ -826,131 +826,26 @@ def clz_comp(n):
826826
827827@pytest .mark .parametrize ("accel_type" , ACCEL_TYPES )
828828def test_tflite_tanh (accel_type ):
829- dtype = "int8"
830829 ifm_shape = [1 , 115 , 32 , 7 ]
831830
832- def create_tflite_graph ():
833- class Model (tf .Module ):
834- @tf .function
835- def tanh_function (self , x ):
836- op = tf .nn .tanh (x )
837- return op
838-
839- model = Model ()
840- concrete_func = model .tanh_function .get_concrete_function (
841- tf .TensorSpec (ifm_shape , dtype = tf .float32 )
842- )
843-
844- # Convert the model
845- def representative_dataset ():
846- for _ in range (100 ):
847- data = np .random .rand (* tuple (ifm_shape ))
848- yield [data .astype (np .float32 )]
849-
850- converter = tf .lite .TFLiteConverter .from_concrete_functions ([concrete_func ])
851- converter .optimizations = [tf .lite .Optimize .DEFAULT ]
852- converter .representative_dataset = representative_dataset
853- converter .target_spec .supported_ops = [tf .lite .OpsSet .TFLITE_BUILTINS_INT8 ]
854- converter .inference_input_type = tf .int8
855- converter .inference_output_type = tf .int8
856- tflite_model = converter .convert ()
857- return tflite_model
858-
859- tflite_graph = create_tflite_graph ()
860-
861- tflite_model = tflite .Model .Model .GetRootAsModel (tflite_graph , 0 )
862-
863- relay_module , params = relay .frontend .from_tflite (
864- tflite_model ,
865- shape_dict = {"input" : ifm_shape },
866- dtype_dict = {"input" : dtype },
867- )
868- mod = partition_for_ethosu (relay_module , params )
869-
870- # Generate reference data
871- input_data , output_data = infra .generate_ref_data_tflite (tflite_graph )
872-
873- compiled_models = infra .build_source (
874- mod ,
875- input_data ,
876- output_data ,
877- accel_type ,
878- )
879-
880- # Assumes only two runtime.Modules are created -- i.e. single offload module
881- ethosu_module = compiled_models [0 ].executor_factory .lib .imported_modules [0 ].imported_modules [0 ]
831+ @tf .function
832+ def tanh_func (x ):
833+ op = tf .nn .tanh (x )
834+ return op
882835
883- # Verify generated C source
884- get_artifacts = tvm ._ffi .get_global_func ("runtime.module.ethos-u.get_artifacts" )
885- compilation_artifacts = get_artifacts (ethosu_module )
886- cmms = bytes .fromhex (compilation_artifacts [0 ].command_stream )
887- infra .print_payload (cmms )
888- infra .verify_source (compiled_models , accel_type )
836+ _compare_tvm_with_tflite (tanh_func , [ifm_shape ], accel_type )
889837
890838
891839@pytest .mark .parametrize ("accel_type" , ACCEL_TYPES )
892- @pytest .mark .parametrize ("ifm_shape" , [[1 , 115 , 32 , 7 ], [1 , 4 , 5 , 2 ]])
893- def test_tflite_sigmoid (accel_type , ifm_shape ):
894- dtype = "int8"
895-
896- def create_tflite_graph ():
897- tf .config .run_functions_eagerly (True )
898-
899- class Model (tf .Module ):
900- @tf .function
901- def tanh_function (self , x ):
902- op = tf .nn .sigmoid (x )
903- return op
904-
905- model = Model ()
906- concrete_func = model .tanh_function .get_concrete_function (
907- tf .TensorSpec (ifm_shape , dtype = tf .float32 )
908- )
909-
910- # Convert the model
911- def representative_dataset ():
912- for _ in range (100 ):
913- data = np .random .rand (* tuple (ifm_shape ))
914- yield [data .astype (np .float32 )]
915-
916- converter = tf .lite .TFLiteConverter .from_concrete_functions ([concrete_func ])
917- converter .optimizations = [tf .lite .Optimize .DEFAULT ]
918- converter .representative_dataset = representative_dataset
919- converter .target_spec .supported_ops = [tf .lite .OpsSet .TFLITE_BUILTINS_INT8 ]
920- converter .inference_input_type = tf .int8
921- converter .inference_output_type = tf .int8
922- tflite_model = converter .convert ()
923- return tflite_model
924-
925- tflite_graph = create_tflite_graph ()
926- tflite_model = tflite .Model .Model .GetRootAsModel (tflite_graph , 0 )
927-
928- relay_module , params = relay .frontend .from_tflite (
929- tflite_model ,
930- shape_dict = {"input" : ifm_shape },
931- dtype_dict = {"input" : dtype },
932- )
933- mod = partition_for_ethosu (relay_module , params )
934-
935- # Generate reference data
936- input_data , output_data = infra .generate_ref_data_tflite (tflite_graph )
937-
938- compiled_models = infra .build_source (
939- mod ,
940- input_data ,
941- output_data ,
942- accel_type ,
943- )
840+ def test_tflite_sigmoid (accel_type ):
841+ ifm_shape = [1 , 135 , 41 , 6 ]
944842
945- # Assumes only two runtime.Modules are created -- i.e. single offload module
946- ethosu_module = compiled_models [0 ].executor_factory .lib .imported_modules [0 ].imported_modules [0 ]
843+ @tf .function
844+ def sigmoid_function (x ):
845+ op = tf .nn .sigmoid (x )
846+ return op
947847
948- # Verify generated C source
949- get_artifacts = tvm ._ffi .get_global_func ("runtime.module.ethos-u.get_artifacts" )
950- compilation_artifacts = get_artifacts (ethosu_module )
951- cmms = bytes .fromhex (compilation_artifacts [0 ].command_stream )
952- infra .print_payload (cmms )
953- infra .verify_source (compiled_models , accel_type )
848+ _compare_tvm_with_tflite (sigmoid_function , [ifm_shape ], accel_type )
954849
955850
956851if __name__ == "__main__" :
0 commit comments