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: Add error handling for memory buffer allocation in ggml_init to prevent segmentation fault #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ahmetkca
Copy link

Problem

When loading a large language model like Alpaca 30B, the ggml_init function tries to allocate a significant amount of memory (25.5 GB). Even if the system has sufficient RAM (e.g., 32 GB), the available memory might not be enough due to other processes or system requirements. In the current implementation, we blindly try to allocate the required memory without checking if the malloc operation was successful or failed. If the memory allocation fails, it can result in segmentation faults and undefined behavior.

I encountered this issue when trying to load the Alpaca 30B model and experienced a segmentation fault. To address this problem, we should check the return value of malloc and handle memory allocation failures properly.

Changes

  • Added a check for memory buffer allocation failure in ggml_init.
  • If the memory buffer allocation fails, an error message is printed to stderr, and the function returns NULL.
  • The critical section is ended before returning NULL to prevent any potential deadlocks.

Motivation

By checking the return value of malloc and not creating the ggml_context when memory allocation fails, we can provide better error handling and prevent segmentation faults. This approach will also make it easier for users to identify the cause of the problem when there isn't enough available memory for the required allocation.

Request for Feedback

I would appreciate feedback on the error handling implementation, the additional context provided, and any suggestions for improvements.

- Check if mem_buffer is NULL
- Print error message to stderr
- Print debug message
- End critical section
- Return NULL if memory buffer allocation fails
@trevtravtrev
Copy link

Seems very nice. Suddenly I haven't been able to run my 30B alpaca model that was previously running fine. I haven't touched or updated a single thing. Crashing while loading the model with no errors. I wonder if this would fix?

@@ -2447,6 +2447,16 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
/*.scratch_save =*/ { 0, 0, NULL, },
};

if (ctx->mem_buffer == NULL) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a GGML_ASSERT macro which can be used.

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.

3 participants