diff --git a/modelscope/pipelines/accelerate/vllm.py b/modelscope/pipelines/accelerate/vllm.py index 5c11c29b..15ced4bb 100644 --- a/modelscope/pipelines/accelerate/vllm.py +++ b/modelscope/pipelines/accelerate/vllm.py @@ -42,6 +42,24 @@ class Vllm(InferFramework): The string batch or the token list batch to input to the model. kwargs: Sampling parameters. """ + + # convert hf generate config to vllm + do_sample = kwargs.pop('do_sample', None) + num_beam = kwargs.pop('num_beam', 1) + max_length = kwargs.pop('max_length', None) + max_new_tokens = kwargs.pop('max_new_tokens', None) + + # for vllm, default to do_sample/greedy(depends on temperature). + # for hf, do_sample=false, num_beam=1 -> greedy(default) + # do_sample=ture, num_beam=1 -> sample + # do_sample=false, num_beam>1 -> beam_search + if not do_sample and num_beam > 1: + kwargs['use_beam_search'] = True + if max_length: + kwargs['max_tokens'] = max_length - len(prompts[0]) + if max_new_tokens: + kwargs['max_tokens'] = max_new_tokens + from vllm import SamplingParams sampling_params = SamplingParams(**kwargs) if isinstance(prompts[0], str): diff --git a/modelscope/pipelines/nlp/llm_pipeline.py b/modelscope/pipelines/nlp/llm_pipeline.py index 5cd2dcb1..a0791c31 100644 --- a/modelscope/pipelines/nlp/llm_pipeline.py +++ b/modelscope/pipelines/nlp/llm_pipeline.py @@ -104,12 +104,20 @@ class LLMPipeline(Pipeline): if isinstance(model, str) and is_official_hub_path(model): logger.info(f'initiate model from location {model}.') - if self.llm_framework is not None: + if self.llm_framework: model_dir = model if os.path.exists( model) else snapshot_download(model) - return self._wrap_infer_framework(model_dir, - self.llm_framework) - elif is_model(model): + try: + model = self._wrap_infer_framework(model_dir, + self.llm_framework) + logger.info(f'initiate model with {framework}.') + return model + except Exception as e: + self.llm_framework = None + logger.warning( + f'Cannot using llm_framework with {model}, ' + f'ignoring llm_framework={self.llm_framework} : {e}') + if is_model(model): return Model.from_pretrained( model, invoked_by=Invoke.PIPELINE,