Skip to content

Commit 797b25a

Browse files
make model references agnostic to the bin file name
1 parent 53ca3b4 commit 797b25a

File tree

3 files changed

+54
-40
lines changed

3 files changed

+54
-40
lines changed

alpaca.js

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -65,50 +65,47 @@ class Alpaca {
6565
* 5. Download models + convert + quantize
6666
*
6767
**************************************************************************************************************/
68-
const outputFile = path.resolve(this.home, 'models', model, 'ggml-model-q4_0.bin')
69-
if (fs.existsSync(outputFile)) {
70-
console.log(`Skip conversion, file already exists: ${outputFile}`)
71-
} else {
68+
69+
const currentVersions = {
70+
"7B": 'ggml-model-q4_0.bin',
71+
"13B": 'ggml-model-q4_1.bin',
72+
"30B": 'ggml-model-q4_0.bin',
73+
}
74+
75+
const outputFile = path.resolve(this.home, 'models', model, currentVersions[model])
76+
77+
const modelExists = (f) => {
78+
if (fs.existsSync(f)) {
79+
console.log(`Skip conversion, file already exists: ${f}`)
80+
return true
81+
}
82+
// delete other model files in folder in case of an upgrade
83+
84+
const dir = path.dirname(f);
85+
const files = fs.readdirSync(dir);
86+
if (files.length !== 0) console.log("Upgrading model, removing old files...")
87+
88+
for (const file of files) {
89+
fs.unlinkSync(path.join(dir, file));
90+
console.log(`Removed old model file: ${file} in ${dir}`)
91+
}
92+
return false
93+
}
94+
95+
if (!modelExists(outputFile)) {
7296
const dir = path.resolve(this.home, "models", model)
7397
console.log("dir", dir)
7498
await fs.promises.mkdir(dir, { recursive: true }).catch((e) => {
7599
console.log("mkdir", e)
76100
})
77101
console.log("downloading torrent")
78102
let url
79-
switch (model) {
80-
case "7B":
81-
//await this.root.torrent.add('magnet:?xt=urn:btih:5aaceaec63b03e51a98f04fd5c42320b2a033010&dn=ggml-alpaca-7b-q4.bin&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Fopentracker.i2p.rocks%3A6969%2Fannounce', dir)
82-
//console.log("renaming")
83-
//await fs.promises.rename(
84-
// path.resolve(dir, "ggml-alpaca-7b-q4.bin"),
85-
// path.resolve(dir, "ggml-model-q4_0.bin")
86-
//)
87-
url = "https://huggingface.co/Pi3141/alpaca-7B-ggml/resolve/main/ggml-model-q4_0.bin"
88-
await this.root.down(url, path.resolve(dir, "ggml-model-q4_0.bin"))
89-
break;
90-
91-
case "13B":
92-
/*
93-
await this.root.torrent.add('magnet:?xt=urn:btih:053b3d54d2e77ff020ebddf51dad681f2a651071&dn=ggml-alpaca-13b-q4.bin&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Fopentracker.i2p.rocks%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.com%3A2810%2Fannounce', dir)
94-
console.log("renaming")
95-
await fs.promises.rename(
96-
path.resolve(dir, "ggml-alpaca-13b-q4.bin"),
97-
path.resolve(dir, "ggml-model-q4_0.bin")
98-
)
99-
*/
100-
url = "https://huggingface.co/Pi3141/alpaca-13B-ggml/resolve/main/ggml-model-q4_1.bin"
101-
await this.root.down(url, path.resolve(dir, "ggml-model-q4_0.bin"))
102-
break;
103103

104-
case "30B":
105-
url = "https://huggingface.co/Pi3141/alpaca-30B-ggml/resolve/main/ggml-model-q4_0.bin"
106-
await this.root.down(url, path.resolve(dir, "ggml-model-q4_0.bin"))
107-
break;
108-
109-
default:
110-
console.log("Select either model 7B, 13B, or 30B")
111-
break;
104+
if (Object.keys(currentVersions).includes(model)) {
105+
url = `https://huggingface.co/Pi3141/alpaca-${model}-ggml/resolve/main/${currentVersions[model]}`
106+
await this.root.down(url, path.resolve(dir, currentVersions[model]))
107+
} else {
108+
console.log("Select either model 7B, 13B, or 30B")
112109
}
113110
}
114111
}

index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,19 @@ class Dalai {
225225
seed: req.seed || -1,
226226
threads: req.threads || 8,
227227
n_predict: req.n_predict || 128,
228-
model: `models/${Model || "7B"}/ggml-model-q4_0.bin`,
228+
model: "",
229229
}
230230

231231
let e = await exists(path.resolve(this.home, Core, "models", Model))
232232
if (!e) {
233233
cb(`File does not exist: ${Model}. Try "dalai ${Core} get ${Model}" first.`)
234234
return
235+
} else {
236+
const potentialBinFiles = await fs.promises.readdir(path.resolve(this.home, Core, "models", Model))
237+
if (potentialBinFiles.length === 0) {
238+
cb(`No model files found in ${Model}. Try "dalai ${Core} get ${Model}" first.`)
239+
}
240+
o.model = path.resolve(this.home, Core, "models", Model, potentialBinFiles[0])
235241
}
236242

237243
if (req.top_k) o.top_k = req.top_k
@@ -367,8 +373,11 @@ class Dalai {
367373

368374
console.log({ modelFolders })
369375
for(let modelFolder of modelFolders) {
370-
let e = await exists(path.resolve(modelsPath, modelFolder, 'ggml-model-q4_0.bin'))
371-
if (e) {
376+
// get bin files in model folder
377+
378+
const binFiles = fs.readdirSync(path.resolve(modelsPath, modelFolder)).filter((f) => f.endsWith(".bin"))
379+
380+
if (binFiles.length !== 0) {
372381
modelNames.push(`${core}.${modelFolder}`)
373382
console.log("exists", modelFolder)
374383
}

llama.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,16 @@ npx dalai install 7B 13B
111111
}
112112
for(let i=0; i<num[model]; i++) {
113113
const suffix = (i === 0 ? "" : `.${i}`)
114+
115+
const potentialOutputFiles = fs.readdirSync(`./models/${model}/`).filter(f => f.endsWith(`.bin`))
116+
117+
if (potentialOutputFiles.length === 0) {
118+
throw new Error(`No bin file found in ./models/${model}/`)
119+
}
120+
114121
const outputFile1 = path.resolve(this.home, `./models/${model}/ggml-model-f16.bin${suffix}`)
115-
const outputFile2 = path.resolve(this.home, `./models/${model}/ggml-model-q4_0.bin${suffix}`)
122+
const outputFile2 = path.resolve(this.home, `./models/${model}/${potentialOutputFiles[0]}${suffix}`)
123+
116124
if (fs.existsSync(outputFile1) && fs.existsSync(outputFile2)) {
117125
console.log(`Skip quantization, files already exists: ${outputFile1} and ${outputFile2}}`)
118126
continue

0 commit comments

Comments
 (0)