forked from SciSharp/LLamaSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Totally rewritten the LLamaEmbedder based on https://github.com/ggerg…
…anov/llama.cpp/tree/master/examples/embedding. New embedder properly handles pooling, either returning one embedding for the whole sequence or one per token. - Added `Encode` methods to `LLamaContext` - Moved some native methods from `NativeApi` to `SafeLLamaContextHandle` and wrapped them properly - Added `HasDecoder` property to `SafeLlamaModelHandle`. This function doesn't exist in the current version of llama.cpp, will need to be hooked up in the next binary update - Added some normalization methods as extensions on span/array. This required adding a dependency on `System.Numerics.Tensors`
- Loading branch information
1 parent
df8cc71
commit 4f3f0ed
Showing
12 changed files
with
408 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
using System; | ||
using System.Numerics.Tensors; | ||
|
||
namespace LLama.Extensions; | ||
|
||
/// <summary> | ||
/// Extensions to span which apply <b>in-place</b> normalization | ||
/// </summary> | ||
public static class SpanNormalizationExtensions | ||
{ | ||
/// <summary> | ||
/// <b>In-place</b> multiple every element by 32760 and divide every element in the span by the max absolute value in the span | ||
/// </summary> | ||
/// <param name="vector"></param> | ||
/// <returns>The same array</returns> | ||
public static float[] MaxAbsoluteNormalization(this float[] vector) | ||
{ | ||
vector.AsSpan().MaxAbsoluteNormalization(); | ||
return vector; | ||
} | ||
|
||
/// <summary> | ||
/// <b>In-place</b> multiple every element by 32760 and divide every element in the span by the max absolute value in the span | ||
/// </summary> | ||
/// <param name="vector"></param> | ||
/// <returns>The same span</returns> | ||
public static Span<float> MaxAbsoluteNormalization(this Span<float> vector) | ||
{ | ||
var factor = 32760 / TensorPrimitives.MaxMagnitude(vector); | ||
TensorPrimitives.Multiply(vector, factor, vector); | ||
return vector; | ||
} | ||
|
||
/// <summary> | ||
/// <b>In-place</b> divide every element in the array by the sum of absolute values in the array | ||
/// </summary> | ||
/// <remarks>Also known as "Manhattan normalization".</remarks> | ||
/// <param name="vector"></param> | ||
/// <returns>The same array</returns> | ||
public static float[] TaxicabNormalization(this float[] vector) | ||
{ | ||
vector.AsSpan().TaxicabNormalization(); | ||
return vector; | ||
} | ||
|
||
/// <summary> | ||
/// <b>In-place</b> divide every element in the span by the sum of absolute values in the span | ||
/// </summary> | ||
/// <remarks>Also known as "Manhattan normalization".</remarks> | ||
/// <param name="vector"></param> | ||
/// <returns>The same span</returns> | ||
public static Span<float> TaxicabNormalization(this Span<float> vector) | ||
{ | ||
var sumAbs = TensorPrimitives.SumOfMagnitudes(vector); | ||
TensorPrimitives.Divide(vector, sumAbs, vector); | ||
return vector; | ||
} | ||
|
||
/// <summary> | ||
/// <b>In-place</b> divide every element by the euclidean length of the vector | ||
/// </summary> | ||
/// <remarks>Also known as "L2 normalization".</remarks> | ||
/// <param name="vector"></param> | ||
/// <returns>The same array</returns> | ||
public static float[] EuclideanNormalization(this float[] vector) | ||
{ | ||
vector.AsSpan().EuclideanNormalization(); | ||
return vector; | ||
} | ||
|
||
/// <summary> | ||
/// <b>In-place</b> divide every element by the euclidean length of the vector | ||
/// </summary> | ||
/// <remarks>Also known as "L2 normalization".</remarks> | ||
/// <param name="vector"></param> | ||
/// <returns>The same span</returns> | ||
public static Span<float> EuclideanNormalization(this Span<float> vector) | ||
{ | ||
var norm = TensorPrimitives.Norm(vector); | ||
TensorPrimitives.Divide(vector, norm, vector); | ||
return vector; | ||
} | ||
|
||
/// <summary> | ||
/// <b>In-place</b> apply p-normalization. https://en.wikipedia.org/wiki/Norm_(mathematics)#p-norm | ||
/// <list type="bullet"> | ||
/// <item>For p = 1, this is taxicab normalization</item> | ||
/// <item>For p = 2, this is euclidean normalization</item> | ||
/// <item>As p => infinity, this approaches infinity norm or maximum norm</item> | ||
/// </list> | ||
/// </summary> | ||
/// <param name="vector"></param> | ||
/// <param name="p"></param> | ||
/// <returns>The same array</returns> | ||
public static float[] PNormalization(this float[] vector, int p) | ||
{ | ||
vector.AsSpan().PNormalization(p); | ||
return vector; | ||
} | ||
|
||
/// <summary> | ||
/// <b>In-place</b> apply p-normalization. https://en.wikipedia.org/wiki/Norm_(mathematics)#p-norm | ||
/// <list type="bullet"> | ||
/// <item>For p = 1, this is taxicab normalization</item> | ||
/// <item>For p = 2, this is euclidean normalization</item> | ||
/// <item>As p => infinity, this approaches infinity norm or maximum norm</item> | ||
/// </list> | ||
/// </summary> | ||
/// <param name="vector"></param> | ||
/// <param name="p"></param> | ||
/// <returns>The same span</returns> | ||
public static Span<float> PNormalization(this Span<float> vector, int p) | ||
{ | ||
if (p == 2) | ||
return vector.EuclideanNormalization(); | ||
|
||
var sum = 0.0; | ||
for (var i = 0; i < vector.Length; i++) | ||
sum += MathF.Pow(vector[i], p); | ||
var divisor = (float)Math.Pow(sum, 1.0 / p); | ||
|
||
TensorPrimitives.Divide(vector, divisor, vector); | ||
|
||
return vector; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.