Class: Transformers::Pipeline
- Inherits:
-
Object
- Object
- Transformers::Pipeline
- Defined in:
- lib/transformers/pipelines/base.rb
Direct Known Subclasses
ChunkPipeline, EmbeddingPipeline, FeatureExtractionPipeline, ImageClassificationPipeline, ImageFeatureExtractionPipeline, RerankingPipeline, TextClassificationPipeline
Instance Method Summary collapse
- #_ensure_tensor_on_device(inputs, device) ⇒ Object
- #call(inputs, *args, num_workers: nil, batch_size: nil, **kwargs) ⇒ Object
- #check_model_type(supported_models) ⇒ Object
- #get_iterator(inputs, num_workers, batch_size, preprocess_params, forward_params, postprocess_params) ⇒ Object
-
#initialize(model, tokenizer: nil, feature_extractor: nil, image_processor: nil, modelcard: nil, framework: nil, task: "", device: nil, **kwargs) ⇒ Pipeline
constructor
A new instance of Pipeline.
- #torch_dtype ⇒ Object
Constructor Details
#initialize(model, tokenizer: nil, feature_extractor: nil, image_processor: nil, modelcard: nil, framework: nil, task: "", device: nil, **kwargs) ⇒ Pipeline
Returns a new instance of Pipeline.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/transformers/pipelines/base.rb', line 94 def initialize( model, tokenizer: nil, feature_extractor: nil, image_processor: nil, modelcard: nil, framework: nil, task: "", device: nil, **kwargs ) if framework.nil? raise Todo end @task = task @model = model @tokenizer = tokenizer @feature_extractor = feature_extractor @image_processor = image_processor @modelcard = modelcard @framework = framework if device.nil? if Torch::CUDA.available? || Torch::Backends::MPS.available? Transformers.logger.warn( "Hardware accelerator e.g. GPU is available in the environment, but no `device` argument" + " is passed to the `Pipeline` object. Model will be on CPU." ) end end if @framework == "pt" if device.is_a?(String) @device = Torch.device(device) else # TODO update default in 0.2.0 @device = @model.device end else raise Todo end # TODO Fix eql? for Torch::Device in Torch.rb if @device.type != @model.device.type || @device.index != @model.device.index @model.to(@device) end @call_count = 0 @batch_size = kwargs.delete(:batch_size) @num_workers = kwargs.delete(:num_workers) @preprocess_params, @forward_params, @postprocess_params = _sanitize_parameters(**kwargs) end |
Instance Method Details
#_ensure_tensor_on_device(inputs, device) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/transformers/pipelines/base.rb', line 152 def _ensure_tensor_on_device(inputs, device) # TODO move inputs = inputs.to_h if inputs.is_a?(BatchEncoding) if inputs.is_a?(ModelOutput) inputs.instance_variable_get(:@data).transform_values! { |tensor| _ensure_tensor_on_device(tensor, device) } inputs elsif inputs.is_a?(Hash) inputs.transform_values { |tensor| _ensure_tensor_on_device(tensor, device) } elsif inputs.is_a?(Array) inputs.map { |item| _ensure_tensor_on_device(item, device) } elsif inputs.is_a?(Torch::Tensor) inputs.to(device) else inputs end end |
#call(inputs, *args, num_workers: nil, batch_size: nil, **kwargs) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/transformers/pipelines/base.rb', line 217 def call(inputs, *args, num_workers: nil, batch_size: nil, **kwargs) if args.any? Transformers.logger.warn("Ignoring args : #{args}") end if num_workers.nil? if @num_workers.nil? num_workers = 0 else num_workers = @num_workers end end if batch_size.nil? if @batch_size.nil? batch_size = 1 else batch_size = @batch_size end end preprocess_params, forward_params, postprocess_params = _sanitize_parameters(**kwargs) preprocess_params = @preprocess_params.merge(preprocess_params) forward_params = @forward_params.merge(forward_params) postprocess_params = @postprocess_params.merge(postprocess_params) @call_count += 1 if @call_count > 10 && @framework == "pt" && @device.type == "cuda" Transformers.logger.warn( "You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a" + " dataset" ) end is_dataset = inputs.is_a?(Torch::Utils::Data::Dataset) is_generator = inputs.is_a?(Enumerable) is_list = inputs.is_a?(Array) _is_iterable = is_dataset || is_generator || is_list # TODO make the get_iterator work also for `tf` (and `flax`). can_use_iterator = @framework == "pt" && (is_dataset || is_generator || is_list) if is_list if can_use_iterator final_iterator = get_iterator( inputs, num_workers, batch_size, preprocess_params, forward_params, postprocess_params ) outputs = final_iterator.to_a outputs else run_multi(inputs, preprocess_params, forward_params, postprocess_params) end else run_single(inputs, preprocess_params, forward_params, postprocess_params) end end |
#check_model_type(supported_models) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/transformers/pipelines/base.rb', line 170 def check_model_type(supported_models) if !supported_models.is_a?(Array) supported_models_names = [] supported_models.each do |_, model_name| # Mapping can now contain tuples of models for the same configuration. if model_name.is_a?(Array) supported_models_names.concat(model_name) else supported_models_names << model_name end end supported_models = supported_models_names end if !supported_models.include?(@model.class.name.split("::").last) Transformers.logger.error( "The model '#{@model.class.name}' is not supported for #{@task}. Supported models are" + " #{supported_models}." ) end end |
#get_iterator(inputs, num_workers, batch_size, preprocess_params, forward_params, postprocess_params) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/transformers/pipelines/base.rb', line 191 def get_iterator( inputs, num_workers, batch_size, preprocess_params, forward_params, postprocess_params ) if inputs.respond_to?(:size) dataset = PipelineDataset.new(inputs, method(:preprocess), preprocess_params) else if num_workers > 1 Transformers.logger.warn( "For iterable dataset using num_workers>1 is likely to result" + " in errors since everything is iterable, setting `num_workers: 1`" + " to guarantee correctness." ) num_workers = 1 end dataset = PipelineIterator.new(inputs, method(:preprocess), preprocess_params) end # TODO hack by collating feature_extractor and image_processor feature_extractor = !@feature_extractor.nil? ? @feature_extractor : @image_processor collate_fn = batch_size == 1 ? method(:no_collate_fn) : pad_collate_fn(@tokenizer, feature_extractor) dataloader = Torch::Utils::Data::DataLoader.new(dataset, batch_size: batch_size, collate_fn: collate_fn) # num_workers: num_workers, model_iterator = PipelineIterator.new(dataloader, method(:forward), forward_params, loader_batch_size: batch_size) final_iterator = PipelineIterator.new(model_iterator, method(:postprocess), postprocess_params) final_iterator end |
#torch_dtype ⇒ Object
148 149 150 |
# File 'lib/transformers/pipelines/base.rb', line 148 def torch_dtype @model.dtype end |