Fix: Add error handling for memory buffer allocation in ggml_init to prevent segmentation fault #142
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 themalloc
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
ggml_init
.Motivation
By checking the return value of
malloc
and not creating theggml_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.