cbspace commited on
Commit
a32d620
·
1 Parent(s): b550616

Improve inference

Browse files
Files changed (2) hide show
  1. app.py +2 -2
  2. model.py +5 -4
app.py CHANGED
@@ -13,7 +13,7 @@ ffn_dim = embed_dim * 4
13
  n_vocab = 50257
14
  max_seq_len = 576
15
  dropout = 0.1
16
- top_p_value = 0.6
17
 
18
  @spaces.GPU
19
  def load_model():
@@ -43,7 +43,7 @@ app = gr.Interface(
43
  [
44
  gr.Textbox(label='Prompt', lines=3),
45
  gr.Number(label='Output Tokens', value=100),
46
- gr.Slider(0.1, 2.0, step=0.1, value=0.7, label='Temperature')
47
  ],
48
  gr.Textbox(label='Output', lines=11)
49
  )
 
13
  n_vocab = 50257
14
  max_seq_len = 576
15
  dropout = 0.1
16
+ top_p_value = 0.8
17
 
18
  @spaces.GPU
19
  def load_model():
 
43
  [
44
  gr.Textbox(label='Prompt', lines=3),
45
  gr.Number(label='Output Tokens', value=100),
46
+ gr.Slider(0.1, 2.0, step=0.1, value=0.8, label='Temperature')
47
  ],
48
  gr.Textbox(label='Output', lines=11)
49
  )
model.py CHANGED
@@ -60,8 +60,8 @@ class GPTModel(nn.Module):
60
  logits = self(context)[0,-1,:]
61
 
62
  if top_p:
63
- probs = torch.nn.functional.softmax(logits, dim=-1)
64
- sorted_probs, sorted_indices = torch.sort(probs, dim=-1)
65
  cumulative_probs = torch.cumsum(sorted_probs, dim=-1)
66
  sorted_mask = cumulative_probs <= top_p
67
  sorted_mask[..., 1:] = sorted_mask[..., :-1].clone()
@@ -71,8 +71,9 @@ class GPTModel(nn.Module):
71
  probs_sampled = torch.multinomial(filtered_probs, 1).item()
72
  selected_token = sorted_indices[probs_sampled].item()
73
  elif top_k:
74
- topk_probs, topk_indices = logits.topk(topk_elements)
75
- probs = nn.functional.softmax(topk_probs / temperature, dim=-1)
 
76
  probs_sampled = torch.multinomial(probs, 1).item()
77
  selected_token = topk_indices[probs_sampled].item()
78
  else: # Greedy decoding
 
60
  logits = self(context)[0,-1,:]
61
 
62
  if top_p:
63
+ probs = torch.nn.functional.softmax(logits / temperature, dim=-1)
64
+ sorted_probs, sorted_indices = torch.sort(probs, dim=-1, descending=True)
65
  cumulative_probs = torch.cumsum(sorted_probs, dim=-1)
66
  sorted_mask = cumulative_probs <= top_p
67
  sorted_mask[..., 1:] = sorted_mask[..., :-1].clone()
 
71
  probs_sampled = torch.multinomial(filtered_probs, 1).item()
72
  selected_token = sorted_indices[probs_sampled].item()
73
  elif top_k:
74
+ scaled_logits = logits / temperature
75
+ topk_probs, topk_indices = scaled_logits.topk(topk_elements)
76
+ probs = nn.functional.softmax(topk_probs, dim=-1)
77
  probs_sampled = torch.multinomial(probs, 1).item()
78
  selected_token = topk_indices[probs_sampled].item()
79
  else: # Greedy decoding