File size: 1,637 Bytes
4968ea3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 62 | """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
|