tensor_stream-opencl 0.3.1 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b433e9e7ab38a517c21b57065e5a43b112640fd7c419fb7baa2f3319128cdacf
4
- data.tar.gz: fab7d48513cb0f8481e151d18b088782918cb1539b59586613a00c4d5f5aeed2
3
+ metadata.gz: 1c00d22564cef50ae50f6302975c6bd35d1e594d70f3645f2c2fa5f7bf31ad8c
4
+ data.tar.gz: 31bb29c838d9ee652d52b9053781110b13e4b80937d777e368df54349c1ea2a9
5
5
  SHA512:
6
- metadata.gz: 04d106f5ee5fac49eba20ff143bb2212a1cafd5140fc04cee20958ffea0c5909d352824948badf16ec5bc8ca2a7b13b4dcf7748eb03cbd6dc8a466c6ae0f5040
7
- data.tar.gz: e17171f28641ce3496c0b338b6913c96e10d9fd5ce93b7980dae6edef00e63e5f7c4dcb60ed04fed5271a474b4940d069ebcf6a00bbfd3c4e6eafa2c0c4f26ed
6
+ metadata.gz: 4aca3b8e774e9ff7b8b50fb25511616069bb5357d8b0c9dab1af2b8681a5aabd6b580e57912750c2fa6326a29dd9b4ef10ade9685d897e1c004f54a5022c0bcd
7
+ data.tar.gz: d5f689330ef270475a5ea907cded4d1c9c1abdc160e1f565974a9e4b5dfd39bf0b3d5c42444252f0c1efc73d3a6001c8b8be0282aa065412feeb20082d4404eb
@@ -0,0 +1,35 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [master]
6
+ pull_request:
7
+
8
+ jobs:
9
+ test:
10
+ runs-on: ubuntu-latest
11
+ strategy:
12
+ fail-fast: false
13
+ matrix:
14
+ ruby: ["3.1", "3.3", "3.4"]
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+ - name: Clone core next to this repo (shared spec support + path gem)
18
+ run: git clone --depth 1 https://github.com/jedld/tensor_stream.git ../tensor_stream
19
+ - name: Install system dependencies (POCL OpenCL ICD + jpeg gem headers)
20
+ run: sudo apt-get update -q && sudo apt-get install -yq --no-install-recommends pocl-opencl-icd ocl-icd-libopencl1 libjpeg-dev
21
+ - name: Verify OpenCL device is visible
22
+ run: ls /etc/OpenCL/vendors && cat /etc/OpenCL/vendors/*.icd && ldconfig -p | grep -i libopencl
23
+ - name: Set up Ruby ${{ matrix.ruby }}
24
+ uses: ruby/setup-ruby@v1
25
+ with:
26
+ ruby-version: ${{ matrix.ruby }}
27
+ - name: Install gems
28
+ run: |
29
+ # numo-narray and narray need a legacy C dialect on GCC 14+
30
+ # runners (C23 removed K&R-style empty parameter lists)
31
+ bundle config set --local build.numo-narray "--with-cflags=-std=gnu17"
32
+ bundle config set --local build.narray "--with-cflags=-std=gnu17"
33
+ bundle install --jobs 4 --retry 3
34
+ - name: Run specs (OpenCL evaluator on POCL CPU device)
35
+ run: bundle exec rspec
data/CHANGELOG.md ADDED
@@ -0,0 +1,38 @@
1
+ # Changelog
2
+
3
+ ## [0.4.0] - 2026-12-06
4
+
5
+ ### Fixed
6
+ - Kernel template compilation on Ruby 3+: the removed positional
7
+ `ERB.new(src, nil, '%')` form now uses `trim_mode:`; previously every
8
+ kernel compile raised.
9
+ - Heap corruption in the softmax family (`softmax`, `log_softmax`,
10
+ `softmax_cross_entropy_with_logits(_v2)`, `sparse_*`, `*_grad`):
11
+ rank-1 logits were misread as `shape[0]` rows of that same width, so
12
+ kernels wrote `n^2` elements into `n`-element buffers.
13
+ - `unstack`/`split`: sub-buffer regions that violate
14
+ `CL_DEVICE_MEM_BASE_ADDR_ALIGN` (e.g. small slices) now fall back to
15
+ discrete device buffers with a rect copy instead of failing with
16
+ `CL_MISALIGNED_SUB_BUFFER_OFFSET` on strict ICDs (POCL).
17
+ - Inputs arriving from the ruby evaluator's numo-narray fast path are
18
+ converted to ruby arrays before being packed into GPU buffers.
19
+ - Implemented the evaluator storage-manager hook required by core
20
+ (`get_storage_manager`).
21
+ - Latent NameError in the sub-buffer fallback branches.
22
+
23
+ ### Changed
24
+ - Requires `tensor_stream >= 1.3`.
25
+ - The GPU-resident "variable container" model was removed:
26
+ `variable_v2`, `assign*`, `apply_*` optimizers and `save_ts/restore_ts`
27
+ now execute on the ruby evaluator, matching core's host-side variable
28
+ storage (values transition to GPU buffers per op). Device-resident
29
+ variable storage with on-device apply kernels is future work.
30
+ - `narray_ffi` is now an explicit dependency.
31
+ - CI runs the full shared-op suite against the POCL software OpenCL
32
+ ICD (Ruby 3.1/3.3/3.4), so no GPU drivers are needed to develop the
33
+ backend; GPU-device-specific specs self-skip when no GPU is present.
34
+ - Added `bin/opencl_check.rb` smoke tool; README documents the
35
+ compatibility and development setup.
36
+
37
+ ## [0.3.2] and earlier
38
+ - See git history.
data/Gemfile CHANGED
@@ -4,3 +4,19 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in tensor_stream-opencl.gemspec
6
6
  gemspec
7
+
8
+ gem "benchmark"
9
+ gem "drb"
10
+ gem "mutex_m"
11
+ gem "bigdecimal"
12
+
13
+ # develop against the local core checkout when present (monorepo style
14
+ # sibling checkout); CI tracks core master until 1.3+ is pushed to
15
+ # rubygems, after which the released gem dependency also resolves
16
+ if Dir.exist?(File.expand_path("../tensor_stream", __dir__))
17
+ gem "tensor_stream", path: "../tensor_stream"
18
+ else
19
+ gem "tensor_stream", github: "jedld/tensor_stream"
20
+ end
21
+
22
+ gem "numo-narray"
data/README.md CHANGED
@@ -81,10 +81,38 @@ ts.device('/device:GPU:1') do
81
81
  end
82
82
  ```
83
83
 
84
+ ## Compatibility and architecture notes
85
+
86
+ - Requires tensor_stream >= 1.3 (works with the numo-narray fast path: values
87
+ coming from the ruby evaluator are converted to OpenCL buffers at
88
+ evaluator transitions).
89
+ - Tested on Ruby 3.1–3.4. CI runs the full shared op suite against a POCL
90
+ (software) OpenCL device, so no GPU is needed to develop or verify the
91
+ backend. Ruby 4.x is not supported yet: the opencl_ruby_ffi/narray_ffi
92
+ native extensions corrupt memory there (crashes surface mid-suite).
93
+ - Variable storage is host side (the ruby evaluator owns `variable_v2` /
94
+ `assign` / `apply_*` optimizer ops); values cross to GPU buffers per op.
95
+ GPU-resident variable storage with on-device apply kernels is planned.
96
+ - Eager execution (`tf.enable_eager_execution`) always evaluates on the
97
+ ruby evaluator; the OpenCL evaluator is for session/graph execution.
98
+
84
99
  ## Development
85
100
 
86
101
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
87
102
 
103
+ The specs need an OpenCL device. Without GPU drivers, install the POCL
104
+ software ICD (`sudo apt-get install pocl-opencl-icd` on Debian/Ubuntu);
105
+ CI uses exactly this. If your system loader doesn't pick it up
106
+ automatically, point `opencl_ruby_ffi` at it:
107
+
108
+ ```sh
109
+ export OCL_ICD_VENDORS=/path/to/vendors.d # directory containing pocl.icd
110
+ export LD_LIBRARY_PATH=/path/to/lib # contains libpocl.so
111
+ ```
112
+
113
+ Run `bundle exec ruby bin/opencl_check.rb` for a quick device + evaluator
114
+ smoke test before running the full suite.
115
+
88
116
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
89
117
 
90
118
  ## Contributing
@@ -102,50 +102,71 @@ puts TensorStream::Evaluator.default_evaluators
102
102
 
103
103
  sess2 = tf.session
104
104
 
105
- if os == :macosx
106
- puts `sysctl -n machdep.cpu.brand_string`
107
- else
108
- puts `cat /proc/cpuinfo | grep "model name" | head -1`
109
- end
105
+ cpu = if os == :macosx
106
+ `sysctl -n machdep.cpu.brand_string`
107
+ else
108
+ `cat /proc/cpuinfo | grep "model name" | head -1`
109
+ end
110
+
110
111
  device = TensorStream::Evaluator::OpenclEvaluator.default_device.native_device
111
- puts "OpenCL device #{device.platform.to_s} #{device.name}"
112
- Benchmark.bmbm do |x|
113
- x.report("ruby argmin :") { 100.times do sess.run(argmin) end }
114
- x.report("opencl argmin :") { 100.times do sess2.run(argmin) end }
115
- x.report("ruby bias_add_grad :") { 100.times do sess.run(bias_add_grad) end }
116
- x.report("opencl bias_add_grad :") { 100.times do sess2.run(bias_add_grad) end }
117
- x.report("ruby bias_add :") { 100.times do sess.run(bias_add) end }
118
- x.report("opencl bias_add :") { 100.times do sess2.run(bias_add) end }
119
- x.report("ruby conv2d_backprop :") { 100.times do sess.run(conv2d_grad) end }
120
- x.report("opencl conv2d_backprop :") { 100.times do sess2.run(conv2d_grad) end }
121
- x.report("ruby conv2d :") { 100.times do sess.run(conv2d) end }
122
- x.report("opencl conv2d :") { 100.times do sess2.run(conv2d) end }
123
- x.report("ruby arr index :") { 100.times do sess.run(index) end }
124
- x.report("opencl arr index :") { 100.times do sess2.run(index) end }
125
- x.report("ruby min :") { 100.times do sess.run(min) end }
126
- x.report("opencl min :") { 100.times do sess2.run(min) end }
127
- x.report("ruby sum :") { 100.times do sess.run(sum) end }
128
- x.report("opencl sum :") { 100.times do sess2.run(sum) end }
129
- x.report("ruby sum axis 1 :") { 100.times do sess.run(sum_axis_1) end }
130
- x.report("opencl sum axis 1 :") { 100.times do sess2.run(sum_axis_1) end }
131
- x.report("ruby split :") { 100.times do sess.run(split) end }
132
- x.report("opencl split :") { 100.times do sess2.run(split) end }
133
- x.report("ruby add_n :") { 100.times do sess.run(add_n) end }
134
- x.report("opencl add_n :") { 100.times do sess2.run(add_n) end }
135
- x.report("ruby ooo matmul :") { 100.times do sess.run(out_of_order) end }
136
- x.report("opencl ooo matmul :") { 100.times do sess2.run(out_of_order) end }
137
- x.report("ruby softmax :") { 100.times do sess.run(softmax) end }
138
- x.report("opencl softmax :") { 100.times do sess2.run(softmax) end }
139
- x.report("ruby matmul :") { 100.times do sess.run(matmul) end }
140
- x.report("opencl matmul :") { 100.times do sess2.run(matmul) end }
141
- x.report("ruby :") { 100.times do sess.run(model, feed_dict: { p => rand, q => rand }) end }
142
- x.report("opencl :") { 100.times do sess2.run(model, feed_dict: { p => rand, q => rand }) end }
143
- x.report("ruby single function :") { 100.times do sess.run(single_function_test, feed_dict: { p => rand, q => rand }) end }
144
- x.report("opencl single function :") { 100.times do sess2.run(single_function_test, feed_dict: { p => rand, q => rand }) end }
145
- x.report("ruby pow float :") { 100.times do sess.run(pow_f, feed_dict: { p => rand, q => rand }) end }
146
- x.report("opencl pow float :") { 100.times do sess2.run(pow_f, feed_dict: { p => rand, q => rand }) end }
147
- x.report("ruby pow int :") { 100.times do sess.run(pow_i, feed_dict: { p => rand, q => rand }) end }
148
- x.report("opencl pow int :") { 100.times do sess2.run(pow_i, feed_dict: { p => rand, q => rand }) end }
149
- x.report("ruby dropout :") { 100.times do sess.run(dropout) end }
150
- x.report("opencl dropout :") { 100.times do sess2.run(dropout) end }
151
- end
112
+ cl_device = "#{device.platform.to_s} #{device.name}"
113
+
114
+ tests = {
115
+ "argmin" => argmin,
116
+ "bias_add_grad" => bias_add_grad,
117
+ "bias_add" => bias_add,
118
+ "conv2d_backprop" => conv2d_grad,
119
+ "conv2d" => conv2d,
120
+ "index" =>index,
121
+ "min" => min,
122
+ "sum" => sum,
123
+ "sum axis 1" => sum_axis_1,
124
+ "split" => split,
125
+ "add_n" => add_n,
126
+ "out of order matmul" => out_of_order,
127
+ "softmax" => softmax,
128
+ "matmul" => matmul,
129
+ "test model" => ->(sess) { sess.run(model, feed_dict: { p => rand, q => rand }) },
130
+ "single function test" => ->(sess) { sess.run(single_function_test, feed_dict: { p => rand, q => rand }) },
131
+ "pow (float)" => ->(sess) { sess.run(pow_f, feed_dict: { p => rand, q => rand }) },
132
+ "pow (int)" => ->(sess) { sess.run(pow_i, feed_dict: { p => rand, q => rand }) },
133
+ "dropout" => dropout
134
+ }
135
+
136
+ stats = {
137
+ "ruby" => {},
138
+ "opencl" => {},
139
+ }
140
+
141
+ puts "rehersal"
142
+ tests.each do |k, v|
143
+ if v.is_a?(Proc)
144
+ r = Benchmark.measure("ruby #{k}") { 10.times do v.call(sess) end }
145
+ r = Benchmark.measure("opencl #{k}") { 10.times do v.call(sess2) end }
146
+ else
147
+ r = Benchmark.measure("ruby #{k}") { 10.times do sess.run(v) end }
148
+ r = Benchmark.measure("opencl #{k}") { 10.times do sess2.run(v) end }
149
+ end
150
+ end
151
+
152
+ puts "writing benchmark"
153
+
154
+ tests.each do |k, v|
155
+ if v.is_a?(Proc)
156
+ r = Benchmark.measure(k) { 100.times do v.call(sess) end }
157
+ stats["ruby"][r.label] = { real: r.real, stime: r.stime, total: r.total, utime: r.utime }
158
+ r = Benchmark.measure(k) { 100.times do v.call(sess2) end }
159
+ stats["opencl"][r.label] = { real: r.real, stime: r.stime, total: r.total, utime: r.utime }
160
+ else
161
+ r = Benchmark.measure(k) { 100.times do sess.run(v) end }
162
+ stats["ruby"][r.label] = { real: r.real, stime: r.stime, total: r.total, utime: r.utime }
163
+ r = Benchmark.measure(k) { 100.times do sess2.run(v) end }
164
+ stats["opencl"][r.label] = { real: r.real, stime: r.stime, total: r.total, utime: r.utime }
165
+ end
166
+ end
167
+
168
+ output = {
169
+ "#{RUBY_ENGINE }-#{RUBY_VERSION}/#{os}/#{cpu.strip.gsub("model name\t: ", "")}/#{cl_device.strip}" => stats
170
+ }
171
+ current_benchmark = JSON.parse(File.read('benchmark.json'))
172
+ File.write("benchmark_#{Time.now.strftime('%Y%m%d%H%M')}.json", JSON.pretty_generate(current_benchmark.merge(output)))