Shreyas094 commited on
Commit
a65ba38
1 Parent(s): 463f840

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -4
app.py CHANGED
@@ -2,6 +2,8 @@ import os
2
  import torch
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
  from huggingface_hub import login
 
 
5
 
6
  # Directly assign your Hugging Face token here
7
  hf_token = "your_hugging_face_api_token"
@@ -19,24 +21,49 @@ try:
19
  except ImportError:
20
  raise ImportError("The sentencepiece library is required for this tokenizer. Please install it with `pip install sentencepiece`.")
21
 
 
 
 
 
22
  tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=hf_token)
23
  model = AutoModelForCausalLM.from_pretrained(model_name, use_auth_token=hf_token)
24
 
 
 
 
25
  # Check if a GPU is available and if not, fall back to CPU
26
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
27
- model.to(device)
 
 
 
 
28
 
29
  # Example text input
30
  text_input = "How did Tesla perform in Q1 2024?"
31
 
 
 
 
32
  # Tokenize the input text
33
  inputs = tokenizer(text_input, return_tensors="pt").to(device)
34
 
35
- # Generate a response without sampling (deterministic)
36
- outputs = model.generate(**inputs, max_length=150, do_sample=False)
 
 
 
 
 
 
 
37
 
38
  # Decode the generated tokens to a readable string
39
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
40
 
41
  # Print the response
42
- print(response)
 
 
 
 
 
2
  import torch
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
  from huggingface_hub import login
5
+ import time
6
+ import torch.quantization
7
 
8
  # Directly assign your Hugging Face token here
9
  hf_token = "your_hugging_face_api_token"
 
21
  except ImportError:
22
  raise ImportError("The sentencepiece library is required for this tokenizer. Please install it with `pip install sentencepiece`.")
23
 
24
+ # Start time to measure execution time
25
+ start_time = time.time()
26
+
27
+ # Load tokenizer and model
28
  tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=hf_token)
29
  model = AutoModelForCausalLM.from_pretrained(model_name, use_auth_token=hf_token)
30
 
31
+ # Quantize the model
32
+ quantized_model = torch.quantization.quantize_dynamic(model, {torch.nn.Linear}, dtype=torch.qint8)
33
+
34
  # Check if a GPU is available and if not, fall back to CPU
35
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
36
+ quantized_model.to(device)
37
+
38
+ # Measure time for loading tokenizer, model, and quantization
39
+ loading_time = time.time() - start_time
40
+ print(f"Time taken to load tokenizer, model, and quantize: {loading_time:.2f} seconds")
41
 
42
  # Example text input
43
  text_input = "How did Tesla perform in Q1 2024?"
44
 
45
+ # Start time for inference
46
+ inference_start_time = time.time()
47
+
48
  # Tokenize the input text
49
  inputs = tokenizer(text_input, return_tensors="pt").to(device)
50
 
51
+ # Measure time for tokenization
52
+ tokenization_time = time.time() - inference_start_time
53
+
54
+ # Generate a response
55
+ outputs = quantized_model.generate(**inputs, max_length=150, do_sample=False)
56
+
57
+ # Measure time for inference
58
+ inference_time = time.time() - inference_start_time
59
+ print(f"Time taken for inference with quantized model: {inference_time:.2f} seconds")
60
 
61
  # Decode the generated tokens to a readable string
62
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
63
 
64
  # Print the response
65
+ print(f"Generated response: {response}")
66
+
67
+ # Total execution time
68
+ total_time = time.time() - start_time
69
+ print(f"Total execution time with quantized model: {total_time:.2f} seconds")