Class: Informers::PreTrainedModel

Inherits:
Object
  • Object
show all
Defined in:
lib/informers/models.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, session) ⇒ PreTrainedModel

Returns a new instance of PreTrainedModel.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/informers/models.rb', line 66

def initialize(config, session)
  super()

  @config = config
  @session = session

  @output_names = nil

  model_name = MODEL_CLASS_TO_NAME_MAPPING[self.class]
  model_type = MODEL_TYPE_MAPPING[model_name]

  case model_type
  when MODEL_TYPES[:DecoderOnly]
    raise Todo
  when MODEL_TYPES[:Seq2Seq], MODEL_TYPES[:Vision2Seq]
    raise Todo
  when MODEL_TYPES[:EncoderDecoder]
    raise Todo
  else
    @forward = method(:encoder_forward)
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



64
65
66
# File 'lib/informers/models.rb', line 64

def config
  @config
end

Class Method Details

.construct_session(pretrained_model_name_or_path, file_name, **options) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/informers/models.rb', line 137

def self.construct_session(pretrained_model_name_or_path, file_name, **options)
  prefix = "onnx/"
  if file_name.start_with?("../")
    prefix = ""
    file_name = file_name[3..]
  elsif file_name.start_with?("/")
    prefix = ""
    file_name = file_name[1..]
  end
  model_file_name = "#{prefix}#{file_name}#{options[:quantized] ? "_quantized" : ""}.onnx"
  path = Utils::Hub.get_model_file(pretrained_model_name_or_path, model_file_name, true, **options)

  OnnxRuntime::InferenceSession.new(path)
end

.from_pretrained(pretrained_model_name_or_path, quantized: true, progress_callback: nil, config: nil, cache_dir: nil, local_files_only: false, revision: "main", model_file_name: nil) ⇒ Object



89
90
91
92
93
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
# File 'lib/informers/models.rb', line 89

def self.from_pretrained(
  pretrained_model_name_or_path,
  quantized: true,
  progress_callback: nil,
  config: nil,
  cache_dir: nil,
  local_files_only: false,
  revision: "main",
  model_file_name: nil
)
  options = {
    quantized:,
    progress_callback:,
    config:,
    cache_dir:,
    local_files_only:,
    revision:,
    model_file_name:
  }

  model_name = MODEL_CLASS_TO_NAME_MAPPING[self]
  model_type = MODEL_TYPE_MAPPING[model_name]

  if model_type == MODEL_TYPES[:DecoderOnly]
    raise Todo

  elsif model_type == MODEL_TYPES[:Seq2Seq] || model_type == MODEL_TYPES[:Vision2Seq]
    raise Todo

  elsif model_type == MODEL_TYPES[:MaskGeneration]
    raise Todo

  elsif model_type == MODEL_TYPES[:EncoderDecoder]
    raise Todo

  else
    if model_type != MODEL_TYPES[:EncoderOnly]
      warn "Model type for '#{model_name || config&.model_type}' not found, assuming encoder-only architecture. Please report this."
    end
    info = [
      AutoConfig.from_pretrained(pretrained_model_name_or_path, **options),
      construct_session(pretrained_model_name_or_path, options[:model_file_name] || "model", **options)
    ]
  end

  new(*info)
end

Instance Method Details

#call(model_inputs, **kwargs) ⇒ Object



152
153
154
# File 'lib/informers/models.rb', line 152

def call(model_inputs, **kwargs)
  @forward.(model_inputs, **kwargs)
end