Browse Source

llama_decode lock (#595)

* Added a lock object into `SafeLlamaModelHandle` which all calls to `llama_decode` (in the `SafeLLamaContextHandle`) lock first. This prevents two contexts from running inference on the same model at the same time, which seems to be unsafe in llama.cpp.

* Modified the lock to be global over _all_ inferences. This seems to be necessary (at least with the CUDA backend).
tags/0.11.0
Martin Evans GitHub 1 year ago
parent
commit
ce4de7d607
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
1 changed files with 15 additions and 2 deletions
  1. +15
    -2
      LLama/Native/SafeLLamaContextHandle.cs

+ 15
- 2
LLama/Native/SafeLLamaContextHandle.cs View File

@@ -192,6 +192,18 @@ namespace LLama.Native
#endregion

#region infer
/// <summary>
/// This object exists to ensure there is only ever 1 inference running at a time. This is a workaround for thread safety issues in llama.cpp itself.
/// Most notably CUDA, which seems to use some global singleton resources and will crash if multiple inferences are run (even against different models).
///
/// For more information see these issues:
/// - https://github.com/SciSharp/LLamaSharp/issues/596
/// - https://github.com/ggerganov/llama.cpp/issues/3960
///
/// If these are ever resolved this lock can probably be removed.
/// </summary>
private static readonly object GlobalInferenceLock = new();

/// <summary>
/// </summary>
/// <param name="batch"></param>
@@ -202,8 +214,9 @@ namespace LLama.Native
/// </returns>
public DecodeResult Decode(LLamaBatch batch)
{
using (batch.ToNativeBatch(out var nb))
return (DecodeResult)NativeApi.llama_decode(this, nb);
lock (GlobalInferenceLock)
using (batch.ToNativeBatch(out var nb))
return (DecodeResult)NativeApi.llama_decode(this, nb);
}

/// <summary>


Loading…
Cancel
Save