Skip to content
Snippets Groups Projects
Commit e1e26246 authored by Yaman Umuroglu's avatar Yaman Umuroglu
Browse files

[Test] remove old MultiThreshold impl, fix test

parent 45d2ad62
No related branches found
No related tags found
No related merge requests found
import numpy as np
def compare(x, y):
if x >= y:
return 1.0
else:
return 0.0
def execute(v, thresholds, out_scale=None, out_bias=None):
# the inputs are expected to be in the shape (N,C,H,W)
# N : Batch size
# C : Number of channels
# H : Heigth of the input images
# W : Width of the input images
#
# the thresholds are expected to be in the shape (C, B)
# C : Number of channels (must be the same value as C in input tensor or 1
# if all channels use the same threshold value)
# B : Desired activation steps => i.e. for 4-bit activation, B=7 (2^(n)-1 and n=4)
# the output tensor will be scaled by out_scale and biased by out_bias
# assert threshold shape
is_global_threshold = thresholds.shape[0] == 1
assert (v.shape[1] == thresholds.shape[0]) or is_global_threshold
# save the required shape sizes for the loops (N, C and B)
num_batch = v.shape[0]
num_channel = v.shape[1]
num_act = thresholds.shape[1]
# reshape inputs to enable channel-wise reading
vr = v.reshape((v.shape[0], v.shape[1], -1))
# save the new shape size of the images
num_img_elem = vr.shape[2]
# initiate output tensor
ret = np.zeros_like(vr)
# iterate over thresholds channel-wise
for t in range(num_channel):
channel_thresh = thresholds[0] if is_global_threshold else thresholds[t]
# iterate over batches
for b in range(num_batch):
# iterate over image elements on which the thresholds should be applied
for elem in range(num_img_elem):
# iterate over the different thresholds that correspond to one channel
for a in range(num_act):
# apply successive thresholding to every element of one channel
ret[b][t][elem] += compare(vr[b][t][elem], channel_thresh[a])
if out_scale is None:
out_scale = 1.0
if out_bias is None:
out_bias = 0.0
return out_scale * ret.reshape(v.shape) + out_bias
import numpy as np
import finn.core.multithreshold as multi_thresh
from finn.custom_op.multithreshold import MultiThreshold
def test_execute_multi_thresholding():
......@@ -194,10 +194,11 @@ def test_execute_multi_thresholding():
),
)
results = multi_thresh.execute(inputs, thresholds)
multi_thresh = MultiThreshold()
results = multi_thresh._execute(inputs, thresholds)
assert (results == outputs).all()
results_scaled = multi_thresh.execute(inputs, thresholds, 2.0, -1.0)
results_scaled = multi_thresh._execute(inputs, thresholds, 2.0, -1.0)
outputs_scaled = 2.0 * outputs - 1.0
assert (results_scaled == outputs_scaled).all()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment