description
llama3_inference.py
close
data_object
quantization_config.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import torch from transformers import AutoModelForCausalLM, AutoTokenizer # TODO: Optimize PagedAttention integration class Llama3InferenceEngine: def __init__(self, model_id, quantization=None): self.device = 'cuda' if torch.cuda.is_available() else 'cpu' self.tokenizer = AutoTokenizer.from_pretrained(model_id) if quantization == '4bit': # Loading in 4-bit precision for memory efficiency self.model = AutoModelForCausalLM.from_pretrained( model_id, load_in_4bit=True, device_map='auto' ) def optimize_inference(self, prompt): inputs = self.tokenizer(prompt, return_tensors='pt').to(self.device) with torch.inference_mode(): # Current bottleneck: Token generation loop output = self.model.generate( **inputs, max_new_tokens=50, use_cache=True ) return self.tokenizer.decode(output[0]) # Initializing engine... engine = Llama3InferenceEngine('meta-llama/Llama-3-8b', quantization='4bit')|
terminal
TERMINAL
Python 3.11.4
close
[10:42:01]
System init...
[10:42:02]
Loading weights (4-bit quantization)...
[10:42:04]
Allocating VRAM segments [################----] 82%
[10:42:05]
Model loaded successfully.
[10:42:05]
Inference Start...
[10:42:06]
Processing prompt batch (size=1)...
[10:42:06]
> Generating tokens...
_