Skip to content

Commit

Permalink
Merge branch 'SciSharp:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
SignalRT authored Aug 16, 2023
2 parents 972f106 + eab073b commit 22ed4c3
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 23 deletions.
65 changes: 49 additions & 16 deletions LLama/Native/LLamaContextParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public struct LLamaContextParams
/// <summary>
/// rms norm epsilon (TEMP - will be moved to model hparams)
/// </summary>
public float rms_norm_eps;
public float rms_norm_eps;

/// <summary>
/// number of layers to store in VRAM
Expand Down Expand Up @@ -76,49 +76,82 @@ public struct LLamaContextParams
/// <summary>
/// if true, reduce VRAM usage at the cost of performance
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool low_vram;
public bool low_vram
{
get => Utils.SignedByteToBool(_low_vram);
set => _low_vram = Utils.BoolToSignedByte(value);
}
private sbyte _low_vram;

/// <summary>
/// if true, use experimental mul_mat_q kernels
/// </summary>
[MarshalAs(UnmanagedType.I1)] public bool mul_mat_q;
public bool mul_mat_q
{
get => Utils.SignedByteToBool(_mul_mat_q);
set => _mul_mat_q = Utils.BoolToSignedByte(value);
}
private sbyte _mul_mat_q;

/// <summary>
/// use fp16 for KV cache
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool f16_kv;
public bool f16_kv
{
get => Utils.SignedByteToBool(_f16_kv);
set => _f16_kv = Utils.BoolToSignedByte(value);
}
private sbyte _f16_kv;

/// <summary>
/// the llama_eval() call computes all logits, not just the last one
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool logits_all;
public bool logits_all
{
get => Utils.SignedByteToBool(_logits_all);
set => _logits_all = Utils.BoolToSignedByte(value);
}
private sbyte _logits_all;

/// <summary>
/// only load the vocabulary, no weights
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool vocab_only;
public bool vocab_only
{
get => Utils.SignedByteToBool(_vocab_only);
set => _vocab_only = Utils.BoolToSignedByte(value);
}
private sbyte _vocab_only;

/// <summary>
/// use mmap if possible
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool use_mmap;
public bool use_mmap
{
get => Utils.SignedByteToBool(_use_mmap);
set => _use_mmap = Utils.BoolToSignedByte(value);
}
private sbyte _use_mmap;

/// <summary>
/// force system to keep model in RAM
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool use_mlock;
public bool use_mlock
{
get => Utils.SignedByteToBool(_use_mlock);
set => _use_mlock = Utils.BoolToSignedByte(value);
}
private sbyte _use_mlock;

/// <summary>
/// embedding mode only
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool embedding;
public bool embedding
{
get => Utils.SignedByteToBool(_embedding);
set => _embedding = Utils.BoolToSignedByte(value);
}
private sbyte _embedding;
}
}

16 changes: 12 additions & 4 deletions LLama/Native/LLamaModelQuantizeParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ public struct LLamaModelQuantizeParams
/// <summary>
/// allow quantizing non-f32/f16 tensors
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool allow_requantize;
public bool allow_requantize
{
get => Utils.SignedByteToBool(_allow_requantize);
set => _allow_requantize = Utils.BoolToSignedByte(value);
}
private sbyte _allow_requantize;

/// <summary>
/// quantize output.weight
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool quantize_output_tensor;
public bool quantize_output_tensor
{
get => Utils.SignedByteToBool(_quantize_output_tensor);
set => _quantize_output_tensor = Utils.BoolToSignedByte(value);
}
private sbyte _quantize_output_tensor;
}
}
8 changes: 6 additions & 2 deletions LLama/Native/LLamaTokenDataArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ public struct LLamaTokenDataArrayNative
/// <summary>
/// Indicates if the items in the array are sorted
/// </summary>
[MarshalAs(UnmanagedType.I1)]
public bool sorted;
public bool sorted
{
get => Utils.SignedByteToBool(_sorted);
set => _sorted = Utils.BoolToSignedByte(value);
}
private sbyte _sorted;

/// <summary>
/// Create a new LLamaTokenDataArrayNative around the data in the LLamaTokenDataArray
Expand Down
1 change: 1 addition & 0 deletions LLama/OldVersion/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,6 @@ public static unsafe string PtrToStringUTF8(IntPtr ptr)
return Encoding.UTF8.GetString(bytes.ToArray());
#endif
}

}
}
21 changes: 21 additions & 0 deletions LLama/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,26 @@ public static string PtrToString(IntPtr ptr, Encoding encoding)
}
#endif
}

/// <summary>
/// Converts a bool "value" to a signed byte of "1" for true and "0" for false to be compatible with a 1 byte C-style bool.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static sbyte BoolToSignedByte(bool value)
{
return value ? (sbyte)1 : (sbyte)0;
}

/// <summary>
/// Converts a sbyte "value" to a C# bool.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool SignedByteToBool(sbyte value)
{
return value > 0 ? true : false;
}

}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ LLamaSharp provides two ways to run inference: `LLamaExecutor` and `ChatSession`
using LLama.Common;
using LLama;

string modelPath = "<Your model path>" // change it to your own model path
string modelPath = "<Your model path>"; // change it to your own model path
var prompt = "Transcript of a dialog, where the User interacts with an Assistant named Bob. Bob is helpful, kind, honest, good at writing, and never fails to answer the User's requests immediately and with precision.\r\n\r\nUser: Hello, Bob.\r\nBob: Hello. How may I help you today?\r\nUser: Please tell me the largest city in Europe.\r\nBob: Sure. The largest city in Europe is Moscow, the capital of Russia.\r\nUser:"; // use the "chat-with-bob" prompt here.
// Initialize a chat session
Expand Down

0 comments on commit 22ed4c3

Please sign in to comment.