Browse Source

Added some comments on various native methods

tags/v0.5.1
Martin Evans 2 years ago
parent
commit
bcf06e2652
2 changed files with 54 additions and 1 deletions
  1. +41
    -1
      LLama/Native/NativeApi.cs
  2. +13
    -0
      LLama/Native/SamplingApi.cs

+ 41
- 1
LLama/Native/NativeApi.cs View File

@@ -11,8 +11,16 @@ namespace LLama.Native
{
using llama_token = Int32;

/// <summary>
/// Callback from llama.cpp with log messages
/// </summary>
/// <param name="level"></param>
/// <param name="message"></param>
public delegate void LLamaLogCallback(ILLamaLogger.LogLevel level, string message);

/// <summary>
/// Direct translation of the llama.cpp API
/// </summary>
public unsafe partial class NativeApi
{
static NativeApi()
@@ -41,18 +49,43 @@ namespace LLama.Native
[DllImport(libraryName, EntryPoint = "llama_mmap_supported", CallingConvention = CallingConvention.Cdecl)]
public static extern bool llama_empty_call();

/// <summary>
/// Create a LLamaContextParams with default values
/// </summary>
/// <returns></returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern LLamaContextParams llama_context_default_params();

/// <summary>
/// Create a LLamaModelQuantizeParams with default values
/// </summary>
/// <returns></returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern LLamaModelQuantizeParams llama_model_quantize_default_params();

/// <summary>
/// Check if memory mapping is supported
/// </summary>
/// <returns></returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool llama_mmap_supported();

/// <summary>
/// Check if memory lockingis supported
/// </summary>
/// <returns></returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool llama_mlock_supported();

/// <summary>
/// Export a static computation graph for context of 511 and batch size of 1
/// NOTE: since this functionality is mostly for debugging and demonstration purposes, we hardcode these
/// parameters here to keep things simple
/// IMPORTANT: do not use for anything else other than debugging and testing!
/// </summary>
/// <param name="ctx"></param>
/// <param name="fname"></param>
/// <returns></returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern int llama_eval_export(SafeLLamaContextHandle ctx, string fname);

@@ -67,6 +100,13 @@ namespace LLama.Native
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr llama_load_model_from_file(string path_model, LLamaContextParams @params);

/// <summary>
/// Create a new llama_context with the given model.
/// Return value should always be wrapped in SafeLLamaContextHandle!
/// </summary>
/// <param name="model"></param>
/// <param name="params"></param>
/// <returns></returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr llama_new_context_with_model(SafeLlamaModelHandle model, LLamaContextParams @params);

@@ -79,7 +119,7 @@ namespace LLama.Native
public static extern void llama_backend_init(bool numa);

/// <summary>
/// Frees all allocated memory
/// Frees all allocated memory in the given llama_context
/// </summary>
/// <param name="ctx"></param>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]


+ 13
- 0
LLama/Native/SamplingApi.cs View File

@@ -1,8 +1,14 @@
using System;

#pragma warning disable IDE1006 // Naming Styles

namespace LLama.Native
{
using llama_token = Int32;

/// <summary>
/// Direct translation of the llama.cpp sampling API
/// </summary>
public unsafe class SamplingApi
{
/// <summary>
@@ -140,6 +146,13 @@ namespace LLama.Native
NativeApi.llama_sample_typical(ctx, ref st, p, min_keep);
}

/// <summary>
/// Sample with temperature.
/// As temperature increases, the prediction becomes diverse but also vulnerable to hallucinations -- generating tokens that are sensible but not factual
/// </summary>
/// <param name="ctx"></param>
/// <param name="candidates"></param>
/// <param name="temp"></param>
public static void llama_sample_temperature(SafeLLamaContextHandle ctx, LLamaTokenDataArray candidates, float temp)
{
using var handle = LLamaTokenDataArrayNative.Create(candidates, out var st);


Loading…
Cancel
Save