Skip to content

Commit 1a13d1d

Browse files
luce-georgioukevin-delmasAnthonyFaureGignoux
authored
6 add binary reader for cycle accurate simulator (#10)
* Add specifications for the binary reader * Implementing the Map associating logical addresses and values (for Instructions) * edits * added back build_scratchpad * Add specifications for the binary reader * Implementing the Map associating logical addresses and values (for Instructions) * edits * added back build_scratchpad * binary file instructions * Solve package naming conflict Add BinaryReader Add Test on binary reader * adding binary files to resources * changed names of binary files and edits scala files * tests binaryreader * data types and tests for INP, WGT, INSNs * implementation in progress of Success/Failure and offsets * success/failure implementation and offset test * edits * imported binary data for ComputeTest * new files * binary files more examples * binary files * computeTest attempts * Clean BinaryReader * Add modification with precision for datatype Add integration to ComputeTest * BinaryReader - modified computeAddresses so that it returns an array of bits * BinaryReader - computeAddresses fixed to return Map of address and its element in bits * ComputeTest - tests for 16x16_relu and average pooling * ComputeTest - import of binary files for average pooling and disregarding load instructions * ComputeTest - operations gemm alu tests, issue with Average Pooling 16x16 test (missing a division by 2) * ComputeTest - rectified binary data for lenet5 layer1 * Compiler - started implementing memory address file generating * Compiler - removed implementation for base address file generation * BinaryReaderTest - fixed the tests * BinaryReader - removed FIXME * ComputeTest - added more binary files for tests and added tests * ComputeTest - debugging tests * ComputeTest - debugging tests * ComputeTest - debugging tests and adding binary files for tests * ComputeTest - resolution issue with expected_out and debugging tests * ComputeTest - rectified binary data for conv1 * ComputeTest - debug almost done for tests * Add debug parameters to manage the print (default: debug = false -> no print) * ComputeTest - updated binary files for tests * ComputeTest - tests are working, remains the issue of indexes in expected_out for average_pooling * BinaryReader - worked on readability * Update average_pooling.py * Compiler - generating cvs file for base memory addresses * Compiler - generating a new binary file average_pooling_sram that keeps all of the sram data * ComputeTest - csv reader and started implementing in computetest * ComputeTest - implementation of base addresses computing and expected_out_sram generating completed * Compiler - fixed avg_pool_sram so that the right values are in X_padded (version that works for layer1 but not for average_pooling in ComputeTest) * Compiler - modified avg_pool_sram so that it works for average_pooling test in ComputeTest (the function no longer generates the right expected_out_sram for lenet5_layer1) * Compiler - changed back avg_pool_sram so that it works for layer1 (binary files for layer1 and average_pooling are ok) * BinaryReader - implementation of boolean fromResources to get files from compiler_output or a resources folder * Set debug to false and LeNet-5 tests are tagged as LongTests * Update add debug variable to others tests * Fix some issues with debug parameters --------- Co-authored-by: kevin delmas <[email protected]> Co-authored-by: FAURE-GIGNOUX Anthony <[email protected]>
1 parent 1b0a289 commit 1a13d1d

File tree

103 files changed

+4202
-373
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+4202
-373
lines changed

compiler/data_definition/average_pooling.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ def reference_average_pooling(matrix, kernel_size=2, stride=2, debug=False):
5555
return pooled_matrix, channel_size, pooled_height
5656

5757

58+
def avg_pool_sram(matrix, kernel_size=2, stride=2, debug=False):
59+
# Get the height (H) and width (W) of the input matrix
60+
pooled_matrix = matrix.copy()
61+
H, W = pooled_matrix.shape
62+
63+
# Compute the size of the square channel tensor (sqrt(H))
64+
channel_size = int(np.sqrt(H)) # Each column will be reshaped into channel_size x channel_size
65+
66+
# Calculate the dimensions of the pooled matrix after the pooling operation
67+
pooled_height = (channel_size - kernel_size) // stride + 1
68+
69+
# Add 2 by 2
70+
for idx in range(0, H, 2):
71+
pooled_matrix[idx] = pooled_matrix[idx, :] + pooled_matrix[idx+1, :]
72+
73+
# Add line + shift right
74+
for i in range(0, channel_size - kernel_size + 1, stride): # Row
75+
for k in range(0, channel_size - kernel_size + 1, stride): # Col
76+
pooled_matrix[i*channel_size + k] = np.floor( (pooled_matrix[i*channel_size + k, :] + pooled_matrix[(i+1)*channel_size + k, :])/(kernel_size**2) )
77+
78+
return pooled_matrix, channel_size, pooled_height
79+
5880
# Index computation
5981
# -------------
6082
def average_pooling_indexes(in_tensor_size=4, out_tensor_size=2, kernel_size=2, stride=2):
@@ -94,14 +116,21 @@ def average_pooling_indexes(in_tensor_size=4, out_tensor_size=2, kernel_size=2,
94116
# -------------
95117
if __name__ == '__main__':
96118
kernel = 2
97-
stride = 1
119+
stride = 2
98120

99-
test_matrix = np.random.randint(-128, 127, size=(3**2, 2), dtype=np.int8)
121+
# INIT MATRIX
122+
test_matrix = np.random.randint(-128, 127, size=(16, 2), dtype=np.int32)
100123
print(f"The matrix: \n{test_matrix} \n")
101124

125+
# REFERENCE
102126
pooled_matrix, in_tensor, out_tensor = reference_average_pooling(test_matrix, kernel, stride, debug=True)
103127
print(f"The pooled matrix: \n{pooled_matrix} \n")
104128

129+
# SRAM RESULT
130+
sram_matrix, _, _ = avg_pool_sram(test_matrix, kernel, stride, debug=True)
131+
print(f"The SRAM matrix: \n{sram_matrix} \n")
132+
133+
# INDEXES COMPUTATION
105134
indexes = average_pooling_indexes(in_tensor, out_tensor, kernel, stride)
106135
print("The indexes:")
107136
for index in indexes:

compiler/data_definition/main_matrix_generator.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# ---------------
33
import os
44
import sys
5+
import csv
56
import importlib
67
import numpy as np
78
import matrix_generator as MG
@@ -70,15 +71,34 @@ def main(config_file):
7071
if (config.doAvgPool):
7172
if (config.isInitRandom):
7273
ACC_pooled_ref, in_tensor, out_tensor = AP.reference_average_pooling(ACC_matrix, config.Avg_kernel, config.Avg_stride)
74+
ACC_pooled_sram, _, _ = AP.avg_pool_sram(ACC_matrix, config.Avg_kernel, config.Avg_stride)
7375
else:
7476
ACC_pooled_ref, in_tensor, out_tensor = AP.reference_average_pooling(X_padded, config.Avg_kernel, config.Avg_stride)
77+
ACC_pooled_sram, _, _ = AP.avg_pool_sram(X_padded, config.Avg_kernel, config.Avg_stride)
7578

7679
# Write the result
7780
ACC_pooled = MG.matrix_padding(matrix=ACC_pooled_ref, block_size=config.block_size, isWeight=False, isSquare=config.isSquare)
7881
C_pooled = MM.truncate_to_int8(ACC_pooled)
82+
ACC_pooled_sram2 = MG.matrix_padding(matrix=ACC_pooled_sram, block_size=config.block_size, isWeight=False, isSquare=config.isSquare)
83+
C_pooled_sram = MM.truncate_to_int8(ACC_pooled_sram2)
7984

8085
# Overwrite the result
8186
C_blocks, _ = MS.matrix_splitting(matrix=C_pooled, block_size=config.block_size, isWeight=False, isSquare=config.isSquare)
87+
C_blocks_sram, _ = MS.matrix_splitting(matrix=C_pooled_sram, block_size=config.block_size, isWeight=False, isSquare=config.isSquare)
88+
89+
C_empty = MG.matrix_creation(n_row=C_padded.shape[0], n_col=C_padded.shape[1], isInitRandom=False, dtype=np.int8)
90+
91+
# Compute memory addresses for data
92+
93+
object_info = [(A_padded.shape[0] * A_blocks_col * 16, 16), # INP
94+
(B_padded.shape[0] * B_blocks_col * 16, 256), # WGT
95+
((C_pooled if config.doAvgPool else C_padded).shape[0] * C_blocks_col * 16, 16), # OUT
96+
(20 * 4, 4), # UOP - 20 chosen arbitrarily as number of UOPs rarely exceeds 10
97+
(X_padded.shape[0] * X_blocks_col * 64, 64)] # ACC
98+
99+
100+
memory_addresses = MA.memory_base_address(object_info)
101+
82102

83103
# Write binary files
84104
if (config.doWriteBinaryFile):
@@ -87,7 +107,29 @@ def main(config_file):
87107
B_blocks_file_path = os.path.join(output_dir, 'weight.bin')
88108
X_blocks_file_path = os.path.join(output_dir, 'accumulator.bin')
89109
C_padded_file_path = os.path.join(output_dir, 'expected_out.bin')
90-
110+
C_padded_sram_file_path = os.path.join(output_dir, 'expected_out_sram.bin')
111+
C_empty_file_path = os.path.join(output_dir, 'out.bin')
112+
memory_addresses_data_file_path = os.path.join(output_dir, 'memory_addresses.csv')
113+
114+
alloc_names = {
115+
'Alloc1': 'inp',
116+
'Alloc2': 'wgt',
117+
'Alloc3': 'out',
118+
'Alloc4': 'uop',
119+
'Alloc5': 'acc'}
120+
121+
# Write memory addresses
122+
with open(memory_addresses_data_file_path, 'w', newline='') as csvfile:
123+
writer = csv.writer(csvfile)
124+
for ligne in memory_addresses:
125+
if alloc_names[ligne['object']] in ['inp', 'wgt', 'out']:
126+
writer.writerow([alloc_names[ligne['object']], '0x0000'])
127+
else:
128+
writer.writerow([alloc_names[ligne['object']], ligne['phys_hex']])
129+
130+
# Write C empty matrix
131+
C_empty.tofile(C_empty_file_path)
132+
91133
# Write A_block matrix
92134
with open(A_blocks_file_path, 'wb') as f:
93135
for block in A_blocks:
@@ -105,7 +147,15 @@ def main(config_file):
105147
block.tofile(f)
106148

107149
# Write C_padded (expected result)
108-
C_padded.tofile(C_padded_file_path)
150+
with open(C_padded_file_path, 'wb') as f:
151+
for block in C_blocks:
152+
block.tofile(f)
153+
154+
# Write C_padded_sram (expected result with all vectors for average pooling comparison)
155+
if (config.doAvgPool):
156+
with open(C_padded_sram_file_path, 'wb') as f:
157+
for block in C_blocks_sram:
158+
block.tofile(f)
109159

110160
# Confirm the binary files generation
111161
print("\nBinary files successfully generated.\n")
@@ -199,7 +249,6 @@ def main(config_file):
199249
print("\n\n DRAM 'physical' VTA ADDRESSES:")
200250
for addr in vta_addr:
201251
print(addr)
202-
203252
# End of the execution
204253
return 0
205254

0 commit comments

Comments
 (0)