77
88test_root_path = Path (__file__ ).parent
99
10- def flat_distribution (shape ):
11- return np .random .rand (* shape )
12-
13-
14- @pytest .fixture ()
15- def generate_data (function , input_shape ):
16- return function ((1000 , * input_shape ))
17-
18-
19- # TODO: include latency strategy with flat_distribution when it can be made to pass
20- @pytest .mark .parametrize ('backend,strategy,function,input_shape,io_type' , [
21- ('Vivado' , 'stable' , flat_distribution , (4 ,), 'io_parallel' ),
22- ('Quartus' , 'stable' , flat_distribution , (4 ,), 'io_parallel' ),
23-
24- # IO_stram avaliable just for VIVADO
25- ('Vivado' , 'stable' , flat_distribution , (4 ,), 'io_stream' ),
26- ('Vivado' , 'stable' , flat_distribution , (4 , 4 , 3 ), 'io_stream' )
10+ @pytest .mark .parametrize ('backend' , ['Vivado' , 'Quartus' ])
11+ @pytest .mark .parametrize ('input_shape, io_type' , [
12+ ((8 , ), 'io_parallel' ),
13+ ((8 , ), 'io_stream' ),
14+ ((8 , 8 , 3 ), 'io_stream' )
2715 ])
28- def test_softsign (backend , strategy , generate_data , input_shape , io_type ):
29- X = generate_data
16+ def test_softsign (backend , input_shape , io_type ):
17+ X = np . random . rand ( 1000 , * input_shape )
3018 model = tf .keras .models .Sequential ()
3119 model .add (tf .keras .layers .Activation (input_shape = input_shape , activation = 'softsign' , name = 'softsign' ))
3220 model .compile ()
3321
34- f_type = 'ac_fixed<18,8,true,AC_RND,AC_SAT>' if backend == 'Quartus' else 'ap_fixed<18,8,AP_RND,AP_SAT>'
35- cfg = hls4ml .utils .config_from_keras_model (model , granularity = 'name' )
36- cfg ['LayerName' ]['softsign' ]['Strategy' ] = strategy
37- cfg ['LayerName' ]['softsign' ]['inv_table_t' ] = f_type
38- cfg ['LayerName' ]['softsign' ]['exp_table_t' ] = f_type
39-
40- odir = str (test_root_path / 'hls4mlprj_softsign_{}' .format (strategy ))
22+ cfg = hls4ml .utils .config_from_keras_model (model , granularity = 'name' )
23+ odir = str (test_root_path / 'hls4mlprj_softsign_{}_{}_{}' .format (backend , io_type , str (input_shape )))
4124 hls_model = hls4ml .converters .convert_from_keras_model (model , hls_config = cfg , io_type = io_type ,
4225 output_dir = odir , backend = backend )
4326 hls_model .compile ()
4427
4528 y_keras = model .predict (X )
4629 y_hls4ml = hls_model .predict (X ).reshape (y_keras .shape )
47-
4830 acc_hls4ml = accuracy_score (np .argmax (y_keras , axis = - 1 ).ravel (), np .argmax (y_hls4ml , axis = - 1 ).ravel ())
4931
5032 print ('Accuracy hls4ml relative to keras: {}' .format (acc_hls4ml ))
51-
52- assert acc_hls4ml >= 0.98
53-
54-
33+ assert acc_hls4ml >= 0.97
0 commit comments