Skip to content

Commit 7f19a1a

Browse files
committed
fix activation matching, fix bw edge case handle
1 parent fe0ff2f commit 7f19a1a

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

hls4ml/model/optimizer/passes/bit_exact.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -431,29 +431,29 @@ def _(layer: Concatenate):
431431

432432
@produce_kif.register
433433
def _(layer: Activation):
434-
fn_name = layer.attributes.attributes['activation']
434+
fn_name = layer.attributes.attributes['activation'].lower()
435435
k, i, f = get_input_kifs(layer)[0]
436436

437-
if fn_name == 'linear':
438-
return k, i, f
439-
if fn_name == 'relu':
440-
print(k.__class__)
441-
k = np.zeros_like(k)
442-
return k, i, f
443-
if fn_name == 'tanh':
444-
i = np.minimum(i, 1)
445-
f = np.full_like(f, 126)
446-
return k, i, f
447-
if fn_name == 'sigmoid':
448-
k = np.zeros_like(k)
449-
i = np.minimum(i, 1)
450-
f = np.full_like(f, 126)
451-
return k, i, f
452-
453-
k = np.zeros_like(k)
454-
i = np.full_like(i, 1)
455-
f = np.full_like(f, 126)
456-
return k, i, f
437+
match fn_name:
438+
case 'linear':
439+
return k, i, f
440+
case 'relu':
441+
k = np.zeros_like(k)
442+
return k, i, f
443+
case 'tanh':
444+
i = np.minimum(i, 1)
445+
f = np.full_like(f, 126)
446+
return k, i, f
447+
case 'sigmoid':
448+
k = np.zeros_like(k)
449+
i = np.minimum(i, 1)
450+
f = np.full_like(f, 126)
451+
return k, i, f
452+
case _:
453+
k = np.zeros_like(k)
454+
i = np.full_like(i, 1)
455+
f = np.full_like(f, 126)
456+
return k, i, f
457457

458458

459459
@produce_kif.register
@@ -476,10 +476,12 @@ def default_register_precision(layer: Layer):
476476
_pk, _pi, _pf = produce_kif(layer) # Maximum possible k,i,f output from this layer
477477
_rk, _ri, _rf = requested_kif(layer) # Maximum possible k,i,f may be utilized by the next layer
478478
_ok, _oi, _of = np.minimum(_pk, _rk), np.minimum(_pi, _ri), np.minimum(_pf, _rf)
479-
_oi += ((_pf > _rf) & (_pi <= _ri)).astype(np.int8) # Corner cases overflow prevention
479+
ok, oi, of = kif_arrs_to_ints((_ok, _oi, _of))
480+
481+
if np.max(_pf) > np.max(_rf) and np.max(_pi) <= np.max(_ri):
482+
oi += 1 # Edge cases overflow prevention
480483

481-
result_kif = kif_arrs_to_ints((_ok, _oi, _of))
482-
result_t = to_hls4ml_fixed(*result_kif, f'{layer.name}_t')
484+
result_t = to_hls4ml_fixed(ok, oi, of, f'{layer.name}_t')
483485
layer.attributes.attributes['result_t'] = result_t
484486
layer.get_output_variable().type = result_t
485487

0 commit comments

Comments
 (0)