Skip to content

Commit

Permalink
add processing element and prepare for synthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
marph91 committed Feb 21, 2021
1 parent 36f43d4 commit ce6bcdb
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
sim_build
.mypy_cache
.pytest_cache
Expand Down
3 changes: 2 additions & 1 deletion sim/test_window_convolution_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def get_threshold(self):
threshold.append(int(threshold_pos))

return concatenate_integers(
self.replace_minus(threshold), bitwidth=math.ceil(math.log2(kernel_size[0] ** 2 * image_shape[2] + 1))
self.replace_minus(threshold),
bitwidth=math.ceil(math.log2(kernel_size[0] ** 2 * image_shape[2] + 1)),
)

cases = (
Expand Down
18 changes: 9 additions & 9 deletions src/maximum_pooling.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ architecture behavioral of maximum_pooling is

begin

proc_output_valid : process (isl_clk) is
begin

if (rising_edge(isl_clk)) then
sl_valid_out <= isl_valid;
end if;

end process proc_output_valid;

gen_channel : for channel in 0 to C_CHANNEL - 1 generate

proc_maximum_pooling : process (isl_clk) is
Expand All @@ -32,22 +41,13 @@ begin
begin

if (rising_edge(isl_clk)) then
sl_valid_out <= '0';

if (isl_valid = '1') then
sl_valid_out <= '1';

-- report "aaa: " & to_string(islv_data(31 downto 24)) & " " & to_string(islv_data(23 downto 16))
-- & " " & to_string(islv_data(15 downto 8)) & " " & to_string(islv_data(7 downto 0));
for col in 0 to C_KERNEL_SIZE - 1 loop
for row in 0 to C_KERNEL_SIZE - 1 loop
slv_roi(col + row * C_KERNEL_SIZE) := islv_data(channel + col * C_CHANNEL + row * C_CHANNEL * C_KERNEL_SIZE);
-- report "bbb: " & to_string(channel + col * C_CHANNEL + row * C_CHANNEL * C_KERNEL_SIZE) & " " & to_string(channel) & " " & to_string(row) & " " & to_string(col) & " " & to_string(slv_roi(col + row * C_KERNEL_SIZE));
end loop;
end loop;

-- report "ccc: " & to_string(slv_roi) & " " & to_string(slv_roi = (slv_roi'range => '0'));

if (slv_roi = (slv_roi'range => '0')) then
slv_data_out(channel) <= '0';
else
Expand Down
79 changes: 62 additions & 17 deletions src/processing_element.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,87 @@ library ieee;

library cnn_lib;

library util;
use util.math_pkg.all;

entity processing_element is
generic (
-- TODO: input bitwidth, for now = 1

C_KERNEL_SIZE : integer range 2 to 3 := 2;
C_INPUT_CHANNEL : integer
-- C_OUTPUT_CHANNEL : integer; --> 1 output channel
C_CONVOLUTION_KERNEL_SIZE : integer := 3;
C_CONVOLUTION_STRIDE : integer := 1;

C_MAXIMUM_POOLING_KERNEL_SIZE : integer := 2;
C_MAXIMUM_POOLING_STRIDE : integer := 2;

C_INPUT_CHANNEL : integer := 8;
C_OUTPUT_CHANNEL : integer := 8;

C_IMG_WIDTH : integer := 4;
C_IMG_HEIGHT : integer := 4
);
port (
isl_clk : in std_logic;
isl_valid : in std_logic;
islv_data : in std_logic_vector(7 downto 0);
oslv_data : out std_logic_vector(7 downto 0);
osl_valid : out std_logic
isl_clk : in std_logic;
isl_start : in std_logic;
isl_valid : in std_logic;
islv_weights : in std_logic_vector(C_CONVOLUTION_KERNEL_SIZE ** 2 * C_INPUT_CHANNEL * C_OUTPUT_CHANNEL - 1 downto 0);
islv_threshold : in std_logic_vector(log2(C_CONVOLUTION_KERNEL_SIZE ** 2 * C_INPUT_CHANNEL + 1) * C_OUTPUT_CHANNEL - 1 downto 0);
islv_data : in std_logic_vector(7 downto 0);
oslv_data : out std_logic_vector(7 downto 0);
osl_valid : out std_logic
);
end entity processing_element;

architecture behavioral of processing_element is

signal sl_add : std_logic := '0';
signal slv_multiplication_result : std_logic_vector(islv_data'range);
signal sl_valid_convolution : std_logic := '0';
signal slv_data_convolution : std_logic_vector(C_OUTPUT_CHANNEL - 1 downto 0);

signal sl_valid_out : std_logic := '0';
signal slv_data_out : std_logic_vector(oslv_data'range);

begin

proc_processing_element : process (isl_clk) is
begin
i_window_convolution_activation : entity cnn_lib.window_convolution_activation
generic map (
C_KERNEL_SIZE => C_CONVOLUTION_KERNEL_SIZE,
C_STRIDE => C_CONVOLUTION_STRIDE,

C_INPUT_CHANNEL => C_INPUT_CHANNEL,
C_OUTPUT_CHANNEL => C_OUTPUT_CHANNEL,

C_IMG_WIDTH => C_IMG_WIDTH,
C_IMG_HEIGHT => C_IMG_HEIGHT
)
port map (
isl_clk => isl_clk,
isl_start => isl_start,
isl_valid => isl_valid,
islv_data => islv_data,
islv_weights => islv_weights,
islv_threshold => islv_threshold,
oslv_data => slv_data_convolution,
osl_valid => sl_valid_convolution
);

i_window_maximum_pooling : entity cnn_lib.window_maximum_pooling
generic map (
C_KERNEL_SIZE => C_CONVOLUTION_KERNEL_SIZE,
C_STRIDE => C_CONVOLUTION_STRIDE,

if (rising_edge(isl_clk)) then
-- window_convolution_activation
-- window_maximum_pooling
end if;
C_CHANNEL => C_OUTPUT_CHANNEL,

end process proc_processing_element;
C_IMG_WIDTH => C_IMG_WIDTH,
C_IMG_HEIGHT => C_IMG_HEIGHT
)
port map (
isl_clk => isl_clk,
isl_start => isl_start,
isl_valid => sl_valid_convolution,
islv_data => slv_data_convolution,
oslv_data => slv_data_out,
osl_valid => sl_valid_out
);

oslv_data <= slv_data_out;
osl_valid <= sl_valid_out;
Expand Down
8 changes: 5 additions & 3 deletions src/window_convolution_activation.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ library ieee;
use ieee.numeric_std.all;

library cnn_lib;

library util;
use util.math_pkg.all;

Expand Down Expand Up @@ -45,7 +46,7 @@ architecture behavioral of window_convolution_activation is
type t_slv_array_1d is array(natural range <>) of std_logic_vector;

constant C_POST_CONVOLUTION_BITWIDTH : integer := log2(C_KERNEL_SIZE * C_KERNEL_SIZE * C_INPUT_CHANNEL + 1);
signal a_data_convolution : t_slv_array_1d(0 to C_OUTPUT_CHANNEL - 1)(C_POST_CONVOLUTION_BITWIDTH - 1 downto 0);
signal a_data_convolution : t_slv_array_1d(0 to C_OUTPUT_CHANNEL - 1)(C_POST_CONVOLUTION_BITWIDTH - 1 downto 0);

signal sl_valid_batch_normalization : std_logic := '0';
signal slv_data_batch_normalization : std_logic_vector(C_OUTPUT_CHANNEL * 1 - 1 downto 0);
Expand Down Expand Up @@ -98,8 +99,8 @@ begin

i_convolution : entity cnn_lib.convolution
generic map (
C_KERNEL_SIZE => C_KERNEL_SIZE,
C_INPUT_CHANNEL => C_INPUT_CHANNEL
C_KERNEL_SIZE => C_KERNEL_SIZE,
C_INPUT_CHANNEL => C_INPUT_CHANNEL
)
port map (
isl_clk => isl_clk,
Expand All @@ -109,6 +110,7 @@ begin
oslv_data => a_data_convolution(output_channel),
osl_valid => sl_valid_convolution
);

-- output channel increments fastest
a_weights(output_channel) <= get_fastest_increment(islv_weights, output_channel, C_OUTPUT_CHANNEL);

Expand Down

0 comments on commit ce6bcdb

Please sign in to comment.