generate.py 869 B

12345678910111213141516171819202122232425262728293031
  1. from peft import PeftModel
  2. from transformers import LLaMATokenizer, LLaMAForCausalLM
  3. tokenizer = LLaMATokenizer.from_pretrained("decapoda-research/llama-7b-hf")
  4. model = LLaMAForCausalLM.from_pretrained(
  5. "decapoda-research/llama-7b-hf",
  6. load_in_8bit=True,
  7. device_map="auto",
  8. )
  9. model = PeftModel.from_pretrained(model, "tloen/alpaca-lora-7b")
  10. PROMPT = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
  11. ### Instruction:
  12. Write a poem about the following topic.
  13. ### Input:
  14. Cars
  15. ### Response:"""
  16. inputs = tokenizer(
  17. PROMPT,
  18. return_tensors="pt",
  19. )
  20. generation_output = model.generate(
  21. **inputs, return_dict_in_generate=True, output_scores=True, max_new_tokens=128
  22. )
  23. for s in generation_output.sequences:
  24. print(tokenizer.decode(s))