-
Notifications
You must be signed in to change notification settings - Fork 896
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Functions related to BERT or similar models""" | ||
|
||
import logging | ||
from typing import List, Tuple | ||
from peft import prepare_model_for_kbit_training | ||
|
||
|
||
import numpy as np # type: ignore | ||
from transformers import LlamaModel, AutoTokenizer # type: ignore | ||
|
||
from stanza.models.coref.config import Config | ||
from stanza.models.coref.const import Doc | ||
|
||
import torch.nn as nn | ||
|
||
logger = logging.getLogger('stanza') | ||
|
||
def load_llama(config: Config): | ||
logger.debug(f"Loading {config.llama_model}...") | ||
|
||
base_llama_name = config.llama_model.split("/")[-1] | ||
tokenizer_kwargs = config.tokenizer_kwargs.get(base_llama_name, {}) | ||
if tokenizer_kwargs: | ||
logger.debug(f"Using tokenizer kwargs: {tokenizer_kwargs}") | ||
tokenizer = AutoTokenizer.from_pretrained(config.llama_model, **tokenizer_kwargs) | ||
model = prepare_model_for_kbit_training(LlamaModel.from_pretrained(config.llama_model, load_in_8bit=True)) | ||
# .to(config.device) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
logger.debug("Llama successfully loaded.") | ||
|
||
return model, tokenizer | ||
|
||
|
||
|
because bits and bytes through
load_in_8_bit
handles device placement on your behalf