Skip to content

6 add binary reader for cycle accurate simulator #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
d7c346f
Add specifications for the binary reader
kevin-delmas Apr 8, 2025
f83efbd
Implementing the Map associating logical addresses and values (for In…
lusssse Apr 10, 2025
1dc3f05
edits
lusssse Apr 10, 2025
b32f686
added back build_scratchpad
lusssse Apr 10, 2025
2d4f619
Add specifications for the binary reader
kevin-delmas Apr 8, 2025
b30264f
Implementing the Map associating logical addresses and values (for In…
lusssse Apr 10, 2025
d0e0e7d
edits
lusssse Apr 10, 2025
6f1a999
added back build_scratchpad
lusssse Apr 10, 2025
a79ac65
Merge branch '6-add-binary-reader-for-cycle-accurate-simulator' of gi…
kevin-delmas Apr 10, 2025
11642b4
binary file instructions
lusssse Apr 10, 2025
2dbb496
Merge branch '6-add-binary-reader-for-cycle-accurate-simulator' of ht…
lusssse Apr 10, 2025
61335d9
Solve package naming conflict
kevin-delmas Apr 10, 2025
372ada2
adding binary files to resources
lusssse Apr 10, 2025
2d39bec
changed names of binary files and edits scala files
lusssse Apr 10, 2025
90b136e
tests binaryreader
lusssse Apr 10, 2025
f5d6c78
data types and tests for INP, WGT, INSNs
lusssse Apr 11, 2025
fc0f4f6
implementation in progress of Success/Failure and offsets
lusssse Apr 11, 2025
d0f8b23
success/failure implementation and offset test
lusssse Apr 14, 2025
45c0b44
edits
lusssse Apr 14, 2025
36d581d
imported binary data for ComputeTest
lusssse Apr 15, 2025
28c622b
new files
lusssse Apr 15, 2025
4379c9f
binary files more examples
lusssse Apr 15, 2025
253c227
binary files
lusssse Apr 15, 2025
6f7d226
computeTest attempts
lusssse Apr 15, 2025
d54b9e1
Clean BinaryReader
kevin-delmas Apr 15, 2025
e84afca
Add modification with precision for datatype
kevin-delmas Apr 15, 2025
7076688
BinaryReader - modified computeAddresses so that it returns an array …
lusssse Apr 16, 2025
f903dd9
BinaryReader - computeAddresses fixed to return Map of address and it…
lusssse Apr 17, 2025
a548b60
ComputeTest - tests for 16x16_relu and average pooling
lusssse Apr 18, 2025
8035c24
ComputeTest - import of binary files for average pooling and disregar…
lusssse Apr 18, 2025
c0b8f58
ComputeTest - operations gemm alu tests, issue with Average Pooling 1…
lusssse Apr 18, 2025
a887ca7
ComputeTest - rectified binary data for lenet5 layer1
lusssse Apr 28, 2025
e03652a
Compiler - started implementing memory address file generating
lusssse Apr 28, 2025
9966578
Compiler - removed implementation for base address file generation
lusssse Apr 29, 2025
c0d48fd
BinaryReaderTest - fixed the tests
lusssse Apr 29, 2025
7afb139
BinaryReader - removed FIXME
lusssse Apr 29, 2025
de99aa6
ComputeTest - added more binary files for tests and added tests
lusssse Apr 29, 2025
8c0b605
Merge branch 'main' into 6-add-binary-reader-for-cycle-accurate-simul…
AnthonyFaureGignoux Apr 29, 2025
7dc66dd
ComputeTest - debugging tests
lusssse Apr 30, 2025
875ec20
Merge branch 'main' into 6-add-binary-reader-for-cycle-accurate-simul…
AnthonyFaureGignoux May 2, 2025
0e85a1a
ComputeTest - debugging tests
lusssse May 5, 2025
d96ec98
ComputeTest - debugging tests and adding binary files for tests
lusssse May 6, 2025
c7a402e
ComputeTest - resolution issue with expected_out and debugging tests
lusssse May 7, 2025
0530263
ComputeTest - rectified binary data for conv1
lusssse May 7, 2025
b2333f3
ComputeTest - debug almost done for tests
lusssse May 7, 2025
1a89799
Add debug parameters to manage the print (default: debug = false -> n…
AnthonyFaureGignoux May 9, 2025
14f31b0
ComputeTest - updated binary files for tests
lusssse May 12, 2025
2d87a91
ComputeTest - tests are working, remains the issue of indexes in expe…
lusssse May 12, 2025
f2c401f
BinaryReader - worked on readability
lusssse May 13, 2025
793cdd5
Update average_pooling.py
AnthonyFaureGignoux May 13, 2025
2700791
Compiler - generating cvs file for base memory addresses
lusssse May 13, 2025
e7b747e
Merge branch '6-add-binary-reader-for-cycle-accurate-simulator' of ht…
lusssse May 13, 2025
6a1d0df
Compiler - generating a new binary file average_pooling_sram that kee…
lusssse May 13, 2025
c3b7198
ComputeTest - csv reader and started implementing in computetest
lusssse May 13, 2025
7df0999
ComputeTest - implementation of base addresses computing and expected…
lusssse May 14, 2025
37702ed
Compiler - fixed avg_pool_sram so that the right values are in X_padd…
lusssse May 14, 2025
a33df75
Compiler - modified avg_pool_sram so that it works for average_poolin…
lusssse May 14, 2025
a7a00d3
Compiler - changed back avg_pool_sram so that it works for layer1 (bi…
lusssse May 14, 2025
41e0dcc
BinaryReader - implementation of boolean fromResources to get files f…
lusssse May 14, 2025
aca4679
Set debug to false and LeNet-5 tests are tagged as LongTests
AnthonyFaureGignoux May 14, 2025
415a5b3
Update add debug variable to others tests
AnthonyFaureGignoux May 14, 2025
fef1380
Fix some issues with debug parameters
AnthonyFaureGignoux May 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions compiler/data_definition/average_pooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ def reference_average_pooling(matrix, kernel_size=2, stride=2, debug=False):
return pooled_matrix, channel_size, pooled_height


def avg_pool_sram(matrix, kernel_size=2, stride=2, debug=False):
# Get the height (H) and width (W) of the input matrix
pooled_matrix = matrix.copy()
H, W = pooled_matrix.shape

# Compute the size of the square channel tensor (sqrt(H))
channel_size = int(np.sqrt(H)) # Each column will be reshaped into channel_size x channel_size

# Calculate the dimensions of the pooled matrix after the pooling operation
pooled_height = (channel_size - kernel_size) // stride + 1

# Add 2 by 2
for idx in range(0, H, 2):
pooled_matrix[idx] = pooled_matrix[idx, :] + pooled_matrix[idx+1, :]

# Add line + shift right
for i in range(0, channel_size - kernel_size + 1, stride): # Row
for k in range(0, channel_size - kernel_size + 1, stride): # Col
pooled_matrix[i*channel_size + k] = np.floor( (pooled_matrix[i*channel_size + k, :] + pooled_matrix[(i+1)*channel_size + k, :])/(kernel_size**2) )

return pooled_matrix, channel_size, pooled_height

# Index computation
# -------------
def average_pooling_indexes(in_tensor_size=4, out_tensor_size=2, kernel_size=2, stride=2):
Expand Down Expand Up @@ -94,14 +116,21 @@ def average_pooling_indexes(in_tensor_size=4, out_tensor_size=2, kernel_size=2,
# -------------
if __name__ == '__main__':
kernel = 2
stride = 1
stride = 2

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

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

# SRAM RESULT
sram_matrix, _, _ = avg_pool_sram(test_matrix, kernel, stride, debug=True)
print(f"The SRAM matrix: \n{sram_matrix} \n")

# INDEXES COMPUTATION
indexes = average_pooling_indexes(in_tensor, out_tensor, kernel, stride)
print("The indexes:")
for index in indexes:
Expand Down
55 changes: 52 additions & 3 deletions compiler/data_definition/main_matrix_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# ---------------
import os
import sys
import csv
import importlib
import numpy as np
import matrix_generator as MG
Expand Down Expand Up @@ -70,15 +71,34 @@ def main(config_file):
if (config.doAvgPool):
if (config.isInitRandom):
ACC_pooled_ref, in_tensor, out_tensor = AP.reference_average_pooling(ACC_matrix, config.Avg_kernel, config.Avg_stride)
ACC_pooled_sram, _, _ = AP.avg_pool_sram(ACC_matrix, config.Avg_kernel, config.Avg_stride)
else:
ACC_pooled_ref, in_tensor, out_tensor = AP.reference_average_pooling(X_padded, config.Avg_kernel, config.Avg_stride)
ACC_pooled_sram, _, _ = AP.avg_pool_sram(X_padded, config.Avg_kernel, config.Avg_stride)

# Write the result
ACC_pooled = MG.matrix_padding(matrix=ACC_pooled_ref, block_size=config.block_size, isWeight=False, isSquare=config.isSquare)
C_pooled = MM.truncate_to_int8(ACC_pooled)
ACC_pooled_sram2 = MG.matrix_padding(matrix=ACC_pooled_sram, block_size=config.block_size, isWeight=False, isSquare=config.isSquare)
C_pooled_sram = MM.truncate_to_int8(ACC_pooled_sram2)

# Overwrite the result
C_blocks, _ = MS.matrix_splitting(matrix=C_pooled, block_size=config.block_size, isWeight=False, isSquare=config.isSquare)
C_blocks_sram, _ = MS.matrix_splitting(matrix=C_pooled_sram, block_size=config.block_size, isWeight=False, isSquare=config.isSquare)

C_empty = MG.matrix_creation(n_row=C_padded.shape[0], n_col=C_padded.shape[1], isInitRandom=False, dtype=np.int8)

# Compute memory addresses for data

object_info = [(A_padded.shape[0] * A_blocks_col * 16, 16), # INP
(B_padded.shape[0] * B_blocks_col * 16, 256), # WGT
((C_pooled if config.doAvgPool else C_padded).shape[0] * C_blocks_col * 16, 16), # OUT
(20 * 4, 4), # UOP - 20 chosen arbitrarily as number of UOPs rarely exceeds 10
(X_padded.shape[0] * X_blocks_col * 64, 64)] # ACC


memory_addresses = MA.memory_base_address(object_info)


# Write binary files
if (config.doWriteBinaryFile):
Expand All @@ -87,7 +107,29 @@ def main(config_file):
B_blocks_file_path = os.path.join(output_dir, 'weight.bin')
X_blocks_file_path = os.path.join(output_dir, 'accumulator.bin')
C_padded_file_path = os.path.join(output_dir, 'expected_out.bin')

C_padded_sram_file_path = os.path.join(output_dir, 'expected_out_sram.bin')
C_empty_file_path = os.path.join(output_dir, 'out.bin')
memory_addresses_data_file_path = os.path.join(output_dir, 'memory_addresses.csv')

alloc_names = {
'Alloc1': 'inp',
'Alloc2': 'wgt',
'Alloc3': 'out',
'Alloc4': 'uop',
'Alloc5': 'acc'}

# Write memory addresses
with open(memory_addresses_data_file_path, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
for ligne in memory_addresses:
if alloc_names[ligne['object']] in ['inp', 'wgt', 'out']:
writer.writerow([alloc_names[ligne['object']], '0x0000'])
else:
writer.writerow([alloc_names[ligne['object']], ligne['phys_hex']])

# Write C empty matrix
C_empty.tofile(C_empty_file_path)

# Write A_block matrix
with open(A_blocks_file_path, 'wb') as f:
for block in A_blocks:
Expand All @@ -105,7 +147,15 @@ def main(config_file):
block.tofile(f)

# Write C_padded (expected result)
C_padded.tofile(C_padded_file_path)
with open(C_padded_file_path, 'wb') as f:
for block in C_blocks:
block.tofile(f)

# Write C_padded_sram (expected result with all vectors for average pooling comparison)
if (config.doAvgPool):
with open(C_padded_sram_file_path, 'wb') as f:
for block in C_blocks_sram:
block.tofile(f)

# Confirm the binary files generation
print("\nBinary files successfully generated.\n")
Expand Down Expand Up @@ -199,7 +249,6 @@ def main(config_file):
print("\n\n DRAM 'physical' VTA ADDRESSES:")
for addr in vta_addr:
print(addr)

# End of the execution
return 0

Expand Down
Loading
Loading