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 3

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

Line No. Rev Author Line
1 3 ojosynariz
----------------------------------------------------------------------------------
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
   -- Assign values to a integer vector from a string:
142
   -- Arguments:
143
   --    str_v : string to be converted
144
   --    n : number of elements of the vector
145
   -- Return: assigned integer vector
146
   function assign_ints(str_v : string; n : integer) return int_vector is
147
      variable i : integer := n-1;   ---- element counter
148
      variable d_power : integer := 1; -- decimal power
149
      variable ret : int_vector(n-1 downto 0) := (others => 0); -- return value
150
   begin
151
      for c in str_v'length downto 1 loop -- read every character in str_v
152
         if str_v(c) = ' ' then -- a space separates a new element
153
            assert i > 0
154
               report "Error in assign_ints: number of elements in string is greater than n."
155
               severity error;
156
            i := i -1; -- decrease element counter to start calculate a new element
157
            d_power := 1; -- reset the decimal power to 1
158
         else
159
            assert is_digit(str_v(c)) -- assert the new character is a digit
160
               report "Error in assign_ints: character " & str_v(c) & " is not a digit."
161
               severity error;
162
            -- add the value of the new charactar to the element calculation ( + ("<new_digit>" - "0") * d_power):
163
            ret(i) := ret(i) + (character'pos(str_v(c))-character'pos('0'))*d_power;
164
            d_power := d_power*10; -- increase the decimal power for the next digit
165
         end if;
166
      end loop;
167
      assert i = 0
168
         report "Error in assign_ints: number of elements in string is less than n."
169
         severity error;
170
      return ret;
171
   end assign_ints;
172
 
173
   -- Assign values to an activation function type vector from a string:
174
   -- Arguments:
175
   --    str_v : string to be converted
176
   --    n : number of elements of the vector
177
   -- Return: assigned activation function type vector
178
   function assign_ftype(str_v : string; n : integer) return ftype_vector is
179
      variable i : integer := 0; -- element counter
180
      variable l : integer := 1; -- element length counter
181
      variable ret : ftype_vector(n-1 downto 0) := (others => "linear"); -- return value
182
   begin
183
      for c in 1 to str_v'length loop -- read every character in str_v
184
         if str_v(c) = ' ' then -- a space separates a new element
185
            i := i +1; -- increase element counter to start calculate a new element
186
            l := 1; -- reset element length counter
187
         else
188
            ret(i)(l) := str_v(c);
189
            l := l +1; -- increase element length counter
190
         end if;
191
      end loop;
192
      assert i = n-1
193
         report "Error in assign_ftype: number of elements in string is less than n."
194
         severity error;
195
      return ret;
196
   end assign_ftype;
197
 
198
   -- Assign values to an layer type vector from a string:
199
   -- Arguments:
200
   --    str_v : string to be converted
201
   --    n : number of elements of the vector
202
   -- Return: assigned layer type vector
203
   function assign_ltype(str_v : string; n : integer) return ltype_vector is
204
      variable i : integer := 0; -- element counter
205
      variable l : integer := 1; -- element length counter
206
      variable ret : ltype_vector(n-1 downto 0) := (others => "SP"); -- return value
207
   begin
208
      for c in 1 to str_v'length loop
209
         if str_v(c) = ' ' then -- a space separates a new element
210
            i := i +1; -- increase element counter to start calculate a new element
211
            l := 1; -- reset element length counter
212
         else
213
            assert str_v(c) = 'P' or str_v(c) = 'S'
214
               report "Error in assign_ltype: character " & str_v(c) & " is not 'P' (parallel) or 'S' (serial)."
215
               severity error;
216
            ret(i)(l) := str_v(c);
217
            l := l +1; -- increase element length counter
218
         end if;
219
      end loop;
220
      assert i = n-1
221
         report "Error in assign_ltype: number of elements do not coincide with number of introduced elements."
222
         severity error;
223
      return ret;
224
   end assign_ltype;
225
 
226
   -- Calculate the total weight and bias memory address length:
227
   -- Arguments:
228
   --    NumIn : number of inputs of the network
229
   --    NumN : number of neurons of each layer
230
   --    n : number of layers (number of elements of NumN)
231
   -- Return: total weight and bias memory address length (integer)
232
   function calculate_addr_l(NumIn : integer; NumN : int_vector; n : integer) return integer is -- matrix + b_sel
233
      variable addr_l : integer := log2(NumIn)+log2(NumN(0)); -- return value. Initialized with the weight memory length of the first layer
234
   begin
235
      -- Calculate the maximum of the weight memory length:
236
      for i in 1 to n-1 loop
237
         addr_l := max2( addr_l, log2(NumN(i-1)+log2(NumN(i))) );
238
      end loop;
239
      addr_l := addr_l +1; -- add bias select bit
240
      return addr_l;
241
   end calculate_addr_l;
242
 
243
   -- Base two logarithm for int_vector:
244
   -- Arguments:
245
   --    v : integer vector
246
   --    n : number of elements of the vector
247
   -- Return : integer vector of the base two logarithms of each elment of v
248
   function log2(v : int_vector; n : integer) return int_vector is
249
      variable ret : int_vector(n-1 downto 0); -- return value
250
   begin
251
      -- for each element of v, calculate its base two logarithm:
252
      for i in 0 to n-1 loop
253
         ret(i) := log2(v(i));
254
      end loop;
255
      return ret;
256
   end log2;
257
 
258
   -- Assign the weight and bias memory address lenght of each layer:
259
   -- Arguments:
260
   --    NumIn : number of inputs of the network
261
   --    NumN : number of neurons of each layer
262
   --    n : number of layers (number of elements of NumN and the return integer vector)
263
   -- Return: weight and bias memory address lenght of each layer (integer vector)
264
   function assign_addrl(NumIn : integer; NumN : int_vector; n : integer) return int_vector is
265
      variable ret : int_vector(n-1 downto 0); -- return value
266
   begin
267
      ret(0) := log2(NumIn)+log2(NumN(0)); -- Weight memory length of the first layer
268
      for i in 1 to n-1 loop
269
         ret(i) := log2(NumN(i-1))+log2(NumN(i));
270
      end loop;
271
      return ret;
272
   end assign_addrl;
273
 
274
   -- Returns the max value of the input integer vector:
275
   function calculate_max(v : int_vector) return integer is
276
      variable ac_max : integer := 0; -- return value
277
   begin
278
      for i in 0 to v'length-1 loop
279
         ac_max := max2(ac_max,v(i));
280
      end loop;
281
      return ac_max;
282
   end calculate_max;
283
 
284
   -- Calculate the maximum of the multiplications of two vectors element by element
285
   -- Arguments:
286
   --    v1 : input vector 1
287
   --    v2 : input vector 2
288
   -- Return: maximum of the multiplications of two vectors element by element
289
   function calculate_max_mul(v1 : int_vector; v2 : int_vector) return integer is
290
      variable ac_max : integer := 0;
291
   begin
292
      assert v1'length = v2'length
293
         report "Error in calculate_max_mul: vector's length do not coincide."
294
         severity error;
295
      for i in 0 to v1'length-1 loop
296
         ac_max := max2(ac_max,v1(i)*v2(i));
297
      end loop;
298
      return ac_max;
299
   end calculate_max_mul;
300
 
301
end layers_pkg;

powered by: WebSVN 2.1.0

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