TwistedDoor

How to run Bloom AI

Sep 18th, 2022
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. # Official instructions: https://huggingface.co/docs/transformers/main/en/model_doc/bloom#transformers.BloomForCausalLM
  2. # Find help here: https://discuss.huggingface.co
  3. # Nice guide here: https://towardsdatascience.com/getting-started-with-bloom-9e3295459b65
  4. # python -m venv name
  5. # source name/bin/activate
  6. # pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cu117 # From https://pytorch.org/get-started/locally/
  7. # pip install transformers bitsandbytes accelerate
  8. # accelerate config
  9. # python
  10. import torch
  11. from transformers import BloomTokenizerFast, BloomForCausalLM
  12. tokenizer = BloomTokenizerFast.from_pretrained("bigscience/bloom-3b")
  13. model = BloomForCausalLM.from_pretrained("bigscience/bloom-3b", device_map="auto", load_in_8bit=True)
  14. prompt = "Dave picked up the baseball and"
  15. result_length = 200
  16. inputs = tokenizer(prompt, return_tensors="pt")
  17. device = torch.device("cuda:0")
  18. inputs2 = inputs.to(device)
  19. greedy = model.generate(inputs2["input_ids"],max_length=result_length)[0]
  20. beam = model.generate(inputs2["input_ids"],max_length=result_length, num_beams=2, no_repeat_ngram_size=2,early_stopping=True)[0]
  21. sampling = model.generate(inputs2["input_ids"],max_length=result_length, do_sample=True, top_k=50, top_p=0.9)[0]
  22. print(tokenizer.decode(greedy))
  23. print(tokenizer.decode(beam))
  24. print(tokenizer.decode(sampling))
Advertisement
Add Comment
Please, Sign In to add comment