| """Base model loading utilities for Qwen3-8B.""" |
|
|
| import torch |
| from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
| from src.utils import load_yaml, setup_logger |
|
|
| logger = setup_logger(__name__) |
|
|
|
|
| def load_base_model(config_path: str = "configs/model/base_model.yaml"): |
| """Load the base LLM (Qwen3-8B). |
| |
| Args: |
| config_path: Path to base model config YAML. |
| |
| Returns: |
| (model, tokenizer) tuple. |
| """ |
| config = load_yaml(config_path) |
|
|
| logger.info(f"Loading base model from {config['model_name_or_path']}") |
|
|
| dtype_map = { |
| "bfloat16": torch.bfloat16, |
| "float16": torch.float16, |
| "float32": torch.float32, |
| } |
| torch_dtype = dtype_map.get(config.get("torch_dtype", "bfloat16"), torch.bfloat16) |
|
|
| model = AutoModelForCausalLM.from_pretrained( |
| config["model_name_or_path"], |
| torch_dtype=torch_dtype, |
| device_map=config.get("device_map", "auto"), |
| trust_remote_code=config.get("trust_remote_code", True), |
| tp_plan=None, |
| ) |
|
|
| tokenizer = load_tokenizer(config["model_name_or_path"]) |
|
|
| logger.info(f"Model loaded: {model.__class__.__name__}, dtype={torch_dtype}") |
| return model, tokenizer |
|
|
|
|
| def load_tokenizer(model_name_or_path: str): |
| """Load the tokenizer. |
| |
| Args: |
| model_name_or_path: Model path. |
| |
| Returns: |
| Tokenizer instance. |
| """ |
| tokenizer = AutoTokenizer.from_pretrained( |
| model_name_or_path, |
| trust_remote_code=True, |
| ) |
| if tokenizer.pad_token is None: |
| tokenizer.pad_token = tokenizer.eos_token |
| tokenizer.padding_side = "right" |
| return tokenizer |
|
|