From 9ecd51c63abbcc22a19851b429ea9d65264a1b07 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Fri, 22 Mar 2024 20:03:22 +0100 Subject: [PATCH] Split python scripts --- .gitignore | 4 +- tools/__init__.py | 0 ...enerate_test_results.py => conv2d_test.py} | 67 ++----------------- tools/gen_random_vector.py | 15 +++++ tools/pooling_test.py | 41 ++++++++++++ tools/softmax_tes.py | 17 +++++ tools/utils.py | 7 ++ 7 files changed, 89 insertions(+), 62 deletions(-) create mode 100644 tools/__init__.py rename tools/{generate_test_results.py => conv2d_test.py} (72%) create mode 100644 tools/gen_random_vector.py create mode 100644 tools/pooling_test.py create mode 100644 tools/softmax_tes.py create mode 100644 tools/utils.py diff --git a/.gitignore b/.gitignore index cb8bdfe..ad8a97c 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,6 @@ build/ .cache venv -docs \ No newline at end of file +docs + +__pycache__ \ No newline at end of file diff --git a/tools/__init__.py b/tools/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tools/generate_test_results.py b/tools/conv2d_test.py similarity index 72% rename from tools/generate_test_results.py rename to tools/conv2d_test.py index f70fdaa..71f97c3 100644 --- a/tools/generate_test_results.py +++ b/tools/conv2d_test.py @@ -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 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() \ No newline at end of file diff --git a/tools/gen_random_vector.py b/tools/gen_random_vector.py new file mode 100644 index 0000000..6a6fc2b --- /dev/null +++ b/tools/gen_random_vector.py @@ -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 ") + exit(1) + + vector = gen_random_vector(int(argv[1])) + utils.print_cpp_vector(vector) \ No newline at end of file diff --git a/tools/pooling_test.py b/tools/pooling_test.py new file mode 100644 index 0000000..f2af0e0 --- /dev/null +++ b/tools/pooling_test.py @@ -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() diff --git a/tools/softmax_tes.py b/tools/softmax_tes.py new file mode 100644 index 0000000..b26bfe8 --- /dev/null +++ b/tools/softmax_tes.py @@ -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() \ No newline at end of file diff --git a/tools/utils.py b/tools/utils.py new file mode 100644 index 0000000..47a53e4 --- /dev/null +++ b/tools/utils.py @@ -0,0 +1,7 @@ +def print_cpp_vector(vector): + print("std::vector expected = {", end="") + for i in range(len(vector)): + if i != 0: + print(", ", end="") + print(str(round(vector[i].item(), 5)) + "f", end="") + print("};")