| 1 |
8 |
jstefanowi |
----------------------------------------------------------------------------------
|
| 2 |
|
|
-- Company: CEI - UPM
|
| 3 |
|
|
-- Engineer: David Aledo
|
| 4 |
|
|
--
|
| 5 |
|
|
-- Create Date: 01.10.2015
|
| 6 |
|
|
-- Design Name: Configurable ANN
|
| 7 |
|
|
-- Pakage Name: layers_pkg
|
| 8 |
|
|
-- Project Name:
|
| 9 |
|
|
-- Target Devices:
|
| 10 |
|
|
-- Tool Versions:
|
| 11 |
|
|
-- Description: define array types for generics, functions to give them values from
|
| 12 |
|
|
-- string generics, and other help functions
|
| 13 |
|
|
-- Dependencies:
|
| 14 |
|
|
--
|
| 15 |
|
|
-- Revision:
|
| 16 |
|
|
-- Revision 0.01 - File Created
|
| 17 |
|
|
-- Additional Comments:
|
| 18 |
|
|
--
|
| 19 |
|
|
----------------------------------------------------------------------------------
|
| 20 |
|
|
|
| 21 |
|
|
library IEEE;
|
| 22 |
|
|
use IEEE.STD_LOGIC_1164.all;
|
| 23 |
|
|
|
| 24 |
|
|
--library proc_common_v3_00_a; -- Deprecated libray from XPS tool
|
| 25 |
|
|
--use proc_common_v3_00_a.proc_common_pkg.all;
|
| 26 |
|
|
|
| 27 |
|
|
package layers_pkg is
|
| 28 |
|
|
|
| 29 |
|
|
-- Array types for generics:
|
| 30 |
|
|
type int_vector is array (natural range <>) of integer; -- Generic integer vector
|
| 31 |
|
|
type ltype_vector is array (integer range <>) of string(1 to 2); -- Layer type vector
|
| 32 |
|
|
type ftype_vector is array (integer range <>) of string(1 to 6); -- Activation function type vector
|
| 33 |
|
|
-- Note: these strings cannot be unconstrined
|
| 34 |
|
|
|
| 35 |
|
|
-- Functions to assign values to vector types from string generics:
|
| 36 |
|
|
-- Arguments:
|
| 37 |
|
|
-- str_v : string to be converted
|
| 38 |
|
|
-- n : number of elements of the vector
|
| 39 |
|
|
-- Return: assigned vector
|
| 40 |
|
|
function assign_ints(str_v : string; n : integer) return int_vector;
|
| 41 |
|
|
function assign_ltype(str_v : string; n : integer) return ltype_vector;
|
| 42 |
|
|
function assign_ftype(str_v : string; n : integer) return ftype_vector;
|
| 43 |
|
|
|
| 44 |
|
|
-- Other functions:
|
| 45 |
|
|
|
| 46 |
|
|
-- Argument: c : character to be checked
|
| 47 |
|
|
-- Return: TRUE if c is 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9
|
| 48 |
|
|
function is_digit(c : character) return boolean;
|
| 49 |
|
|
|
| 50 |
|
|
-- Base two logarithm for int_vector:
|
| 51 |
|
|
-- Arguments:
|
| 52 |
|
|
-- v : integer vector
|
| 53 |
|
|
-- n : number of elements of the vector
|
| 54 |
|
|
-- Return : integer vector of the base two logarithms of each elment of v
|
| 55 |
|
|
function log2(v : int_vector; n : integer) return int_vector;
|
| 56 |
|
|
|
| 57 |
|
|
-- Calculate the total weight and bias memory address length:
|
| 58 |
|
|
-- Arguments:
|
| 59 |
|
|
-- NumIn : number of inputs of the network
|
| 60 |
|
|
-- NumN : number of neurons of each layer
|
| 61 |
|
|
-- n : number of layers (number of elements of NumN)
|
| 62 |
|
|
-- Return: total weight and bias memory address length (integer)
|
| 63 |
|
|
function calculate_addr_l(NumIn : integer; NumN : int_vector; n : integer) return integer;
|
| 64 |
|
|
|
| 65 |
|
|
-- Assign the weight and bias memory address lenght of each layer:
|
| 66 |
|
|
-- Arguments:
|
| 67 |
|
|
-- NumIn : number of inputs of the network
|
| 68 |
|
|
-- NumN : number of neurons of each layer
|
| 69 |
|
|
-- n : number of layers (number of elements of NumN and the return integer vector)
|
| 70 |
|
|
-- Return: weight and bias memory address lenght of each layer (integer vector)
|
| 71 |
|
|
function assign_addrl(NumIn : integer; NumN : int_vector; n : integer) return int_vector;
|
| 72 |
|
|
|
| 73 |
|
|
-- Calculate the maximum of the multiplications of two vectors element by element
|
| 74 |
|
|
-- Arguments:
|
| 75 |
|
|
-- v1 : input vector 1
|
| 76 |
|
|
-- v2 : input vector 2
|
| 77 |
|
|
-- Return: maximum of the multiplications of two vectors element by element
|
| 78 |
|
|
function calculate_max_mul(v1 : int_vector; v2 : int_vector) return integer;
|
| 79 |
|
|
|
| 80 |
|
|
-- Returns the max value of the input integer vector:
|
| 81 |
|
|
function calculate_max(v : int_vector) return integer;
|
| 82 |
|
|
|
| 83 |
|
|
-- Adding needed functions from the deprecated libray proc_common_v3_00_a:
|
| 84 |
|
|
function max2 (num1, num2 : integer) return integer;
|
| 85 |
|
|
function log2(x : natural) return integer;
|
| 86 |
|
|
|
| 87 |
|
|
end layers_pkg;
|
| 88 |
|
|
|
| 89 |
|
|
package body layers_pkg is
|
| 90 |
|
|
|
| 91 |
|
|
function max2 (num1, num2 : integer) return integer is
|
| 92 |
|
|
begin
|
| 93 |
|
|
if num1 >= num2 then
|
| 94 |
|
|
return num1;
|
| 95 |
|
|
else
|
| 96 |
|
|
return num2;
|
| 97 |
|
|
end if;
|
| 98 |
|
|
end function max2;
|
| 99 |
|
|
|
| 100 |
|
|
-- Function log2 -- returns number of bits needed to encode x choices
|
| 101 |
|
|
-- x = 0 returns 0
|
| 102 |
|
|
-- x = 1 returns 0
|
| 103 |
|
|
-- x = 2 returns 1
|
| 104 |
|
|
-- x = 4 returns 2, etc.
|
| 105 |
|
|
function log2(x : natural) return integer is
|
| 106 |
|
|
variable i : integer := 0;
|
| 107 |
|
|
variable val: integer := 1;
|
| 108 |
|
|
begin
|
| 109 |
|
|
if x = 0 then
|
| 110 |
|
|
return 0;
|
| 111 |
|
|
else
|
| 112 |
|
|
for j in 0 to 29 loop -- for loop for XST
|
| 113 |
|
|
if val >= x then null;
|
| 114 |
|
|
else
|
| 115 |
|
|
i := i+1;
|
| 116 |
|
|
val := val*2;
|
| 117 |
|
|
end if;
|
| 118 |
|
|
end loop;
|
| 119 |
|
|
-- Fix per CR520627 XST was ignoring this anyway and printing a
|
| 120 |
|
|
-- Warning in SRP file. This will get rid of the warning and not
|
| 121 |
|
|
-- impact simulation.
|
| 122 |
|
|
-- synthesis translate_off
|
| 123 |
|
|
assert val >= x
|
| 124 |
|
|
report "Function log2 received argument larger" &
|
| 125 |
|
|
" than its capability of 2^30. "
|
| 126 |
|
|
severity failure;
|
| 127 |
|
|
-- synthesis translate_on
|
| 128 |
|
|
return i;
|
| 129 |
|
|
end if;
|
| 130 |
|
|
end function log2;
|
| 131 |
|
|
|
| 132 |
|
|
|
| 133 |
|
|
function is_digit(c : character) return boolean is
|
| 134 |
|
|
begin
|
| 135 |
|
|
case c is
|
| 136 |
|
|
when '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => return true;
|
| 137 |
|
|
when others => return false;
|
| 138 |
|
|
end case;
|
| 139 |
|
|
end is_digit;
|
| 140 |
|
|
|
| 141 |
|
|
|
| 142 |
|
|
-- Assign values to a integer vector from a string:
|
| 143 |
|
|
-- Arguments:
|
| 144 |
|
|
-- str_v : string to be converted
|
| 145 |
|
|
-- n : number of elements of the vector
|
| 146 |
|
|
-- Return: assigned integer vector
|
| 147 |
|
|
function assign_ints(str_v : string; n : integer) return int_vector is
|
| 148 |
|
|
variable i : integer := n-1; ---- element counter
|
| 149 |
|
|
variable d_power : integer := 1; -- decimal power
|
| 150 |
|
|
variable ret : int_vector(n-1 downto 0) := (others => 0); -- return value
|
| 151 |
|
|
begin
|
| 152 |
|
|
for c in str_v'length downto 1 loop -- read every character in str_v
|
| 153 |
|
|
if str_v(c) = ' ' then -- a space separates a new element
|
| 154 |
|
|
assert i > 0
|
| 155 |
|
|
report "Error in assign_ints: number of elements in string is greater than n."
|
| 156 |
|
|
severity error;
|
| 157 |
|
|
i := i -1; -- decrease element counter to start calculate a new element
|
| 158 |
|
|
d_power := 1; -- reset the decimal power to 1
|
| 159 |
|
|
else
|
| 160 |
|
|
assert is_digit(str_v(c)) -- assert the new character is a digit
|
| 161 |
|
|
report "Error in assign_ints: character " & str_v(c) & " is not a digit."
|
| 162 |
|
|
severity error;
|
| 163 |
|
|
-- add the value of the new charactar to the element calculation ( + ("<new_digit>" - "0") * d_power):
|
| 164 |
|
|
ret(i) := ret(i) + (character'pos(str_v(c))-character'pos('0'))*d_power;
|
| 165 |
|
|
d_power := d_power*10; -- increase the decimal power for the next digit
|
| 166 |
|
|
end if;
|
| 167 |
|
|
end loop;
|
| 168 |
|
|
assert i = 0
|
| 169 |
|
|
report "Error in assign_ints: number of elements in string is less than n."
|
| 170 |
|
|
severity error;
|
| 171 |
|
|
return ret;
|
| 172 |
|
|
end assign_ints;
|
| 173 |
|
|
|
| 174 |
|
|
-- Assign values to an activation function type vector from a string:
|
| 175 |
|
|
-- Arguments:
|
| 176 |
|
|
-- str_v : string to be converted
|
| 177 |
|
|
-- n : number of elements of the vector
|
| 178 |
|
|
-- Return: assigned activation function type vector
|
| 179 |
|
|
function assign_ftype(str_v : string; n : integer) return ftype_vector is
|
| 180 |
|
|
variable i : integer := 0; -- element counter
|
| 181 |
|
|
variable l : integer := 1; -- element length counter
|
| 182 |
|
|
variable ret : ftype_vector(n-1 downto 0) := (others => "linear"); -- return value
|
| 183 |
|
|
begin
|
| 184 |
|
|
for c in 1 to str_v'length loop -- read every character in str_v
|
| 185 |
|
|
if str_v(c) = ' ' then -- a space separates a new element
|
| 186 |
|
|
i := i +1; -- increase element counter to start calculate a new element
|
| 187 |
|
|
l := 1; -- reset element length counter
|
| 188 |
|
|
else
|
| 189 |
|
|
ret(i)(l) := str_v(c);
|
| 190 |
|
|
l := l +1; -- increase element length counter
|
| 191 |
|
|
end if;
|
| 192 |
|
|
end loop;
|
| 193 |
|
|
assert i = n-1
|
| 194 |
|
|
report "Error in assign_ftype: number of elements in string is less than n."
|
| 195 |
|
|
severity error;
|
| 196 |
|
|
return ret;
|
| 197 |
|
|
end assign_ftype;
|
| 198 |
|
|
|
| 199 |
|
|
-- Assign values to an layer type vector from a string:
|
| 200 |
|
|
-- Arguments:
|
| 201 |
|
|
-- str_v : string to be converted
|
| 202 |
|
|
-- n : number of elements of the vector
|
| 203 |
|
|
-- Return: assigned layer type vector
|
| 204 |
|
|
function assign_ltype(str_v : string; n : integer) return ltype_vector is
|
| 205 |
|
|
variable i : integer := 0; -- element counter
|
| 206 |
|
|
variable l : integer := 1; -- element length counter
|
| 207 |
|
|
variable ret : ltype_vector(n-1 downto 0) := (others => "SP"); -- return value
|
| 208 |
|
|
begin
|
| 209 |
|
|
for c in 1 to str_v'length loop
|
| 210 |
|
|
if str_v(c) = ' ' then -- a space separates a new element
|
| 211 |
|
|
i := i +1; -- increase element counter to start calculate a new element
|
| 212 |
|
|
l := 1; -- reset element length counter
|
| 213 |
|
|
else
|
| 214 |
|
|
assert str_v(c) = 'P' or str_v(c) = 'S'
|
| 215 |
|
|
report "Error in assign_ltype: character " & str_v(c) & " is not 'P' (parallel) or 'S' (serial)."
|
| 216 |
|
|
severity error;
|
| 217 |
|
|
ret(i)(l) := str_v(c);
|
| 218 |
|
|
l := l +1; -- increase element length counter
|
| 219 |
|
|
end if;
|
| 220 |
|
|
end loop;
|
| 221 |
|
|
assert i = n-1
|
| 222 |
|
|
report "Error in assign_ltype: number of elements do not coincide with number of introduced elements."
|
| 223 |
|
|
severity error;
|
| 224 |
|
|
return ret;
|
| 225 |
|
|
end assign_ltype;
|
| 226 |
|
|
|
| 227 |
|
|
-- Calculate the total weight and bias memory address length:
|
| 228 |
|
|
-- Arguments:
|
| 229 |
|
|
-- NumIn : number of inputs of the network
|
| 230 |
|
|
-- NumN : number of neurons of each layer
|
| 231 |
|
|
-- n : number of layers (number of elements of NumN)
|
| 232 |
|
|
-- Return: total weight and bias memory address length (integer)
|
| 233 |
|
|
function calculate_addr_l(NumIn : integer; NumN : int_vector; n : integer) return integer is -- matrix + b_sel
|
| 234 |
|
|
variable addr_l : integer := log2(NumIn)+log2(NumN(0)); -- return value. Initialized with the weight memory length of the first layer
|
| 235 |
|
|
begin
|
| 236 |
|
|
-- Calculate the maximum of the weight memory length:
|
| 237 |
|
|
for i in 1 to n-1 loop
|
| 238 |
9 |
jstefanowi |
addr_l := max2( addr_l, log2(NumN(i-1))+log2(NumN(i)) );
|
| 239 |
8 |
jstefanowi |
end loop;
|
| 240 |
|
|
addr_l := addr_l +1; -- add bias select bit
|
| 241 |
|
|
return addr_l;
|
| 242 |
|
|
end calculate_addr_l;
|
| 243 |
|
|
|
| 244 |
|
|
-- Base two logarithm for int_vector:
|
| 245 |
|
|
-- Arguments:
|
| 246 |
|
|
-- v : integer vector
|
| 247 |
|
|
-- n : number of elements of the vector
|
| 248 |
|
|
-- Return : integer vector of the base two logarithms of each elment of v
|
| 249 |
|
|
function log2(v : int_vector; n : integer) return int_vector is
|
| 250 |
|
|
variable ret : int_vector(n-1 downto 0); -- return value
|
| 251 |
|
|
begin
|
| 252 |
|
|
-- for each element of v, calculate its base two logarithm:
|
| 253 |
|
|
for i in 0 to n-1 loop
|
| 254 |
|
|
ret(i) := log2(v(i));
|
| 255 |
|
|
end loop;
|
| 256 |
|
|
return ret;
|
| 257 |
|
|
end log2;
|
| 258 |
|
|
|
| 259 |
|
|
-- Assign the weight and bias memory address lenght of each layer:
|
| 260 |
|
|
-- Arguments:
|
| 261 |
|
|
-- NumIn : number of inputs of the network
|
| 262 |
|
|
-- NumN : number of neurons of each layer
|
| 263 |
|
|
-- n : number of layers (number of elements of NumN and the return integer vector)
|
| 264 |
|
|
-- Return: weight and bias memory address lenght of each layer (integer vector)
|
| 265 |
|
|
function assign_addrl(NumIn : integer; NumN : int_vector; n : integer) return int_vector is
|
| 266 |
|
|
variable ret : int_vector(n-1 downto 0); -- return value
|
| 267 |
|
|
begin
|
| 268 |
|
|
ret(0) := log2(NumIn)+log2(NumN(0)); -- Weight memory length of the first layer
|
| 269 |
|
|
for i in 1 to n-1 loop
|
| 270 |
|
|
ret(i) := log2(NumN(i-1))+log2(NumN(i));
|
| 271 |
|
|
end loop;
|
| 272 |
|
|
return ret;
|
| 273 |
|
|
end assign_addrl;
|
| 274 |
|
|
|
| 275 |
|
|
-- Returns the max value of the input integer vector:
|
| 276 |
|
|
function calculate_max(v : int_vector) return integer is
|
| 277 |
|
|
variable ac_max : integer := 0; -- return value
|
| 278 |
|
|
begin
|
| 279 |
|
|
for i in 0 to v'length-1 loop
|
| 280 |
|
|
ac_max := max2(ac_max,v(i));
|
| 281 |
|
|
end loop;
|
| 282 |
|
|
return ac_max;
|
| 283 |
|
|
end calculate_max;
|
| 284 |
|
|
|
| 285 |
|
|
-- Calculate the maximum of the multiplications of two vectors element by element
|
| 286 |
|
|
-- Arguments:
|
| 287 |
|
|
-- v1 : input vector 1
|
| 288 |
|
|
-- v2 : input vector 2
|
| 289 |
|
|
-- Return: maximum of the multiplications of two vectors element by element
|
| 290 |
|
|
function calculate_max_mul(v1 : int_vector; v2 : int_vector) return integer is
|
| 291 |
|
|
variable ac_max : integer := 0;
|
| 292 |
|
|
begin
|
| 293 |
|
|
assert v1'length = v2'length
|
| 294 |
|
|
report "Error in calculate_max_mul: vector's length do not coincide."
|
| 295 |
|
|
severity error;
|
| 296 |
|
|
for i in 0 to v1'length-1 loop
|
| 297 |
|
|
ac_max := max2(ac_max,v1(i)*v2(i));
|
| 298 |
|
|
end loop;
|
| 299 |
|
|
return ac_max;
|
| 300 |
|
|
end calculate_max_mul;
|
| 301 |
|
|
|
| 302 |
|
|
end layers_pkg;
|