torchrec 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +3 -0
- data/LICENSE.txt +30 -0
- data/README.md +62 -0
- data/lib/torchrec/models/deepfm/dense_arch.rb +21 -0
- data/lib/torchrec/models/deepfm/over_arch.rb +19 -0
- data/lib/torchrec/models/dlrm/dense_arch.rb +18 -0
- data/lib/torchrec/modules/activation/swish_layer_norm.rb +20 -0
- data/lib/torchrec/modules/mlp/mlp.rb +51 -0
- data/lib/torchrec/modules/mlp/perceptron.rb +22 -0
- data/lib/torchrec/modules/utils.rb +12 -0
- data/lib/torchrec/version.rb +3 -0
- data/lib/torchrec.rb +20 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b20b0c478258d789f1b6a6b2f80e3350600b6ce23f6ee1146cfb6819551a1150
|
4
|
+
data.tar.gz: 443383a994cd25d6225f84401e0a47886b848e590551ae0eec2eb31dba9468bd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a8e6ce693978cd21505f4b7378efe92a87ee672ece2d012cf3ee69dc9a3a3332cc57a84d1e0edcc7c317385aaae212a8351e54d5ea33ad338f795ec7138c5cdc
|
7
|
+
data.tar.gz: eb105de85d924fd2d3c7ef444a61caecdfb8008a5f25dba7297bb233b04f2662a47e144305301bd60f6cc1e9734bb1551946911047e8f654581b5fb192330cf2
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
BSD 3-Clause License
|
2
|
+
|
3
|
+
Copyright (c) Meta Platforms, Inc. and affiliates.
|
4
|
+
Copyright (c) Andrew Kane.
|
5
|
+
All rights reserved.
|
6
|
+
|
7
|
+
Redistribution and use in source and binary forms, with or without
|
8
|
+
modification, are permitted provided that the following conditions are met:
|
9
|
+
|
10
|
+
* Redistributions of source code must retain the above copyright notice, this
|
11
|
+
list of conditions and the following disclaimer.
|
12
|
+
|
13
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
14
|
+
this list of conditions and the following disclaimer in the documentation
|
15
|
+
and/or other materials provided with the distribution.
|
16
|
+
|
17
|
+
* Neither the name of the copyright holder nor the names of its
|
18
|
+
contributors may be used to endorse or promote products derived from
|
19
|
+
this software without specific prior written permission.
|
20
|
+
|
21
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
22
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
23
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
24
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
25
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
26
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
27
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
28
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
29
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
30
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# TorchRec Ruby
|
2
|
+
|
3
|
+
Deep learning recommendation systems for Ruby
|
4
|
+
|
5
|
+
[![Build Status](https://github.com/ankane/torchrec-ruby/workflows/build/badge.svg?branch=master)](https://github.com/ankane/torchrec-ruby/actions)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application’s Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem "torchrec"
|
13
|
+
```
|
14
|
+
|
15
|
+
## Getting Started
|
16
|
+
|
17
|
+
This library follows the [Python API](https://pytorch.org/torchrec/). Many methods and options are missing at the moment. PRs welcome!
|
18
|
+
|
19
|
+
## Models
|
20
|
+
|
21
|
+
DeepFM
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
TorchRec::Models::DeepFM::DenseArch.new(in_features, hidden_layer_size, embedding_dim)
|
25
|
+
TorchRec::Models::DeepFM::OverArch.new(in_features)
|
26
|
+
```
|
27
|
+
|
28
|
+
DLRM
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
TorchRec::Models::DLRM::DenseArch.new(in_features, layer_sizes, device: nil)
|
32
|
+
```
|
33
|
+
|
34
|
+
## Modules
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
TorchRec::Modules::Activation::SwishLayerNorm.new(input_dims, device: nil)
|
38
|
+
TorchRec::Modules::MLP::MLP.new(in_size, layer_sizes, bias: true, activation: :relu, device: nil)
|
39
|
+
TorchRec::Modules::MLP::Perceptron.new(in_size, out_size, bias: true, activation: Torch.method(:relu), device: nil)
|
40
|
+
```
|
41
|
+
|
42
|
+
## History
|
43
|
+
|
44
|
+
View the [changelog](https://github.com/ankane/torchrec-ruby/blob/master/CHANGELOG.md)
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
49
|
+
|
50
|
+
- [Report bugs](https://github.com/ankane/torchrec-ruby/issues)
|
51
|
+
- Fix bugs and [submit pull requests](https://github.com/ankane/torchrec-ruby/pulls)
|
52
|
+
- Write, clarify, or fix documentation
|
53
|
+
- Suggest or add new features
|
54
|
+
|
55
|
+
To get started with development:
|
56
|
+
|
57
|
+
```sh
|
58
|
+
git clone https://github.com/ankane/torchrec-ruby.git
|
59
|
+
cd torchrec-ruby
|
60
|
+
bundle install
|
61
|
+
bundle exec rake test
|
62
|
+
```
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module TorchRec
|
2
|
+
module Models
|
3
|
+
module DeepFM
|
4
|
+
class DenseArch < Torch::NN::Module
|
5
|
+
def initialize(in_features, hidden_layer_size, embedding_dim)
|
6
|
+
super()
|
7
|
+
@model = Torch::NN::Sequential.new(
|
8
|
+
Torch::NN::Linear.new(in_features, hidden_layer_size),
|
9
|
+
Torch::NN::ReLU.new,
|
10
|
+
Torch::NN::Linear.new(hidden_layer_size, embedding_dim),
|
11
|
+
Torch::NN::ReLU.new
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
def forward(features)
|
16
|
+
@model.call(features)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module TorchRec
|
2
|
+
module Models
|
3
|
+
module DeepFM
|
4
|
+
class OverArch < Torch::NN::Module
|
5
|
+
def initialize(in_features)
|
6
|
+
super()
|
7
|
+
@model = Torch::NN::Sequential.new(
|
8
|
+
Torch::NN::Linear.new(in_features, 1),
|
9
|
+
Torch::NN::Sigmoid.new
|
10
|
+
)
|
11
|
+
end
|
12
|
+
|
13
|
+
def forward(features)
|
14
|
+
@model.call(features)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module TorchRec
|
2
|
+
module Models
|
3
|
+
module DLRM
|
4
|
+
class DenseArch < Torch::NN::Module
|
5
|
+
def initialize(in_features, layer_sizes, device: nil)
|
6
|
+
super()
|
7
|
+
@model = Modules::MLP::MLP.new(
|
8
|
+
in_features, layer_sizes, bias: true, activation: :relu, device: device
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
def forward(features)
|
13
|
+
@model.call(features)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module TorchRec
|
2
|
+
module Modules
|
3
|
+
module Activation
|
4
|
+
class SwishLayerNorm < Torch::NN::Module
|
5
|
+
def initialize(input_dims, device: nil)
|
6
|
+
super()
|
7
|
+
@norm = Torch::NN::Sequential.new(
|
8
|
+
# TODO add device
|
9
|
+
Torch::NN::LayerNorm.new(input_dims), #, device: device),
|
10
|
+
Torch::NN::Sigmoid.new
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
def forward(input)
|
15
|
+
input * @norm.call(input)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module TorchRec
|
2
|
+
module Modules
|
3
|
+
module MLP
|
4
|
+
class MLP < Torch::NN::Module
|
5
|
+
def initialize(in_size, layer_sizes, bias: true, activation: :relu, device: nil)
|
6
|
+
super()
|
7
|
+
|
8
|
+
if activation == :relu
|
9
|
+
activation = Torch.method(:relu)
|
10
|
+
elsif activation == :sigmoid
|
11
|
+
activation = Torch.method(:sigmoid)
|
12
|
+
end
|
13
|
+
|
14
|
+
if !activation.is_a?(Symbol)
|
15
|
+
@mlp = Torch::NN::Sequential.new(
|
16
|
+
*layer_sizes.length.times.map do |i|
|
17
|
+
Perceptron.new(
|
18
|
+
i > 0 ? layer_sizes[i - 1] : in_size,
|
19
|
+
layer_sizes[i],
|
20
|
+
bias: bias,
|
21
|
+
activation: Utils.extract_module_or_tensor_callable(activation),
|
22
|
+
device: device
|
23
|
+
)
|
24
|
+
end
|
25
|
+
)
|
26
|
+
else
|
27
|
+
if activation == :swish_layernorm
|
28
|
+
@mlp = Torch::NN::Sequential.new(
|
29
|
+
*layer_sizes.length.times.map do |i|
|
30
|
+
Perceptron.new(
|
31
|
+
i > 0 ? layer_sizes[i - 1] : in_size,
|
32
|
+
layer_sizes[i],
|
33
|
+
bias: bias,
|
34
|
+
activation: Activation::SwishLayerNorm.new(layer_sizes[i], device: device),
|
35
|
+
device: device
|
36
|
+
)
|
37
|
+
end
|
38
|
+
)
|
39
|
+
else
|
40
|
+
raise ArgumentError, "This MLP only supports activation function of :relu, :sigmoid, and :swish_layernorm"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def forward(input)
|
46
|
+
@mlp.call(input)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module TorchRec
|
2
|
+
module Modules
|
3
|
+
module MLP
|
4
|
+
class Perceptron < Torch::NN::Module
|
5
|
+
def initialize(in_size, out_size, bias: true, activation: Torch.method(:relu), device: nil)
|
6
|
+
super()
|
7
|
+
@out_size = out_size
|
8
|
+
@in_size = in_size
|
9
|
+
@linear = Torch::NN::Linear.new(
|
10
|
+
# TODO add device
|
11
|
+
@in_size, @out_size, bias: bias #, device: device
|
12
|
+
)
|
13
|
+
@activation_fn = activation
|
14
|
+
end
|
15
|
+
|
16
|
+
def forward(input)
|
17
|
+
@activation_fn.call(@linear.call(input))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/torchrec.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# dependencies
|
2
|
+
require "torch"
|
3
|
+
|
4
|
+
# models
|
5
|
+
require "torchrec/models/deepfm/dense_arch"
|
6
|
+
require "torchrec/models/deepfm/over_arch"
|
7
|
+
require "torchrec/models/dlrm/dense_arch"
|
8
|
+
|
9
|
+
# modules
|
10
|
+
require "torchrec/modules/activation/swish_layer_norm"
|
11
|
+
require "torchrec/modules/mlp/mlp"
|
12
|
+
require "torchrec/modules/mlp/perceptron"
|
13
|
+
require "torchrec/modules/utils"
|
14
|
+
|
15
|
+
# other
|
16
|
+
require "torchrec/version"
|
17
|
+
|
18
|
+
module TorchRec
|
19
|
+
class Error < StandardError; end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: torchrec
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Kane
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-02-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: torch-rb
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description:
|
28
|
+
email: andrew@ankane.org
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- CHANGELOG.md
|
34
|
+
- LICENSE.txt
|
35
|
+
- README.md
|
36
|
+
- lib/torchrec.rb
|
37
|
+
- lib/torchrec/models/deepfm/dense_arch.rb
|
38
|
+
- lib/torchrec/models/deepfm/over_arch.rb
|
39
|
+
- lib/torchrec/models/dlrm/dense_arch.rb
|
40
|
+
- lib/torchrec/modules/activation/swish_layer_norm.rb
|
41
|
+
- lib/torchrec/modules/mlp/mlp.rb
|
42
|
+
- lib/torchrec/modules/mlp/perceptron.rb
|
43
|
+
- lib/torchrec/modules/utils.rb
|
44
|
+
- lib/torchrec/version.rb
|
45
|
+
homepage: https://github.com/ankane/torchrec-ruby
|
46
|
+
licenses:
|
47
|
+
- BSD-3-Clause
|
48
|
+
metadata: {}
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '2.6'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubygems_version: 3.3.3
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: Deep learning recommendation systems for Ruby
|
68
|
+
test_files: []
|