| 12 | |
| 13 | |
| 14 | class InternlmModel(ToolModel): |
| 15 | def __init__(self, model_path: str, peft_path: str = None, template: str = "default", trust_remote_code=True, tensor_parallel_size=1, gpu_memory_utilization=0.25): |
| 16 | self.model_path = model_path |
| 17 | self.peft_path = peft_path |
| 18 | self.template = template |
| 19 | self.trust_remote_code = trust_remote_code |
| 20 | self.tensor_parallel_size = tensor_parallel_size |
| 21 | self.gpu_memory_utilization = gpu_memory_utilization |
| 22 | self.load_model(self.model_path, self.peft_path, self.trust_remote_code, self.tensor_parallel_size, self.gpu_memory_utilization) |
| 23 | |
| 24 | def generate( |
| 25 | self, prompts: str, |
| 26 | template: str = None, |
| 27 | generate_configs: GenerateConfigs =None, |
| 28 | ) -> list: |
| 29 | '''产出对应结果''' |
| 30 | |
| 31 | template = self.template if template is None else template |
| 32 | |
| 33 | params = self.generate_params(generate_configs) |
| 34 | |
| 35 | if template == "default": |
| 36 | inputs = self.tokenizer(prompts, return_tensors="pt") |
| 37 | inputs["input_ids"] = inputs["input_ids"].cuda() |
| 38 | inputs["attention_mask"] = inputs["attention_mask"].cuda() |
| 39 | |
| 40 | inputs.update(params) |
| 41 | output = self.model.generate(**inputs) |
| 42 | predict = self.tokenizer.decode(output[0].tolist()) |
| 43 | predict = predict.split("\n")[-1] |
| 44 | predict = predict.replace("<|endoftext|>", "").replace("</s>", "") |
| 45 | return predict |
| 46 | elif template != "default": |
| 47 | output, _ = self.model.chat(self.tokenizer, prompts, history=None, **params) |
| 48 | return output |
| 49 | # params = self.generate_params(generate_configs) |
| 50 | # sampling_params = SamplingParams(**params) |
| 51 | # prompts = [prompts] if isinstance(prompts, str) else prompts |
| 52 | # outputs = self.model.generate(prompts, sampling_params) |
| 53 | # return [i.outputs[0].text for i in outputs] |
| 54 | |
| 55 | def generate_params( |
| 56 | self, generate_configs: GenerateConfigs, |
| 57 | ): |
| 58 | '''generate param''' |
| 59 | kargs = generate_configs.dict() |
| 60 | params = { |
| 61 | "max_new_tokens": kargs.get("max_new_tokens", 128), |
| 62 | "top_k": kargs.get("top_k", 50), |
| 63 | "top_p": kargs.get("top_p", 0.95), |
| 64 | "temperature": kargs.get("temperature", 1.0), |
| 65 | } |
| 66 | |
| 67 | # params = { |
| 68 | # "n": 1, |
| 69 | # "max_tokens": kargs.get("max_new_tokens", 128), |
| 70 | # "best_of": kargs.get("beam_bums", 1), |
| 71 | # "top_k": kargs.get("top_k", 50), |
nothing calls this directly
no outgoing calls
no test coverage detected