oobabooga / text-generation-webui

A Gradio web UI for Large Language Models.
GNU Affero General Public License v3.0
39.89k stars 5.23k forks source link

Resolving the 'LlamaCppModel' Object Missing 'Device' Attribute Error in OpenAI LogitsBiasProcessor function - Ubuntu Linux #6176

Open BlueprintCoding opened 3 months ago

BlueprintCoding commented 3 months ago

Describe the bug

I recently ran into an issue while using the OogaBooga Text Generation Web UI with some GGUF models. The models loaded fine into Text Gen Web UI, but when I tried to use the OpenAI API extension for text completions via SillyTavern, I got an error saying the LlamaCppModel object had no attribute device. After some troubleshooting, I figured out how to fix it. This may be hyperspecific to my hardware and software version but I did not find any other information about this on the web so I figured I'd make a post about it.

I was able to fix this error by modifying the "LogitsBiasProcessor" function in the /extensions/openai/completions.py Changing line 36:

self.values = torch.tensor(values, dtype=torch.float, device=shared.model.device)" 

to the following:

if hasattr(shared.model, 'device'):
    self.values = torch.tensor(values, dtype=torch.float, device=shared.model.device)
else:
    self.values = torch.tensor(values, dtype=torch.float)

This fix checks if the shared.model object has a device attribute before attempting to use it. If the device attribute exists, the code creates a tensor with the specified values on the given device (e.g., a GPU). If the device attribute does not exist, it defaults to creating the tensor without specifying a device, which typically means it will be created on the CPU. This ensures compatibility with models that do not have the device attribute, preventing the AttributeError and allowing the code to run smoothly.

Is there an existing issue for this?

Reproduction

Screenshot

Text Web UI Model Settings

Model Settings

Logs

### Error Details

Exception in ASGI application
Traceback (most recent call last):
  File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/sse_starlette/sse.py", line 247, in __call__
    await wrap(partial(self.listen_for_disconnect, receive))
  File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/sse_starlette/sse.py", line 236, in wrap
    await func()
  File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/sse_starlette/sse.py", line 191, in listen_for_disconnect
    message = await receive()
              ^^^^^^^^^^^^^^^
  File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 553, in receive
    await self.message_event.wait()
  File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/asyncio/locks.py", line 213, in wait
    await fut
asyncio.exceptions.CancelledError: Cancelled by cancel scope

During handling of the above exception, another exception occurred:

  + Exception Group Traceback (most recent call last):
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 399, in run_asgi
  |     result = await app(  # type: ignore[func-returns-value]
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 70, in __call__
  |     return await self.app(scope, receive, send)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__
  |     await super().__call__(scope, receive, send)
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/starlette/applications.py", line 123, in __call__
  |     await self.middleware_stack(scope, receive, send)
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__
  |     raise exc
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__
  |     await self.app(scope, receive, _send)
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/starlette/middleware/cors.py", line 85, in __call__
  |     await self.app(scope, receive, send)
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 65, in __call__
  |     await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
  |     raise exc
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
  |     await app(scope, receive, sender)
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/starlette/routing.py", line 756, in __call__
  |     await self.middleware_stack(scope, receive, send)
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/starlette/routing.py", line 776, in app
  |     await route.handle(scope, receive, send)
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/starlette/routing.py", line 297, in handle
  |     await self.app(scope, receive, send)
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/starlette/routing.py", line 77, in app
  |     await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
  |     raise exc
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
  |     await app(scope, receive, sender)
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/starlette/routing.py", line 75, in app
  |     await response(scope, receive, send)
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/sse_starlette/sse.py", line 233, in __call__
  |     async with anyio.create_task_group() as task_group:
  |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 680, in __aexit__
  |     raise BaseExceptionGroup(
  | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/sse_starlette/sse.py", line 236, in wrap
    |     await func()
    |   File "/path/to/text-generation-webui/installer_files/env/lib/python3.11/site-packages/sse_starlette/sse.py", line 221, in stream_response
    |     async for data in self.body_iterator:
    |   File "/path/to/text-generation-webui/extensions/openai/script.py", line 106, in generator
    |     for resp in response:
    |   File "/path/to/text-generation-webui/extensions/openai/completions.py", line 559, in stream_completions
    |     for resp in completions_common(body, is_legacy, stream=True):
    |   File "/path/to/text-generation-webui/extensions/openai/completions.py", line 402, in completions_common
    |     generate_params = process_parameters(body, is_legacy=is_legacy)
    |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/path/to/text-generation-webui/extensions/openai/completions.py", line 113, in process_parameters
    |     logits_processor = [LogitsBiasProcessor(logit_bias)]
    |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/path/to/text-generation-webui/extensions/openai/completions.py", line 39, in __init__
    |     self.values = torch.tensor(values, dtype=torch.float, device=shared.model.device)
    |                                                                  ^^^^^^^^^^^^^^^^^^^
    | AttributeError: 'LlamaCppModel' object has no attribute 'device'
    +------------------------------------

System Info

Operating System: Ubuntu 22.04.04 LTS VM
Processor: 2 X Intel® Xeon(R) CPU E5-2690 v4 @ 2.60GHz × 14 core
Graphics: Tesla P40/PCIe/SSE2 / Tesla P40/PCIe/SSE2
Nvidia Driver Version: 555.42.02
CUDA Version: 12.5
Text-Web-UI Version: 1.8
Model: TheBloke / nous-capybara-limarpv3-34b.Q4_K_M-gguf
BlueprintCoding commented 3 months ago

I also tested if this fix adversely effects Windows installs of Text Web UI. I found no issues in my testing with the change.

goodglitch commented 2 months ago

I also run into this problem whenever I try to use "logit_bias" field in OpenAI API call. In my case it gives error for both llama.cpp and ExLlamav2 loaders. I also tried to run GGUF model on CPU, but got the same error. So I think the problem is more general than just GGUF models on GPU.