Skip to content

Commit

Permalink
Merge pull request SciSharp#920 from martindevans/fix_llama_get_logit…
Browse files Browse the repository at this point in the history
…s_ith_null

Fix `llama_get_logits_ith` Null Handling
  • Loading branch information
martindevans authored Sep 4, 2024
2 parents 26a7114 + 846d2dc commit 0db26a4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
21 changes: 20 additions & 1 deletion LLama/Exceptions/RuntimeError.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using LLama.Native;

namespace LLama.Exceptions;
Expand Down Expand Up @@ -56,4 +56,23 @@ public LLamaDecodeError(DecodeResult returnCode)
{
ReturnCode = returnCode;
}
}

/// <summary>
/// `llama_get_logits_ith` returned null, indicating that the index was invalid
/// </summary>
public class GetLogitsInvalidIndexException
: RuntimeError
{
/// <summary>
/// The incorrect index passed to the `llama_get_logits_ith` call
/// </summary>
public int Index { get; }

/// <inheritdoc />
public GetLogitsInvalidIndexException(int index)
: base($"llama_get_logits_ith({index}) returned null")
{
Index = index;
}
}
3 changes: 3 additions & 0 deletions LLama/Native/SafeLLamaContextHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,9 @@ public Span<float> GetLogitsIth(int i)
unsafe
{
var logits = llama_get_logits_ith(this, i);
if (logits == null)
throw new GetLogitsInvalidIndexException(i);

return new Span<float>(logits, model.VocabCount);
}
}
Expand Down

0 comments on commit 0db26a4

Please sign in to comment.