Split python scripts

This commit is contained in:
2024-03-22 20:03:22 +01:00
parent 87db47089e
commit 9ecd51c63a
7 changed files with 89 additions and 62 deletions

4
.gitignore vendored
View File

@@ -36,4 +36,6 @@ build/
.cache
venv
docs
docs
__pycache__

0
tools/__init__.py Normal file
View File

View File

@@ -1,5 +1,6 @@
import torch
from utils import print_cpp_vector
def _conv2d(in_channels,
out_channels,
@@ -23,29 +24,6 @@ def _conv2d(in_channels,
output = torch.flatten(output)
return output
def _print_cpp_vector(vector):
print("std::vector<float> expected = {", end="")
for i in range(len(vector)):
if i != 0:
print(", ", end="")
print(str(round(vector[i].item(), 5)) + "f", end="")
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
@@ -101,7 +79,8 @@ def gen_convd_padded_test_result():
padding,
inputs,
weights)
_print_cpp_vector(output)
print_cpp_vector(output)
def gen_convd_strided_test_result():
@@ -146,46 +125,12 @@ def gen_convd_strided_test_result():
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
])
output = torch.nn.Softmax(dim=0)(input)
_print_cpp_vector(output)
def gen_max_pool_test_result():
input = _get_pool_input()
output = torch.nn.MaxPool2d(kernel_size=2, stride=2)(input)
output = torch.flatten(output)
_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)
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("Max pool test:")
gen_max_pool_test_result()
print("Avg pool test:")
gen_avg_pool_test_result()
gen_convd_strided_test_result()

View File

@@ -0,0 +1,15 @@
import numpy as np
import utils
from sys import argv
def gen_random_vector(size):
return np.random.rand(size)
if __name__ == "__main__":
if len(argv) < 2:
print("Usage: python gen_random_vector.py <size>")
exit(1)
vector = gen_random_vector(int(argv[1]))
utils.print_cpp_vector(vector)

41
tools/pooling_test.py Normal file
View File

@@ -0,0 +1,41 @@
import torch
from utils import print_cpp_vector
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_max_pool_test_result():
input = _get_pool_input()
output = torch.nn.MaxPool2d(kernel_size=2, stride=2)(input)
output = torch.flatten(output)
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("Max pool test:")
gen_max_pool_test_result()
print("Avg pool test:")
gen_avg_pool_test_result()

17
tools/softmax_tes.py Normal file
View File

@@ -0,0 +1,17 @@
import torch
from utils import print_cpp_vector
def gen_softmax_test_result():
input = torch.tensor([
0.573, 0.619, 0.732, 0.055, 0.243
])
output = torch.nn.Softmax(dim=0)(input)
print_cpp_vector(output)
if __name__ == "__main__":
print("Generating test results...")
print("Softmax test:")
gen_softmax_test_result()

7
tools/utils.py Normal file
View File

@@ -0,0 +1,7 @@
def print_cpp_vector(vector):
print("std::vector<float> expected = {", end="")
for i in range(len(vector)):
if i != 0:
print(", ", end="")
print(str(round(vector[i].item(), 5)) + "f", end="")
print("};")