transformers-rb 0.1.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 (65) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +3 -0
  3. data/LICENSE.txt +203 -0
  4. data/README.md +163 -0
  5. data/lib/transformers/activations.rb +57 -0
  6. data/lib/transformers/configuration_utils.rb +285 -0
  7. data/lib/transformers/convert_slow_tokenizer.rb +90 -0
  8. data/lib/transformers/data/processors/squad.rb +115 -0
  9. data/lib/transformers/dynamic_module_utils.rb +25 -0
  10. data/lib/transformers/feature_extraction_utils.rb +110 -0
  11. data/lib/transformers/hf_hub/constants.rb +71 -0
  12. data/lib/transformers/hf_hub/errors.rb +11 -0
  13. data/lib/transformers/hf_hub/file_download.rb +764 -0
  14. data/lib/transformers/hf_hub/utils/_errors.rb +94 -0
  15. data/lib/transformers/hf_hub/utils/_headers.rb +109 -0
  16. data/lib/transformers/image_processing_base.rb +169 -0
  17. data/lib/transformers/image_processing_utils.rb +63 -0
  18. data/lib/transformers/image_transforms.rb +208 -0
  19. data/lib/transformers/image_utils.rb +165 -0
  20. data/lib/transformers/modeling_outputs.rb +81 -0
  21. data/lib/transformers/modeling_utils.rb +888 -0
  22. data/lib/transformers/models/auto/auto_factory.rb +138 -0
  23. data/lib/transformers/models/auto/configuration_auto.rb +61 -0
  24. data/lib/transformers/models/auto/feature_extraction_auto.rb +20 -0
  25. data/lib/transformers/models/auto/image_processing_auto.rb +104 -0
  26. data/lib/transformers/models/auto/modeling_auto.rb +80 -0
  27. data/lib/transformers/models/auto/tokenization_auto.rb +160 -0
  28. data/lib/transformers/models/bert/configuration_bert.rb +65 -0
  29. data/lib/transformers/models/bert/modeling_bert.rb +836 -0
  30. data/lib/transformers/models/bert/tokenization_bert.rb +115 -0
  31. data/lib/transformers/models/bert/tokenization_bert_fast.rb +52 -0
  32. data/lib/transformers/models/distilbert/configuration_distilbert.rb +63 -0
  33. data/lib/transformers/models/distilbert/modeling_distilbert.rb +616 -0
  34. data/lib/transformers/models/distilbert/tokenization_distilbert.rb +114 -0
  35. data/lib/transformers/models/distilbert/tokenization_distilbert_fast.rb +71 -0
  36. data/lib/transformers/models/vit/configuration_vit.rb +60 -0
  37. data/lib/transformers/models/vit/image_processing_vit.rb +170 -0
  38. data/lib/transformers/models/vit/modeling_vit.rb +506 -0
  39. data/lib/transformers/pipelines/_init.rb +348 -0
  40. data/lib/transformers/pipelines/base.rb +301 -0
  41. data/lib/transformers/pipelines/feature_extraction.rb +47 -0
  42. data/lib/transformers/pipelines/image_classification.rb +110 -0
  43. data/lib/transformers/pipelines/image_feature_extraction.rb +56 -0
  44. data/lib/transformers/pipelines/pt_utils.rb +53 -0
  45. data/lib/transformers/pipelines/question_answering.rb +508 -0
  46. data/lib/transformers/pipelines/text_classification.rb +123 -0
  47. data/lib/transformers/pipelines/token_classification.rb +282 -0
  48. data/lib/transformers/ruby_utils.rb +33 -0
  49. data/lib/transformers/sentence_transformer.rb +37 -0
  50. data/lib/transformers/tokenization_utils.rb +152 -0
  51. data/lib/transformers/tokenization_utils_base.rb +937 -0
  52. data/lib/transformers/tokenization_utils_fast.rb +386 -0
  53. data/lib/transformers/torch_utils.rb +25 -0
  54. data/lib/transformers/utils/_init.rb +31 -0
  55. data/lib/transformers/utils/generic.rb +107 -0
  56. data/lib/transformers/utils/hub.rb +209 -0
  57. data/lib/transformers/utils/import_utils.rb +45 -0
  58. data/lib/transformers/utils/logging.rb +52 -0
  59. data/lib/transformers/version.rb +3 -0
  60. data/lib/transformers-rb.rb +1 -0
  61. data/lib/transformers.rb +100 -0
  62. data/licenses/LICENSE-huggingface-hub.txt +201 -0
  63. data/licenses/LICENSE-sentence-transformers.txt +201 -0
  64. data/licenses/NOTICE-sentence-transformers.txt +5 -0
  65. metadata +161 -0
@@ -0,0 +1,114 @@
1
+ # Copyright 2018 The HuggingFace Inc. team.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Transformers
16
+ module Distilbert
17
+ class DistilBertTokenizer < PreTrainedTokenizer
18
+ VOCAB_FILES_NAMES = {vocab_file: "vocab.txt"}
19
+
20
+ self.vocab_files_names = VOCAB_FILES_NAMES
21
+ self.model_input_names = ["input_ids", "attention_mask"]
22
+
23
+ class BasicTokenizer
24
+ attr_reader :do_lower_case, :tokenize_chinese_chars, :strip_accents
25
+
26
+ def initialize(
27
+ do_lower_case: true,
28
+ never_split: nil,
29
+ tokenize_chinese_chars: true,
30
+ strip_accents: nil,
31
+ do_split_on_punc: true
32
+ )
33
+ if never_split.nil?
34
+ never_split = []
35
+ end
36
+ @do_lower_case = do_lower_case
37
+ @never_split = Set.new(never_split)
38
+ @tokenize_chinese_chars = tokenize_chinese_chars
39
+ @strip_accents = strip_accents
40
+ @do_split_on_punc = do_split_on_punc
41
+ end
42
+ end
43
+
44
+ class WordpieceTokenizer
45
+ def initialize(vocab:, unk_token:, max_input_chars_per_word: 100)
46
+ @vocab = vocab
47
+ @unk_token = unk_token
48
+ @max_input_chars_per_word = max_input_chars_per_word
49
+ end
50
+ end
51
+
52
+ attr_reader :vocab, :basic_tokenizer
53
+
54
+ def initialize(
55
+ vocab_file:,
56
+ do_lower_case: true,
57
+ do_basic_tokenize: true,
58
+ never_split: nil,
59
+ unk_token: "[UNK]",
60
+ sep_token: "[SEP]",
61
+ pad_token: "[PAD]",
62
+ cls_token: "[CLS]",
63
+ mask_token: "[MASK]",
64
+ tokenize_chinese_chars: true,
65
+ strip_accents: nil,
66
+ **kwargs
67
+ )
68
+ @vocab = load_vocab(vocab_file)
69
+ @ids_to_tokens = @vocab.invert
70
+ @do_basic_tokenize = do_basic_tokenize
71
+ if do_basic_tokenize
72
+ @basic_tokenizer =
73
+ BasicTokenizer.new(
74
+ do_lower_case: do_lower_case,
75
+ never_split: never_split,
76
+ tokenize_chinese_chars: tokenize_chinese_chars,
77
+ strip_accents: strip_accents
78
+ )
79
+ end
80
+ @wordpiece_tokenizer = WordpieceTokenizer.new(vocab: @vocab, unk_token: unk_token.to_s)
81
+
82
+ super(
83
+ do_lower_case: do_lower_case,
84
+ do_basic_tokenize: do_basic_tokenize,
85
+ never_split: never_split,
86
+ unk_token: unk_token,
87
+ sep_token: sep_token,
88
+ pad_token: pad_token,
89
+ cls_token: cls_token,
90
+ mask_token: mask_token,
91
+ tokenize_chinese_chars: tokenize_chinese_chars,
92
+ strip_accents: strip_accents,
93
+ **kwargs
94
+ )
95
+ end
96
+
97
+ def _convert_token_to_id(token)
98
+ @vocab.fetch(token, @vocab.fetch(@unk_token))
99
+ end
100
+
101
+ private
102
+
103
+ def load_vocab(vocab_file)
104
+ vocab = {}
105
+ tokens = File.readlines(vocab_file)
106
+ tokens.each_with_index do |token, index|
107
+ token = token.chomp
108
+ vocab[token] = index
109
+ end
110
+ vocab
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,71 @@
1
+ # Copyright 2018 The HuggingFace Inc. team.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Transformers
16
+ module Distilbert
17
+ class DistilBertTokenizerFast < PreTrainedTokenizerFast
18
+ VOCAB_FILES_NAMES = {vocab_file: "vocab.txt", tokenizer_file: "tokenizer.json"}
19
+
20
+ self.vocab_files_names = VOCAB_FILES_NAMES
21
+ self.model_input_names = ["input_ids", "attention_mask"]
22
+ self.slow_tokenizer_class = DistilBertTokenizer
23
+
24
+ def initialize(
25
+ vocab_file: nil,
26
+ tokenizer_file: nil,
27
+ do_lower_case: true,
28
+ unk_token: "[UNK]",
29
+ sep_token: "[SEP]",
30
+ pad_token: "[PAD]",
31
+ cls_token: "[CLS]",
32
+ mask_token: "[MASK]",
33
+ tokenize_chinese_chars: true,
34
+ strip_accents: nil,
35
+ **kwargs
36
+ )
37
+ super(
38
+ vocab_file,
39
+ tokenizer_file: tokenizer_file,
40
+ do_lower_case: do_lower_case,
41
+ unk_token: unk_token,
42
+ sep_token: sep_token,
43
+ pad_token: pad_token,
44
+ cls_token: cls_token,
45
+ mask_token: mask_token,
46
+ tokenize_chinese_chars: tokenize_chinese_chars,
47
+ strip_accents: strip_accents,
48
+ **kwargs
49
+ )
50
+
51
+ if @backend_tokenizer
52
+ raise Todo
53
+ end
54
+
55
+ @do_lower_case = do_lower_case
56
+ end
57
+
58
+ def build_inputs_with_special_tokens(token_ids_0, token_ids_1 = nil)
59
+ raise Todo
60
+ end
61
+
62
+ def create_token_type_ids_from_sequences(token_ids_0, token_ids_1 = nil)
63
+ raise Todo
64
+ end
65
+
66
+ def save_vocabulary(save_directory, filename_prefix: nil)
67
+ raise Todo
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,60 @@
1
+ # Copyright 2021 Google AI and The HuggingFace Inc. team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Transformers
16
+ module Vit
17
+ class ViTConfig < PretrainedConfig
18
+ self.model_type = "vit"
19
+
20
+ attr_reader :hidden_size, :num_hidden_layers, :num_attention_heads, :intermediate_size,
21
+ :hidden_act, :hidden_dropout_prob, :attention_probs_dropout_prob, :initializer_range,
22
+ :layer_norm_eps, :image_size, :patch_size, :num_channels, :qkv_bias, :encoder_stride
23
+
24
+ def initialize(
25
+ hidden_size: 768,
26
+ num_hidden_layers: 12,
27
+ num_attention_heads: 12,
28
+ intermediate_size: 3072,
29
+ hidden_act: "gelu",
30
+ hidden_dropout_prob: 0.0,
31
+ attention_probs_dropout_prob: 0.0,
32
+ initializer_range: 0.02,
33
+ layer_norm_eps: 1e-12,
34
+ image_size: 224,
35
+ patch_size: 16,
36
+ num_channels: 3,
37
+ qkv_bias: true,
38
+ encoder_stride: 16,
39
+ **kwargs
40
+ )
41
+ super(**kwargs)
42
+
43
+ @hidden_size = hidden_size
44
+ @num_hidden_layers = num_hidden_layers
45
+ @num_attention_heads = num_attention_heads
46
+ @intermediate_size = intermediate_size
47
+ @hidden_act = hidden_act
48
+ @hidden_dropout_prob = hidden_dropout_prob
49
+ @attention_probs_dropout_prob = attention_probs_dropout_prob
50
+ @initializer_range = initializer_range
51
+ @layer_norm_eps = layer_norm_eps
52
+ @image_size = image_size
53
+ @patch_size = patch_size
54
+ @num_channels = num_channels
55
+ @qkv_bias = qkv_bias
56
+ @encoder_stride = encoder_stride
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,170 @@
1
+ # Copyright 2022 The HuggingFace Inc. team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Transformers
16
+ module Vit
17
+ class ViTImageProcessor < BaseImageProcessor
18
+ def initialize(
19
+ do_resize: true,
20
+ size: nil,
21
+ resample: :bilinear,
22
+ do_rescale: true,
23
+ rescale_factor: 1 / 255.0,
24
+ do_normalize: true,
25
+ image_mean: nil,
26
+ image_std: nil,
27
+ **kwargs
28
+ )
29
+ super(**kwargs)
30
+ size = !size.nil? ? size : {height: 224, width: 224}
31
+ size = ImageProcessingUtils.get_size_dict(size)
32
+ @do_resize = do_resize
33
+ @do_rescale = do_rescale
34
+ @do_normalize = do_normalize
35
+ @size = size
36
+ @resample = resample
37
+ @rescale_factor = rescale_factor
38
+ @image_mean = !image_mean.nil? ? image_mean : IMAGENET_STANDARD_MEAN
39
+ @image_std = !image_std.nil? ? image_std : IMAGENET_STANDARD_STD
40
+ @valid_processor_keys = [
41
+ :images,
42
+ :do_resize,
43
+ :size,
44
+ :resample,
45
+ :do_rescale,
46
+ :rescale_factor,
47
+ :do_normalize,
48
+ :image_mean,
49
+ :image_std,
50
+ :return_tensors,
51
+ :data_format,
52
+ :input_data_format
53
+ ]
54
+ end
55
+
56
+ def resize(
57
+ image,
58
+ size,
59
+ resample: :bilinear,
60
+ data_format: nil,
61
+ input_data_format: nil,
62
+ **kwargs
63
+ )
64
+ size = ImageProcessingUtils.get_size_dict(size)
65
+ if !size.include?(:height) || !size.include?(:width)
66
+ raise ArgumentError, "The `size` dictionary must contain the keys `height` and `width`. Got #{size.keys}"
67
+ end
68
+ output_size = [size[:height], size[:width]]
69
+ ImageTransforms.resize(
70
+ image,
71
+ output_size,
72
+ resample: resample,
73
+ data_format: data_format,
74
+ input_data_format: input_data_format,
75
+ **kwargs
76
+ )
77
+ end
78
+
79
+ def preprocess(
80
+ images,
81
+ do_resize: nil,
82
+ size: nil,
83
+ resample: nil,
84
+ do_rescale: nil,
85
+ rescale_factor: nil,
86
+ do_normalize: nil,
87
+ image_mean: nil,
88
+ image_std: nil,
89
+ return_tensors: nil,
90
+ data_format: ChannelDimension::FIRST,
91
+ input_data_format: nil,
92
+ **kwargs
93
+ )
94
+ do_resize = !do_resize.nil? ? do_resize : @do_resize
95
+ do_rescale = !do_rescale.nil? ? do_rescale : @do_rescale
96
+ do_normalize = !do_normalize.nil? ? do_normalize : @do_normalize
97
+ resample = !resample.nil? ? resample : @resample
98
+ rescale_factor = !rescale_factor.nil? ? rescale_factor : @rescale_factor
99
+ image_mean = !image_mean.nil? ? image_mean : @image_mean
100
+ image_std = !image_std.nil? ? image_std : @image_std
101
+
102
+ size = !size.nil? ? size : @size
103
+ size_dict = ImageProcessingUtils.get_size_dict(size)
104
+
105
+ images = ImageUtils.make_list_of_images(images)
106
+
107
+ ImageUtils.validate_kwargs(captured_kwargs: kwargs.keys, valid_processor_keys: @valid_processor_keys)
108
+
109
+ if !ImageUtils.valid_images(images)
110
+ raise ArgumentError,
111
+ "Invalid image type. Must be of type Vips::Image, Numo::NArray, or Torch::Tensor."
112
+ end
113
+ ImageUtils.validate_preprocess_arguments(
114
+ do_rescale: do_rescale,
115
+ rescale_factor: rescale_factor,
116
+ do_normalize: do_normalize,
117
+ image_mean: image_mean,
118
+ image_std: image_std,
119
+ do_resize: do_resize,
120
+ size: size,
121
+ resample: resample
122
+ )
123
+
124
+ # All transformations expect numo arrays.
125
+ images = images.map { |image| ImageUtils.to_numo_array(image) }
126
+
127
+ if ImageUtils.is_scaled_image(images[0]) && do_rescale
128
+ Transformers.logger.warn(
129
+ "It looks like you are trying to rescale already rescaled images. If the input" +
130
+ " images have pixel values between 0 and 1, set `do_rescale: false` to avoid rescaling them again."
131
+ )
132
+ end
133
+
134
+ if input_data_format.nil?
135
+ # We assume that all images have the same channel dimension format.
136
+ input_data_format = ImageUtils.infer_channel_dimension_format(images[0])
137
+ end
138
+
139
+ if do_resize
140
+ images =
141
+ images.map do |image|
142
+ resize(image, size_dict, resample: resample, input_data_format: input_data_format)
143
+ end
144
+ end
145
+
146
+ if do_rescale
147
+ images =
148
+ images.map do |image|
149
+ rescale(image, rescale_factor, input_data_format: input_data_format)
150
+ end
151
+ end
152
+
153
+ if do_normalize
154
+ images =
155
+ images.map do |image|
156
+ normalize(image, image_mean, image_std, input_data_format: input_data_format)
157
+ end
158
+ end
159
+
160
+ images =
161
+ images.map do |image|
162
+ ImageTransforms.to_channel_dimension_format(image, data_format, input_channel_dim: input_data_format)
163
+ end
164
+
165
+ data = {pixel_values: images}
166
+ BatchFeature.new(data: data, tensor_type: return_tensors)
167
+ end
168
+ end
169
+ end
170
+ end