Skip to content
Snippets Groups Projects
Commit 8b9a9a1d authored by Matthew  Newson's avatar Matthew Newson
Browse files

Add rescaling and unit tests for matmul

parent 76c5fa5d
No related branches found
No related tags found
2 merge requests!710.4.0,!48Adding MatMul operator
Pipeline #74197 failed
......@@ -10,12 +10,13 @@ template<int INPUT_A_DIMS[], int INPUT_B_DIMS[], int OUTPUT_DIMS[],
int _SIZE_DIM_IN_A, int _SIZE_DIM_IN_B, int SIZE_DIM_OUT,
ActivationFunction_T ACTIVATION,
typename Input_T, typename Output_T,
typename Rescaling>
typename Rescaling_T>
__attribute__((always_inline)) inline
void matmul_forward (
const Input_T* __restrict inputs1,
const Input_T* __restrict inputs2,
Output_T* __restrict outputs)
Output_T* __restrict outputs,
const Rescaling_T& __restrict rescaling)
{
//initialize arrays storing broadcasted(or not) dims
......
......@@ -22,9 +22,8 @@ int {{name|upper}}_INPUT_A_DIMS[] = { {{ in_dims[0]|join(", ") }} };
int {{name|upper}}_INPUT_B_DIMS[] = { {{ in_dims[1]|join(", ") }} };
#define {{ name|upper }}_ACTIVATION {{ activation }}
static const {{ rescaling }} {{ name|upper }}_RESCALING = {};
static const {{ rescaling }} {{ name|upper }}_RESCALING = {};
{% include "./_rescaling.jinja" %}
{#- Calculate sizes #}
......
......@@ -7,7 +7,7 @@ matmul_forward<{{name|upper}}_INPUT_A_DIMS,
{{name|upper}}_SIZE_DIM_IN_B,
{{name|upper}}_SIZE_DIM_OUT,
{{name|upper}}_ACTIVATION>
({{in_name[0]}}, {{in_name[1]}}, {{out_name[0]}});
({{in_name[0]}}, {{in_name[1]}}, {{out_name[0]}}, {{name|upper}}_RESCALING);
{% include "./_save_outputs.jinja" %}
{% include "./_aidge_cmp.jinja" %}
{% endfilter %}
......@@ -480,377 +480,50 @@ class test_operator_export(unittest.TestCase):
self.unit_test_export(model, "MatMul", [[8, 4]])
def test_concat_axis_2(self):
print("ConcatAxis2")
def test_matmul_larger(self):
print("MatmulLarger")
model = aidge_core.sequential([
aidge_core.Producer([1, 5, 7], name="producer"),
aidge_core.Concat(nb_inputs=2, axis=2, name="concat")
aidge_core.MatMul(name="MatMul")
])
self.unit_test_export(model, "ConcatAxis2", [[1, 5, 7]])
self.unit_test_export(model, "MatMul", [[1, 7, 5]])
def test_concat_axis_negative(self):
print("ConcatAxisNegative")
def test_matmul_higher(self):
print("MatMulHigher")
model = aidge_core.sequential([
aidge_core.Producer([1, 5, 7], name="producer"),
aidge_core.Concat(nb_inputs=2, axis=-2, name="concat")
])
self.unit_test_export(model, "ConcatAxisNegative", [[1, 5, 7]])
def test_conv2D(self):
print("Conv2D")
model = aidge_core.sequential([
aidge_core.Conv2D(in_channels=3, out_channels=3, kernel_dims=(3, 3), name="conv")
])
self.unit_test_export(model, "Conv2D", [[1, 3, 12, 12]], False, False)
def test_convDepthWise2D(self):
print("ConvDepthWise2D")
model = aidge_core.sequential([
aidge_core.ConvDepthWise2D(nb_channels=3, kernel_dims=(3, 3), name="conv")
])
self.unit_test_export(model, "ConvDepthWise2D", [[1, 3, 12, 12]], False, False)
def test_max_pooling(self):
print("MaxPooling2D")
model = aidge_core.sequential([
aidge_core.MaxPooling2D(kernel_dims=(3, 3), name="max_pool")
])
self.unit_test_export(model, "MaxPooling2D", [[1, 2, 12, 12]], False, False)
def test_avg_pooling(self):
print("AvgPooling2D")
model = aidge_core.sequential([
aidge_core.AvgPooling2D(kernel_dims=(3, 3), name="avg_pool")
])
self.unit_test_export(model, "AvgPooling2D", [[1, 2, 12, 12]], False, False)
def test_pad2D(self):
print("Pad2D")
model = aidge_core.sequential([
aidge_core.Pad2D((1, 1, 1, 1), name="pad2d")
])
self.unit_test_export(model, "Pad2D", [[1, 1, 11, 11]])
def test_pad2D_larger(self):
print("Pad2DLarger")
model = aidge_core.sequential([
aidge_core.Pad2D((1, 3, 1, 3), name="pad2d")
])
self.unit_test_export(model, "Pad2DLarger", [[1, 1, 7, 11]])
def test_pad2D_higher(self):
print("Pad2DHigher")
model = aidge_core.sequential([
aidge_core.Pad2D((3, 1, 3, 1), name="pad2d")
])
self.unit_test_export(model, "Pad2DHigher", [[1, 1, 11, 7]])
def test_pad2D_mismatch(self):
print("Pad2DMismatch")
model = aidge_core.sequential([
aidge_core.Pad2D((1, 3, 5, 7), name="pad2d")
])
self.unit_test_export(model, "Pad2DMismatch", [[3, 5, 11, 7]])
def test_pad2D_denser(self):
print("Pad2DDenser")
model = aidge_core.sequential([
aidge_core.Pad2D((3, 3, 3, 3), name="pad2d")
])
self.unit_test_export(model, "Pad2DDenser", [[1, 5, 7, 11]])
def test_pad2D_with_bigger_batch_size(self):
print("Pad2DBiggerBatchSize")
model = aidge_core.sequential([
aidge_core.Pad2D((1, 1, 1, 1), name="pad2d")
])
self.unit_test_export(model, "Pad2DBiggerBatchSize", [[3, 5, 7, 11]])
@unittest.expectedFailure
def test_pad2D_not_constant(self):
print("Pad2DNotConstant")
model = aidge_core.sequential([
aidge_core.Pad2D((3, 3, 3, 3), border_type=aidge_core.pad_border_type.Wrap, name="pad2d")
])
self.unit_test_export(model, "Pad2DNotConstant", [[1, 5, 7, 11]])
def test_batchnorm2D(self):
print("BatchNormalization2D")
model = aidge_core.sequential([
aidge_core.BatchNorm2D(nb_features=10, epsilon=2e-5, name="bn")
])
self.unit_test_export(model, "BatchNorm2D", [[1, 1, 5, 5]], False, False)
def test_batchnorm2D_Larger(self):
print("BatchNormalization2DLarger")
model = aidge_core.sequential([
aidge_core.BatchNorm2D(nb_features=10, epsilon=2e-5, name="bn")
])
self.unit_test_export(model, "BatchNorm2DLarger", [[1, 1, 5, 7]], False, False)
def test_batchnorm2D_Higher(self):
print("BatchNormalization2DHigher")
model = aidge_core.sequential([
aidge_core.BatchNorm2D(nb_features=10, epsilon=2e-5, name="bn")
])
self.unit_test_export(model, "BatchNorm2DHigher", [[1, 1, 7, 5]], False, False)
def test_batchnorm2D_Denser(self):
print("BatchNormalization2DDenser")
model = aidge_core.sequential([
aidge_core.BatchNorm2D(nb_features=10, epsilon=2e-5, name="bn")
])
self.unit_test_export(model, "BatchNorm2DDenser", [[1, 3, 5, 7]], False, False)
def test_batchnorm2D_with_bigger_batch_size(self):
print("BatchNormalization2DBiggerBatchSize")
model = aidge_core.sequential([
aidge_core.BatchNorm2D(nb_features=10, epsilon=2e-5, name="bn")
])
self.unit_test_export(model, "BatchNormalization2DBiggerBatchSize", [[4, 3, 5, 7]], False, False)
def test_batchnorm2D_Larger(self):
print("BatchNormalization2DLarger")
model = aidge_core.sequential([
aidge_core.BatchNorm2D(nb_features=10, epsilon=2e-5, name="bn")
])
self.unit_test_export(model, "BatchNorm2DLarger", [[1, 1, 5, 7]], False, False)
def test_batchnorm2D_Higher(self):
print("BatchNormalization2DHigher")
model = aidge_core.sequential([
aidge_core.BatchNorm2D(nb_features=10, epsilon=2e-5, name="bn")
])
self.unit_test_export(model, "BatchNorm2DHigher", [[1, 1, 7, 5]], False, False)
def test_batchnorm2D_Denser(self):
print("BatchNormalization2DDenser")
model = aidge_core.sequential([
aidge_core.BatchNorm2D(nb_features=10, epsilon=2e-5, name="bn")
])
self.unit_test_export(model, "BatchNorm2DDenser", [[1, 3, 5, 7]], False, False)
def test_Conv(self):
model = aidge_core.sequential([
aidge_core.Conv2D(1, 1, [3, 3], name="InputNode")
])
initFiller(model)
self.unit_test_export(model, "Conv", [[1, 1, 9, 9]])
def test_MatMul(self):
print("Matmul")
model = aidge_core.sequential([
aidge_core.Producer([1, 5, 5], name="producer"),
aidge_core.Add(name="add")
])
self.unit_test_export(model, "Add", [[1, 5, 5]])
def test_add_larger(self):
print("AddLarger")
model = aidge_core.sequential([
aidge_core.Producer([1, 7, 5], name="producer"),
aidge_core.Add(name="add")
])
self.unit_test_export(model, "Add", [[1, 7, 5]])
def test_add_higher(self):
print("AddHigher")
model = aidge_core.sequential([
aidge_core.Producer([1, 5, 7], name="producer"),
aidge_core.Add(name="add")
])
self.unit_test_export(model, "Add", [[1, 5, 7]])
# "Broadcast not supported yet in export operator"
@unittest.expectedFailure
def test_add_simple_broadcast(self):
print("AddSimpleBroadcast")
model = aidge_core.sequential([
aidge_core.Producer([1, 1, 5], name="producer"),
aidge_core.Add(name="add")
])
self.unit_test_export(model, "AddSimpleBroadcast", [[1, 7, 5]])
# "Broadcast not supported yet in export operator"
@unittest.expectedFailure
def test_add_double_broadcast(self):
print("AddDoubleBroadcast")
model = aidge_core.sequential([
aidge_core.Producer([1, 1, 7], name="producer"),
aidge_core.Add(name="add")
])
self.unit_test_export(model, "AddDoubleBroadcast", [[1, 5, 1]])
def test_sub(self):
print("Sub")
model = aidge_core.sequential([
aidge_core.Producer([1, 5, 5], name="producer"),
aidge_core.Sub(name="sub")
])
self.unit_test_export(model, "Sub", [[1, 5, 5]])
def test_sub_larger(self):
print("SubLarger")
model = aidge_core.sequential([
aidge_core.Producer([1, 7, 5], name="producer"),
aidge_core.Sub(name="sub")
])
self.unit_test_export(model, "Sub", [[1, 7, 5]])
def test_sub_higher(self):
print("SubHigher")
model = aidge_core.sequential([
aidge_core.Producer([1, 5, 7], name="producer"),
aidge_core.Sub(name="sub")
])
self.unit_test_export(model, "Sub", [[1, 5, 7]])
# "Broadcast not supported yet in export operator"
@unittest.expectedFailure
def test_sub_simple_broadcast(self):
print("SubSimpleBroadcast")
model = aidge_core.sequential([
aidge_core.Producer([1, 1, 5], name="producer"),
aidge_core.Sub(name="sub")
])
self.unit_test_export(model, "SubSimpleBroadcast", [[1, 7, 5]])
# "Broadcast not supported yet in export operator"
@unittest.expectedFailure
def test_sub_double_broadcast(self):
print("SubDoubleBroadcast")
model = aidge_core.sequential([
aidge_core.Producer([1, 1, 7], name="producer"),
aidge_core.Sub(name="sub")
])
self.unit_test_export(model, "SubDoubleBroadcast", [[1, 5, 1]])
def test_mul(self):
print("Mul")
model = aidge_core.sequential([
aidge_core.Producer([1, 5, 5], name="producer"),
aidge_core.Mul(name="mul")
])
self.unit_test_export(model, "Mul", [[1, 5, 5]])
def test_mul_larger(self):
print("MulLarger")
model = aidge_core.sequential([
aidge_core.Producer([1, 7, 5], name="producer"),
aidge_core.Mul(name="mul")
])
self.unit_test_export(model, "Mul", [[1, 7, 5]])
def test_mul_higher(self):
print("MulHigher")
model = aidge_core.sequential([
aidge_core.Producer([1, 5, 7], name="producer"),
aidge_core.Mul(name="mul")
aidge_core.MatMul(name="matmul")
])
self.unit_test_export(model, "Mul", [[1, 5, 7]])
self.unit_test_export(model, "MatMul", [[1, 7, 1]])
# "Broadcast not supported yet in export operator"
@unittest.expectedFailure
def test_mul_simple_broadcast(self):
print("MulSimpleBroadcast")
def test_matmul_simple_broadcast(self):
print("MatMulSimpleBroadcast")
model = aidge_core.sequential([
aidge_core.Producer([1, 1, 5], name="producer"),
aidge_core.Mul(name="mul")
aidge_core.MatMul(name="MatMul")
])
self.unit_test_export(model, "MulSimpleBroadcast", [[1, 7, 5]])
self.unit_test_export(model, "MatMulSimpleBroadcast", [[1, 5, 7]])
# "Broadcast not supported yet in export operator"
@unittest.expectedFailure
def test_mul_double_broadcast(self):
print("MulDoubleBroadcast")
def test_matmul_double_broadcast(self):
print("MatMulDoubleBroadcast")
model = aidge_core.sequential([
aidge_core.Producer([1, 1, 7], name="producer"),
aidge_core.Mul(name="mul")
aidge_core.Producer([3, 1, 5], name="producer"),
aidge_core.MatMul(name="MatMul")
])
self.unit_test_export(model, "MulDoubleBroadcast", [[1, 5, 1]])
self.unit_test_export(model, "MatMulDoubleBroadcast", [[3, 5, 5]])
def test_mul_batch(self):
print("MulBatch")
def test_matmul_batch(self):
print("MatMulBatch")
model = aidge_core.sequential([
aidge_core.Producer([3, 5, 7], name="producer"),
aidge_core.Mul(name="mul")
])
self.unit_test_export(model, "MulBatch", [[3, 5, 7]])
def test_concat(self):
print("Concat")
model = aidge_core.sequential([
aidge_core.Producer([1, 5, 7], name="producer"),
aidge_core.Concat(nb_inputs=2, axis=1, name="concat")
])
self.unit_test_export(model, "Concat", [[1, 5, 7]])
def test_transpose(self):
print("Transpose")
model = aidge_core.sequential([
aidge_core.Transpose(output_dims_order=[0, 2, 3, 1], name="transpose")
])
self.unit_test_export(model, "Transpose", [[1, 7, 8, 2]])
def test_reshape(self):
print("Reshape")
model = aidge_core.sequential([
aidge_core.Reshape(name="reshape")
])
shape = aidge_core.Producer(aidge_core.Tensor(np.array([7, 2, 1, 8], dtype=np.int64)), name="shape")
shape.add_child(model.get_node("reshape"), 0, 1)
model.add(shape)
self.unit_test_export(model, "Reshape", [[1, 7, 8, 2]], random_weights=False)
def test_matmul(self):
print("MatMul")
model = aidge_core.sequential([
aidge_core.Producer([4, 8], name="producer"),
aidge_core.MatMul(name="matmul")
])
self.unit_test_export(model, "MatMul", [[8, 4]])
self.unit_test_export(model, "MatMulBatch", [[3, 7, 7]])
def test_concat_axis_2(self):
print("ConcatAxis2")
......@@ -1030,6 +703,7 @@ class test_operator_export(unittest.TestCase):
])
initFiller(model)
self.unit_test_export(model, "Conv", [[1, 1, 9, 9]])
if __name__ == '__main__':
unittest.main()
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