Skip to content

metal : try to utilize more of the shared memory using smaller views - #2011

Closed
ggerganov wants to merge 1 commit into
masterfrom
try-fix-metal
Closed

ggerganov wants to merge 1 commit into
masterfrom
try-fix-metal

Conversation

@ggerganov

Copy link
Copy Markdown
Member

Not sure if this helps

cc @CyborgArmy83 please give it a try and see if you can load more models with this patch

@gianpaj

This comment was marked as off-topic.

@EliasOenal

Copy link
Copy Markdown

@ggerganov This sadly does not seem to allow using more than recommendedMaxWorkingSetSize. Tested on M1 Max with 32GB RAM. I set the ctx size to 2700 to trigger the limit, 2600 was still below and worked fine.

ggml_metal_init: recommendedMaxWorkingSetSize = 21845.34 MB
ggml_metal_init: hasUnifiedMemory             = true
ggml_metal_init: maxTransferRate              = built-in GPU
llama_new_context_with_model: max tensor size =   166.63 MB
ggml_metal_add_buffer: allocated 'data            ' buffer, size =  4096.00 MB, offs =            0
ggml_metal_add_buffer: allocated 'data            ' buffer, size =  4096.00 MB, offs =   4120215552
ggml_metal_add_buffer: allocated 'data            ' buffer, size =  4096.00 MB, offs =   8240431104
ggml_metal_add_buffer: allocated 'data            ' buffer, size =  3204.09 MB, offs =  12360646656, (15492.48 / 21845.34)
ggml_metal_add_buffer: allocated 'eval            ' buffer, size =  1280.00 MB, (16772.48 / 21845.34)
ggml_metal_add_buffer: allocated 'kv              ' buffer, size =  4096.00 MB, offs =            0
ggml_metal_add_buffer: allocated 'kv              ' buffer, size =    19.30 MB, offs =   4294950912, (20887.78 / 21845.34)
ggml_metal_add_buffer: allocated 'scr0            ' buffer, size =   512.00 MB, (21399.78 / 21845.34)
ggml_metal_add_buffer: allocated 'scr1            ' buffer, size =   512.00 MB, (21911.78 / 21845.34), warning: current allocated size is greater than the recommended max working set size

system_info: n_threads = 8 / 10 | AVX = 0 | AVX2 = 0 | AVX512 = 0 | AVX512_VBMI = 0 | AVX512_VNNI = 0 | FMA = 0 | NEON = 1 | ARM_FMA = 1 | F16C = 0 | FP16_VA = 1 | WASM_SIMD = 0 | BLAS = 1 | SSE3 = 0 | VSX = 0 | 
sampling: repeat_last_n = 64, repeat_penalty = 1.100000, presence_penalty = 0.000000, frequency_penalty = 0.000000, top_k = 40, tfs_z = 1.000000, top_p = 0.950000, typical_p = 1.000000, temp = 0.800000, mirostat = 0, mirostat_lr = 0.100000, mirostat_ent = 5.000000
generate: n_ctx = 2700, n_batch = 512, n_predict = -1, n_keep = 0


ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil

@linghunjiushu

Copy link
Copy Markdown

@ggerganov This sadly does not seem to allow using more than recommendedMaxWorkingSetSize. Tested on M1 Max with 32GB RAM. I set the ctx size to 2700 to trigger the limit, 2600 was still below and worked fine.

ggml_metal_init: recommendedMaxWorkingSetSize = 21845.34 MB
ggml_metal_init: hasUnifiedMemory             = true
ggml_metal_init: maxTransferRate              = built-in GPU
llama_new_context_with_model: max tensor size =   166.63 MB
ggml_metal_add_buffer: allocated 'data            ' buffer, size =  4096.00 MB, offs =            0
ggml_metal_add_buffer: allocated 'data            ' buffer, size =  4096.00 MB, offs =   4120215552
ggml_metal_add_buffer: allocated 'data            ' buffer, size =  4096.00 MB, offs =   8240431104
ggml_metal_add_buffer: allocated 'data            ' buffer, size =  3204.09 MB, offs =  12360646656, (15492.48 / 21845.34)
ggml_metal_add_buffer: allocated 'eval            ' buffer, size =  1280.00 MB, (16772.48 / 21845.34)
ggml_metal_add_buffer: allocated 'kv              ' buffer, size =  4096.00 MB, offs =            0
ggml_metal_add_buffer: allocated 'kv              ' buffer, size =    19.30 MB, offs =   4294950912, (20887.78 / 21845.34)
ggml_metal_add_buffer: allocated 'scr0            ' buffer, size =   512.00 MB, (21399.78 / 21845.34)
ggml_metal_add_buffer: allocated 'scr1            ' buffer, size =   512.00 MB, (21911.78 / 21845.34), warning: current allocated size is greater than the recommended max working set size

system_info: n_threads = 8 / 10 | AVX = 0 | AVX2 = 0 | AVX512 = 0 | AVX512_VBMI = 0 | AVX512_VNNI = 0 | FMA = 0 | NEON = 1 | ARM_FMA = 1 | F16C = 0 | FP16_VA = 1 | WASM_SIMD = 0 | BLAS = 1 | SSE3 = 0 | VSX = 0 | 
sampling: repeat_last_n = 64, repeat_penalty = 1.100000, presence_penalty = 0.000000, frequency_penalty = 0.000000, top_k = 40, tfs_z = 1.000000, top_p = 0.950000, typical_p = 1.000000, temp = 0.800000, mirostat = 0, mirostat_lr = 0.100000, mirostat_ent = 5.000000
generate: n_ctx = 2700, n_batch = 512, n_predict = -1, n_keep = 0


ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil
ggml_metal_get_buffer: error: buffer is nil

Thanks for your method.

I met same problem " failed with status 5", and I change n_ctx from 4096 to 2000, then it works fine.

my env : apple m2 pro 16gb
model : llama-2-13b-chat.ggmlv3.q4_0.bin

@RDearnaley

RDearnaley commented Sep 10, 2023 •

Copy link
Copy Markdown

What would be nice was if reducing --gpulayers fixed the problem, so that only the layers that Metal was actually running counted towards the recommendedMaxWorkingSetSize. I have an M2 MacBook Pro with 64GB, recommendedMaxWorkingSetSize = 49152.00 MB, so I can run LLama2 70B 6-bit quantized on CPU (rather slow), but not on GPU — I guess I'll need to redownload one of the 5-bit quantized versions, rather than just shifting a few layers to CPU threads.

Also, it would be nice if hitting this situation gave a more comprehensible error message than:

GGML_ASSERT: ggml-metal.m:1221: false

without the end user having to first fritz with the code to get GGML Metal debug logging turned back on and then decypher the meaning of the rather opaque:

ggml_metal_graph_compute: command buffer 4 failed with status 5

Perhaps something like "Current allocated size is greater than the recommended max working set size: Metal ran out of memory for running your model on GPU (try a smaller context length or smaller model)"

@RDearnaley

RDearnaley commented Sep 10, 2023 •

Copy link
Copy Markdown

According to https://developer.apple.com/documentation/metal/mtlcommandbufferstatus/error status 5 is a generic 'an error occurred during your command' status, and application code should be checking the error property for more details. So we should do that before emitting a diagnostic message like I suggested above.

Also, from what I'm reading, recommendedMaxWorkingSetSize is an approximate, soft, recommended limit for an application to play nice with Metal, and the

(55454.44 / 49152.00), warning: current allocated size is greater than the recommended max working set size

messages that we're emitting are warnings, not errors, so I'm surprised we're getting a hard refusal for exceeding this. But when I checked the error details, I do seem to be getting a actual outOfMemory error code from Metal, so perhaps we kept going past the warnings until we hit an actual out-of-memory error.

Anyway, I implemented code to check the error status and emit a diagnostic message if Metal's out of memory, see:

LostRuins#429

I'm not familiar with Objective-C so it may be hacky, but the basic outline is there. This should probably be propagated back into llama.cpp, but I'm leaving that as an excise for someone else.

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.

5 participants