Class: Transformers::BaseAutoModelClass

Inherits:
Object
  • Object
show all
Extended by:
ClassAttribute
Defined in:
lib/transformers/models/auto/auto_factory.rb

Class Method Summary collapse

Methods included from ClassAttribute

class_attribute

Class Method Details

.from_pretrained(pretrained_model_name_or_path, *model_args, **kwargs) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/transformers/models/auto/auto_factory.rb', line 22

def from_pretrained(pretrained_model_name_or_path, *model_args, **kwargs)
  config = kwargs.delete(:config)
  trust_remote_code = kwargs.delete(:trust_remote_code)
  hub_kwargs_names = [
    :cache_dir,
    :force_download,
    :local_files_only,
    :proxies,
    :resume_download,
    :revision,
    :subfolder,
    :use_auth_token,
    :token
  ]
  hub_kwargs = hub_kwargs_names.select { |k| kwargs.key?(k) }.to_h { |name| [name, kwargs.delete(name)] }
  code_revision = kwargs.delete(:code_revision)
  commit_hash = kwargs.delete(:_commit_hash)

  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,
        _raise_exceptions_for_gated_repo: false,
        _raise_exceptions_for_missing_entries: false,
        _raise_exceptions_for_connection_errors: false,
        **hub_kwargs
      )
      commit_hash = Utils::Hub.extract_commit_hash(resolved_config_file, commit_hash)
    else
      raise Todo
    end
  end

  if !config.is_a?(PretrainedConfig)
    config, kwargs =
      AutoConfig.from_pretrained(
        pretrained_model_name_or_path,
        return_unused_kwargs: true,
        trust_remote_code: trust_remote_code,
        code_revision: code_revision,
        _commit_hash: commit_hash,
        **hub_kwargs,
        **kwargs
      )
  end

  model_class = _get_model_class(config, _model_mapping)
  model_class.from_pretrained(
    pretrained_model_name_or_path, *model_args, config: config, **hub_kwargs, **kwargs
  )
end