Instructions to use echan24/mistral-finetune with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use echan24/mistral-finetune with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1") model = PeftModel.from_pretrained(base_model, "echan24/mistral-finetune") - Notebooks
- Google Colab
- Kaggle
File size: 3,123 Bytes
ca44afe 3cd9771 ca44afe 3cd9771 ca44afe 3cd9771 ca44afe 3cd9771 ca44afe | 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | from typing import Dict, List, Any
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import PeftModel, PeftConfig
import torch
class EndpointHandler:
def __init__(self, path="rafikixx/mistral-finetune"):
'''
this method will be called when starting the Endpoint and will receive 1 argument,
a string with the path to your model weights. This allows you to load your model
correctly
inputs
------
path: string with path to model weights, this is required by HuggingFace
https://huggingface.co/docs/inference-endpoints/guides/custom_handler
'''
# load the model
config = PeftConfig.from_pretrained(path)
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16
)
self.model = AutoModelForCausalLM.from_pretrained(
config.base_model_name_or_path,
return_dict=True,
load_in_4bit=True,
device_map={"":0},
trust_remote_code=True,
quantization_config=bnb_config,
)
self.tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
self.tokenizer.pad_token = self.tokenizer.eos_token
self.model = PeftModel.from_pretrained(self.model, path)
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
'''
method will be called on every request and receive a dictionary with your request body
as a python dictionary. It will always contain the inputs key.
Args:
data: A dictionary with a inputs key e.g. {'inputs': 'What is NFA Rule 2-2?',
'parameters': {'max_new_tokens': 64}}
returns a list containing a dictionary
'''
inputs = data.pop('inputs', data)
parameters = data.pop('parameters', None)
prompt = f'''### Question:
{inputs}
### Answer:
'''.strip()
batch = self.tokenizer(
prompt,
padding=True,
truncation=True,
return_tensors='pt'
)
batch = batch.to('cuda:0')
generation_config = self.model.generation_config
generation_config.top_p = 0.7
generation_config.temperature = 0.7
generation_config.max_new_tokens = parameters.get('max_new_tokens', 32)
generation_config.num_return_sequences = 1
generation_config.pad_token_id = self.tokenizer.eos_token_id
generation_config.eos_token_id = self.tokenizer.eos_token_id
with torch.cuda.amp.autocast():
output_tokens = self.model.generate(
input_ids = batch.input_ids,
generation_config=generation_config,
)
generated_text = self.tokenizer.decode(output_tokens[0])
prediction = {'answer': generated_text.split('### Answer:')[1].strip()}
return prediction |