siddiVinayaka commited on
Commit
7249e51
·
verified ·
1 Parent(s): e66d0ff

Add confidence & latency to inference.py and update inference.py

Browse files
Files changed (1) hide show
  1. inference.py +8 -0
inference.py CHANGED
@@ -11,6 +11,7 @@ import json
11
  import re
12
  import torch
13
  import torch.nn.functional as F
 
14
  from pathlib import Path
15
  from typing import List, Tuple, Dict, Any, Optional, Sequence
16
  import tiktoken
@@ -173,10 +174,15 @@ def predict_decision(
173
  inp_tensor = torch.tensor([tokens], device=device, dtype=torch.long)
174
  pos_tensor = torch.tensor([spans], device=device, dtype=torch.long)
175
 
 
176
  with torch.no_grad():
177
  logits, _, _, _ = model(inp_tensor, candidate_positions=pos_tensor, last_token_only=True)
178
  raw_logits = logits[0, 0].clone()
179
 
 
 
 
 
180
  temp = 1.1
181
  if calibration and "temperatures" in calibration:
182
  temp = calibration["temperatures"].get(f"choice|{len(options)}", 1.1)
@@ -188,6 +194,8 @@ def predict_decision(
188
  return {
189
  "predicted_index": pred_idx,
190
  "predicted_option": options[pred_idx],
 
 
191
  "probabilities": {opt: round(p, 4) for opt, p in zip(options, probs)},
192
  }
193
 
 
11
  import re
12
  import torch
13
  import torch.nn.functional as F
14
+ import time
15
  from pathlib import Path
16
  from typing import List, Tuple, Dict, Any, Optional, Sequence
17
  import tiktoken
 
174
  inp_tensor = torch.tensor([tokens], device=device, dtype=torch.long)
175
  pos_tensor = torch.tensor([spans], device=device, dtype=torch.long)
176
 
177
+ t0 = time.perf_counter()
178
  with torch.no_grad():
179
  logits, _, _, _ = model(inp_tensor, candidate_positions=pos_tensor, last_token_only=True)
180
  raw_logits = logits[0, 0].clone()
181
 
182
+ if device.type == "cuda":
183
+ torch.cuda.synchronize()
184
+ latency_ms = (time.perf_counter() - t0) * 1000.0
185
+
186
  temp = 1.1
187
  if calibration and "temperatures" in calibration:
188
  temp = calibration["temperatures"].get(f"choice|{len(options)}", 1.1)
 
194
  return {
195
  "predicted_index": pred_idx,
196
  "predicted_option": options[pred_idx],
197
+ "confidence": round(probs[pred_idx], 4),
198
+ "latency_ms": round(latency_ms, 2),
199
  "probabilities": {opt: round(p, 4) for opt, p in zip(options, probs)},
200
  }
201