GlobalModel_generated.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import os
  2. import fire
  3. import gradio as gr
  4. import torch
  5. import transformers
  6. from peft import (
  7. PeftModel,
  8. LoraConfig,
  9. get_peft_model,
  10. get_peft_model_state_dict,
  11. prepare_model_for_int8_training,
  12. set_peft_model_state_dict,
  13. )
  14. from transformers import GenerationConfig, LlamaForCausalLM, LlamaTokenizer,AutoTokenizer
  15. from utils.callbacks import Iteratorize, Stream
  16. from utils.prompter import Prompter
  17. if torch.cuda.is_available():
  18. device = "cuda"
  19. else:
  20. device = "cpu"
  21. try:
  22. if torch.backends.mps.is_available():
  23. device = "mps"
  24. except:
  25. pass
  26. def main(
  27. load_8bit: bool = False,
  28. base_model: str = "",
  29. lora_weights_path: str = "",
  30. lora_config_path: str= "", # provide only the file path, excluding the file name 'adapter_config.json'
  31. prompt_template: str = "", # The prompt template to use, will default to alpaca.
  32. server_name: str = "127.0.0.1",
  33. share_gradio: bool = False,
  34. ):
  35. base_model = base_model or os.environ.get("BASE_MODEL", "")
  36. assert (
  37. base_model
  38. ), "Please specify a --base_model, e.g. --base_model='huggyllama/llama-7b'"
  39. prompter = Prompter(prompt_template)
  40. tokenizer = LlamaTokenizer.from_pretrained(base_model)
  41. if not lora_weights_path.endswith(".bin"):
  42. if device == "cuda":
  43. model = LlamaForCausalLM.from_pretrained(
  44. base_model,
  45. load_in_8bit=load_8bit,
  46. torch_dtype=torch.float16,
  47. device_map="auto",
  48. )
  49. model = PeftModel.from_pretrained(
  50. model,
  51. lora_weights_path,
  52. torch_dtype=torch.float16,
  53. )
  54. elif device == "mps":
  55. model = LlamaForCausalLM.from_pretrained(
  56. base_model,
  57. device_map={"": device},
  58. torch_dtype=torch.float16,
  59. )
  60. model = PeftModel.from_pretrained(
  61. model,
  62. lora_weights_path,
  63. device_map={"": device},
  64. torch_dtype=torch.float16,
  65. )
  66. else:
  67. model = LlamaForCausalLM.from_pretrained(
  68. base_model, device_map={"": device}, low_cpu_mem_usage=True
  69. )
  70. model = PeftModel.from_pretrained(
  71. model,
  72. lora_weights_path,
  73. device_map={"": device},
  74. )
  75. else:
  76. model = LlamaForCausalLM.from_pretrained(
  77. base_model,
  78. load_in_8bit=True,
  79. torch_dtype=torch.float16,
  80. device_map="auto",
  81. )
  82. model = prepare_model_for_int8_training(model)
  83. config = LoraConfig.from_pretrained(lora_config_path)
  84. lora_weights = torch.load(lora_weights_path)
  85. model = PeftModel(model, config)
  86. set_peft_model_state_dict(model,lora_weights,"default")
  87. del lora_weights
  88. # unwind broken decapoda-research config
  89. model.config.pad_token_id = tokenizer.pad_token_id = 0 # unk
  90. model.config.bos_token_id = 1
  91. model.config.eos_token_id = 2
  92. if not load_8bit:
  93. model.half() # seems to fix bugs for some users.
  94. model.eval()
  95. def evaluate(
  96. instruction,
  97. input=None,
  98. temperature=0.1,
  99. top_p=0.75,
  100. top_k=40,
  101. num_beams=4,
  102. max_new_tokens=128,
  103. stream_output=True,
  104. **kwargs,
  105. ):
  106. prompt = prompter.generate_prompt(instruction, input)
  107. inputs = tokenizer(prompt, return_tensors="pt")
  108. input_ids = inputs["input_ids"].to(device)
  109. generation_config = GenerationConfig(
  110. temperature=temperature,
  111. top_p=top_p,
  112. top_k=top_k,
  113. num_beams=num_beams,
  114. **kwargs,
  115. )
  116. generate_params = {
  117. "input_ids": input_ids,
  118. "generation_config": generation_config,
  119. "return_dict_in_generate": True,
  120. "output_scores": True,
  121. "max_new_tokens": max_new_tokens,
  122. }
  123. if stream_output:
  124. # Stream the reply 1 token at a time.
  125. # This is based on the trick of using 'stopping_criteria' to create an iterator,
  126. # from https://github.com/oobabooga/text-generation-webui/blob/ad37f396fc8bcbab90e11ecf17c56c97bfbd4a9c/modules/text_generation.py#L216-L243.
  127. def generate_with_callback(callback=None, **kwargs):
  128. kwargs.setdefault(
  129. "stopping_criteria", transformers.StoppingCriteriaList()
  130. )
  131. kwargs["stopping_criteria"].append(
  132. Stream(callback_func=callback)
  133. )
  134. with torch.no_grad():
  135. model.generate(**kwargs)
  136. def generate_with_streaming(**kwargs):
  137. return Iteratorize(
  138. generate_with_callback, kwargs, callback=None
  139. )
  140. with generate_with_streaming(**generate_params) as generator:
  141. for output in generator:
  142. # new_tokens = len(output) - len(input_ids[0])
  143. decoded_output = tokenizer.decode(output)
  144. if output[-1] in [tokenizer.eos_token_id]:
  145. break
  146. yield prompter.get_response(decoded_output)
  147. return # early return for stream_output
  148. # Without streaming
  149. with torch.no_grad():
  150. generation_output = model.generate(
  151. input_ids=input_ids,
  152. generation_config=generation_config,
  153. return_dict_in_generate=True,
  154. output_scores=True,
  155. max_new_tokens=max_new_tokens,
  156. )
  157. s = generation_output.sequences[0]
  158. output = tokenizer.decode(s)
  159. yield prompter.get_response(output)
  160. sherpherd_UI=gr.Interface(
  161. fn=evaluate,
  162. inputs=[
  163. gr.components.Textbox(
  164. lines=2,
  165. label="Instruction",
  166. placeholder="Tell me about alpacas.",
  167. ),
  168. gr.components.Textbox(lines=2, label="Input", placeholder="none"),
  169. gr.components.Slider(
  170. minimum=0, maximum=1, value=0.1, label="Temperature"
  171. ),
  172. gr.components.Slider(
  173. minimum=0, maximum=1, value=0.75, label="Top p"
  174. ),
  175. gr.components.Slider(
  176. minimum=0, maximum=100, step=1, value=40, label="Top k"
  177. ),
  178. gr.components.Slider(
  179. minimum=1, maximum=4, step=1, value=4, label="Beams"
  180. ),
  181. gr.components.Slider(
  182. minimum=1, maximum=2000, step=1, value=128, label="Max tokens"
  183. ),
  184. gr.components.Checkbox(label="Stream output"),
  185. ],
  186. outputs=[
  187. gr.inputs.Textbox(
  188. lines=5,
  189. label="Output",
  190. )
  191. ],
  192. title="FederatedGPT-shepherd",
  193. description="Shepherd is a LLM that has been fine-tuned in a federated manner ",
  194. ).queue()
  195. sherpherd_UI.launch(share=True)
  196. if __name__ == "__main__":
  197. fire.Fire(main)