Skip to content

[Question]:when conducting a knowledge base search in the chat interface, it was found that ollama has a high cpu usage rate? #7881

Description

@zhugh2333

Self Checks

  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (Language Policy).
  • Non-english title submitions will be closed directly ( 非英文标题的提交将会被直接关闭 ) (Language Policy).
  • Please do not modify this template :) and fill in all the required fields.

Describe your problem

Currently, when conducting a knowledge base search in the chat interface, it was found that ollama has a high cpu usage rate, but the GPU load is indeed empty, and the search takes more than ten minutes to complete. What happened during this process? Why does such a phenomenon occur?

Activity

  1. zhugh2333 commented on May 27, 2025

    @zhugh2333
    Author
  2. added
    🐞 bugSomething isn't working, pull request that fix bug.
    on May 27, 2025
  3. dosubot commented on May 27, 2025

    @dosubot

    Based on the information available, the high CPU usage and empty GPU load during knowledge base searches with the Ollama model in the RAGFlow chat interface may be due to the Ollama model not utilizing the GPU as expected. This can happen if the environment is not configured to force all model layers to run on the GPU—by default, some layers may fall back to the CPU, leading to high CPU usage and slow performance.

    To address this, you can try setting the following environment variables before launching the Ollama service:

    export OLLAMA_NUM_GPU=999
    export SYCL_PI_LEVEL_ZERO_USE_IMMEDIATE_COMMANDLISTS=1
    export no_proxy=localhost,127.0.0.1
    export ZES_ENABLE_SYSMAN=1
    export SYCL_CACHE_PERSISTENT=1

    These settings are especially important if you are using Intel Arc™ A-Series Graphics on Linux (Kernel 6.2 or newer). Setting OLLAMA_NUM_GPU=999 ensures all model layers use the GPU, which should reduce CPU load and improve performance. More details can be found in the official deployment guide.

    If you have already set these variables and are still experiencing issues, there may be other factors at play, such as software limitations, model compatibility, or integration details within RAGFlow. At this time, there is no additional documentation or issue thread with a confirmed solution for this specific problem.

    If you need further troubleshooting, please provide details about your hardware, operating system, and the exact Ollama model you are using.

    To reply, just mention @dosu.


    Share context across your team and agents. Try Dosu.

    Leave Feedback Learn about ragflow with Dosu Add Dosu to your team

  4. RNGMARTIN commented on May 30, 2025

    @RNGMARTIN

    @KevinHuSh I found the same problem after updating ragflow to 0.19.0. When using agent based on models in ollama, the GPU is 0 but CPU is extremly high. What's going on? There wasn't such situation before updating.

  5. konn-submarine-bu commented on May 30, 2025

    @konn-submarine-bu

    SAME PROBLEM

  6. zhugh2333 commented on May 30, 2025

    @zhugh2333
    Author

    I have tried a temporary solution, OLLAMA_NUM_GPU=999 in the ollama service,But I don't know the root cause

  7. zhugh2333 commented on May 30, 2025

    @zhugh2333
    Author

    /etc/systemd/system/ollama.service

    [Unit]
    Description=Ollama Service
    After=network-online.target

    [Service]
    ExecStart=/usr/local/bin/ollama serve
    User=ollama
    Group=ollama
    .........

    • Environment="OLLAMA_NUM_GPU=999"
  8. RNGMARTIN commented on May 31, 2025

    @RNGMARTIN

    /etc/systemd/system/ollama.service

    [Unit] Description=Ollama Service After=network-online.target

    [Service] ExecStart=/usr/local/bin/ollama serve User=ollama Group=ollama .........

    • Environment="OLLAMA_NUM_GPU=999"

    Mine is on windows and i have tried to add enviornment, but the problem is still happening.

  9. zhugh2333 commented on Jun 3, 2025

    @zhugh2333
    Author

    "OLLAMA_NUM_GPU=999" doesn't work. Even if this option is set, this problem will recur

  10. zhugh2333 commented on Jun 3, 2025

    @zhugh2333
    Author
  11. dosubot commented on Jun 3, 2025

    @dosubot

    The high CPU usage and empty GPU load with Ollama models in RAGFlow after updating to 0.19.0 is likely due to how GPU resources are managed in newer versions. The RAGFlow codebase itself does not control whether Ollama uses the CPU or GPU; it delegates all hardware decisions to the Ollama backend via its API. This means that if Ollama is not configured or able to use the GPU, all inference will run on the CPU, causing the symptoms you describe.

    One known cause is the context size (num_ctx parameter). If this is set too high (for example, 32768), Ollama may run into GPU resource constraints and fall back to CPU, especially in newer versions after v0.17.2. Reducing or removing the num_ctx parameter can resolve this and allow the GPU to be used again. This issue is documented in recent discussions and is specific to newer versions of RAGFlow and Ollama models (source).

    Other performance tips include disabling the reranker feature, turning off "Use RAPTOR to enhance retrieval," and disabling "Auto-keyword" and "Auto-question" features, as these can all increase CPU load if left enabled (source).

    If you have already set environment variables like OLLAMA_NUM_GPU=999 and the problem persists, double-check the Ollama backend logs and configuration. The RAGFlow client code does not enforce or log the backend device status, so any device selection issues must be resolved in the Ollama server environment.

    If you continue to see high CPU usage and no GPU load, try lowering or commenting out the num_ctx parameter in your Ollama model configuration and restart the Ollama service. This has resolved similar issues for other users in recent versions (source).

    Let me know if you need more specific troubleshooting steps or have questions about your configuration.

    To reply, just mention @dosu.


    Docs are dead. Just use Dosu.

    Leave Feedback Ask Dosu about ragflow Share Dosu with your team

  12. zhugh2333 commented on Jun 3, 2025

    @zhugh2333
    Author

    class OllamaChat(Base):
    def init(self, key, model_name, **kwargs):
    super().init(key, model_name, base_url=None)

        self.client = Client(host=kwargs["base_url"]) if not key or key == "x" else Client(host=kwargs["base_url"], headers={"Authorization": f"Bearer {key}"})
        self.model_name = model_name
    
    def chat(self, system, history, gen_conf):
        if system:
            history.insert(0, {"role": "system", "content": system})
        if "max_tokens" in gen_conf:
            del gen_conf["max_tokens"]
        try:
            # Calculate context size
            ctx_size = self._calculate_dynamic_ctx(history)
    

    Now the code is calculated dynamically. How can I reduce num_ctx @dosubot

  13. dosubot commented on Jun 3, 2025

    @dosubot

    The context size (num_ctx) in your OllamaChat class is determined by the _calculate_dynamic_ctx method, which sums up the estimated token count for all messages in the history, applies a 1.2x buffer, and then rounds up to the nearest multiple of 8192 tokens. This logic is hardcoded and not configurable via environment variables or runtime parameters. To reduce num_ctx, you have two main options:

    1. Reduce the message history size or content length: Fewer or shorter messages in history will directly lower the calculated context size.
    2. Modify the source code: You can edit the _calculate_dynamic_ctx method to use a smaller buffer ratio (for example, change 1.2 to 1.0), or set a lower minimum block size (change 8192 to a smaller value), or even cap the maximum context size. For example:
    # In rag/llm/chat_model.py
    def _calculate_dynamic_ctx(self, history):
        ...
        # Apply a smaller buffer ratio
        total_tokens_with_buffer = int(total_tokens * 1.0)
        # Lower the minimum context size
        if total_tokens_with_buffer <= 4096:
            ctx_size = 4096
        else:
            ctx_multiplier = (total_tokens_with_buffer // 4096) + 1
            ctx_size = ctx_multiplier * 4096
        # Optionally, cap the maximum context size
        ctx_size = min(ctx_size, 8192)
        return ctx_size

    Reference

    Reducing num_ctx can help prevent high CPU usage and force Ollama to use the GPU, especially if your hardware has limited GPU memory. This approach has resolved similar issues for other users when large context sizes caused Ollama to fall back to CPU-only inference Reference.

    For further performance improvements, consider disabling features like the reranker, RAPTOR retrieval, auto-keyword, and auto-question, as these can also increase CPU load Reference.

    To reply, just mention @dosu.


    Share context across your team and agents. Try Dosu.

    Leave Feedback Learn about ragflow with Dosu Add Dosu to your team

  14. zhugh2333 commented on Jun 3, 2025

    @zhugh2333
    Author

    Now the local environment is using a docker image. How should it be modified. @dosubot

  15. dosubot commented on Jun 3, 2025

    @dosubot

    To reduce the context size (num_ctx) in your Docker-based RAGFlow environment, you need to modify the _calculate_dynamic_ctx method in the Python source code, then rebuild and redeploy your Docker image. Here’s how you can do it:

    1. Edit the file rag/llm/chat_model.py in your local copy of the RAGFlow source. Locate the _calculate_dynamic_ctx method in the OllamaChat class. Adjust the buffer ratio, block size, or cap the maximum context size. For example, you might change:
    # Original logic
    total_tokens_with_buffer = int(total_tokens * 1.2)
    if total_tokens_with_buffer <= 8192:
        ctx_size = 8192
    else:
        ctx_multiplier = (total_tokens_with_buffer // 8192) + 1
        ctx_size = ctx_multiplier * 8192

    to something like:

    total_tokens_with_buffer = int(total_tokens * 1.0)
    if total_tokens_with_buffer <= 4096:
        ctx_size = 4096
    else:
        ctx_multiplier = (total_tokens_with_buffer // 4096) + 1
        ctx_size = ctx_multiplier * 4096
    ctx_size = min(ctx_size, 8192)  # Optional cap

    This will reduce the context size and help avoid high CPU usage and CPU fallback when GPU memory is limited (reference).

    1. Save your changes.

    2. Rebuild your Docker image. In your project root, run:

    docker build -t your-custom-ragflow .

    or, if you use Docker Compose:

    docker-compose build

    This ensures your code changes are included in the new image (reference).

    1. Restart your container using the new image:
    docker-compose up -d

    or with plain Docker:

    docker run ... your-custom-ragflow

    No Docker-specific environment variables are needed for this change; the adjustment is purely in the Python code. After redeploying, the new context size logic will take effect for all OllamaChat operations in your container.

    If you use a prebuilt image and cannot rebuild, you would need to mount the modified Python file into the container, but rebuilding is the recommended and cleanest approach.

    To reply, just mention @dosu.


    Share context across your team and agents. Try Dosu.

    Leave Feedback Learn about ragflow with Dosu Add Dosu to your team

  16. RNGMARTIN commented on Jun 3, 2025

    @RNGMARTIN

    It should be a bug, perhaps will be fixed. @KevinHuSh

  17. zhugh2333 commented on Jun 3, 2025

    @zhugh2333
    Author

    @yingfeng This problem might be a bug, Looking forward to your reply to this question. Thank you.

  18. KevinHuSh commented on Jun 3, 2025

    @KevinHuSh
    Contributor

    What about running Ollama with OLLAMA_KEEP_ALIVE=-1 ?

  19. zhugh2333 commented on Jun 3, 2025

    @zhugh2333
    Author

    ok, I'll have a try. thanks.

  20. konn-submarine-bu commented on Jun 3, 2025

    @konn-submarine-bu

    i handled my problem after setting OLLAMA_NUM_GPU=1 and updating ollama to latest version

  21. RNGMARTIN commented on Jun 3, 2025

    @RNGMARTIN

    What about running Ollama with OLLAMA_KEEP_ALIVE=-1 ?

    Mine cannot set this because the space for GPU is limited and I have other use for other software which have to free the model of ollama, is there any other plan? @KevinHuSh

  22. RNGMARTIN commented on Jun 3, 2025

    @RNGMARTIN

    i handled my problem after setting OLLAMA_NUM_GPU=1 and updating ollama to latest version

    my ollama version is 0.9.0. I will try upgrading ragflow again by setting this.

  23. zhugh2333 commented on Jun 3, 2025

    @zhugh2333
    Author

    The latest ollama has just been updated. The displayed version number is 0.9.0. I will test OLLAMA_NUM_GPU=1

  24. RNGMARTIN commented on Jun 3, 2025

    @RNGMARTIN

    i handled my problem after setting OLLAMA_NUM_GPU=1 and updating ollama to latest version

    I tried. It doesn't work.

  25. RNGMARTIN commented on Jun 3, 2025

    @RNGMARTIN

    The latest ollama has just been updated. The displayed version number is 0.9.0. I will test OLLAMA_NUM_GPU=1

    Have you tried this plan? Mine didn't work.

  26. konn-submarine-bu commented on Jun 3, 2025

    @konn-submarine-bu

    To be honest, i even dont know how it works etheir, this problem appeared when i updated ollama to 0.7.0 and suddenly disappeared... maybe you can check ollama issues webpage and find something useful

  27. RNGMARTIN commented on Jun 3, 2025

    @RNGMARTIN

    To be honest, i even dont know how it works etheir, this problem appeared when i updated ollama to 0.7.0 and suddenly disappeared... maybe you can check ollama issues webpage and find something useful

    my ollama now is 0.9.0 and there is no such problem for ragflow 0.17. When upgrading to 0.19, the problem appears. I have tried twice.
    @KevinHuSh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐞 bugSomething isn't working, pull request that fix bug.🙋‍♀️ questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions