Implement avg pool test

This commit is contained in:
2024-03-20 21:57:22 +01:00
parent dfff0360d9
commit 6f4cdf3792
7 changed files with 147 additions and 32 deletions

View File

@@ -1,8 +1,20 @@
import torch
def _conv2d(in_channels, out_channels, kernel_size, stride, padding, inputs, weights):
conv2d = torch.nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=False)
def _conv2d(in_channels,
out_channels,
kernel_size,
stride,
padding,
inputs,
weights):
conv2d = torch.nn.Conv2d(in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding,
bias=False)
conv2d.weight = torch.nn.Parameter(weights)
output = conv2d(inputs)
@@ -11,6 +23,7 @@ def _conv2d(in_channels, out_channels, kernel_size, stride, padding, inputs, wei
output = torch.flatten(output)
return output
def _print_cpp_vector(vector):
print("std::vector<float> expected = {", end="")
for i in range(len(vector)):
@@ -20,6 +33,19 @@ def _print_cpp_vector(vector):
print("};")
def _get_pool_input():
return torch.tensor([
0.573, 0.619, 0.732, 0.055,
0.243, 0.316, 0.573, 0.619,
0.712, 0.055, 0.243, 0.316,
0.573, 0.619, 0.742, 0.055,
0.473, 0.919, 0.107, 0.073,
0.073, 0.362, 0.973, 0.059,
0.473, 0.455, 0.283, 0.416,
0.532, 0.819, 0.732, 0.850
]).reshape(1, 2, 4, 4)
def gen_convd_padded_test_result():
in_channels = 3
@@ -68,9 +94,16 @@ def gen_convd_padded_test_result():
0.011, 0.345, 0.678
], dtype=torch.float).reshape(2, 3, 3, 3)
output = _conv2d(in_channels, out_channels, kernel_size, stride, padding, inputs, weights)
output = _conv2d(in_channels,
out_channels,
kernel_size,
stride,
padding,
inputs,
weights)
_print_cpp_vector(output)
def gen_convd_strided_test_result():
in_channels = 2
@@ -78,7 +111,7 @@ def gen_convd_strided_test_result():
kernel_size = 3
stride = 2
padding = 3
input = torch.tensor([
0.946, 0.879, 0.382, 0.542, 0.453,
0.128, 0.860, 0.778, 0.049, 0.974,
@@ -106,9 +139,16 @@ def gen_convd_strided_test_result():
0.939, 0.891, 0.006
], dtype=torch.float).reshape(2, 2, 3, 3)
output = _conv2d(in_channels, out_channels, kernel_size, stride, padding, input, weights)
output = _conv2d(in_channels,
out_channels,
kernel_size,
stride,
padding,
input,
weights)
_print_cpp_vector(output)
def gen_softmax_test_result():
input = torch.tensor([
0.573, 0.619, 0.732, 0.055, 0.243
@@ -117,17 +157,9 @@ def gen_softmax_test_result():
output = torch.nn.Softmax(dim=0)(input)
_print_cpp_vector(output)
def gen_max_pool_test_result():
input = torch.tensor([
0.573, 0.619, 0.732, 0.055,
0.243, 0.316, 0.573, 0.619,
0.712, 0.055, 0.243, 0.316,
0.573, 0.619, 0.742, 0.055,
0.473, 0.919, 0.107, 0.073,
0.073, 0.362, 0.973, 0.059,
0.473, 0.455, 0.283, 0.416,
0.532, 0.819, 0.732, 0.850
]).reshape(1, 2, 4, 4)
input = _get_pool_input()
output = torch.nn.MaxPool2d(kernel_size=2, stride=2)(input)
output = torch.flatten(output)
@@ -135,13 +167,25 @@ def gen_max_pool_test_result():
_print_cpp_vector(output)
def gen_avg_pool_test_result():
input = _get_pool_input()
output = torch.nn.AvgPool2d(kernel_size=2, stride=2)(input)
output = torch.flatten(output)
_print_cpp_vector(output)
if __name__ == "__main__":
# print("Generating test results...")
# print("Padded convolution test:")
# gen_convd_padded_test_result()
# print("Strided convolution test:")
# gen_convd_strided_test_result()
# print("Softmax test:")
# gen_softmax_test_result()
print("Generating test results...")
print("Padded convolution test:")
gen_convd_padded_test_result()
print("Strided convolution test:")
gen_convd_strided_test_result()
print("Softmax test:")
gen_softmax_test_result()
print("Max pool test:")
gen_max_pool_test_result()
gen_max_pool_test_result()
print("Avg pool test:")
gen_avg_pool_test_result()