Guest User

model_loader.py

a guest
May 6th, 2025
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.98 KB | None | 0 0
  1. import torch
  2. import os
  3. os.environ["TOKENIZERS_PARALLELISM"] = "false"
  4. from peft import get_peft_model, LoraConfig
  5. from transformers import BitsAndBytesConfig
  6. from transformers import Qwen2_5_VLForConditionalGeneration, Qwen2VLForConditionalGeneration, Qwen2_5_VLProcessor, AutoModelForCausalLM, AutoTokenizer, AutoProcessor, Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
  7. from qwen_vl_utils import process_vision_info
  8.  
  9. def load_gen_model_and_processor(config):
  10.     """ BitsAndBytesConfig is only for non-quantized models!
  11.        AWQ models are already quantized lol
  12.        requires config.model_id
  13.                 config.use_qlora
  14.    """
  15.  
  16.     bnb_config = BitsAndBytesConfig(load_in_4bit=True,
  17.                                     bnb_4bit_use_double_quant=True,
  18.                                     bnb_4bit_quant_type="nf4",
  19.                                     bnb_4bit_compute_dtype=torch.float16,
  20.                                     bnb_4bit_quant_storage=config.quant_storage_dtype,
  21.                                     )
  22.  
  23.     # figure out which GPU this process should load onto
  24.     local_rank = int(os.environ.get("LOCAL_RANK", 0))
  25.     device_str = f"cuda:{local_rank}"
  26.  
  27.     # change the device_map accordingly
  28.     model = Qwen2_5_VLForConditionalGeneration.from_pretrained(config.model_id,
  29.                                                                quantization_config= bnb_config if config.use_qlora else None,
  30.                                                                torch_dtype=config.quant_storage_dtype or torch.float16,)
  31.    
  32.     # quantisizing weights (lmao what does this even mean :cry:)
  33.     # extracts from config, default is false
  34.  
  35.     if getattr(config, "use_qlora", False):
  36.         peft_config = LoraConfig(
  37.             lora_alpha=config.lora_alpha,
  38.             lora_dropout=config.lora_dropout,
  39.             r=config.lora_r,
  40.             bias="none",
  41.             target_modules=["q_proj", "v_proj"],
  42.             task_type="CAUSAL_LM",
  43.         )
  44.  
  45.         model = get_peft_model(model, peft_config)
  46.    
  47.     processor = Qwen2_5_VLProcessor.from_pretrained(config.model_id, use_fast=True)
  48.  
  49.     return model, processor
  50.  
  51. def load_mini_gen_model_and_processor(config):
  52.     """ BitsAndBytesConfig is only for non-quantized models!
  53.        AWQ models are already quantized lol
  54.        requires config.model_id
  55.                 config.use_qlora
  56.    """
  57.  
  58.     bnb_config = BitsAndBytesConfig(load_in_4bit=True,
  59.                                     bnb_4bit_use_double_quant=True,
  60.                                     bnb_4bit_quant_type="nf4",
  61.                                     bnb_4bit_compute_dtype=torch.float16,
  62.                                     bnb_4bit_quant_storage=config.quant_storage_dtype,
  63.                                     )
  64.  
  65.     # figure out which GPU this process should load onto
  66.     local_rank = int(os.environ.get("LOCAL_RANK", 0))
  67.     device_str = f"cuda:{local_rank}"
  68.  
  69.     # change the device_map accordingly
  70.     model = Qwen2VLForConditionalGeneration.from_pretrained(config.small_model_id,
  71.                                                                quantization_config= bnb_config if config.use_qlora else None,
  72.                                                                device_map = {"": device_str},
  73.                                                                torch_dtype=config.quant_storage_dtype or torch.float16,)
  74.    
  75.     if getattr(config, "use_qlora", False):
  76.         peft_config = LoraConfig(
  77.             lora_alpha=config.lora_alpha,
  78.             lora_dropout=config.lora_dropout,
  79.             r=config.lora_r,
  80.             bias="none",
  81.             target_modules=["q_proj", "v_proj"],
  82.             task_type="CAUSAL_LM",
  83.         )
  84.  
  85.         model = get_peft_model(model, peft_config)
  86.    
  87.     processor = AutoProcessor.from_pretrained(config.model_id, use_fast=True)
  88.  
  89.     return model, processor
  90.  
  91. def load_eval_vl_model_and_processor (config):
  92.     bnb_config = BitsAndBytesConfig(load_in_4bit=True,
  93.                                     bnb_4bit_use_double_quant=True,
  94.                                     bnb_4bit_quant_type="nf4",
  95.                                     bnb_4bit_compute_dtype=torch.float16,
  96.                                     bnb_4bit_quant_storage=config.quant_storage_dtype,
  97.                                     )
  98.  
  99.     local_rank = int(os.environ.get("LOCAL_RANK", 0))
  100.     device_str = f"cuda:{local_rank}"
  101.  
  102.     model = Qwen2_5_VLForConditionalGeneration.from_pretrained(config.eval_vl_model_id,
  103.                                                                quantization_config= bnb_config if config.use_qlora else None,
  104.                                                                device_map = {"": device_str},
  105.                                                                torch_dtype=config.quant_storage_dtype or torch.float16,)
  106.      
  107.     if getattr(config, "use_qlora", False):
  108.         peft_config = LoraConfig(
  109.             lora_alpha=config.lora_alpha,
  110.             lora_dropout=config.lora_dropout,
  111.             r=config.lora_r,
  112.             bias="none",
  113.             target_modules=["q_proj", "v_proj"],
  114.             task_type="CAUSAL_LM",
  115.         )
  116.  
  117.         model = get_peft_model(model, peft_config)
  118.    
  119.     processor = Qwen2_5_VLProcessor.from_pretrained(config.eval_vl_model_id, use_fast=True)
  120.  
  121.     return model, processor
  122.  
  123. def load_eval_model_and_tokenizer (config):
  124.     bnb_config = BitsAndBytesConfig(load_in_4bit=True,
  125.                                 bnb_4bit_use_double_quant=True,
  126.                                 bnb_4bit_quant_type="nf4",
  127.                                 bnb_4bit_compute_dtype=torch.float16,
  128.                                 bnb_4bit_quant_storage=config.quant_storage_dtype,
  129.                                 )
  130.  
  131.     local_rank = int(os.environ.get("LOCAL_RANK", 0))
  132.     device_str = f"cuda:{local_rank}"
  133.  
  134.     model = AutoModelForCausalLM.from_pretrained(config.eval_model_id,
  135.                                                 quantization_config= bnb_config if config.use_qlora else None,
  136.                                                 device_map = {"": device_str},
  137.                                                 torch_dtype=config.quant_storage_dtype or torch.float16,)
  138.    
  139.     if getattr(config, "use_qlora", False):
  140.         peft_config = LoraConfig(
  141.             lora_alpha=config.lora_alpha,
  142.             lora_dropout=config.lora_dropout,
  143.             r=config.lora_r,
  144.             bias="none",
  145.             target_modules=["q_proj", "v_proj"],
  146.             task_type="CAUSAL_LM",
  147.         )
  148.  
  149.         model = get_peft_model(model, peft_config)
  150.    
  151.     tokenizer = AutoTokenizer.from_pretrained(config.eval_model_id,
  152.                                             quantization_config= bnb_config if config.use_qlora else None,
  153.                                             device_map = {"": device_str},
  154.                                             torch_dtype=config.quant_storage_dtype or torch.float16,)
  155.  
  156.     return model, tokenizer
  157.  
  158.  
Advertisement
Add Comment
Please, Sign In to add comment