using System; using System.Buffers; using System.Runtime.InteropServices; using llama_token = System.Int32; namespace LLama.Native { /// /// Contains an array of LLamaTokenData, potentially sorted. /// public struct LLamaTokenDataArray { /// /// The LLamaTokenData /// public readonly Memory data; /// /// Indicates if `data` is sorted by logits in descending order. If this is false the token data is in _no particular order_. /// public bool sorted; /// /// Create a new LLamaTokenDataArray /// /// /// public LLamaTokenDataArray(Memory tokens, bool isSorted = false) { data = tokens; sorted = isSorted; } /// /// Create a new LLamaTokenDataArray, copying the data from the given logits /// /// /// public static LLamaTokenDataArray Create(ReadOnlySpan logits) { var candidates = new LLamaTokenData[logits.Length]; for (var token_id = 0; token_id < logits.Length; token_id++) candidates[token_id] = new LLamaTokenData(token_id, logits[token_id], 0.0f); return new LLamaTokenDataArray(candidates); } } /// /// Contains a pointer to an array of LLamaTokenData which is pinned in memory. /// [StructLayout(LayoutKind.Sequential)] public struct LLamaTokenDataArrayNative { /// /// A pointer to an array of LlamaTokenData /// /// Memory must be pinned in place for all the time this LLamaTokenDataArrayNative is in use public IntPtr data; /// /// Number of LLamaTokenData in the array /// public ulong size; /// /// Indicates if the items in the array are sorted /// public bool sorted { get => Convert.ToBoolean(_sorted); set => _sorted = Convert.ToSByte(value); } private sbyte _sorted; /// /// Create a new LLamaTokenDataArrayNative around the data in the LLamaTokenDataArray /// /// Data source /// Created native array /// A memory handle, pinning the data in place until disposed public static MemoryHandle Create(LLamaTokenDataArray array, out LLamaTokenDataArrayNative native) { var handle = array.data.Pin(); unsafe { native = new LLamaTokenDataArrayNative { data = new IntPtr(handle.Pointer), size = (ulong)array.data.Length, sorted = array.sorted }; } return handle; } } }