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
626
627
628
629
630
631
632
633
634
635
636
|
# File 'lib/transformers/modeling_utils.rb', line 259
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)
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.(resolved_config_file, commit_hash)
else
commit_hash = config._commit_hash
end
end
if !device_map.nil?
raise Todo
end
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
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
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 if pre_quantized || !quantization_config.nil?
raise Todo
else
hf_quantizer = nil
end
if !hf_quantizer.nil?
raise Todo
end
is_sharded = false
sharded_metadata = nil
_loading_info = nil
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
if use_safetensors != false
filename = _add_variant(SAFE_WEIGHTS_NAME, variant)
else
filename = _add_variant(WEIGHTS_NAME, variant)
end
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)
if resolved_archive_file.nil? && filename == _add_variant(SAFE_WEIGHTS_NAME, variant)
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
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)
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)
_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
}
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
if is_sharded
raise Todo
end
metadata = 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|
metadata = f.metadata
end
if metadata["format"] == "pt"
else
raise ArgumentError,
"Incompatible safetensors file. File metadata is not ['pt'] but #{metadata["format"]}"
end
end
from_pt = !(from_tf || from_flax)
if from_pt
if !is_sharded && state_dict.nil?
state_dict = load_state_dict(resolved_archive_file)
end
dtype_orig = nil
if !torch_dtype.nil?
raise Todo
end
if is_sharded
loaded_state_dict_keys = sharded_metadata["all_checkpoint_keys"]
else
loaded_state_dict_keys = state_dict.keys
end
end
config.name_or_path = pretrained_model_name_or_path
model = ModelingUtils.no_init_weights { new(config, *model_args, **model_kwargs) }
config = model.config
if device_map.is_a?(String)
raise Todo
elsif !device_map.nil?
raise Todo
end
if from_pt
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, resolved_archive_file,
pretrained_model_name_or_path,
ignore_mismatched_sizes: ignore_mismatched_sizes,
sharded_metadata: 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
model.tie_weights
model.eval
if model.can_generate && !pretrained_model_name_or_path.nil?
raise Todo
end
if !device_map.nil?
raise Todo
end
if !hf_quantizer.nil?
raise Todo
end
if output_loading_info
raise Todo
end
model
end
|