From 45b01d5a785473502df7dd546a3ca5ce3359d1da Mon Sep 17 00:00:00 2001 From: zombieguy <11479875+zombieguy98@users.noreply.github.com> Date: Wed, 23 Aug 2023 19:36:14 +0100 Subject: [PATCH] Improved type conversion Type conversion is now done in the property rather than the utils class and uses the System.Convert class to ensure consistency. --- LLama/Native/LLamaContextParams.cs | 32 ++++++++++++------------ LLama/Native/LLamaModelQuantizeParams.cs | 12 +++++---- LLama/Native/LLamaTokenDataArray.cs | 4 +-- LLama/Utils.cs | 22 +--------------- 4 files changed, 26 insertions(+), 44 deletions(-) diff --git a/LLama/Native/LLamaContextParams.cs b/LLama/Native/LLamaContextParams.cs index 8747dba4..4e8a02cf 100644 --- a/LLama/Native/LLamaContextParams.cs +++ b/LLama/Native/LLamaContextParams.cs @@ -77,8 +77,8 @@ namespace LLama.Native /// public bool low_vram { - get => Utils.SignedByteToBool(_low_vram); - set => _low_vram = Utils.BoolToSignedByte(value); + get => Convert.ToBoolean(_low_vram); + set => _low_vram = Convert.ToSByte(value); } private sbyte _low_vram; @@ -87,8 +87,8 @@ namespace LLama.Native /// public bool mul_mat_q { - get => Utils.SignedByteToBool(_mul_mat_q); - set => _mul_mat_q = Utils.BoolToSignedByte(value); + get => Convert.ToBoolean(_mul_mat_q); + set => _mul_mat_q = Convert.ToSByte(value); } private sbyte _mul_mat_q; @@ -97,8 +97,8 @@ namespace LLama.Native /// public bool f16_kv { - get => Utils.SignedByteToBool(_f16_kv); - set => _f16_kv = Utils.BoolToSignedByte(value); + get => Convert.ToBoolean(_f16_kv); + set => _f16_kv = Convert.ToSByte(value); } private sbyte _f16_kv; @@ -107,8 +107,8 @@ namespace LLama.Native /// public bool logits_all { - get => Utils.SignedByteToBool(_logits_all); - set => _logits_all = Utils.BoolToSignedByte(value); + get => Convert.ToBoolean(_logits_all); + set => _logits_all = Convert.ToSByte(value); } private sbyte _logits_all; @@ -117,8 +117,8 @@ namespace LLama.Native /// public bool vocab_only { - get => Utils.SignedByteToBool(_vocab_only); - set => _vocab_only = Utils.BoolToSignedByte(value); + get => Convert.ToBoolean(_vocab_only); + set => _vocab_only = Convert.ToSByte(value); } private sbyte _vocab_only; @@ -127,8 +127,8 @@ namespace LLama.Native /// public bool use_mmap { - get => Utils.SignedByteToBool(_use_mmap); - set => _use_mmap = Utils.BoolToSignedByte(value); + get => Convert.ToBoolean(_use_mmap); + set => _use_mmap = Convert.ToSByte(value); } private sbyte _use_mmap; @@ -137,8 +137,8 @@ namespace LLama.Native /// public bool use_mlock { - get => Utils.SignedByteToBool(_use_mlock); - set => _use_mlock = Utils.BoolToSignedByte(value); + get => Convert.ToBoolean(_use_mlock); + set => _use_mlock = Convert.ToSByte(value); } private sbyte _use_mlock; @@ -147,8 +147,8 @@ namespace LLama.Native /// public bool embedding { - get => Utils.SignedByteToBool(_embedding); - set => _embedding = Utils.BoolToSignedByte(value); + get => Convert.ToBoolean(_embedding); + set => _embedding = Convert.ToSByte(value); } private sbyte _embedding; } diff --git a/LLama/Native/LLamaModelQuantizeParams.cs b/LLama/Native/LLamaModelQuantizeParams.cs index 3335918a..128e30aa 100644 --- a/LLama/Native/LLamaModelQuantizeParams.cs +++ b/LLama/Native/LLamaModelQuantizeParams.cs @@ -1,4 +1,6 @@ -namespace LLama.Native +using System; + +namespace LLama.Native { /// /// Quantizer parameters used in the native API @@ -20,8 +22,8 @@ /// public bool allow_requantize { - get => Utils.SignedByteToBool(_allow_requantize); - set => _allow_requantize = Utils.BoolToSignedByte(value); + get => Convert.ToBoolean(_allow_requantize); + set => _allow_requantize = Convert.ToSByte(value); } private sbyte _allow_requantize; @@ -30,8 +32,8 @@ /// public bool quantize_output_tensor { - get => Utils.SignedByteToBool(_quantize_output_tensor); - set => _quantize_output_tensor = Utils.BoolToSignedByte(value); + get => Convert.ToBoolean(_quantize_output_tensor); + set => _quantize_output_tensor = Convert.ToSByte(value); } private sbyte _quantize_output_tensor; } diff --git a/LLama/Native/LLamaTokenDataArray.cs b/LLama/Native/LLamaTokenDataArray.cs index b90235d2..add3b703 100644 --- a/LLama/Native/LLamaTokenDataArray.cs +++ b/LLama/Native/LLamaTokenDataArray.cs @@ -53,8 +53,8 @@ namespace LLama.Native /// public bool sorted { - get => Utils.SignedByteToBool(_sorted); - set => _sorted = Utils.BoolToSignedByte(value); + get => Convert.ToBoolean(_sorted); + set => _sorted = Convert.ToSByte(value); } private sbyte _sorted; diff --git a/LLama/Utils.cs b/LLama/Utils.cs index 482e1fd1..f6bc7202 100644 --- a/LLama/Utils.cs +++ b/LLama/Utils.cs @@ -84,26 +84,6 @@ namespace LLama } #endif } - - /// - /// 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. - /// - /// - /// - public static sbyte BoolToSignedByte(bool value) - { - return value ? (sbyte)1 : (sbyte)0; - } - - /// - /// Converts a sbyte "value" to a C# bool. - /// - /// - /// - public static bool SignedByteToBool(sbyte value) - { - return value > 0; - } - + } }