Skip to content

Use aligned_alloc() if aligned allocations are required #880

Closed
@Rhialto

Description

@Rhialto

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • I am running the latest code. Development is very rapid so there are no tagged versions as of now.
  • I carefully followed the README.md.
  • I searched using keywords relevant to my issue to make sure that I am creating a new issue that is not already open (or closed).
  • I reviewed the Discussions, and have a new bug or useful enhancement to share.

Expected Behavior

quantize should run and not abort with an assertion failure

Current Behavior

GGML_ASSERT: ggml.c:2990: ((uintptr_t) (ctx->mem_buffer))%GGML_MEM_ALIGN == 0
Abort trap (core dumped)

Environment and Context

  • Operating System

NetBSD 9.3

Failure Information (for bugs)

The memory allocated by malloc is alingned to 8, not 16 (GGML_MEM_ALIGN)

Fix:

diff --git a/ggml.c b/ggml.c
index 326b8e8..efa3f15 100644
--- a/ggml.c
+++ b/ggml.c
@@ -2974,7 +2974,7 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
 
     *ctx = (struct ggml_context) {
         /*.mem_size           =*/ params.mem_size,
-        /*.mem_buffer         =*/ params.mem_buffer ? params.mem_buffer : malloc(params.mem_size),
+        /*.mem_buffer         =*/ params.mem_buffer ? params.mem_buffer : aligned_alloc(GGML_MEM_ALIGN , params.mem_size),
         /*.mem_buffer_owned   =*/ params.mem_buffer ? false : true,
         /*.no_alloc           =*/ params.no_alloc,
         /*.n_objects          =*/ 0,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions