Skip to content

Commit

Permalink
make model references agnostic to the bin file name
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelCharles committed Apr 4, 2023
1 parent 53ca3b4 commit 4adf88d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 40 deletions.
71 changes: 35 additions & 36 deletions alpaca.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,50 +65,49 @@ class Alpaca {
* 5. Download models + convert + quantize
*
**************************************************************************************************************/
const outputFile = path.resolve(this.home, 'models', model, 'ggml-model-q4_0.bin')
if (fs.existsSync(outputFile)) {
console.log(`Skip conversion, file already exists: ${outputFile}`)
} else {

const currentVersions = {
"7B": 'ggml-model-q4_0.bin',
"13B": 'ggml-model-q4_1.bin',
"30B": 'ggml-model-q4_0.bin',
}

const outputFiles = Object.keys(currentVersions).map((m) => {
return path.resolve(this.home, 'models', m, currentVersions[m])
})

const modelExists = outputFiles.some((f) => {
if (fs.existsSync(f)) {
console.log(`Skip conversion, file already exists: ${f}`)
return true
}
// delete other model files in folder in case of an upgrade

const dir = path.dirname(f);
const files = fs.readdirSync(dir);
if (files.length !== 0) console.log("Upgrading model, removing old files...")

for (const file of files) {
fs.unlinkSync(path.join(dir, file));
console.log(`Removed old model file: ${file} in ${dir}`)
}
return false
})

if (!modelExists) {
const dir = path.resolve(this.home, "models", model)
console.log("dir", dir)
await fs.promises.mkdir(dir, { recursive: true }).catch((e) => {
console.log("mkdir", e)
})
console.log("downloading torrent")
let url
switch (model) {
case "7B":
//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)
//console.log("renaming")
//await fs.promises.rename(
// path.resolve(dir, "ggml-alpaca-7b-q4.bin"),
// path.resolve(dir, "ggml-model-q4_0.bin")
//)
url = "https://huggingface.co/Pi3141/alpaca-7B-ggml/resolve/main/ggml-model-q4_0.bin"
await this.root.down(url, path.resolve(dir, "ggml-model-q4_0.bin"))
break;

case "13B":
/*
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)
console.log("renaming")
await fs.promises.rename(
path.resolve(dir, "ggml-alpaca-13b-q4.bin"),
path.resolve(dir, "ggml-model-q4_0.bin")
)
*/
url = "https://huggingface.co/Pi3141/alpaca-13B-ggml/resolve/main/ggml-model-q4_1.bin"
await this.root.down(url, path.resolve(dir, "ggml-model-q4_0.bin"))
break;

case "30B":
url = "https://huggingface.co/Pi3141/alpaca-30B-ggml/resolve/main/ggml-model-q4_0.bin"
await this.root.down(url, path.resolve(dir, "ggml-model-q4_0.bin"))
break;

default:
console.log("Select either model 7B, 13B, or 30B")
break;
if (Object.keys(currentVersions).includes(model)) {
url = `https://huggingface.co/Pi3141/alpaca-${model}-ggml/resolve/main/${currentVersions[model]}`
await this.root.down(url, path.resolve(dir, currentVersions[model]))
} else {
console.log("Select either model 7B, 13B, or 30B")
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,19 @@ class Dalai {
seed: req.seed || -1,
threads: req.threads || 8,
n_predict: req.n_predict || 128,
model: `models/${Model || "7B"}/ggml-model-q4_0.bin`,
model: "",
}

let e = await exists(path.resolve(this.home, Core, "models", Model))
if (!e) {
cb(`File does not exist: ${Model}. Try "dalai ${Core} get ${Model}" first.`)
return
} else {
const potentialBinFiles = await fs.promises.readdir(path.resolve(this.home, Core, "models", Model))
if (potentialBinFiles.length === 0) {
cb(`No model files found in ${Model}. Try "dalai ${Core} get ${Model}" first.`)
}
o.model = path.resolve(this.home, Core, "models", Model, potentialBinFiles[0])
}

if (req.top_k) o.top_k = req.top_k
Expand Down Expand Up @@ -367,8 +373,11 @@ class Dalai {

console.log({ modelFolders })
for(let modelFolder of modelFolders) {
let e = await exists(path.resolve(modelsPath, modelFolder, 'ggml-model-q4_0.bin'))
if (e) {
// get bin files in model folder

const binFiles = fs.readdirSync(path.resolve(modelsPath, modelFolder)).filter((f) => f.endsWith(".bin"))

if (binFiles.length !== 0) {
modelNames.push(`${core}.${modelFolder}`)
console.log("exists", modelFolder)
}
Expand Down
10 changes: 9 additions & 1 deletion llama.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,16 @@ npx dalai install 7B 13B
}
for(let i=0; i<num[model]; i++) {
const suffix = (i === 0 ? "" : `.${i}`)

const potentialOutputFiles = fs.readdirSync(`./models/${model}/`).filter(f => f.endsWith(`.bin`))

if (potentialOutputFiles.length === 0) {
throw new Error(`No bin file found in ./models/${model}/`)
}

const outputFile1 = path.resolve(this.home, `./models/${model}/ggml-model-f16.bin${suffix}`)
const outputFile2 = path.resolve(this.home, `./models/${model}/ggml-model-q4_0.bin${suffix}`)
const outputFile2 = path.resolve(this.home, `./models/${model}/${potentialOutputFiles[0]}${suffix}`)

if (fs.existsSync(outputFile1) && fs.existsSync(outputFile2)) {
console.log(`Skip quantization, files already exists: ${outputFile1} and ${outputFile2}}`)
continue
Expand Down

0 comments on commit 4adf88d

Please sign in to comment.