Skip to content

Commit

Permalink
Simplify the nextTick trick
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Jul 10, 2024
1 parent a56c790 commit f015a49
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
8 changes: 3 additions & 5 deletions llm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'node:fs'
import path from 'node:path'
import nextTick from 'tick-promise'
import {core as mx, nn} from '@frost-beta/mlx'

import {Model} from './model.js'
Expand Down Expand Up @@ -52,11 +53,8 @@ export async function* step(promptTokens, model, topP = 1, temperature = 1) {
const [token, prob] = mx.tidy(() => forward(tokens))
tokens = [token]
// Yield the result in the next tick of loop, so GC can get a chance to run.
// TODO(zcbenz): Use the async API after this MLX issue is solved:
// https://github.com/ml-explore/mlx/issues/1251
yield await new Promise((resolve) => {
process.nextTick(() => resolve([token, prob]))
})
await nextTick()
yield [token, prob]
}
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"dependencies": {
"@frost-beta/mlx": "0.0.11",
"@lenml/tokenizers": "1.0.2"
"@lenml/tokenizers": "1.0.2",
"tick-promise": "1.0.0"
}
}

0 comments on commit f015a49

Please sign in to comment.