dar-tau commited on
Commit
ac09cd4
·
verified ·
1 Parent(s): 62f6f76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -3
app.py CHANGED
@@ -61,7 +61,7 @@ def past_kv_to_device(past_kv, device):
61
  return tuple((k.to(device).detach(), v.to(device).detach()) for k, v in past_kv)
62
 
63
  def detach_past_kv(past_kv):
64
- return tuple((k.detach(), v.detach()) for k, v in past_kv)
65
 
66
  @spaces.GPU
67
  def set_past_key_values():
@@ -72,7 +72,7 @@ def set_past_key_values():
72
  test_messages = [*start_messages, {'role': 'user', 'content': 'Hello World!'}]
73
  tokenized_test = tokenizer.apply_chat_template(test_messages, return_tensors='pt')
74
  assert (tokenized_test[:, :tokenized.shape[1]] == tokenized).all().cpu().item()
75
- return gr.State(detach_past_kv(model(tokenized.to(model.device)).past_key_values))
76
 
77
 
78
  @spaces.GPU
@@ -90,7 +90,7 @@ def generate(text, past_key_values):
90
  if __name__ == "__main__":
91
  with torch.no_grad():
92
  past_key_values = set_past_key_values()
93
- print(f'{past_key_values.past_key_values=}')
94
  demo = gr.Interface(generate,
95
  inputs=["textbox", gr.State(past_key_values)], outputs="textbox")
96
  demo.launch()
 
61
  return tuple((k.to(device).detach(), v.to(device).detach()) for k, v in past_kv)
62
 
63
  def detach_past_kv(past_kv):
64
+ return tuple((k.detach().numpy().tolist(), v.detach().numpy().tolist()) for k, v in past_kv)
65
 
66
  @spaces.GPU
67
  def set_past_key_values():
 
72
  test_messages = [*start_messages, {'role': 'user', 'content': 'Hello World!'}]
73
  tokenized_test = tokenizer.apply_chat_template(test_messages, return_tensors='pt')
74
  assert (tokenized_test[:, :tokenized.shape[1]] == tokenized).all().cpu().item()
75
+ return detach_past_kv(model(tokenized.to(model.device)).past_key_values)
76
 
77
 
78
  @spaces.GPU
 
90
  if __name__ == "__main__":
91
  with torch.no_grad():
92
  past_key_values = set_past_key_values()
93
+ print(f'{past_key_values=}')
94
  demo = gr.Interface(generate,
95
  inputs=["textbox", gr.State(past_key_values)], outputs="textbox")
96
  demo.launch()