Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vllm sampling params #2871

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions swift/llm/infer/infer_engine/vllm_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,20 @@ def _load_generation_config(self) -> None:
max_new_tokens = kwargs.get('max_new_tokens')
if max_new_tokens is not None:
kwargs['max_tokens'] = max_new_tokens
parameters = inspect.signature(SamplingParams.__init__).parameters
parameters = inspect.signature(SamplingParams).parameters
for k, v in kwargs.copy().items():
if k not in parameters or v is None:
kwargs.pop(k)
continue
# Check if parameter class has from_optional method
param_type = parameters[k].annotation

if str(param_type).startswith('typing.Optional'):
# Extract the actual type from Optional
param_type = param_type.__args__[0]
if hasattr(param_type, 'from_optional') and v is not None:
kwargs[k] = param_type.from_optional(v)

self.generation_config = SamplingParams(**kwargs)
else:
self.generation_config = SamplingParams()
Expand Down Expand Up @@ -246,18 +256,18 @@ def _get_logprobs(tokenizer: PreTrainedTokenizerBase,
return {'content': res}

def _prepare_generation_config(self, request_config: RequestConfig) -> SamplingParams:
kwargs = {'max_tokens': request_config.max_tokens}
for key in ['temperature', 'top_k', 'top_p', 'repetition_penalty']:
import msgspec
kwargs = msgspec.structs.asdict(self.generation_config)

for key in ['max_tokens', 'temperature', 'top_k', 'top_p', 'repetition_penalty']:
new_value = getattr(request_config, key)
if new_value is None:
kwargs[key] = getattr(self.generation_config, key)
else:
if new_value:
kwargs[key] = new_value

if request_config.logprobs:
kwargs['logprobs'] = 1
if request_config.top_logprobs is not None:
kwargs['logprobs'] = max(1, request_config.top_logprobs)
top_logprobs = request_config.top_logprobs or 1
kwargs['logprobs'] = max(1, top_logprobs)


# TODO: beam search
for key in ['n', 'best_of', 'frequency_penalty', 'presence_penalty', 'seed']:
Expand Down