-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.rb
174 lines (146 loc) · 5.06 KB
/
interface.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# rubyを用いてHDLRubyの回路記述を
# ジェネリックにインスタンス化して
# VerilogやVHDLの記述に変換する
# プログラム
require 'HDLRuby'
configure_high
require_relative 'network_constructor.rb'
#!/usr/bin/ruby
require 'fileutils'
require 'HDLRuby'
require 'HDLRuby/hruby_check.rb'
# require 'ripper'
require 'HDLRuby/hruby_low2high'
require 'HDLRuby/hruby_low2c'
require 'HDLRuby/hruby_low2vhd'
require 'HDLRuby/hruby_low_fix_types'
# require 'HDLRuby/hruby_low_expand_types' # For now dormant
require 'HDLRuby/hruby_low_without_outread'
require 'HDLRuby/hruby_low_with_bool'
require 'HDLRuby/hruby_low_bool2select'
require 'HDLRuby/hruby_low_without_select'
require 'HDLRuby/hruby_low_without_namespace'
require 'HDLRuby/hruby_low_without_bit2vector'
require 'HDLRuby/hruby_low_with_port'
require 'HDLRuby/hruby_low_with_var'
require 'HDLRuby/hruby_low_without_concat'
require 'HDLRuby/hruby_low_without_connection'
require 'HDLRuby/hruby_low_cleanup'
require 'HDLRuby/hruby_verilog.rb'
require 'HDLRuby/backend/hruby_allocator'
require 'HDLRuby/backend/hruby_c_allocator'
# Instantiate it for checking.
$network_constructor_caller = proc { |*args,&blk| network_constructor(*args,&blk) }
class NN4H
include HDLRuby
def initialize(columns, func, typ, integer_width, decimal_width, address_width, inputs, weights, biases)
@columns = columns
@func = func
@typ = typ
@integer_width = integer_width
@decimal_width = decimal_width
@address_width = address_width
@inputs = inputs
@weights = weights
@biases = biases
end
def instantiate
# return network_constructor(@columns, @func, @typ, @integer_width, @decimal_width, @address_width, @weights, @biases).(:neural_network)
return $network_constructor_caller.(@columns, @func, @typ, @integer_width, @decimal_width, @address_width, @inputs, @weights, @biases).(:neural_network)
end
def to_verilog(top_instance)
# Generate the low level representation.
top_system = top_instance.to_low.systemT
top_system.each_systemT_deep do |systemT|
systemT.to_upper_space!
systemT.to_global_systemTs!
systemT.initial_concat_to_timed!
systemT.with_port!
end
input = "neural_network.rb"
basename = File.basename(input, File.extname(input))
output = "neural_network"
basename = output + "/" + basename
# Create a directory if necessary.
unless File.directory?(output)
FileUtils.mkdir_p(output)
end
# File name counter.
# $namecount = 0
# Prepare the initial name for the main file.
name = basename + ".v"
# Multiple files generation mode.
top_system.each_systemT_deep do |systemT|
# Generate the name if necessary.
unless name
name = output + "/" +
HDLRuby::Verilog.name_to_verilog(systemT.name) +
".v"
end
# Open the file for current systemT
outfile = File.open(name,"w")
# Generate the Verilog code in to.
outfile << systemT.to_verilog
# Close the file.
outfile.close
# Clears the name.
name = nil
end
#output = []
# Single file generation mode.
#top_system.each_systemT_deep.reverse_each do |systemT|
# output << systemT.to_verilog
#end
# Displays it
#puts output.size
end
def to_vhdl(top_instance)
top_system = top_instance.to_low.systemT
top_system.each_systemT_deep do |systemT|
systemT.outread2inner! #unless $options[:vhdl08] || $options[:alliance]
systemT.with_boolean!
systemT.boolean_in_assign2select! #unless $options[:alliance]
systemT.bit2vector2inner! #unless $options[:vhdl08] || $options[:alliance]
systemT.select2case! # if $options[:alliance]
systemT.break_concat_assigns! # if $options[:alliance]
systemT.to_upper_space!
systemT.to_global_systemTs!
systemT.break_types!
systemT.with_port!
systemT.with_var!
systemT.cleanup!
end
output = []
# Single file generation mode.
top_system.each_systemT_deep.reverse_each do |systemT|
output << systemT.to_vhdl
end
# Displays it
puts output.size
end
end
# データ型の宣言
integer_width = 4 # 整数部のビット幅
decimal_width = 4 # 実数部のビット幅
address_width = 4 # lutのアドレスのビット幅
typ = signed[integer_width, decimal_width] # データ型
tanh = proc{ |i| Math.tanh(i) }
# ニューラルネットワークの構造
columns = [2, 2, 1]
func = [tanh, tanh] # 活性化関数
# 重みを持つ層の形
neuron_columns = columns[1..-1]
# 重みとバイアスの配列の形状
weights_geometry = neuron_columns.zip(columns[0..-2])
biases_geometry = neuron_columns.map{ |col| col }
# ランダムに初期化した重みとバイアスの配列
biases = biases_geometry.map{ |size| size.times.map{ rand(-1.0..1.0) }}
weights = weights_geometry.map{ |shape| Array.new(shape[0], shape[1].times.map{ rand(-1.0..1.0) } ) }
inputs = [1, 1]
puts "biases : #{biases}"
puts "weights : #{weights}"
puts "inputs : #{inputs}"
nn = NN4H.new(columns, func, typ, integer_width, decimal_width, address_width, inputs, weights, biases)
instance = nn.instantiate
nn.to_verilog(instance)
#to_vhdl(neural_network)