Skip to content

Commit

Permalink
add model settings templates
Browse files Browse the repository at this point in the history
fix model_free
  • Loading branch information
guinmoon committed Jul 18, 2023
1 parent ebcb35b commit 05a533d
Show file tree
Hide file tree
Showing 22 changed files with 362 additions and 125 deletions.
51 changes: 49 additions & 2 deletions LLMFarm.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
150CAD022A3CE7B30015CD66 /* GPTNeox.swift in Sources */ = {isa = PBXBuildFile; fileRef = 150CAD002A3CE7B30015CD66 /* GPTNeox.swift */; };
15141E162A6438AE0060E767 /* Starcoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15141E152A6438AE0060E767 /* Starcoder.swift */; };
15141E172A6438AE0060E767 /* Starcoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15141E152A6438AE0060E767 /* Starcoder.swift */; };
15141E5C2A651F8C0060E767 /* model_setting_templates in Resources */ = {isa = PBXBuildFile; fileRef = 15141E5B2A651F8C0060E767 /* model_setting_templates */; };
15141E712A65799D0060E767 /* ModelSettingsTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15141E702A65799D0060E767 /* ModelSettingsTemplate.swift */; };
155FBB9F2A460566004DD5AE /* GPTBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 155FBB9E2A460566004DD5AE /* GPTBase.swift */; };
155FBBA02A460566004DD5AE /* GPTBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 155FBB9E2A460566004DD5AE /* GPTBase.swift */; };
155FBBA22A48B5C1004DD5AE /* Replit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 155FBBA12A48B5C0004DD5AE /* Replit.swift */; };
Expand Down Expand Up @@ -73,6 +75,16 @@
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
15141E6B2A6577160060E767 /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
};
15E415CF2A18A2970057A15E /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 12;
Expand All @@ -87,6 +99,18 @@
/* Begin PBXFileReference section */
150CAD002A3CE7B30015CD66 /* GPTNeox.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GPTNeox.swift; sourceTree = "<group>"; };
15141E152A6438AE0060E767 /* Starcoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Starcoder.swift; sourceTree = "<group>"; };
15141E362A6488A60060E767 /* Replit 3B.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "Replit 3B.json"; sourceTree = "<group>"; };
15141E372A6488A60060E767 /* dolly v2 3b.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "dolly v2 3b.json"; sourceTree = "<group>"; };
15141E382A6488A60060E767 /* orca mini 3b.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "orca mini 3b.json"; sourceTree = "<group>"; };
15141E392A6488A60060E767 /* openllama 3b.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "openllama 3b.json"; sourceTree = "<group>"; };
15141E3A2A6488A60060E767 /* rp-incite-base-v1-3b.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "rp-incite-base-v1-3b.json"; sourceTree = "<group>"; };
15141E3B2A6488A60060E767 /* santacoder.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = santacoder.json; sourceTree = "<group>"; };
15141E3C2A6488A60060E767 /* saiga 7B.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "saiga 7B.json"; sourceTree = "<group>"; };
15141E3D2A6488A60060E767 /* cerebras 2.7b.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "cerebras 2.7b.json"; sourceTree = "<group>"; };
15141E3F2A6488A70060E767 /* stablelm-tuned-alpha-3b.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "stablelm-tuned-alpha-3b.json"; sourceTree = "<group>"; };
15141E402A6488A70060E767 /* AI Dungeon 2 Classic.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "AI Dungeon 2 Classic.json"; sourceTree = "<group>"; };
15141E5B2A651F8C0060E767 /* model_setting_templates */ = {isa = PBXFileReference; lastKnownFileType = folder; name = model_setting_templates; path = LLMFarm/model_setting_templates; sourceTree = "<group>"; };
15141E702A65799D0060E767 /* ModelSettingsTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModelSettingsTemplate.swift; sourceTree = "<group>"; };
155FBB9E2A460566004DD5AE /* GPTBase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GPTBase.swift; sourceTree = "<group>"; };
155FBBA12A48B5C0004DD5AE /* Replit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Replit.swift; sourceTree = "<group>"; };
1560D6162A2D1A3D00918330 /* AddChatView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddChatView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -171,6 +195,23 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
15141E332A64875F0060E767 /* model_setting_templates */ = {
isa = PBXGroup;
children = (
15141E402A6488A70060E767 /* AI Dungeon 2 Classic.json */,
15141E3D2A6488A60060E767 /* cerebras 2.7b.json */,
15141E372A6488A60060E767 /* dolly v2 3b.json */,
15141E392A6488A60060E767 /* openllama 3b.json */,
15141E382A6488A60060E767 /* orca mini 3b.json */,
15141E362A6488A60060E767 /* Replit 3B.json */,
15141E3A2A6488A60060E767 /* rp-incite-base-v1-3b.json */,
15141E3C2A6488A60060E767 /* saiga 7B.json */,
15141E3B2A6488A60060E767 /* santacoder.json */,
15141E3F2A6488A70060E767 /* stablelm-tuned-alpha-3b.json */,
);
path = model_setting_templates;
sourceTree = "<group>";
};
1567138A2A5B13C4002BE054 /* metal */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -252,6 +293,7 @@
15F2107F2A191B190021F414 /* LLMFarm */ = {
isa = PBXGroup;
children = (
15141E332A64875F0060E767 /* model_setting_templates */,
15A3EDC42A2121D90030AF5A /* Chats */,
15A3EDC12A2121D90030AF5A /* Contacts */,
15A3EDBD2A2121D90030AF5A /* Settings */,
Expand All @@ -269,6 +311,7 @@
15A3EDBB2A2121C70030AF5A /* ContentView.swift */,
15A3EDD02A21266D0030AF5A /* SearchView.swift */,
156A48D12A52B65C007DF519 /* Orientation_helper.swift */,
15141E702A65799D0060E767 /* ModelSettingsTemplate.swift */,
);
path = LLMFarm;
sourceTree = "<group>";
Expand All @@ -293,6 +336,7 @@
E624E1F52958ECC800996CD1 = {
isa = PBXGroup;
children = (
15141E5B2A651F8C0060E767 /* model_setting_templates */,
1567138A2A5B13C4002BE054 /* metal */,
E6024EC229EA7BEB001FE9B0 /* Packages */,
E624E2102958F21E00996CD1 /* Info.plist */,
Expand Down Expand Up @@ -359,6 +403,7 @@
15F2107A2A191B190021F414 /* Sources */,
15F2107B2A191B190021F414 /* Frameworks */,
15F2107C2A191B190021F414 /* Resources */,
15141E6B2A6577160060E767 /* Embed Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -418,6 +463,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
15141E5C2A651F8C0060E767 /* model_setting_templates in Resources */,
1567138D2A5B155D002BE054 /* ggml-metal.metal in Resources */,
15F210892A191B1B0021F414 /* Preview Assets.xcassets in Resources */,
15F210852A191B1B0021F414 /* Assets.xcassets in Resources */,
Expand Down Expand Up @@ -470,6 +516,7 @@
15F210A32A1921E40021F414 /* Math.swift in Sources */,
155FBBA02A460566004DD5AE /* GPTBase.swift in Sources */,
15A3EDCB2A2121D90030AF5A /* ModelsView.swift in Sources */,
15141E712A65799D0060E767 /* ModelSettingsTemplate.swift in Sources */,
15A3EDC92A2121D90030AF5A /* SettingsItem.swift in Sources */,
15F210A42A1921E40021F414 /* Utils.swift in Sources */,
15A480002A40D2A300B72DC3 /* GPT2.swift in Sources */,
Expand Down Expand Up @@ -560,7 +607,7 @@
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 0.2.2;
MARKETING_VERSION = 0.3.0;
PRODUCT_BUNDLE_IDENTIFIER = com.guinmoon.LLMFarm;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down Expand Up @@ -607,7 +654,7 @@
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 0.2.2;
MARKETING_VERSION = 0.3.0;
PRODUCT_BUNDLE_IDENTIFIER = com.guinmoon.LLMFarm;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
17 changes: 9 additions & 8 deletions LLMFarm/AIChatModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ final class AIChatModel: ObservableObject {
if (chat_config!["use_metal"] != nil){
model_context_param.use_metal = chat_config!["use_metal"] as! Bool
}
try? self.chat?.loadModel(ModelInference.LLamaInference,contextParams: model_context_param)
self.chat?.loadModel(ModelInference.LLamaInference,contextParams: model_context_param)
}else if chat_config!["model_inference"] as! String == "gptneox" {
try? self.chat?.loadModel(ModelInference.GPTNeoxInference,contextParams: model_context_param)
self.chat?.loadModel(ModelInference.GPTNeoxInference,contextParams: model_context_param)
}else if chat_config!["model_inference"] as! String == "gpt2" {
try? self.chat?.loadModel(ModelInference.GPT2,contextParams: model_context_param)
self.chat?.loadModel(ModelInference.GPT2,contextParams: model_context_param)
self.chat?.model.stop_words.append("<|endoftext|>")
}else if chat_config!["model_inference"] as! String == "replit" {
try? self.chat?.loadModel(ModelInference.Replit,contextParams: model_context_param)
self.chat?.loadModel(ModelInference.Replit,contextParams: model_context_param)
self.chat?.model.stop_words.append("<|endoftext|>")
}else if chat_config!["model_inference"] as! String == "starcoder" {
try? self.chat?.loadModel(ModelInference.Starcoder,contextParams: model_context_param)
self.chat?.loadModel(ModelInference.Starcoder,contextParams: model_context_param)
self.chat?.model.stop_words.append("<|endoftext|>")
}
}
Expand All @@ -107,9 +107,9 @@ final class AIChatModel: ObservableObject {
model_lowercase.contains("alpaca") ||
model_lowercase.contains("vic") ||
model_lowercase.contains("orca")){
try? self.chat?.loadModel(ModelInference.LLamaInference)
self.chat?.loadModel(ModelInference.LLamaInference)
}else{
try? self.chat?.loadModel(ModelInference.GPTNeoxInference)
self.chat?.loadModel(ModelInference.GPTNeoxInference)
}
}
if self.chat?.model.context == nil{
Expand All @@ -120,7 +120,8 @@ final class AIChatModel: ObservableObject {
print(model_sample_param)
print(model_context_param)
//Set prompt model if in config or try to set promt format by filename
if (chat_config!["prompt_format"] != nil && chat_config!["prompt_format"]! as! String != "auto"){
if (chat_config!["prompt_format"] != nil && chat_config!["prompt_format"]! as! String != "auto"
&& chat_config!["prompt_format"]! as! String != "{{prompt}}"){
self.chat?.model.custom_prompt_format = chat_config!["prompt_format"]! as! String
self.chat?.model.promptFormat = .Custom
}
Expand Down
60 changes: 59 additions & 1 deletion LLMFarm/FileHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,68 @@ func get_avalible_models() -> [String]?{
return res
} catch {
// failed to read directory – bad permissions, perhaps?
print(error)
}
return res
}

func parse_model_setting_template(template_path:String) -> ModelSettingsTemplate{
var tmp_template:ModelSettingsTemplate = ModelSettingsTemplate()
do{
let data = try Data(contentsOf: URL(fileURLWithPath: template_path), options: .mappedIfSafe)
let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
let jsonResult_dict = jsonResult as? Dictionary<String, AnyObject>
if (jsonResult_dict!["template_name"] != nil){
tmp_template.template_name = jsonResult_dict!["template_name"] as! String
}else{
tmp_template.template_name = (template_path as NSString).lastPathComponent
}
if (jsonResult_dict!["model_inference"] != nil){
tmp_template.inference = jsonResult_dict!["model_inference"] as! String
}
if (jsonResult_dict!["prompt_format"] != nil){
tmp_template.prompt_format = jsonResult_dict!["prompt_format"] as! String
}
if (jsonResult_dict!["n_batch"] != nil){
tmp_template.n_batch = jsonResult_dict!["n_batch"] as! Int32
}
if (jsonResult_dict!["temp"] != nil){
tmp_template.temp = Float(jsonResult_dict!["temp"] as! Double)
}
if (jsonResult_dict!["top_k"] != nil){
tmp_template.top_k = jsonResult_dict!["top_k"] as! Int32
}
if (jsonResult_dict!["top_p"] != nil){
tmp_template.top_p = Float(jsonResult_dict!["top_p"] as! Double)
}
if (jsonResult_dict!["repeat_penalty"] != nil){
tmp_template.repeat_penalty = Float(jsonResult_dict!["repeat_penalty"] as! Double)
}
if (jsonResult_dict!["repeat_last_n"] != nil){
tmp_template.repeat_last_n = jsonResult_dict!["repeat_last_n"] as! Int32
}
}
catch {
print(error)
}
return tmp_template
}

func get_model_setting_templates() -> [ModelSettingsTemplate]{
var model_setting_templates: [ModelSettingsTemplate] = []
model_setting_templates.append(ModelSettingsTemplate())
do{
var templates_path=Bundle.main.resourcePath!.appending("/model_setting_templates")
let tenplate_files = try FileManager.default.contentsOfDirectory(atPath: templates_path)
for tenplate_file in tenplate_files {
model_setting_templates.append(parse_model_setting_template(template_path: templates_path+"/"+tenplate_file))
}
}
catch {
print(error)
}
return model_setting_templates
}

public func get_chat_info(_ chat_fname:String) -> Dictionary<String, AnyObject>? {
do {
Expand All @@ -39,7 +97,7 @@ public func get_chat_info(_ chat_fname:String) -> Dictionary<String, AnyObject>?
let jsonResult_dict = jsonResult as? Dictionary<String, AnyObject>
return jsonResult_dict
} catch {

print(error)
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions LLMFarm/LLMFarmApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct LLMFarmApp: App {
aiChatModel.stop_predict()
}


var body: some Scene {
WindowGroup {

Expand Down
23 changes: 22 additions & 1 deletion LLMFarm/ModelSettingsTemplate.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
//
// model_settings_stemplate.swift
// ModelSettingsTemplate.swift
// LLMFarm
//
// Created by guinmoon on 17.07.2023.
//

import Foundation

struct ModelSettingsTemplate: Hashable {
var template_name: String = "Custom"
var inference = "llama"
var context: Int32 = 2048
var n_batch: Int32 = 512
var temp: Float = 0.9
var top_k: Int32 = 40
var top_p: Float = 0.95
var repeat_last_n: Int32 = 64
var repeat_penalty: Float = 1.174
var prompt_format: String = "{{prompt}}"

func hash(into hasher: inout Hasher) {
hasher.combine(template_name)
}

static func == (lhs: ModelSettingsTemplate, rhs: ModelSettingsTemplate) -> Bool {
return lhs.template_name == rhs.template_name
}
}
Loading

0 comments on commit 05a533d

Please sign in to comment.