OpenCores
URL https://opencores.org/ocsvn/artificial_neural_network/artificial_neural_network/trunk

Subversion Repositories artificial_neural_network

[/] [artificial_neural_network/] [trunk/] [ANN_kernel/] [RTL_VHDL_files/] [layers_pkg.vhd] - Blame information for rev 8

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2019 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.