Class: Transformers::PreTrainedModel

Inherits:
Torch::NN::Module
  • Object
show all
Extended by:
ClassAttribute
Includes:
ModuleUtilsMixin
Defined in:
lib/transformers/modeling_utils.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassAttribute

class_attribute

Methods included from ModuleUtilsMixin

#get_extended_attention_mask, #get_head_mask

Constructor Details

#initialize(config, *inputs, **kwargs) ⇒ PreTrainedModel

Returns a new instance of PreTrainedModel.



138
139
140
141
# File 'lib/transformers/modeling_utils.rb', line 138

def initialize(config, *inputs, **kwargs)
  super()
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



128
129
130
# File 'lib/transformers/modeling_utils.rb', line 128

def config
  @config
end

Class Method Details

.from_pretrained(pretrained_model_name_or_path, *model_args, config: nil, cache_dir: nil, ignore_mismatched_sizes: false, force_download: false, local_files_only: false, token: nil, revision: "main", use_safetensors: nil, **kwargs) ⇒ Object



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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/transformers/modeling_utils.rb', line 248

def from_pretrained(
  pretrained_model_name_or_path,
  *model_args,
  config: nil,
  cache_dir: nil,
  ignore_mismatched_sizes: false,
  force_download: false,
  local_files_only: false,
  token: nil,
  revision: "main",
  use_safetensors: nil,
  **kwargs
)
  state_dict = kwargs.delete(:state_dict)
  from_tf = kwargs.delete(:from_tf) { false }
  from_flax = kwargs.delete(:from_flax) { false }
  resume_download = kwargs.delete(:resume_download) { false }
  proxies = kwargs.delete(:proxies)
  output_loading_info = kwargs.delete(:output_loading_info) { false }
  _use_auth_token = kwargs.delete(:use_auth_token)
  trust_remote_code = kwargs.delete(:trust_remote_code)
  _ = kwargs.delete(:mirror)
  from_pipeline = kwargs.delete(:_from_pipeline)
  from_auto_class = kwargs.delete(:_from_auto) { false }
  _fast_init = kwargs.delete(:_fast_init) { true }
  torch_dtype = kwargs.delete(:torch_dtype)
  low_cpu_mem_usage = kwargs.delete(:low_cpu_mem_usage)
  device_map = kwargs.delete(:device_map)
  _max_memory = kwargs.delete(:max_memory)
  offload_folder = kwargs.delete(:offload_folder)
  offload_state_dict = kwargs.delete(:offload_state_dict) { false }
  load_in_8bit = kwargs.delete(:load_in_8bit) { false }
  load_in_4bit = kwargs.delete(:load_in_4bit) { false }
  quantization_config = kwargs.delete(:quantization_config)
  subfolder = kwargs.delete(:subfolder) { "" }
  commit_hash = kwargs.delete(:_commit_hash)
  variant = kwargs.delete(:variant)
  _adapter_kwargs = kwargs.delete(:adapter_kwargs) { {} }
  _adapter_name = kwargs.delete(:adapter_name) { "default" }
  _use_flash_attention_2 = kwargs.delete(:use_flash_attention_2) { false }

  if use_safetensors.nil? && !is_safetensors_available
    use_safetensors = false
  end
  if trust_remote_code
    Transformers.logger.warn(
      "The argument `trust_remote_code` is to be used with Auto classes. It has no effect here and is" +
      " ignored."
    )
  end

  if commit_hash.nil?
    if !config.is_a?(PretrainedConfig)
      # We make a call to the config file first (which may be absent) to get the commit hash as soon as possible
      resolved_config_file =
        Utils::Hub.cached_file(
          pretrained_model_name_or_path,
          CONFIG_NAME,
          cache_dir: cache_dir,
          force_download: force_download,
          resume_download: resume_download,
          proxies: proxies,
          local_files_only: local_files_only,
          token: token,
          revision: revision,
          subfolder: subfolder,
          _raise_exceptions_for_gated_repo: false,
          _raise_exceptions_for_missing_entries: false,
          _raise_exceptions_for_connection_errors: false,
        )
      commit_hash = Utils::Hub.extract_commit_hash(resolved_config_file, commit_hash)
    else
      commit_hash = config._commit_hash
    end
  end

  if !device_map.nil?
    raise Todo
  end

  # handling bnb config from kwargs, remove after `load_in_{4/8}bit` deprecation.
  if load_in_4bit || load_in_8bit
    raise Todo
  end

  from_pt = !(from_tf || from_flax)

  user_agent = {file_type: "model", framework: "pytorch", from_auto_class: from_auto_class}
  if !from_pipeline.nil?
    user_agent[:using_pipeline] = from_pipeline
  end

  if Utils::Hub.is_offline_mode && !local_files_only
    Transformers.logger.info "Offline mode: forcing local_files_only: true"
    local_files_only = true
  end

  # Load config if we don't provide a configuration
  if !config.is_a?(PretrainedConfig)
    config_path = !config.nil? ? config : pretrained_model_name_or_path
    config, model_kwargs =
      config_class.from_pretrained(
        config_path,
        cache_dir: cache_dir,
        return_unused_kwargs: true,
        force_download: force_download,
        resume_download: resume_download,
        proxies: proxies,
        local_files_only: local_files_only,
        token: token,
        revision: revision,
        subfolder: subfolder,
        _from_auto: from_auto_class,
        _from_pipeline: from_pipeline,
        **kwargs
      )
  else
    # In case one passes a config to `from_pretrained` + "attn_implementation"
    # override the `_attn_implementation` attribute to `attn_implementation` of the kwargs
    # Please see: https://github.com/huggingface/transformers/issues/28038

    # Overwrite `config._attn_implementation` by the one from the kwargs --> in auto-factory
    # we pop attn_implementation from the kwargs but this handles the case where users
    # passes manually the config to `from_pretrained`.
    config = Copy.deepcopy(config)

    kwarg_attn_imp = kwargs.delete(:attn_implementation)
    if !kwarg_attn_imp.nil? && config._attn_implementation != kwarg_attn_imp
      config._attn_implementation = kwarg_attn_imp
    end
    model_kwargs = kwargs
  end

  pre_quantized = false # !config.quantization_config.nil?
  if pre_quantized || !quantization_config.nil?
    raise Todo
  else
    hf_quantizer = nil
  end

  if !hf_quantizer.nil?
    raise Todo
  end

  # This variable will flag if we're loading a sharded checkpoint. In this case the archive file is just the
  # index of the files.
  is_sharded = false
   = nil
  # Load model
  _loading_info = nil

  # Keep in fp32 modules
  keep_in_fp32_modules = nil
  _use_keep_in_fp32_modules = false

  resolved_archive_file = nil
  if !pretrained_model_name_or_path.nil?
    pretrained_model_name_or_path = pretrained_model_name_or_path.to_s
    is_local = Dir.exist?(pretrained_model_name_or_path)
    if is_local
      raise Todo
    elsif File.exist?(File.join(subfolder, pretrained_model_name_or_path))
      _archive_file = pretrained_model_name_or_path
      is_local = true
    else
      # set correct filename
      if use_safetensors != false
        filename = _add_variant(SAFE_WEIGHTS_NAME, variant)
      else
        filename = _add_variant(WEIGHTS_NAME, variant)
      end

      # Load from URL or cache if already cached
      cached_file_kwargs = {
        cache_dir: cache_dir,
        force_download: force_download,
        proxies: proxies,
        resume_download: resume_download,
        local_files_only: local_files_only,
        token: token,
        user_agent: user_agent,
        revision: revision,
        subfolder: subfolder,
        _raise_exceptions_for_gated_repo: false,
        _raise_exceptions_for_missing_entries: false,
        _commit_hash: commit_hash
      }
      resolved_archive_file = Utils::Hub.cached_file(pretrained_model_name_or_path, filename, **cached_file_kwargs)

      # Since we set _raise_exceptions_for_missing_entries=False, we don't get an exception but a None
      # result when internet is up, the repo and revision exist, but the file does not.
      if resolved_archive_file.nil? && filename == _add_variant(SAFE_WEIGHTS_NAME, variant)
        # Maybe the checkpoint is sharded, we try to grab the index name in this case.
        resolved_archive_file = Utils::Hub.cached_file(
          pretrained_model_name_or_path,
          _add_variant(SAFE_WEIGHTS_INDEX_NAME, variant),
          **cached_file_kwargs,
        )
        if !resolved_archive_file.nil?
          is_sharded = true
        elsif use_safetensors
          raise Todo
        else
          # This repo has no safetensors file of any kind, we switch to PyTorch.
          filename = _add_variant(WEIGHTS_NAME, variant)
          resolved_archive_file = Utils::Hub.cached_file(
            pretrained_model_name_or_path, filename, **cached_file_kwargs
          )
        end
      end
      if resolved_archive_file.nil? && filename == _add_variant(WEIGHTS_NAME, variant)
        # Maybe the checkpoint is sharded, we try to grab the index name in this case.
        resolved_archive_file = Utils::Hub.cached_file(
          pretrained_model_name_or_path,
          _add_variant(WEIGHTS_INDEX_NAME, variant),
          **cached_file_kwargs
        )
        if !resolved_archive_file.nil?
          is_sharded = true
        end
      end
      if !local_files_only && !Utils::Hub.is_offline_mode
        if !resolved_archive_file.nil?
          if [WEIGHTS_NAME, WEIGHTS_INDEX_NAME].include?(filename)
            # If the PyTorch file was found, check if there is a safetensors file on the repository
            # If there is no safetensors file on the repositories, start an auto conversion
            _safe_weights_name = is_sharded ? SAFE_WEIGHTS_INDEX_NAME : SAFE_WEIGHTS_NAME
            has_file_kwargs = {
              revision: revision,
              proxies: proxies,
              token: token,
              cache_dir: cache_dir,
              local_files_only: local_files_only
            }
            cached_file_kwargs = {
              cache_dir: cache_dir,
              force_download: force_download,
              resume_download: resume_download,
              local_files_only: local_files_only,
              user_agent: user_agent,
              subfolder: subfolder,
              _raise_exceptions_for_gated_repo: false,
              _raise_exceptions_for_missing_entries: false,
              _commit_hash: commit_hash,
              **has_file_kwargs
            }
            # skip auto conversion
            # if !Utils::Hub.has_file(pretrained_model_name_or_path, safe_weights_name, **has_file_kwargs)
            # end
          end
        else
          raise Todo
        end
      end

      if is_local
        Transformers.logger.info("loading weights file #{archive_file}")
        resolved_archive_file = archive_file
      else
        Transformers.logger.info("loading weights file #{filename} from cache at #{resolved_archive_file}")
      end
    end
  else
    resolved_archive_file = nil
  end

  # We'll need to download and cache each checkpoint shard if the checkpoint is sharded.
  if is_sharded
    raise Todo
  end

   = nil
  if is_safetensors_available && resolved_archive_file.is_a?(String) && resolved_archive_file.end_with?(".safetensors")
    Safetensors.safe_open(resolved_archive_file, framework: "pt") do |f|
       = f.
    end

    if ["format"] == "pt"
      # do nothing
    else
      raise ArgumentError,
        "Incompatible safetensors file. File metadata is not ['pt'] but #{["format"]}"
    end
  end

  from_pt = !(from_tf || from_flax)

  # load pt weights early so that we know which dtype to init the model under
  if from_pt
    if !is_sharded && state_dict.nil?
      # Time to load the checkpoint
      state_dict = load_state_dict(resolved_archive_file)
    end

    # set dtype to instantiate the model under:
    # 1. If torch_dtype is not None, we use that dtype
    # 2. If torch_dtype is "auto", we auto-detect dtype from the loaded state_dict, by checking its first
    #    weights entry that is of a floating type - we assume all floating dtype weights are of the same dtype
    # we also may have config.torch_dtype available, but we won't rely on it till v5
    dtype_orig = nil

    if !torch_dtype.nil?
      raise Todo
    end

    if is_sharded
      loaded_state_dict_keys = ["all_checkpoint_keys"]
    else
      loaded_state_dict_keys = state_dict.keys
    end
  end

  config.name_or_path = pretrained_model_name_or_path

  # Instantiate model.
  model = ModelingUtils.no_init_weights { new(config, *model_args, **model_kwargs) }

  # make sure we use the model's config since the __init__ call might have copied it
  config = model.config

  if device_map.is_a?(String)
    raise Todo
  elsif !device_map.nil?
    raise Todo
  end

  if from_pt
    # restore default dtype
    if !dtype_orig.nil?
      Torch.set_default_dtype(dtype_orig)
    end

    model, _missing_keys, _unexpected_keys, _mismatched_keys, _offload_index, _error_msgs =
      _load_pretrained_model(
        model,
        state_dict,
        loaded_state_dict_keys,  # XXX: rename?
        resolved_archive_file,
        pretrained_model_name_or_path,
        ignore_mismatched_sizes: ignore_mismatched_sizes,
        sharded_metadata: ,
        _fast_init: _fast_init,
        low_cpu_mem_usage: low_cpu_mem_usage,
        device_map: device_map,
        offload_folder: offload_folder,
        offload_state_dict: offload_state_dict,
        dtype: torch_dtype,
        hf_quantizer: hf_quantizer,
        keep_in_fp32_modules: keep_in_fp32_modules
      )
  end

  # make sure token embedding weights are still tied if needed
  model.tie_weights

  # Set model in evaluation mode to deactivate DropOut modules by default
  model.eval

  # If it is a model with generation capabilities, attempt to load the generation config
  if model.can_generate && !pretrained_model_name_or_path.nil?
    raise Todo
  end

  # Dispatch model with hooks on all devices if necessary
  if !device_map.nil?
    raise Todo
  end

  if !hf_quantizer.nil?
    raise Todo
  end

  if output_loading_info
    raise Todo
  end

  model
end

Instance Method Details

#_backward_compatibility_gradient_checkpointingObject



152
153
154
# File 'lib/transformers/modeling_utils.rb', line 152

def _backward_compatibility_gradient_checkpointing
  # TODO
end

#_init_weights(mod) ⇒ Object



177
178
179
# File 'lib/transformers/modeling_utils.rb', line 177

def _init_weights(mod)
  # pass
end

#_initialize_weights(mod) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/transformers/modeling_utils.rb', line 181

def _initialize_weights(mod)
  if mod.instance_variable_defined?(:@is_hf_initialized)
    return
  end
  _init_weights(mod)
  mod.instance_variable_set(:@is_hf_initialized, true)
end

#base_modelObject



156
157
158
# File 'lib/transformers/modeling_utils.rb', line 156

def base_model
  instance_variable_get("@#{self.class.base_model_prefix}") || self
end

#can_generateObject



160
161
162
163
# File 'lib/transformers/modeling_utils.rb', line 160

def can_generate
  # TODO improve
  false
end

#dequantizeObject

Raises:



148
149
150
# File 'lib/transformers/modeling_utils.rb', line 148

def dequantize
  raise Todo
end

#dummy_inputsObject

Raises:



130
131
132
# File 'lib/transformers/modeling_utils.rb', line 130

def dummy_inputs
  raise Todo
end

#frameworkObject



134
135
136
# File 'lib/transformers/modeling_utils.rb', line 134

def framework
  "pt"
end

#get_input_embeddingsObject

Raises:



165
166
167
# File 'lib/transformers/modeling_utils.rb', line 165

def get_input_embeddings
  raise Todo
end

#get_output_embeddingsObject



173
174
175
# File 'lib/transformers/modeling_utils.rb', line 173

def get_output_embeddings
  nil  # Overwrite for models with output embeddings
end

#init_weightsObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/transformers/modeling_utils.rb', line 208

def init_weights
  # Prune heads if needed
  if @config.pruned_heads.any?
    prune_heads(@config.pruned_heads)
  end

  # TODO implement no_init_weights context manager
  _init_weights = false
  if _init_weights
    # Initialize weights
    apply(method(:_initialize_weights))

    # Tie weights should be skipped when not initializing all weights
    # since from_pretrained(...) calls tie weights anyways
    tie_weights
  end
end

#post_initObject



143
144
145
146
# File 'lib/transformers/modeling_utils.rb', line 143

def post_init
  init_weights
  _backward_compatibility_gradient_checkpointing
end

#prune_heads(heads_to_prune) ⇒ Object



226
227
228
229
230
231
232
233
234
# File 'lib/transformers/modeling_utils.rb', line 226

def prune_heads(heads_to_prune)
  # save new sets of pruned heads as union of previously stored pruned heads and newly pruned heads
  heads_to_prune.each do |layer, heads|
    union_heads = Set.new(@config.pruned_heads.fetch(layer, [])) | Set.new(heads)
    @config.pruned_heads[layer] = union_heads.to_a # Unfortunately we have to store it as list for JSON
  end

  base_model._prune_heads(heads_to_prune)
end

#set_input_embeddings(value) ⇒ Object

Raises:



169
170
171
# File 'lib/transformers/modeling_utils.rb', line 169

def set_input_embeddings(value)
  raise Todo
end

#tie_weightsObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/transformers/modeling_utils.rb', line 189

def tie_weights
  if @config.tie_word_embeddings != false
    output_embeddings = get_output_embeddings
    if !output_embeddings.nil?
      raise Todo
    end
  end

  if @config.is_encoder_decoder && @config.tie_encoder_decoder
    raise Todo
  end

  modules.each do |mod|
    if mod.respond_to?(:_tie_weights)
      mod._tie_weights
    end
  end
end

#warn_if_padding_and_no_attention_mask(input_ids, attention_mask) ⇒ Object



236
237
238
239
240
241
242
243
244
245
# File 'lib/transformers/modeling_utils.rb', line 236

def warn_if_padding_and_no_attention_mask(input_ids, attention_mask)
  if !attention_mask.nil? || @config.pad_token_id.nil?
    return
  end

  # Check only the first and last input IDs to reduce overhead.
  if input_ids[0.., [-1, 0]].include?(@config.pad_token_id)
    raise Todo
  end
end