inference.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (c) Meta Platforms, Inc. and affiliates.
  2. # This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.
  3. import fire
  4. import torch
  5. from vllm import LLM
  6. from vllm import LLM, SamplingParams
  7. torch.cuda.manual_seed(42)
  8. torch.manual_seed(42)
  9. def load_model(model_name, tp_size=1):
  10. llm = LLM(model_name, tensor_parallel_size=tp_size)
  11. return llm
  12. def main(
  13. model,
  14. max_new_tokens=100,
  15. user_prompt=None,
  16. top_p=0.9,
  17. temperature=0.8
  18. ):
  19. while True:
  20. if user_prompt is None:
  21. user_prompt = input("Enter your prompt: ")
  22. print(f"User prompt:\n{user_prompt}")
  23. print(f"sampling params: top_p {top_p} and temperature {temperature} for this inference request")
  24. sampling_param = SamplingParams(top_p=top_p, temperature=temperature, max_tokens=max_new_tokens)
  25. outputs = model.generate(user_prompt, sampling_params=sampling_param)
  26. print(f"model output:\n {user_prompt} {outputs[0].outputs[0].text}")
  27. user_prompt = input("Enter next prompt (press Enter to exit): ")
  28. if not user_prompt:
  29. break
  30. def run_script(
  31. model_name: str,
  32. peft_model=None,
  33. tp_size=1,
  34. max_new_tokens=100,
  35. user_prompt=None,
  36. top_p=0.9,
  37. temperature=0.8
  38. ):
  39. model = load_model(model_name, tp_size)
  40. main(model, max_new_tokens, user_prompt, top_p, temperature)
  41. if __name__ == "__main__":
  42. fire.Fire(run_script)