Skip to content

Commit 7f28f3e

Browse files
committed
Read context length and warn if it is very large
1 parent 3ab6cb4 commit 7f28f3e

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

Runtime/LLM.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,13 @@ public void SetModel(string path)
173173
if (!string.IsNullOrEmpty(model))
174174
{
175175
ModelEntry modelEntry = LLMManager.Get(model);
176-
string template = modelEntry != null ? modelEntry.chatTemplate : ChatTemplate.FromGGUF(GetModelLoraPathRuntime(model));
177-
SetTemplate(template);
176+
if (modelEntry == null) modelEntry = new ModelEntry(GetModelLoraPathRuntime(model));
177+
SetTemplate(modelEntry.chatTemplate);
178+
Debug.Log(modelEntry.contextLength);
179+
if (contextSize == 0 && modelEntry.contextLength > 32768)
180+
{
181+
LLMUnitySetup.LogWarning($"The model {path} has very large context size ({modelEntry.contextLength}), consider setting it to a smaller value (<=32768) to avoid filling up the RAM");
182+
}
178183
}
179184
#if UNITY_EDITOR
180185
if (!EditorApplication.isPlaying) EditorUtility.SetDirty(this);

Runtime/LLMChatTemplates.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// @file
22
/// @brief File implementing the chat templates.
3+
using System;
34
using System.Collections.Generic;
45
using System.IO;
56
using UnityEngine;
@@ -113,9 +114,12 @@ public static string FromTemplate(string template)
113114
/// <returns>template name</returns>
114115
public static string FromGGUF(string path)
115116
{
116-
GGUFReader reader = new GGUFReader(path);
117-
string name;
117+
return FromGGUF(new GGUFReader(path), path);
118+
}
118119

120+
public static string FromGGUF(GGUFReader reader, string path)
121+
{
122+
string name;
119123
name = FromTemplate(reader.GetStringField("tokenizer.chat_template"));
120124
if (name != null) return name;
121125

Runtime/LLMGGUF.cs

+22-3
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,35 @@ public ReaderField GetField(string key)
125125
return null;
126126
}
127127

128+
public byte[] GetGenericField(string key)
129+
{
130+
ReaderField field = GetField(key);
131+
if (field == null || field.parts.Count == 0) return null;
132+
return (byte[])field.parts[field.parts.Count - 1];
133+
}
134+
128135
/// <summary>
129136
/// Allows to retrieve a string GGUF field.
130137
/// </summary>
131138
/// <param name="key"> GGUF field to retrieve </param>
132139
/// <returns> Retrieved GGUF value </returns>
133140
public string GetStringField(string key)
134141
{
135-
ReaderField field = GetField(key);
136-
if (field == null || field.parts.Count == 0) return null;
137-
return System.Text.Encoding.UTF8.GetString((byte[])field.parts[field.parts.Count - 1]);
142+
byte[] value = GetGenericField(key);
143+
if (value == null) return null;
144+
return System.Text.Encoding.UTF8.GetString(value);
145+
}
146+
147+
/// <summary>
148+
/// Allows to retrieve an integer GGUF field.
149+
/// </summary>
150+
/// <param name="key"> GGUF field to retrieve </param>
151+
/// <returns> Retrieved GGUF value </returns>
152+
public int GetIntField(string key)
153+
{
154+
byte[] value = GetGenericField(key);
155+
if (value == null) return -1;
156+
return BitConverter.ToInt32(value, 0);
138157
}
139158

140159
private byte[] ReadBytes(int offset, int count)

Runtime/LLMManager.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,25 @@ public class ModelEntry
1717
public string chatTemplate;
1818
public string url;
1919
public bool includeInBuild;
20-
20+
public int contextLength;
2121

2222
public ModelEntry(string path, bool lora = false, string label = null, string url = null)
2323
{
2424
filename = Path.GetFileName(path);
2525
this.label = label == null ? filename : label;
2626
this.lora = lora;
2727
this.path = Path.GetFullPath(path).Replace('\\', '/');
28-
chatTemplate = lora ? null : ChatTemplate.FromGGUF(this.path);
2928
this.url = url;
3029
includeInBuild = true;
30+
chatTemplate = null;
31+
contextLength = -1;
32+
if (!lora)
33+
{
34+
GGUFReader reader = new GGUFReader(this.path);
35+
chatTemplate = ChatTemplate.FromGGUF(reader, this.path);
36+
string arch = reader.GetStringField("general.architecture");
37+
if (arch != null) contextLength = reader.GetIntField($"{arch}.context_length");
38+
}
3139
}
3240

3341
public ModelEntry OnlyRequiredFields()

0 commit comments

Comments
 (0)