mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-06 01:34:22 +00:00
Split python scripts
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -37,3 +37,5 @@ build/
|
|||||||
|
|
||||||
venv
|
venv
|
||||||
docs
|
docs
|
||||||
|
|
||||||
|
__pycache__
|
||||||
0
tools/__init__.py
Normal file
0
tools/__init__.py
Normal file
@@ -1,5 +1,6 @@
|
|||||||
import torch
|
import torch
|
||||||
|
|
||||||
|
from utils import print_cpp_vector
|
||||||
|
|
||||||
def _conv2d(in_channels,
|
def _conv2d(in_channels,
|
||||||
out_channels,
|
out_channels,
|
||||||
@@ -23,29 +24,6 @@ def _conv2d(in_channels,
|
|||||||
output = torch.flatten(output)
|
output = torch.flatten(output)
|
||||||
return 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():
|
def gen_convd_padded_test_result():
|
||||||
|
|
||||||
in_channels = 3
|
in_channels = 3
|
||||||
@@ -101,7 +79,8 @@ def gen_convd_padded_test_result():
|
|||||||
padding,
|
padding,
|
||||||
inputs,
|
inputs,
|
||||||
weights)
|
weights)
|
||||||
_print_cpp_vector(output)
|
|
||||||
|
print_cpp_vector(output)
|
||||||
|
|
||||||
|
|
||||||
def gen_convd_strided_test_result():
|
def gen_convd_strided_test_result():
|
||||||
@@ -146,36 +125,8 @@ def gen_convd_strided_test_result():
|
|||||||
padding,
|
padding,
|
||||||
input,
|
input,
|
||||||
weights)
|
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__":
|
if __name__ == "__main__":
|
||||||
print("Generating test results...")
|
print("Generating test results...")
|
||||||
@@ -183,9 +134,3 @@ if __name__ == "__main__":
|
|||||||
gen_convd_padded_test_result()
|
gen_convd_padded_test_result()
|
||||||
print("Strided convolution test:")
|
print("Strided convolution test:")
|
||||||
gen_convd_strided_test_result()
|
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()
|
|
||||||
15
tools/gen_random_vector.py
Normal file
15
tools/gen_random_vector.py
Normal 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
41
tools/pooling_test.py
Normal 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
17
tools/softmax_tes.py
Normal 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
7
tools/utils.py
Normal 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("};")
|
||||||
Reference in New Issue
Block a user