Skip to content

Chọn vị trí lưu pretrained model từ HuggingFace

Lí do

Theo mặc định, HuggingFace sẽ tải xuống folder và lưu trữ tại /home/username/.cache/. Đây không phải là vị trí được quy hoạch để chứa dữ liệu nặng (ổ OS). Vì vậy, hướng dẫn này sẽ tập trung về cách chọn vị trí lưu pretrained model từ HuggingFace.

Hướng dẫn

Mỗi lần call / khởi tạo model từ HuggingFace qua phương thức from_pretrained, hãy truyền thêm parameter là cache_dir='/workspace/pretrained_location'

Đoạn code trước:

#  begin initializing HF items, need auth token for these
model_config = transformers.AutoConfig.from_pretrained(
    model_id,
    use_auth_token=hf_auth,
    device_map='auto'
)

Đoạn code sau:

# begin initializing HF items, need auth token for these
model_config = transformers.AutoConfig.from_pretrained(
    model_id,
    use_auth_token=hf_auth,
    cache_dir="/workspace/tmp/.hf_cache",
    device_map='auto'
)

Một số ví dụ khác:

model = transformers.AutoModelForCausalLM.from_pretrained(
    model_id,
    trust_remote_code=True,
    config=model_config,
    quantization_config=bnb_config,
    device_map='balanced',
    use_auth_token=hf_auth,
    cache_dir="/workspace/tmp/.hf_cache",
    load_in_8bit=True,
    torch_dtype=bfloat16
)

tokenizer = transformers.AutoTokenizer.from_pretrained(
    model_id,
    use_auth_token=hf_auth,
    cache_dir="/workspace/tmp/.hf_cache"
)


from transformers import LlamaTokenizer
tokenizer = LlamaTokenizer.from_pretrained(
    model_id,
    use_auth_token=hf_auth,
    cache_dir="/workspace/tmp/.hf_cache"
)