Browse Source

Potential fix for .Net Framework issues (#103)

* Added a bool to sbyte Utils convertor

As an attempt to avoid using any MarshalAs attribute for .Net Framework support this Utils method will take in a bool value and return a 1 for true or 0 for false sbyte.

* Changed all bool "MarshalAs" types to sbytes

Changed all previous BOOL types with "MarshalAs" attributes to SBYTEs and changed all the setters of them to use the Utils.BoolToSignedByte() convertor method.

* Fixed Utils bool convertor & added sbyte to bool

Improved the Utils bool convertor just casting an sbyte value to get rid of the unneeded sbyte array and added an sbyte to bool convertor to convert back the way to a C# bool assuming any positive value above 0 is a bool and no bools are packed in the single byte integer.

* bool to & from sbyte conversions via properties

All 1byte bools are now handled where they "sit", via public properties which perform the conversions to keep all external data able to communicate as it did before.
tags/v0.5.1
zombieguy GitHub 2 years ago
parent
commit
10f88ebd0e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 89 additions and 22 deletions
  1. +49
    -16
      LLama/Native/LLamaContextParams.cs
  2. +12
    -4
      LLama/Native/LLamaModelQuantizeParams.cs
  3. +6
    -2
      LLama/Native/LLamaTokenDataArray.cs
  4. +1
    -0
      LLama/OldVersion/Utils.cs
  5. +21
    -0
      LLama/Utils.cs

+ 49
- 16
LLama/Native/LLamaContextParams.cs View File

@@ -32,7 +32,7 @@ namespace LLama.Native
/// <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
@@ -76,49 +76,82 @@ namespace LLama.Native
/// <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;
}
}


+ 12
- 4
LLama/Native/LLamaModelQuantizeParams.cs View File

@@ -20,13 +20,21 @@ namespace LLama.Native
/// <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;
}
}

+ 6
- 2
LLama/Native/LLamaTokenDataArray.cs View File

@@ -51,8 +51,12 @@ namespace LLama.Native
/// <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


+ 1
- 0
LLama/OldVersion/Utils.cs View File

@@ -82,5 +82,6 @@ namespace LLama.OldVersion
return Encoding.UTF8.GetString(bytes.ToArray());
#endif
}

}
}

+ 21
- 0
LLama/Utils.cs View File

@@ -90,5 +90,26 @@ namespace LLama
}
#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;
}

}
}

Loading…
Cancel
Save