7
7
8
8
test_root_path = Path (__file__ ).parent
9
9
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' )
27
15
])
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 )
30
18
model = tf .keras .models .Sequential ()
31
19
model .add (tf .keras .layers .Activation (input_shape = input_shape , activation = 'softsign' , name = 'softsign' ))
32
20
model .compile ()
33
21
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 )))
41
24
hls_model = hls4ml .converters .convert_from_keras_model (model , hls_config = cfg , io_type = io_type ,
42
25
output_dir = odir , backend = backend )
43
26
hls_model .compile ()
44
27
45
28
y_keras = model .predict (X )
46
29
y_hls4ml = hls_model .predict (X ).reshape (y_keras .shape )
47
-
48
30
acc_hls4ml = accuracy_score (np .argmax (y_keras , axis = - 1 ).ravel (), np .argmax (y_hls4ml , axis = - 1 ).ravel ())
49
31
50
32
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