Module: Cohere::Transcribe::ModelIdentity
- Defined in:
- lib/cohere/transcribe/model_identity.rb
Constant Summary collapse
- MODEL_WEIGHT_FILES =
%w[ model.safetensors model.safetensors.index.json pytorch_model.bin pytorch_model.bin.index.json ].freeze
- ADAPTER_WEIGHT_FILES =
%w[adapter_model.safetensors adapter_model.bin].freeze
Class Method Summary collapse
- .classify_model_config(config, reference) ⇒ Object
- .default_model_revision(model_id, revision) ⇒ Object
- .read_json_object(path) ⇒ Object
- .reference(model_id, revision) ⇒ Object
- .resolve(model_id, model_revision = nil, adapter_id = nil, adapter_revision = nil, hub: Hub.new, verify_weight_artifacts: true) ⇒ Object
- .resolve_local_directory(reference, description: "Model") ⇒ Object
-
.verify_model_weight_artifacts(identity, hub: Hub.new) ⇒ Object
Identity resolution is also used while planning runs that may be fully satisfied by checkpoints.
Class Method Details
.classify_model_config(config, reference) ⇒ Object
88 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 |
# File 'lib/cohere/transcribe/model_identity.rb', line 88 def classify_model_config(config, reference) unless config["model_type"] == "cohere_asr" raise ArgumentError, "#{reference} uses model_type=#{python_value_repr(config["model_type"])}; " \ "expected a native Transformers Cohere ASR checkpoint" end architectures = config["architectures"] if python_truthy?(architectures) && !declares_cohere_asr_architecture?(architectures) raise ArgumentError, "#{reference} does not declare CohereAsrForConditionalGeneration" end quantization = config["quantization_config"] return [:dense, nil] if quantization.nil? raise ArgumentError, "#{reference} has an invalid quantization_config" unless quantization.is_a?(Hash) method = python_lower(python_string(quantization.fetch("quant_method", ""))) four_bit = quantization.fetch("load_in_4bit", quantization.fetch("_load_in_4bit", false)) eight_bit = quantization.fetch("load_in_8bit", quantization.fetch("_load_in_8bit", false)) unless [four_bit, eight_bit].all? { |value| [true, false].include?(value) } raise ArgumentError, "#{reference} has an invalid quantization_config: " \ "bitsandbytes load flags must be boolean" end unless method == "bitsandbytes" && four_bit != eight_bit rendered_method = method.empty? ? "unknown" : method raise ArgumentError, "#{reference} uses unsupported saved quantization configuration " \ "#{python_string_repr(rendered_method)}" end [four_bit ? :"bitsandbytes-int4" : :"bitsandbytes-int8", quantization.dup.freeze] end |
.default_model_revision(model_id, revision) ⇒ Object
27 28 29 |
# File 'lib/cohere/transcribe/model_identity.rb', line 27 def default_model_revision(model_id, revision) revision || (model_id == DEFAULT_ASR_MODEL_ID ? DEFAULT_ASR_MODEL_REVISION : nil) end |
.read_json_object(path) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/cohere/transcribe/model_identity.rb', line 220 def read_json_object(path) path = Pathname(path) raise ArgumentError, "Local artifact #{path} is missing or is not a file" unless path.file? payload = JSON.parse(path.read(encoding: "UTF-8")) raise ArgumentError, "#{path} is not a JSON object" unless payload.is_a?(Hash) payload rescue SystemCallError, JSON::ParserError, EncodingError => e raise ArgumentError, "Cannot read JSON object from #{path}: #{e.}" end |
.reference(model_id, revision) ⇒ Object
216 217 218 |
# File 'lib/cohere/transcribe/model_identity.rb', line 216 def reference(model_id, revision) revision ? "#{model_id}@#{revision}" : model_id end |
.resolve(model_id, model_revision = nil, adapter_id = nil, adapter_revision = nil, hub: Hub.new, verify_weight_artifacts: true) ⇒ Object
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/cohere/transcribe/model_identity.rb', line 121 def resolve(model_id, model_revision = nil, adapter_id = nil, adapter_revision = nil, hub: Hub.new, verify_weight_artifacts: true) raise ArgumentError, "adapter_revision requires an adapter_id" if adapter_id.nil? && adapter_revision resolved_model_id, resolved_model_revision, model_dir = resolve_reference( model_id, model_revision, description: "Model", metadata: "config.json", hub: hub, default_revision: default_model_revision(model_id, model_revision) ) if packaged_default_model?(resolved_model_id, resolved_model_revision) format = :dense quantization = nil else config_path = if model_dir Pathname(model_dir).join("config.json") else hub.download( resolved_model_id, "config.json", revision: resolved_model_revision ) end config = read_json_object(config_path) reference = reference(resolved_model_id, resolved_model_revision) format, quantization = classify_model_config(config, reference) end resolved_adapter_id = nil resolved_adapter_revision = nil if adapter_id raise ArgumentError, "PEFT adapters are supported only with dense base models" unless format == :dense resolved_adapter_id, resolved_adapter_revision, adapter_dir = resolve_reference( adapter_id, adapter_revision, description: "Adapter", metadata: "adapter_config.json", hub: hub, default_revision: adapter_revision ) adapter_path = if adapter_dir Pathname(adapter_dir).join("adapter_config.json") else hub.download( resolved_adapter_id, "adapter_config.json", revision: resolved_adapter_revision ) end adapter_config = read_json_object(adapter_path) validate_adapter_config!( adapter_config, resolved_model_id, resolved_model_revision, adapter_id: resolved_adapter_id, adapter_revision: resolved_adapter_revision, hub: hub ) end identity = ResolvedModelIdentity.new( model_id: resolved_model_id, model_revision: resolved_model_revision, model_format: format, quantization_config: quantization, adapter_id: resolved_adapter_id, adapter_revision: resolved_adapter_revision ) verify_model_weight_artifacts(identity, hub: hub) if verify_weight_artifacts identity end |
.resolve_local_directory(reference, description: "Model") ⇒ Object
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 |
# File 'lib/cohere/transcribe/model_identity.rb', line 36 def resolve_local_directory(reference, description: "Model") raise TypeError, "#{description} must be a string" unless reference.is_a?(String) source_path = Pathname(reference) path = begin (source_path) rescue ArgumentError, SystemCallError => e raise ArgumentError, "Cannot resolve #{description.downcase} path #{reference.inspect}: #{e.}" end return utf8_resolved_path(path.realpath.to_s, description) if path.directory? if path.exist? || path.symlink? raise ArgumentError, "#{description} path #{reference.inspect} is not a directory" end explicit_reference = reference.start_with?("./", "../", "~") || [".", "..", "~"].include?(reference) || reference.count("/") > 1 explicit = source_path.absolute? || explicit_reference raise ArgumentError, "#{description} directory #{reference.inspect} does not exist" if explicit nil rescue SystemCallError => e raise ArgumentError, "Cannot resolve #{description.downcase} path #{reference.inspect}: #{e.}" end |
.verify_model_weight_artifacts(identity, hub: Hub.new) ⇒ Object
Identity resolution is also used while planning runs that may be fully satisfied by checkpoints. Keep weight discovery as a separate operation so those runs do not require inference artifacts they will never load.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/cohere/transcribe/model_identity.rb', line 195 def verify_model_weight_artifacts(identity, hub: Hub.new) unless packaged_default_model?(identity.model_id, identity.model_revision) verify_weight_artifacts!( identity.model_id, identity.model_revision, MODEL_WEIGHT_FILES, "Transformers model weights", hub: hub ) end return unless identity.adapter_id verify_weight_artifacts!( identity.adapter_id, identity.adapter_revision, ADAPTER_WEIGHT_FILES, "PEFT adapter weights", hub: hub ) end |