Skip to content

Commit

Permalink
feat(brain): add verify quant callback
Browse files Browse the repository at this point in the history
  • Loading branch information
snowyu committed Aug 11, 2024
1 parent 85b0386 commit d1a9eeb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -557,11 +557,12 @@ _See code: [src/commands/brain/list.ts](https://github.com/offline-ai/cli/blob/v

```
USAGE
$ ai brain refresh [--json] [-c <value>] [--banner]
$ ai brain refresh [--json] [-b <value>] [-u <value>] [-v]
FLAGS
-c, --config=<value> the config file
--[no-]banner show banner
-b, --brainDir=<value> the brains(LLM) directory
-u, --hubUrl=<value> the hub mirror url
-v, --verifyQuant whether verify quant when refresh
GLOBAL FLAGS
--json Format output as json.
Expand Down
13 changes: 11 additions & 2 deletions src/lib/brain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,17 @@ export async function downloadBrain(brain: AIModelSettings, options: {
logLevel?: string,
}) {
const quant = options.quant
const onProgress = options.onProgress
const brains = ToolFunc.get(BRAINS_FUNC_NAME) as LlmModelsFunc
const dryRun = options.dryRun

if (typeof onProgress === 'function') {
brains.on('model:'+DownloadStatusEventName, (_name: string, status: string, info: any) => {
if (info.old === info.quant) {delete info.old}
onProgress('status', status, info)
})
}

let downTasks = await brains.$download({id: brain._id, quant, url: options.url, dryRun})
if (downTasks) {
if (!Array.isArray(downTasks)) {downTasks = [downTasks]}
Expand All @@ -142,8 +151,8 @@ export async function downloadBrain(brain: AIModelSettings, options: {
let leftCount = downTasks.length
const errs: string[] = []
for (const downTask of downTasks) {
if (typeof options.onProgress === 'function') {
download.on(DownloadProgressEventName+':'+downTask!.id, options.onProgress)
if (typeof onProgress === 'function') {
download.on(DownloadProgressEventName+':'+downTask!.id, onProgress)
}
download.on(DownloadStatusEventName+':'+downTask!.id, function(_name: string, status: FileDownloadStatus, idInfo: {url: string, id: string, filepath: string}) {
// console.log(_name, status, idInfo)
Expand Down
10 changes: 9 additions & 1 deletion src/oclif/commands/brain/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,20 @@ export default class DownloadBrainCommand extends AICommand {
const quant = AIModelQuantType[flags.quant]
const progresses: any = {}
const onProgress = function(_name: string, progress: {percent:number, totalBytes:number, transferredBytes:number}, idInfo: {url: string, id?: string, filepath?: string}) {
if (_name === 'status') {
const status = progress as unknown as string
const info = idInfo as unknown as {url: string, quant: number, old: number}
const quant = AIModelQuantType[info.quant]
const old = AIModelQuantType[info.old]
logUpdate(status + ' ' + info.url + ' quant' + (info.old ? `(from ${old} to ${quant})` : `(${quant})`) )
return
}
// console.log('🚀 ~ DownloadBrainCommand ~ onProgress ~ progress:', arguments)
progresses[idInfo.url] = `Downloading ${idInfo.url}... ${(progress.percent * 100).toFixed(2)}% ${progress.transferredBytes} bytes`
const info = Object.keys(progresses).map(k => progresses[k]).join('\n')
logUpdate(info)
}
this.log('Downloading to ' + userConfig.brainDir)
this.log(brain._id, flags.quant, 'Downloading to ' + userConfig.brainDir)
let result: any[]
try {
result = await downloadBrain(brain, {
Expand Down

0 comments on commit d1a9eeb

Please sign in to comment.