-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
171 lines (147 loc) · 5.5 KB
/
model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {core as mx, nn} from '@frost-beta/mlx'
class Attention extends nn.Module {
constructor(args) {
super()
const dim = args.hiddenSize
this.nHeads = args.numAttentionHeads
this.nKVHeads = args.numKeyValueHeads
const headDim = args.hiddenSize / this.nHeads
this.scale = Math.pow(headDim, -0.5)
this.qProj = new nn.Linear(dim, this.nHeads * headDim, false)
this.kProj = new nn.Linear(dim, this.nKVHeads * headDim, false)
this.vProj = new nn.Linear(dim, this.nKVHeads * headDim, false)
this.oProj = new nn.Linear(this.nHeads * headDim, dim, false)
const ropeScale = args.ropeScaling?.type == 'linear' ? 1 / args.ropeScaling.factor
: 1
this.rope = new nn.RoPE(headDim, args.ropeTraditional, args.ropeTheta, ropeScale)
}
forward(x, mask, cache) {
const [B, L, D] = x.shape
let queries = this.qProj.forward(x)
let keys = this.kProj.forward(x)
let values = this.vProj.forward(x)
// Prepare the queries, keys and values for the attention computation.
queries = queries.reshape(B, L, this.nHeads, -1).transpose(0, 2, 1, 3)
keys = keys.reshape(B, L, this.nKVHeads, -1).transpose(0, 2, 1, 3)
values = values.reshape(B, L, this.nKVHeads, -1).transpose(0, 2, 1, 3)
if (cache) {
const [keyCache, valueCache] = cache
queries = this.rope.forward(queries, keyCache.shape[2])
keys = this.rope.forward(keys, keyCache.shape[2])
keys = mx.concatenate([keyCache, keys], 2)
values = mx.concatenate([valueCache, values], 2)
mx.dispose(cache)
} else {
queries = this.rope.forward(queries)
keys = this.rope.forward(keys)
}
let output = mx.fast.scaledDotProductAttention(queries, keys, values, this.scale, mask)
output = output.transpose(0, 2, 1, 3).reshape(B, L, -1)
return [this.oProj.forward(output), [keys, values]]
}
}
class MLP extends nn.Module {
constructor(dim, hiddenDim) {
super()
this.gateProj = new nn.Linear(dim, hiddenDim, false)
this.downProj = new nn.Linear(hiddenDim, dim, false)
this.upProj = new nn.Linear(dim, hiddenDim, false)
}
forward(x) {
return this.downProj.forward(mx.multiply(nn.silu(this.gateProj.forward(x)),
this.upProj.forward(x)))
}
}
class TransformerBlock extends nn.Module {
constructor(args) {
super()
this.numAttentionHeads = args.numAttentionHeads
this.hiddenSize = args.hiddenSize
this.selfAttn = new Attention(args)
this.mlp = new MLP(args.hiddenSize, args.intermediateSize)
this.inputLayernorm = new nn.RMSNorm(args.hiddenSize, args.rmsNormEps)
this.postAttentionLayernorm = new nn.RMSNorm(args.hiddenSize, args.rmsNormEps)
}
forward(x, mask, cache) {
const [r, newCache] = this.selfAttn.forward(this.inputLayernorm.forward(x), mask, cache)
const h = mx.add(x, r)
const out = mx.add(h, this.mlp.forward(this.postAttentionLayernorm.forward(h)))
return [out, newCache]
}
}
class LlamaModel extends nn.Module {
constructor(args) {
super()
this.vocabSize = args.vocabSize
this.numHiddenLayers = args.numHiddenLayers
this.embedTokens = new nn.Embedding(args.vocabSize, args.hiddenSize)
this.layers = []
for (let i = 0; i < args.numHiddenLayers; ++i)
this.layers.push(new TransformerBlock(args))
this.norm = new nn.RMSNorm(args.hiddenSize, args.rmsNormEps)
}
forward(inputs, cache) {
let h = this.embedTokens.forward(inputs)
let mask
if (h.shape[1] > 1) {
mask = nn.MultiHeadAttention.createAdditiveCausalMask(h.shape[1])
mask = mask.astype(h.dtype)
}
cache = cache ?? new Array(this.layers.length)
for (let i in this.layers)
[h, cache[i]] = this.layers[i].forward(h, mask, cache[i])
return [this.norm.forward(h), cache]
}
}
export default class Model extends nn.Module {
constructor(obj) {
const args = modelArgs(obj)
super()
this.modelType = args.modelType
this.model = new LlamaModel(args)
this.lmHead = new nn.Linear(args.hiddenSize, args.vocabSize, false)
}
forward(inputs, cache) {
const [out, updatedCache] = this.model.forward(inputs, cache)
return [this.lmHead.forward(out), updatedCache]
}
get layers() {
return this.model.layers
}
}
function modelArgs({model_type,
hidden_size,
num_hidden_layers,
intermediate_size,
num_attention_heads,
rms_norm_eps,
vocab_size,
num_key_value_heads = null,
rope_theta = 10000,
rope_traditional = false,
rope_scaling = null}) {
if (vocab_size <= 0)
throw new Error('vocabSize must be bigger than zero')
if (rope_scaling) {
const requiredKeys = [ 'factor', 'type' ]
if (!Object.keys(rope_scaling).every(key => requiredKeys.includes(key))) {
throw Error(`rope_scaling must contain keys ${requiredKeys}`)
}
if (rope_scaling.type != 'linear') {
throw Error("rope_scaling 'type' currently only supports 'linear'")
}
}
return {
modelType: model_type,
hiddenSize: hidden_size,
numHiddenLayers: num_hidden_layers,
intermediateSize: intermediate_size,
numAttentionHeads: num_attention_heads,
rmsNormEps: rms_norm_eps,
vocabSize: vocab_size,
numKeyValueHeads: num_key_value_heads ?? num_attention_heads,
ropeTheta: rope_theta,
ropeTraditional: rope_traditional,
ropeScaling: rope_scaling,
}
}