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

[BUGFIX] handle prompt_logprobs in _apply_logits_processors to avoid AssertionError #3867

Closed

Conversation

tjohnson31415
Copy link
Contributor

@tjohnson31415 tjohnson31415 commented Apr 5, 2024

_apply_logits_processors does not handle the case where a sequence is being processed with prompt_logprobs where the logits tensor includes a row for each token in the prompt. Attempting to process a request that includes logits_processors and has prompt_logprobs results in an AssertionError where the code asserts that all rows of the logits tensor were processed.

TODO:

  • add tests that would have caught this bug
  • might need to adjust _apply_min_tokens_penalty as well for correctness since I didn't understand this interaction with prompt_logprobs when I implemented it (maybe separate PR)

NOTE: After making these changes, I found PR #3023 which is also intended to fix this bug by skipping processing of the prompt logits. I think that processing the prompt logits is more in line with the expectation of using prompt_logprobs.

FIX #2800

Script to reproduce the error by processing a request that includes logits_processors and prompt_logprobs.

prompts = [
    "Hello, my name is",
]

def pick_ith(token_ids, logits):
    # skip lower ids that have special tokens
    logits[len(token_ids)+10] = torch.finfo(torch.float16).max
    return logits
    
sampling_params = SamplingParams(
    temperature=0,
    logits_processors=[pick_ith],
    prompt_logprobs=1
)

outputs = llm.generate(prompts, sampling_params)

# Print the outputs.
for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

Error produced:

...
  File "/workspace/vllm/model_executor/layers/logits_processor.py", line 103, in _apply_logits_processors
    assert logits_row_idx == logits.shape[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError
Full Stack Trace
handle: <Handle functools.partial(<function _raise_exception_on_finish at 0x7f708b300040>, error_callback=<bound method AsyncLLMEngine._error_callback of <vllm.engine.async_llm_engine.AsyncLLMEngine object at 0x7f707c2b7a10>>)>
Traceback (most recent call last):
  File "/workspace/vllm/engine/async_llm_engine.py", line 38, in _raise_exception_on_finish
    task.result()
  File "/workspace/vllm/engine/async_llm_engine.py", line 482, in run_engine_loop
    has_requests_in_progress = await asyncio.wait_for(
                               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/vllm/lib/python3.11/asyncio/tasks.py", line 489, in wait_for
    return fut.result()
           ^^^^^^^^^^^^
  File "/workspace/vllm/engine/async_llm_engine.py", line 456, in engine_step
    request_outputs = await self.engine.step_async()
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/vllm/engine/async_llm_engine.py", line 213, in step_async
    output = await self.model_executor.execute_model_async(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/vllm/executor/multi_gpu_executor.py", line 154, in execute_model_async
    all_outputs = await self._run_workers_async("execute_model",
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/vllm/executor/multiproc_gpu_executor.py", line 138, in _run_workers_async
    return await asyncio.gather(*coros)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/vllm/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/vllm/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/vllm/worker/worker.py", line 220, in execute_model
    output = self.model_runner.execute_model(seq_group_metadata_list,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/vllm/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/vllm/worker/model_runner.py", line 666, in execute_model
    logits = self.model.compute_logits(hidden_states, sampling_metadata)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/vllm/model_executor/models/mixtral.py", line 385, in compute_logits
    logits = self.logits_processor(self.lm_head.weight, hidden_states,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/vllm/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/vllm/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/vllm/model_executor/layers/logits_processor.py", line 58, in forward
    logits = _apply_logits_processors(logits, sampling_metadata)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/vllm/model_executor/layers/logits_processor.py", line 103, in _apply_logits_processors
    assert logits_row_idx == logits.shape[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

BEFORE SUBMITTING, PLEASE READ THE CHECKLIST BELOW AND FILL IN THE DESCRIPTION ABOVE


PR Checklist (Click to Expand)

Thank you for your contribution to vLLM! Before submitting the pull request, please ensure the PR meets the following criteria. This helps vLLM maintain the code quality and improve the efficiency of the review process.

PR Title and Classification

Only specific types of PRs will be reviewed. The PR title is prefixed appropriately to indicate the type of change. Please use one of the following:

  • [Bugfix] for bug fixes.
  • [CI/Build] for build or continuous integration improvements.
  • [Doc] for documentation fixes and improvements.
  • [Model] for adding a new model or improving an existing model. Model name should appear in the title.
  • [Frontend] For changes on the vLLM frontend (e.g., OpenAI API server, LLM class, etc.)
  • [Kernel] for changes affecting CUDA kernels or other compute kernels.
  • [Core] for changes in the core vLLM logic (e.g., LLMEngine, AsyncLLMEngine, Scheduler, etc.)
  • [Hardware][Vendor] for hardware-specific changes. Vendor name should appear in the prefix (e.g., [Hardware][AMD]).
  • [Misc] for PRs that do not fit the above categories. Please use this sparingly.

Note: If the PR spans more than one category, please include all relevant prefixes.

Code Quality

The PR need to meet the following code quality standards:

  • We adhere to Google Python style guide and Google C++ style guide.
  • Pass all linter checks. Please use format.sh to format your code.
  • The code need to be well-documented to ensure future contributors can easily understand the code.
  • Include sufficient tests to ensure the project to stay correct and robust. This includes both unit tests and integration tests.
  • Please add documentation to docs/source/ if the PR modifies the user-facing behaviors of vLLM. It helps vLLM user understand and utilize the new features or changes.

Notes for Large Changes

Please keep the changes as concise as possible. For major architectural changes (>500 LOC excluding kernel/data/config/test), we would expect a GitHub issue (RFC) discussing the technical design and justification. Otherwise, we will tag it with rfc-required and might not go through the PR.

What to Expect for the Reviews

The goal of the vLLM team is to be a transparent reviewing machine. We would like to make the review process transparent and efficient and make sure no contributor feel confused or frustrated. However, the vLLM team is small, so we need to prioritize some PRs over others. Here is what you can expect from the review process:

  • After the PR is submitted, the PR will be assigned to a reviewer. Every reviewer will pick up the PRs based on their expertise and availability.
  • After the PR is assigned, the reviewer will provide status update every 2-3 days. If the PR is not reviewed within 7 days, please feel free to ping the reviewer or the vLLM team.
  • After the review, the reviewer will put an action-required label on the PR if there are changes required. The contributor should address the comments and ping the reviewer to re-review the PR.
  • Please respond to all comments within a reasonable time frame. If a comment isn't clear or you disagree with a suggestion, feel free to ask for clarification or discuss the suggestion.

Thank You

Finally, thank you for taking the time to read these guidelines and for your interest in contributing to vLLM. Your contributions make vLLM a great tool for everyone!

@tjohnson31415 tjohnson31415 force-pushed the fix-logits-processor branch from 8c9360d to 12406bf Compare April 5, 2024 17:00
@tjohnson31415
Copy link
Contributor Author

After looking at the behavior a bit more, I found that vLLM's application of sampling parameters to the values returned by prompt_logprobs is not fully consistent: temperature, top_p, top_k, and min_p are applied but presence_penalty, frequency_penalty, and repetition_penalty are not (REF). Applying processing that depends on the previously generated tokens would require iterating over sub-sequences of the prompt tokens, which complicates things. This would apply to LogitsProcessors as well 🤔.

I'm not familiar enough with the use-cases for prompt_logprobs to say that all prompt logits must be processed as if they were generated with the sampling parameters. My main goal with the PR is to avoid the AssertionError, which skipping prompt tokens like in #3023 fixes as well, so closing this PR in favor of that one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug] AssertionError with prompt_logprobs and logits_processors both set
1 participant