Skip to content
Snippets Groups Projects
Commit 8f3170bf authored by Gallas Gaye's avatar Gallas Gaye
Browse files

feat: Added more tests

parent 1044a6b6
No related branches found
No related tags found
2 merge requests!39Update 0.2.1 -> 0.3.0,!36feat: Add missing operators for AIDGE model benchmarking
......@@ -31,6 +31,63 @@ def initFiller(model):
aidge_core.constant_filler(value, 0.01)
else:
pass
import math
def normalize_random_tensor(randList):
for index in np.ndindex(randList.shape):
randList[index] = (math.floor(randList[index] * 21) - 10) / 10
return aidge_core.Tensor(randList.astype(np.float32))
def unit_test_export(graph_view, op_name, in_dims):
graph_view.compile("cpu", aidge_core.dtype.float32, dims=in_dims)
scheduler = aidge_core.SequentialScheduler(graph_view)
# in_tensor = [aidge_core.Tensor(np.random.random(in_dim).astype(np.float32)) for in_dim in in_dims]
in_tensor = [normalize_random_tensor(np.random.rand(*in_dim)) for in_dim in in_dims]
scheduler.forward(data=in_tensor)
export_folder = op_name + "_temp_test"
# Export the model in C++ standalone
aidge_core.export_utils.scheduler_export(
scheduler,
export_folder,
aidge_export_cpp.ExportLibCpp,
memory_manager=aidge_core.mem_info.generate_optimized_memory_info,
memory_manager_args={"stats_folder": f"{export_folder}/stats", "wrapping": False }
)
aidge_core.export_utils.generate_main_compare_cpp(export_folder, graph_view)
print("COMPILATION")
try:
for std_line in run_command(["make"], cwd=export_folder):
print(std_line, end="")
except subprocess.CalledProcessError as e:
print(f"An error occurred: {e}\nFailed to generate export.")
raise SystemExit(1)
print("RUN EXPORT")
pattern = r"Number of equal outputs: (\d+) / (\d+)"
comparison_matched = False
result = False
try:
for std_line in run_command(["./bin/run_export"], cwd=export_folder):
print(std_line, end="")
matches = re.findall(pattern, std_line)
if matches:
if comparison_matched:
raise RuntimeError("Two comparison matched found!")
else:
expected, infered = map(int, matches[0])
result = (expected == infered)
comparison_matched = True
except subprocess.CalledProcessError as e:
print(f"An error occurred: {e}\nFailed to run export for comparison.")
raise SystemExit(1)
if not comparison_matched:
raise RuntimeError("No comparison matched found!")
return result
class test_operator_export(unittest.TestCase):
......@@ -130,6 +187,84 @@ class test_operator_export(unittest.TestCase):
initFiller(model)
self.unit_test_export(model, "FC_img", [[1, 3, 2, 2]])
def test_export_relu(self):
print("ReLU")
model = aidge_core.sequential([
aidge_core.ReLU(name="relu0")
])
self.assertTrue(unit_test_export(model, "ReLU", [[1, 10]]))
def test_export_add(self):
print("Add")
model = aidge_core.sequential([
aidge_core.Producer([1, 10]),
aidge_core.Add()
])
self.assertTrue(unit_test_export(model, "Add", [[1, 10]]))
def test_export_sub(self):
print("Sub")
model = aidge_core.sequential([
aidge_core.Producer([1, 10]),
aidge_core.Sub()
])
self.assertTrue(unit_test_export(model, "Sub", [[1, 10]]))
def test_export_mul(self):
print("Mul")
model = aidge_core.sequential([
aidge_core.Producer([1, 10]),
aidge_core.Mul()
])
self.assertTrue(unit_test_export(model, "Mul", [[1, 10]]))
def test_export_conv2D(self):
print("Conv2D")
model = aidge_core.sequential([
aidge_core.Conv2D(in_channels=3, out_channels=3, kernel_dims=(3, 3))
])
self.assertTrue(unit_test_export(model, "Conv2D", [[1, 3, 12, 12]]))
# def test_export_max_pooling(self):
# print("MaxPooling2D")
# model = aidge_core.sequential([
# aidge_core.MaxPooling2D(kernel_dims=(3, 3))
# ])
# self.assertTrue(unit_test_export(model, "MaxPooling2D", [[1, 2, 12, 12]]))
# def test_export_avg_pooling(self):
# print("AvgPooling2D")
# model = aidge_core.sequential([
# aidge_core.AvgPooling2D(kernel_dims=(3, 3), name="avg_pool0")
# ])
# self.assertTrue(unit_test_export(model, "AvgPooling2D", [[1, 2, 12, 12]]))
# def test_export_pad2D(self):
# print("Pad2D")
# model = aidge_core.sequential([
# aidge_core.Softmax(axis=1, name="sf0")
# ])
# self.assertTrue(unit_test_export(model, "Softmax", [[1, 10]]))
# def test_export_batchnorm2D(self):
# print("BatchNormalization2D")
# model = aidge_core.sequential([
# aidge_core.BatchNorm2D(nb_features=10, epsilon=2e-5)
# ])
# self.assertTrue(unit_test_export(model, "BatchNorm2D", [[1, 10]]))
def test_export_cpp(self):
print("Export test to do")
def test_export_Conv(self):
model = aidge_core.sequential([
aidge_core.Conv2D(1, 1, [3, 3], name="InputNode")
......
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