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

[Test] use binaryXnorMode feature to test bipolar by bipolar

parent cb199796
No related branches found
No related tags found
No related merge requests found
......@@ -27,6 +27,21 @@ def make_single_fclayer_modelwrapper(W, pe, simd, wdt, idt, odt, T=None, tdt=Non
else:
tmem = 0
# there are two ways to implement bipolar weights and inputs for
# StreamingFC:
# - specify their datatypes as such
# - specify their datatypes as BINARY as use binaryXnorMode
if wdt == DataType.BIPOLAR and idt == DataType.BIPOLAR:
# we'll internally convert weights/inputs to binary and specify the
# datatypes as such, and also set the binaryXnorMode attribute to 1
export_wdt = DataType.BINARY
export_idt = DataType.BINARY
binary_xnor_mode = 1
else:
export_wdt = wdt
export_idt = idt
binary_xnor_mode = 0
inp = helper.make_tensor_value_info("inp", TensorProto.FLOAT, [1, mw])
outp = helper.make_tensor_value_info("outp", TensorProto.FLOAT, [1, mh])
if T is not None:
......@@ -52,10 +67,11 @@ def make_single_fclayer_modelwrapper(W, pe, simd, wdt, idt, odt, T=None, tdt=Non
PE=pe,
WMEM=wmem,
TMEM=tmem,
inputDataType=idt.name,
weightDataType=wdt.name,
inputDataType=export_idt.name,
weightDataType=export_wdt.name,
outputDataType=odt.name,
ActVal=actval,
binaryXnorMode=binary_xnor_mode,
)
graph = helper.make_graph(
nodes=[FCLayer_node], name="fclayer_graph", inputs=[inp], outputs=[outp]
......@@ -67,15 +83,23 @@ def make_single_fclayer_modelwrapper(W, pe, simd, wdt, idt, odt, T=None, tdt=Non
model.set_tensor_datatype("inp", idt)
model.set_tensor_datatype("outp", odt)
model.set_tensor_datatype("weights", wdt)
model.set_initializer("weights", W)
if binary_xnor_mode:
# convert bipolar to binary
model.set_initializer("weights", (W + 1) / 2)
else:
model.set_initializer("weights", W)
if T is not None:
model.set_tensor_datatype("thresh", tdt)
model.set_initializer("thresh", T)
return model
def prepare_inputs(model, input_tensor, idt):
return {"inp": input_tensor}
def prepare_inputs(input_tensor, idt, wdt):
if wdt == DataType.BIPOLAR and idt == DataType.BIPOLAR:
# convert bipolar to binary
return {"inp": (input_tensor + 1) / 2}
else:
return {"inp": input_tensor}
# activation: None or DataType
......@@ -132,7 +156,7 @@ def test_fpgadataflow_fclayer(idt, wdt, act, nf, sf, mw, mh):
model = model.transform(CodeGen())
model = model.transform(Compile())
# prepare input data
input_dict = prepare_inputs(model, x, idt)
input_dict = prepare_inputs(x, idt, wdt)
if wdt == DataType.BIPOLAR and idt == DataType.BIPOLAR:
# convert inputs to binary and use xnorpopcountmatmul
y = xp.xnorpopcountmatmul((x + 1) / 2, (W + 1) / 2)
......
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