Skip to content

Commit

Permalink
Add character recognition with bind mechanism (#98)
Browse files Browse the repository at this point in the history
* hdc_exp: update char recog with reverse indexing for bind

* hw: update csr

* hw: update top-levle for better control signals

* tb: update path widths

* asm: add data slice inst

* test: add test for data slicing

* test: formatted
  • Loading branch information
rgantonio authored Nov 5, 2024
1 parent 88262e2 commit f0b2781
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 18 deletions.
30 changes: 27 additions & 3 deletions hdc_exp/char_recog.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def encode_character(
ortho_im,
character_input,
threshold,
debug=False,
encode_mode="indexed",
hv_type="binary",
):
Expand All @@ -129,12 +130,23 @@ def encode_character(

# Cycle through every pixel
if encode_mode == "bind":
# This is a nasty hard fix but the idea is to
# reverse the order because the tool python list reads
# the list in a reverse way...
character_input = character_input[::-1]
for i in range(len(character_input)):
pixel_val_hv = ortho_im[character_input[i]]

pixel_pos_hv = ortho_im[2 + i]
char_hv += bind_hv(pixel_val_hv, pixel_pos_hv)

if debug:
print(f"character_input: {character_input[i]}")
print(f"position: {i}")
print(f"pixel_val_hv: {pixel_val_hv}")
print(f"pixel_pos_hv: {pixel_pos_hv}")
print(f"char_hv: {pixel_val_hv}")

else:
for i in range(len(character_input)):
# If pixel value is a 1
Expand Down Expand Up @@ -165,6 +177,7 @@ def encode_with_bind(
ortho_im,
character_input,
threshold,
debug=False,
encode_mode="indexed",
hv_type="binary",
):
Expand All @@ -174,6 +187,7 @@ def encode_with_bind(
ortho_im,
character_input,
threshold,
debug=debug,
encode_mode=encode_mode,
hv_type=hv_type,
)
Expand Down Expand Up @@ -433,11 +447,11 @@ def exp_distort_test():
if __name__ == "__main__":
# Some working parameters
seed_size = 32
hv_dim = 512
num_total_im = 1024
hv_dim = 256
num_total_im = 512
num_per_im_bank = int(hv_dim // 4)
threshold = THRESHOLD
encode_mode = "indexed"
encode_mode = "bind"

# Get original data set
file_path = "./data_set/char_recog/characters.txt"
Expand Down Expand Up @@ -481,6 +495,16 @@ def exp_distort_test():
dataset, hv_dim, ortho_im, threshold, encode_mode=encode_mode, hv_type="binary"
)

# query_hv = encode_with_bind(
# hv_dim,
# ortho_im,
# dataset[0],
# threshold,
# debug=True,
# encode_mode=encode_mode,
# hv_type="binary",
# )

# Encode query_hv_list
query_hv_set = []

Expand Down
7 changes: 0 additions & 7 deletions rtl/csr/csr.sv
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,6 @@ module csr import csr_addr_pkg::*; #(
output logic [InstMemAddrWidth-1:0] csr_loop_count_addr2_o,
output logic [InstMemAddrWidth-1:0] csr_loop_count_addr3_o,
output logic [ ObservableWidth-1:0] csr_obs_logic_o,
// Data source configurations
output logic csr_src_mux_a_o,
output logic csr_src_mux_b_o,
output logic [ CsrDataWidth-1:0] csr_auto_count_start_a_o,
output logic [ CsrDataWidth-1:0] csr_auto_count_start_b_o,
output logic [ CsrDataWidth-1:0] csr_auto_count_num_a_o,
output logic [ CsrDataWidth-1:0] csr_auto_count_num_b_o,
// Data slicer configurations
output logic [ SlicerModeWidth-1:0] csr_data_slice_mode_a_o,
output logic [ SlicerModeWidth-1:0] csr_data_slice_mode_b_o,
Expand Down
5 changes: 3 additions & 2 deletions rtl/hypercorex_top.sv
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module hypercorex_top # (
parameter int unsigned HoldFifoDepth = 2,
parameter bit EnableRomIM = 1'b0,
parameter bit FifoFallthrough = 1'b0,
parameter bit FifoFallthroughSlicer = 1'b1,
//---------------------------
// Instruction Memory Parameters
//---------------------------
Expand Down Expand Up @@ -536,7 +537,7 @@ module hypercorex_top # (
.SlicerFifoDepth ( SlicerFifoDepth ),
.CsrDataWidth ( CsrDataWidth ),
// Don't touch!
.FifoFallthrough ( FifoFallthrough ),
.FifoFallthrough ( FifoFallthroughSlicer ),
.ModeWidth ( SlicerModeWidth )
) i_data_slicer_a (
// Clocks and reset
Expand Down Expand Up @@ -564,7 +565,7 @@ module hypercorex_top # (
.SlicerFifoDepth ( SlicerFifoDepth ),
.CsrDataWidth ( CsrDataWidth ),
// Don't touch!
.FifoFallthrough ( FifoFallthrough ),
.FifoFallthrough ( FifoFallthroughSlicer ),
.ModeWidth ( SlicerModeWidth )
) i_data_slicer_b (
// Clocks and reset
Expand Down
12 changes: 6 additions & 6 deletions rtl/tb/tb_hypercorex.sv
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ module tb_hypercorex # (
input logic highdim_mode_i,
// Low dim signals
input logic [TbMemAddrWidth-1:0] im_a_lowdim_wr_addr_i,
input logic [ CsrDataWidth-1:0] im_a_lowdim_wr_data_i,
input logic [ LowDimWidth-1:0] im_a_lowdim_wr_data_i,
input logic im_a_lowdim_wr_en_i,
input logic [TbMemAddrWidth-1:0] im_a_lowdim_rd_addr_i,
output logic [ CsrDataWidth-1:0] im_a_lowdim_rd_data_o,
output logic [ LowDimWidth-1:0] im_a_lowdim_rd_data_o,

input logic [TbMemAddrWidth-1:0] im_b_lowdim_wr_addr_i,
input logic [ CsrDataWidth-1:0] im_b_lowdim_wr_data_i,
input logic [ LowDimWidth-1:0] im_b_lowdim_wr_data_i,
input logic im_b_lowdim_wr_en_i,
input logic [TbMemAddrWidth-1:0] im_b_lowdim_rd_addr_i,
output logic [ CsrDataWidth-1:0] im_b_lowdim_rd_data_o,
output logic [ LowDimWidth-1:0] im_b_lowdim_rd_data_o,
// High dim signals
input logic [TbMemAddrWidth-1:0] im_a_highdim_wr_addr_i,
input logic [ HVDimension-1:0] im_a_highdim_wr_data_i,
Expand Down Expand Up @@ -159,7 +159,7 @@ module tb_hypercorex # (

// Memory module for low dimensional memory IM A
tb_rd_memory # (
.DataWidth ( CsrDataWidth ),
.DataWidth ( LowDimWidth ),
.AddrWidth ( CsrAddrWidth ),
.MemDepth ( TbMemDepth )
) i_im_a_lowdim_memory (
Expand Down Expand Up @@ -215,7 +215,7 @@ module tb_hypercorex # (

// Memory module for low dimensional memory IM B
tb_rd_memory # (
.DataWidth ( CsrDataWidth ),
.DataWidth ( LowDimWidth ),
.AddrWidth ( CsrAddrWidth ),
.MemDepth ( TbMemDepth )
) i_im_b_lowdim_memory (
Expand Down
5 changes: 5 additions & 0 deletions sw/asm/train_char_recog_data_slice.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
imab_bind_bunda
mv_bunda_qhv
am_load
am_search
clr_bunda
Loading

0 comments on commit f0b2781

Please sign in to comment.