torch-rb 0.8.1 → 0.9.1
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 +4 -4
- data/CHANGELOG.md +20 -0
- data/README.md +26 -44
- data/codegen/generate_functions.rb +13 -5
- data/codegen/native_functions.yaml +1103 -373
- data/ext/torch/backends.cpp +2 -2
- data/ext/torch/ruby_arg_parser.cpp +2 -2
- data/ext/torch/ruby_arg_parser.h +19 -5
- data/ext/torch/templates.h +0 -37
- data/ext/torch/tensor.cpp +8 -8
- data/ext/torch/utils.h +0 -6
- data/lib/torch/inspector.rb +1 -1
- data/lib/torch/nn/convnd.rb +2 -0
- data/lib/torch/nn/functional.rb +1 -1
- data/lib/torch/nn/functional_attention.rb +241 -0
- data/lib/torch/nn/module.rb +30 -0
- data/lib/torch/nn/module_list.rb +49 -0
- data/lib/torch/nn/multihead_attention.rb +123 -0
- data/lib/torch/nn/parameter.rb +6 -0
- data/lib/torch/nn/transformer.rb +92 -0
- data/lib/torch/nn/transformer_decoder.rb +25 -0
- data/lib/torch/nn/transformer_decoder_layer.rb +43 -0
- data/lib/torch/nn/transformer_encoder.rb +25 -0
- data/lib/torch/nn/transformer_encoder_layer.rb +36 -0
- data/lib/torch/nn/utils.rb +12 -0
- data/lib/torch/tensor.rb +21 -8
- data/lib/torch/utils/data/data_loader.rb +3 -1
- data/lib/torch/version.rb +1 -1
- data/lib/torch.rb +6 -45
- metadata +11 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5224c74f6e74ed04396dfa0414400af5cb20bc5e654320421116723ffcb8e83
|
4
|
+
data.tar.gz: 6a2881ddacb7610a231ebd5a1c24d0f71a2662f16f360102141dfd0893c13346
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 045432235e1c691ce85fb937a0562d93b1d9bc312fc648d40dafcc24857eeec4f84e6ceab397793171b0046ccfd785a6caeba37902925dcff1f73c760dd57cec
|
7
|
+
data.tar.gz: 2520fa17dcd13be52aaf1256f431d2951f8113f862189b8691f877dcb48ac9f71ac70719600e27d8c8972494c0b7f11c0983c3330fed7ca6aeed552e8500ec22
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,23 @@
|
|
1
|
+
## 0.9.1 (2022-02-02)
|
2
|
+
|
3
|
+
- Moved `like` methods to C++
|
4
|
+
- Fixed memory issue
|
5
|
+
|
6
|
+
## 0.9.0 (2021-10-23)
|
7
|
+
|
8
|
+
- Updated LibTorch to 1.10.0
|
9
|
+
- Added `real` and `imag` methods to tensors
|
10
|
+
|
11
|
+
## 0.8.3 (2021-10-17)
|
12
|
+
|
13
|
+
- Fixed `dup` method for tensors and parameters
|
14
|
+
- Fixed issues with transformers
|
15
|
+
|
16
|
+
## 0.8.2 (2021-10-03)
|
17
|
+
|
18
|
+
- Added transformers
|
19
|
+
- Added left shift and right shift
|
20
|
+
|
1
21
|
## 0.8.1 (2021-06-15)
|
2
22
|
|
3
23
|
- Added `Backends` module
|
data/README.md
CHANGED
@@ -21,22 +21,26 @@ brew install libtorch
|
|
21
21
|
Add this line to your application’s Gemfile:
|
22
22
|
|
23
23
|
```ruby
|
24
|
-
gem
|
24
|
+
gem "torch-rb"
|
25
25
|
```
|
26
26
|
|
27
|
-
It can take
|
27
|
+
It can take 5-10 minutes to compile the extension.
|
28
28
|
|
29
29
|
## Getting Started
|
30
30
|
|
31
|
-
|
31
|
+
A good place to start is [Deep Learning with Torch.rb: A 60 Minute Blitz](tutorials/blitz/README.md).
|
32
32
|
|
33
|
-
|
33
|
+
## Tutorials
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
- [Transfer learning](tutorials/transfer_learning/README.md)
|
36
|
+
- [Sequence models](tutorials/nlp/sequence_models.md)
|
37
|
+
- [Word embeddings](tutorials/nlp/word_embeddings.md)
|
38
38
|
|
39
|
-
|
39
|
+
## Examples
|
40
|
+
|
41
|
+
- [Image classification with MNIST](examples/mnist) ([日本語版](https://qiita.com/kojix2/items/c19c36dc1bf73ea93409))
|
42
|
+
- [Collaborative filtering with MovieLens](examples/movielens)
|
43
|
+
- [Generative adversarial networks](examples/gan)
|
40
44
|
|
41
45
|
## API
|
42
46
|
|
@@ -48,7 +52,7 @@ This library follows the [PyTorch API](https://pytorch.org/docs/stable/torch.htm
|
|
48
52
|
|
49
53
|
You can follow PyTorch tutorials and convert the code to Ruby in many cases. Feel free to open an issue if you run into problems.
|
50
54
|
|
51
|
-
##
|
55
|
+
## Overview
|
52
56
|
|
53
57
|
Some examples below are from [Deep Learning with PyTorch: A 60 Minutes Blitz](https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html)
|
54
58
|
|
@@ -75,7 +79,7 @@ b = Torch.zeros(2, 3)
|
|
75
79
|
|
76
80
|
Each tensor has four properties
|
77
81
|
|
78
|
-
- `dtype` - the data type - `:uint8`, `:int8`, `:int16`, `:int32`, `:int64`, `:float32`,
|
82
|
+
- `dtype` - the data type - `:uint8`, `:int8`, `:int16`, `:int32`, `:int64`, `:float32`, `:float64`, or `:bool`
|
79
83
|
- `layout` - `:strided` (dense) or `:sparse`
|
80
84
|
- `device` - the compute device, like CPU or GPU
|
81
85
|
- `requires_grad` - whether or not to record gradients
|
@@ -214,7 +218,7 @@ Define a neural network
|
|
214
218
|
```ruby
|
215
219
|
class MyNet < Torch::NN::Module
|
216
220
|
def initialize
|
217
|
-
super
|
221
|
+
super()
|
218
222
|
@conv1 = Torch::NN::Conv2d.new(1, 6, 3)
|
219
223
|
@conv2 = Torch::NN::Conv2d.new(6, 16, 3)
|
220
224
|
@fc1 = Torch::NN::Linear.new(16 * 6 * 6, 120)
|
@@ -225,20 +229,10 @@ class MyNet < Torch::NN::Module
|
|
225
229
|
def forward(x)
|
226
230
|
x = Torch::NN::F.max_pool2d(Torch::NN::F.relu(@conv1.call(x)), [2, 2])
|
227
231
|
x = Torch::NN::F.max_pool2d(Torch::NN::F.relu(@conv2.call(x)), 2)
|
228
|
-
x =
|
232
|
+
x = Torch.flatten(x, 1)
|
229
233
|
x = Torch::NN::F.relu(@fc1.call(x))
|
230
234
|
x = Torch::NN::F.relu(@fc2.call(x))
|
231
|
-
|
232
|
-
x
|
233
|
-
end
|
234
|
-
|
235
|
-
def num_flat_features(x)
|
236
|
-
size = x.size[1..-1]
|
237
|
-
num_features = 1
|
238
|
-
size.each do |s|
|
239
|
-
num_features *= s
|
240
|
-
end
|
241
|
-
num_features
|
235
|
+
@fc3.call(x)
|
242
236
|
end
|
243
237
|
end
|
244
238
|
```
|
@@ -402,19 +396,9 @@ Here’s a list of functions to create tensors (descriptions from the [C++ docs]
|
|
402
396
|
Torch.zeros(3) # tensor([0, 0, 0])
|
403
397
|
```
|
404
398
|
|
405
|
-
## Examples
|
406
|
-
|
407
|
-
Here are a few full examples:
|
408
|
-
|
409
|
-
- [Image classification with MNIST](examples/mnist) ([日本語版](https://qiita.com/kojix2/items/c19c36dc1bf73ea93409))
|
410
|
-
- [Collaborative filtering with MovieLens](examples/movielens)
|
411
|
-
- [Sequence models and word embeddings](examples/nlp)
|
412
|
-
- [Generative adversarial networks](examples/gan)
|
413
|
-
- [Transfer learning](examples/transfer-learning)
|
414
|
-
|
415
399
|
## LibTorch Installation
|
416
400
|
|
417
|
-
[Download LibTorch](https://pytorch.org/)
|
401
|
+
[Download LibTorch](https://pytorch.org/) (for Linux, use the `cxx11 ABI` version). Then run:
|
418
402
|
|
419
403
|
```sh
|
420
404
|
bundle config build.torch-rb --with-torch-dir=/path/to/libtorch
|
@@ -424,7 +408,8 @@ Here’s the list of compatible versions.
|
|
424
408
|
|
425
409
|
Torch.rb | LibTorch
|
426
410
|
--- | ---
|
427
|
-
0.
|
411
|
+
0.9.0+ | 1.10.0+
|
412
|
+
0.8.0-0.8.3 | 1.9.0-1.9.1
|
428
413
|
0.6.0-0.7.0 | 1.8.0-1.8.1
|
429
414
|
0.5.0-0.5.3 | 1.7.0-1.7.1
|
430
415
|
0.3.0-0.4.2 | 1.6.0
|
@@ -444,9 +429,7 @@ Then install the gem (no need for `bundle config`).
|
|
444
429
|
|
445
430
|
## Performance
|
446
431
|
|
447
|
-
|
448
|
-
|
449
|
-
Deep learning is significantly faster on a GPU. Install [CUDA](https://developer.nvidia.com/cuda-downloads) and [cuDNN](https://developer.nvidia.com/cudnn) and reinstall the gem.
|
432
|
+
Deep learning is significantly faster on a GPU. With Linux, install [CUDA](https://developer.nvidia.com/cuda-downloads) and [cuDNN](https://developer.nvidia.com/cudnn) and reinstall the gem.
|
450
433
|
|
451
434
|
Check if CUDA is available
|
452
435
|
|
@@ -460,15 +443,14 @@ Move a neural network to a GPU
|
|
460
443
|
net.cuda
|
461
444
|
```
|
462
445
|
|
463
|
-
|
446
|
+
If you don’t have a GPU that supports CUDA, we recommend using a cloud service. [Paperspace](https://www.paperspace.com/) has a great free plan. We’ve put together a [Docker image](https://github.com/ankane/ml-stack) to make it easy to get started. On Paperspace, create a notebook with a custom container. Under advanced options, set the container name to:
|
464
447
|
|
465
|
-
|
466
|
-
|
467
|
-
```sh
|
468
|
-
brew upgrade ruby-build
|
469
|
-
rbenv install [version]
|
448
|
+
```text
|
449
|
+
ankane/ml-stack:torch-gpu
|
470
450
|
```
|
471
451
|
|
452
|
+
And leave the other fields in that section blank. Once the notebook is running, you can run the [MNIST example](https://github.com/ankane/ml-stack/blob/master/torch-gpu/MNIST.ipynb).
|
453
|
+
|
472
454
|
## History
|
473
455
|
|
474
456
|
View the [changelog](https://github.com/ankane/torch.rb/blob/master/CHANGELOG.md)
|
@@ -23,11 +23,14 @@ end
|
|
23
23
|
|
24
24
|
def skip_functions(functions)
|
25
25
|
functions.reject do |f|
|
26
|
-
f.base_name.start_with?("_") ||
|
26
|
+
(f.base_name.start_with?("_") && f.base_name != "__lshift__" && f.base_name != "__rshift__") ||
|
27
27
|
f.base_name.include?("_backward") ||
|
28
28
|
f.base_name.include?("_forward") ||
|
29
29
|
f.base_name == "to" ||
|
30
30
|
f.base_name == "record_stream" ||
|
31
|
+
f.base_name == "is_pinned" ||
|
32
|
+
f.base_name == "pin_memory" ||
|
33
|
+
f.base_name == "fused_moving_avg_obs_fake_quant" ||
|
31
34
|
# in ext.cpp
|
32
35
|
f.base_name == "index" ||
|
33
36
|
f.base_name == "index_put_" ||
|
@@ -133,6 +136,7 @@ def generate_attach_def(name, type, def_method)
|
|
133
136
|
ruby_name = ruby_name.sub(/\Afft_/, "") if type == "fft"
|
134
137
|
ruby_name = ruby_name.sub(/\Alinalg_/, "") if type == "linalg"
|
135
138
|
ruby_name = ruby_name.sub(/\Aspecial_/, "") if type == "special"
|
139
|
+
ruby_name = name if name.start_with?("__")
|
136
140
|
|
137
141
|
# cast for Ruby < 2.7 https://github.com/thisMagpie/fftw/issues/22#issuecomment-49508900
|
138
142
|
cast = RUBY_VERSION.to_f > 2.7 ? "" : "(VALUE (*)(...)) "
|
@@ -386,6 +390,8 @@ def generate_function_params(function, params, remove_self)
|
|
386
390
|
end
|
387
391
|
when "generator", "tensorlist", "intlist"
|
388
392
|
func
|
393
|
+
when "string"
|
394
|
+
"stringViewOptional"
|
389
395
|
else
|
390
396
|
"#{func}Optional"
|
391
397
|
end
|
@@ -423,9 +429,7 @@ def generate_dispatch_params(function, params)
|
|
423
429
|
if function.out?
|
424
430
|
"const Tensor &"
|
425
431
|
else
|
426
|
-
|
427
|
-
# "const c10::optional<at::Tensor> &"
|
428
|
-
"const OptionalTensor &"
|
432
|
+
"const c10::optional<at::Tensor> &"
|
429
433
|
end
|
430
434
|
elsif param[:modifier]
|
431
435
|
if param[:modifier].include?("!") && function.retvals.size > 1
|
@@ -449,7 +453,11 @@ def generate_dispatch_params(function, params)
|
|
449
453
|
when "float[]"
|
450
454
|
"ArrayRef<double>"
|
451
455
|
when "str"
|
452
|
-
|
456
|
+
if param[:optional]
|
457
|
+
"c10::string_view"
|
458
|
+
else
|
459
|
+
"std::string"
|
460
|
+
end
|
453
461
|
when "Scalar", "bool", "ScalarType", "Layout", "Device", "Storage", "Generator", "MemoryFormat", "Storage"
|
454
462
|
param[:type]
|
455
463
|
else
|