tensor_stream-opencl 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +11 -4
  3. data/benchmark/benchmark.rb +91 -0
  4. data/benchmark_intel.txt +36 -0
  5. data/lib/tensor_stream/opencl/array_ops.rb +395 -0
  6. data/lib/tensor_stream/opencl/images_ops.rb +62 -0
  7. data/lib/tensor_stream/opencl/kernels/abs.cl +6 -8
  8. data/lib/tensor_stream/opencl/kernels/acos.cl +3 -4
  9. data/lib/tensor_stream/opencl/kernels/apply_adadelta.cl +2 -4
  10. data/lib/tensor_stream/opencl/kernels/apply_adagrad.cl +12 -0
  11. data/lib/tensor_stream/opencl/kernels/apply_adam.cl +2 -5
  12. data/lib/tensor_stream/opencl/kernels/apply_centered_rms_prop.cl +19 -0
  13. data/lib/tensor_stream/opencl/kernels/apply_gradient.cl +3 -4
  14. data/lib/tensor_stream/opencl/kernels/apply_momentum.cl +2 -4
  15. data/lib/tensor_stream/opencl/kernels/apply_rms_prop.cl +16 -0
  16. data/lib/tensor_stream/opencl/kernels/asin.cl +3 -4
  17. data/lib/tensor_stream/opencl/kernels/ceil.cl +3 -4
  18. data/lib/tensor_stream/opencl/kernels/concat.cl +21 -0
  19. data/lib/tensor_stream/opencl/kernels/cos.cl +3 -5
  20. data/lib/tensor_stream/opencl/kernels/exp.cl +3 -5
  21. data/lib/tensor_stream/opencl/kernels/floor.cl +3 -4
  22. data/lib/tensor_stream/opencl/kernels/log.cl +3 -4
  23. data/lib/tensor_stream/opencl/kernels/log1p.cl +3 -4
  24. data/lib/tensor_stream/opencl/kernels/negate.cl +3 -4
  25. data/lib/tensor_stream/opencl/kernels/reciprocal.cl +3 -4
  26. data/lib/tensor_stream/opencl/kernels/sigmoid.cl +3 -4
  27. data/lib/tensor_stream/opencl/kernels/sign.cl +7 -8
  28. data/lib/tensor_stream/opencl/kernels/sin.cl +3 -4
  29. data/lib/tensor_stream/opencl/kernels/split.cl +17 -0
  30. data/lib/tensor_stream/opencl/kernels/split_n.cl +18 -0
  31. data/lib/tensor_stream/opencl/kernels/sqrt.cl +3 -4
  32. data/lib/tensor_stream/opencl/kernels/square.cl +3 -4
  33. data/lib/tensor_stream/opencl/kernels/tan.cl +3 -4
  34. data/lib/tensor_stream/opencl/kernels/tanh.cl +3 -4
  35. data/lib/tensor_stream/opencl/kernels/tanh_grad.cl +3 -4
  36. data/lib/tensor_stream/opencl/kernels/unpack.cl +23 -0
  37. data/lib/tensor_stream/opencl/nn_ops.rb +111 -26
  38. data/lib/tensor_stream/opencl/opencl_buffer.rb +9 -0
  39. data/lib/tensor_stream/opencl/opencl_evaluator.rb +129 -172
  40. data/lib/tensor_stream/opencl/version.rb +1 -1
  41. data/samples/iris.data +150 -0
  42. data/samples/iris.rb +110 -0
  43. data/samples/mnist_data.rb +65 -0
  44. data/samples/multigpu.rb +73 -0
  45. data/samples/nearest_neighbor.rb +56 -0
  46. data/samples/rnn.rb +108 -0
  47. data/tensor_stream-opencl.gemspec +4 -1
  48. metadata +62 -3
data/samples/rnn.rb ADDED
@@ -0,0 +1,108 @@
1
+ # RNN sample
2
+ #
3
+ # Ruby port Example based on article by Erik Hallström
4
+ # https://medium.com/@erikhallstrm/hello-world-rnn-83cd7105b767
5
+ #
6
+ #
7
+
8
+ require "bundler/setup"
9
+ require 'tensor_stream'
10
+ require 'tensor_stream/opencl'
11
+ require 'pry-byebug'
12
+
13
+
14
+ tf = TensorStream
15
+
16
+ num_epochs = 100
17
+ total_series_length = 50000
18
+ truncated_backprop_length = 15
19
+ state_size = 4
20
+ num_classes = 2
21
+ echo_step = 3
22
+ batch_size = 5
23
+ num_batches = total_series_length / batch_size / truncated_backprop_length
24
+ randomizer = TensorStream.random_uniform([total_series_length], minval: 0, maxval: 2)
25
+
26
+
27
+ def generate_data(randomizer, total_series_length, batch_size, echo_step)
28
+ x = randomizer.eval
29
+ y = x.rotate(-echo_step)
30
+
31
+ y[echo_step] = 0
32
+
33
+ x = TensorStream::TensorShape.reshape(x, [batch_size, -1]) # The first index changing slowest, subseries as rows
34
+ y = TensorStream::TensorShape.reshape(y, [batch_size, -1])
35
+ [x, y]
36
+ end
37
+
38
+ batchX_placeholder = tf.placeholder(:float32, shape: [batch_size, truncated_backprop_length], name: 'batch_x')
39
+ batchY_placeholder = tf.placeholder(:int32, shape: [batch_size, truncated_backprop_length], name: 'batch_y')
40
+
41
+ init_state = tf.placeholder(:float32, shape: [batch_size, state_size], name: 'init_state')
42
+
43
+
44
+ W = tf.variable(tf.random_uniform([state_size+1, state_size]), dtype: :float32, name: 'W')
45
+ b = tf.variable(tf.zeros([state_size]), dtype: :float32, name: 'b')
46
+
47
+ W2 = tf.variable(tf.random_uniform([state_size, num_classes]), dtype: :float32, name: 'W2')
48
+ b2 = tf.variable(tf.zeros([num_classes]), dtype: :float32, name: 'b2')
49
+
50
+
51
+ inputs_series = tf.unpack(batchX_placeholder, axis: 1)
52
+ labels_series = tf.unpack(batchY_placeholder, axis: 1)
53
+
54
+ current_state = init_state
55
+ states_series = []
56
+
57
+ inputs_series.each do |current_input|
58
+ current_input = tf.reshape(current_input, [batch_size, 1])
59
+ input_and_state_concatenated = tf.concat([current_input, current_state], 1) # Increasing number of columns
60
+ next_state = tf.tanh(tf.matmul(input_and_state_concatenated, W) + b) # Broadcasted addition
61
+ states_series << next_state
62
+ current_state = next_state
63
+ end
64
+
65
+ logits_series = states_series.collect do |state|
66
+ tf.matmul(state, W2) + b2
67
+ end
68
+
69
+ predictions_series = logits_series.collect do |logits|
70
+ tf.nn.softmax(logits)
71
+ end
72
+
73
+ losses = logits_series.zip(labels_series).collect do |logits, labels|
74
+ tf.nn.sparse_softmax_cross_entropy_with_logits(logits: logits, labels: labels)
75
+ end
76
+ total_loss = tf.reduce_mean(losses)
77
+
78
+ train_step = TensorStream::Train::AdagradOptimizer.new(0.3).minimize(total_loss)
79
+
80
+ puts "#{tf.get_default_graph.nodes.keys.size} nodes created"
81
+ zeros_state = tf.zeros([batch_size, state_size]).eval
82
+ tf.session do |sess|
83
+ sess.run(tf.global_variables_initializer)
84
+ (0..num_epochs).each do |epoch_idx|
85
+ x, y = generate_data(randomizer, total_series_length, batch_size, echo_step)
86
+ _current_state = zeros_state
87
+ print("New data, epoch", epoch_idx, "\n")
88
+ (0..num_batches - 1).each do |batch_idx|
89
+ start_idx = batch_idx * truncated_backprop_length
90
+ end_idx = start_idx + truncated_backprop_length
91
+
92
+ batchX = x.map { |x| x[start_idx...end_idx] }
93
+ batchY = y.map { |y| y[start_idx...end_idx] }
94
+
95
+ _total_loss, _train_step, _current_state, _predictions_series = sess.run(
96
+ [total_loss, train_step, current_state, predictions_series],
97
+ feed_dict: {
98
+ batchX_placeholder => batchX,
99
+ batchY_placeholder => batchY,
100
+ init_state => _current_state
101
+ })
102
+
103
+ if batch_idx%10 == 0
104
+ print("Step",batch_idx, " Loss ", _total_loss, "\n")
105
+ end
106
+ end
107
+ end
108
+ end
@@ -36,6 +36,9 @@ Gem::Specification.new do |spec|
36
36
  spec.add_development_dependency "rake", "~> 10.0"
37
37
  spec.add_development_dependency "rspec", "~> 3.0"
38
38
  spec.add_development_dependency "pry-byebug"
39
- spec.add_dependency "tensor_stream"
39
+ spec.add_development_dependency "awesome_print"
40
+ spec.add_development_dependency "mnist-learn"
41
+ spec.add_dependency "tensor_stream", "~> 0.9.0"
40
42
  spec.add_dependency "opencl_ruby_ffi"
43
+ spec.add_dependency "oily_png"
41
44
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tensor_stream-opencl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Dayo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-06 00:00:00.000000000 Z
11
+ date: 2018-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,8 +66,50 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: awesome_print
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mnist-learn
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: tensor_stream
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.9.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.9.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: opencl_ruby_ffi
71
113
  requirement: !ruby/object:Gem::Requirement
72
114
  requirements:
73
115
  - - ">="
@@ -81,7 +123,7 @@ dependencies:
81
123
  - !ruby/object:Gem::Version
82
124
  version: '0'
83
125
  - !ruby/object:Gem::Dependency
84
- name: opencl_ruby_ffi
126
+ name: oily_png
85
127
  requirement: !ruby/object:Gem::Requirement
86
128
  requirements:
87
129
  - - ">="
@@ -110,23 +152,31 @@ files:
110
152
  - LICENSE.txt
111
153
  - README.md
112
154
  - Rakefile
155
+ - benchmark/benchmark.rb
156
+ - benchmark_intel.txt
113
157
  - bin/console
114
158
  - bin/setup
115
159
  - lib/tensor_stream/opencl.rb
160
+ - lib/tensor_stream/opencl/array_ops.rb
161
+ - lib/tensor_stream/opencl/images_ops.rb
116
162
  - lib/tensor_stream/opencl/kernels/_bool_operand.cl
117
163
  - lib/tensor_stream/opencl/kernels/_operand.cl
118
164
  - lib/tensor_stream/opencl/kernels/abs.cl
119
165
  - lib/tensor_stream/opencl/kernels/acos.cl
120
166
  - lib/tensor_stream/opencl/kernels/add.cl
121
167
  - lib/tensor_stream/opencl/kernels/apply_adadelta.cl
168
+ - lib/tensor_stream/opencl/kernels/apply_adagrad.cl
122
169
  - lib/tensor_stream/opencl/kernels/apply_adam.cl
170
+ - lib/tensor_stream/opencl/kernels/apply_centered_rms_prop.cl
123
171
  - lib/tensor_stream/opencl/kernels/apply_gradient.cl
124
172
  - lib/tensor_stream/opencl/kernels/apply_momentum.cl
173
+ - lib/tensor_stream/opencl/kernels/apply_rms_prop.cl
125
174
  - lib/tensor_stream/opencl/kernels/argmax.cl
126
175
  - lib/tensor_stream/opencl/kernels/argmin.cl
127
176
  - lib/tensor_stream/opencl/kernels/asin.cl
128
177
  - lib/tensor_stream/opencl/kernels/cast.cl
129
178
  - lib/tensor_stream/opencl/kernels/ceil.cl
179
+ - lib/tensor_stream/opencl/kernels/concat.cl
130
180
  - lib/tensor_stream/opencl/kernels/cond.cl.erb
131
181
  - lib/tensor_stream/opencl/kernels/cos.cl
132
182
  - lib/tensor_stream/opencl/kernels/div.cl.erb
@@ -156,6 +206,8 @@ files:
156
206
  - lib/tensor_stream/opencl/kernels/softmax_cross.cl
157
207
  - lib/tensor_stream/opencl/kernels/softmax_cross_grad.cl
158
208
  - lib/tensor_stream/opencl/kernels/softmax_grad.cl
209
+ - lib/tensor_stream/opencl/kernels/split.cl
210
+ - lib/tensor_stream/opencl/kernels/split_n.cl
159
211
  - lib/tensor_stream/opencl/kernels/sqrt.cl
160
212
  - lib/tensor_stream/opencl/kernels/square.cl
161
213
  - lib/tensor_stream/opencl/kernels/squared_difference.cl
@@ -163,6 +215,7 @@ files:
163
215
  - lib/tensor_stream/opencl/kernels/tan.cl
164
216
  - lib/tensor_stream/opencl/kernels/tanh.cl
165
217
  - lib/tensor_stream/opencl/kernels/tanh_grad.cl
218
+ - lib/tensor_stream/opencl/kernels/unpack.cl
166
219
  - lib/tensor_stream/opencl/kernels/where.cl
167
220
  - lib/tensor_stream/opencl/math_ops.rb
168
221
  - lib/tensor_stream/opencl/nn_ops.rb
@@ -171,6 +224,12 @@ files:
171
224
  - lib/tensor_stream/opencl/opencl_evaluator.rb
172
225
  - lib/tensor_stream/opencl/opencl_template_helper.rb
173
226
  - lib/tensor_stream/opencl/version.rb
227
+ - samples/iris.data
228
+ - samples/iris.rb
229
+ - samples/mnist_data.rb
230
+ - samples/multigpu.rb
231
+ - samples/nearest_neighbor.rb
232
+ - samples/rnn.rb
174
233
  - tensor_stream-opencl.gemspec
175
234
  homepage: https://github.com/jedld/tensor_stream-opencl
176
235
  licenses: