parth parekh commited on
Commit
9e1ad54
β€’
1 Parent(s): 7c83ecd

added more speed

Browse files
Files changed (2) hide show
  1. main.py +14 -3
  2. requirements.txt +3 -1
main.py CHANGED
@@ -1,33 +1,45 @@
1
  import os
2
  import torch
 
3
  from fastapi import FastAPI, Request
4
  from pydantic import BaseModel
5
  from transformers import AutoModelForCausalLM, AutoTokenizer
6
  from dotenv import load_dotenv
 
7
 
8
  # Load environment variables from a .env file (useful for local development)
9
  load_dotenv()
10
 
11
  # Initialize FastAPI app
12
- app = FastAPI(description="Use the Llama-3.2-1B-Instruct model using the api !!",docs_url="/",redoc_url="/doc")
13
 
14
  # Set your Hugging Face token from environment variable
15
  HF_TOKEN = os.getenv("HF_TOKEN")
16
 
17
  MODEL = "meta-llama/Llama-3.2-1B-Instruct"
18
 
 
19
  device = "cuda" if torch.cuda.is_available() else "cpu"
20
  print(f"Using device: {device}")
21
 
 
 
 
 
 
 
22
  # Load model and tokenizer
23
  tokenizer = AutoTokenizer.from_pretrained(MODEL, token=HF_TOKEN)
24
  model = AutoModelForCausalLM.from_pretrained(
25
  MODEL,
26
  token=HF_TOKEN,
27
- torch_dtype=torch.float16, # Use float16 for better GPU memory usage
28
  device_map="auto"
29
  )
30
 
 
 
 
31
  # Pydantic model for input
32
  class PromptRequest(BaseModel):
33
  prompt: str
@@ -49,4 +61,3 @@ async def generate_text(request: PromptRequest):
49
 
50
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
51
  return {"response": response}
52
-
 
1
  import os
2
  import torch
3
+ import multiprocessing
4
  from fastapi import FastAPI, Request
5
  from pydantic import BaseModel
6
  from transformers import AutoModelForCausalLM, AutoTokenizer
7
  from dotenv import load_dotenv
8
+ from accelerate import Accelerator
9
 
10
  # Load environment variables from a .env file (useful for local development)
11
  load_dotenv()
12
 
13
  # Initialize FastAPI app
14
+ app = FastAPI(description="Use the Llama-3.2-1B-Instruct model using the API", docs_url="/", redoc_url="/doc")
15
 
16
  # Set your Hugging Face token from environment variable
17
  HF_TOKEN = os.getenv("HF_TOKEN")
18
 
19
  MODEL = "meta-llama/Llama-3.2-1B-Instruct"
20
 
21
+ # Auto-select CPU or GPU
22
  device = "cuda" if torch.cuda.is_available() else "cpu"
23
  print(f"Using device: {device}")
24
 
25
+ # Set PyTorch to use all available CPU cores if running on CPU
26
+ torch.set_num_threads(multiprocessing.cpu_count())
27
+
28
+ # Initialize Accelerator for managing device allocation
29
+ accelerator = Accelerator()
30
+
31
  # Load model and tokenizer
32
  tokenizer = AutoTokenizer.from_pretrained(MODEL, token=HF_TOKEN)
33
  model = AutoModelForCausalLM.from_pretrained(
34
  MODEL,
35
  token=HF_TOKEN,
36
+ torch_dtype=torch.bfloat16 if device == 'cpu' else torch.float16, # Use bfloat16 for CPUs, float16 for GPUs
37
  device_map="auto"
38
  )
39
 
40
+ # Prepare model for multi-device setup with accelerate
41
+ model, tokenizer = accelerator.prepare(model, tokenizer)
42
+
43
  # Pydantic model for input
44
  class PromptRequest(BaseModel):
45
  prompt: str
 
61
 
62
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
63
  return {"response": response}
 
requirements.txt CHANGED
@@ -3,4 +3,6 @@ transformers
3
  torch
4
  uvicorn
5
  python-dotenv
6
- accelerate>=0.26.0
 
 
 
3
  torch
4
  uvicorn
5
  python-dotenv
6
+ optimum[onnxruntime] # For CPU optimizations with ONNX Runtime
7
+ accelerate # For managing multi-device setup (CPU/GPU)
8
+ gunicorn # For running multiple workers