/
lm_cloud.py
609 lines (486 loc) · 16.3 KB
/
lm_cloud.py
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Serving model parameters for lm_cloud."""
import os
from typing import List, cast
from jax import numpy as jnp
from paxml import base_experiment
from paxml import tasks_lib
from paxml.tasks.lm.params import lm_cloud
from praxis import base_input
from praxis import layers
from praxis import optimizers
from praxis import pax_fiddle
from praxis import schedules
from praxis.layers import activations
from praxis.layers import multi_query_attention
from saxml.server import servable_model_registry
from saxml.server.pax import quantization
from saxml.server.pax.lm.layers import LLaMARotaryEmbedding
from saxml.server.pax.lm.layers import ParallelTransformer
from saxml.server.pax.lm.params import template
@template.make_servable()
class BaseLLaMA(base_experiment.BaseExperiment):
"""Base LLaMA Transformer LM configuration."""
SPM_MODEL = 'gs://cloud-tpu-inference-public/sax-tokenizers/llama/llama-tokenizer.model'
SOS_ID = 1
EOS_ID = 2
# architecture related
NUM_LAYERS = 32
VOCAB_SIZE = 32000
DIMS_PER_HEAD = 128
NUM_HEADS = 32
MODEL_DIMS = 4096
HIDDEN_DIMS = MODEL_DIMS * 4
FPROP_DTYPE = jnp.bfloat16
MODEL_DTYPE = jnp.bfloat16
USE_MQA = False
ACTIVATION_CLS = activations.SiLU
USE_GATED_ACTIVATION = True
RMS_NORM_EPSILON = 1.0e-05
# Sub-class has to specify a mesh.
ICI_MESH_SHAPE = [1, 1, 1]
DCN_MESH_SHAPE = None
DECODE_MESH_TRANSPOSE = None
BATCH_SIZE = 1
NUM_SAMPLES = 1
ENABLE_GENERATE_STREAM = True
STREAM_INTERVAL_STEPS = 16
FPROP_FOR_PREFIX = True
INPUT_SEQ_LEN = 4096
BUCKET_KEYS = [128, 1024, 4096]
MAX_DECODE_STEPS = [128, 512, 1024]
EXTRA_INPUTS = {
'temperature': 0.5,
'per_example_max_decode_steps': 128,
'per_example_top_k': 200,
'per_example_top_p': 0.95,
}
def datasets(self) -> List[pax_fiddle.Config[base_input.BaseInput]]:
return []
def task(self) -> pax_fiddle.Config[tasks_lib.SingleTask]:
"""Returns the task parameters."""
task_p = pax_fiddle.Config(tasks_lib.SingleTask, name='xformer_task')
task_p.model = pax_fiddle.Config(layers.LanguageModel, name='xformer_lm')
model_p = task_p.model
model_p.lm_tpl.packed_input = False
model_p.lm_tpl.model_dims = self.MODEL_DIMS
model_p.lm_tpl.vocab_size = self.VOCAB_SIZE
model_p.lm_tpl.position_emb_tpl = None
model_p.lm_tpl.softmax_tpl = pax_fiddle.Config(
layers.FullSoftmax,
name='output',
input_dims=self.MODEL_DIMS,
num_classes=self.VOCAB_SIZE,
)
model_p.lm_tpl.softmax_tpl.feed_forward_tpl.has_bias = False
model_p.lm_tpl.separate_embedding_tpl = pax_fiddle.Config(
layers.Embedding,
name='tok_embeddings',
input_dims=self.MODEL_DIMS,
num_classes=self.VOCAB_SIZE,
)
ln_tpl = pax_fiddle.Config(
layers.RmsNorm,
name='norm',
direct_scale=True,
epsilon=self.RMS_NORM_EPSILON,
)
model_p.lm_tpl.final_ln_tpl = ln_tpl.clone()
stacked_transformer_tpl = pax_fiddle.Config(layers.StackedTransformer)
stacked_transformer_tpl.model_dims = self.MODEL_DIMS
stacked_transformer_tpl.hidden_dims = self.HIDDEN_DIMS
stacked_transformer_tpl.num_layers = self.NUM_LAYERS
stacked_transformer_tpl.num_heads = self.NUM_HEADS
stacked_transformer_tpl.dim_per_head = self.DIMS_PER_HEAD
transformer_layer_p = cast(
pax_fiddle.Config[layers.Transformer],
stacked_transformer_tpl.transformer_layer_params_tpl,
)
transformer_layer_p.norm_policy = 'pre'
transformer_layer_p.ln_tpl = ln_tpl.clone()
if self.USE_MQA:
transformer_layer_p.tr_atten_tpl = pax_fiddle.Config(
multi_query_attention.MultiQueryDotProductAttention,
num_kv_heads=self.NUM_KV_HEADS,
)
transformer_layer_p.tr_atten_tpl.combine_qkv = False
else:
transformer_layer_p.tr_atten_tpl.internal_enable_per_dim_scale = False
transformer_layer_p.tr_atten_tpl.combine_qkv = True
transformer_layer_p.tr_atten_tpl.internal_enable_query_scale = True
transformer_layer_p.tr_atten_tpl.use_bias = False
transformer_layer_p.tr_atten_tpl.rotary_position_emb_tpl = (
pax_fiddle.Config(LLaMARotaryEmbedding)
)
transformer_layer_p.tr_atten_tpl.use_rotary_position_emb = True
transformer_layer_p.tr_fflayer_tpl.has_bias = False
transformer_layer_p.tr_fflayer_tpl.ln_tpl = ln_tpl.clone()
transformer_layer_p.tr_fflayer_tpl.activation_tpl = pax_fiddle.Config(
self.ACTIVATION_CLS
)
transformer_layer_p.tr_fflayer_tpl.use_gated_activation = (
self.USE_GATED_ACTIVATION
)
model_p.lm_tpl.stacked_transformer_tpl = stacked_transformer_tpl
model_p.fprop_dtype = self.FPROP_DTYPE
model_p.dtype = self.MODEL_DTYPE
# Set sharding
task_p = template.set_decoding_sharding_hparams(
task_p,
mesh_shape=self.ICI_MESH_SHAPE,
decode_mesh_transpose=self.DECODE_MESH_TRANSPOSE,
)
# Unused.
lp = task_p.train.learner
lp.loss_name = 'total_loss'
lp.optimizer = pax_fiddle.Config(
optimizers.ShardedSgd,
learning_rate=1e-3,
lr_schedule=pax_fiddle.Config(schedules.Constant)
)
return task_p
@servable_model_registry.register
class LLaMA7BFP16(BaseLLaMA):
"""7B model on a A100-40GB.
Checkpoint:
gs://sax-data/pax-llama/7B/checkpoint_00000000/
April 14, 2023
Latency = 3.619s with 128 decoded tokens. 27ms per output token
"""
NUM_LAYERS = 32
VOCAB_SIZE = 32000
DIMS_PER_HEAD = 128
NUM_HEADS = 32
MODEL_DIMS = 4096
HIDDEN_DIMS = 11008
BATCH_SIZE = 1
NUM_SAMPLES = 1
INPUT_SEQ_LEN = 128
BUCKET_KEYS = None
MAX_DECODE_STEPS = 32
ENABLE_GENERATE_STREAM = False
ICI_MESH_SHAPE = [1, 1, 1]
@servable_model_registry.register
class LLaMA7BFP16TPUv4(LLaMA7BFP16):
"""7B model on TPU v4-8.
April 14, 2023
Latency = 0.688s with 128 decoded tokens. 5ms per output token
"""
ICI_MESH_SHAPE = [1, 1, 4]
@property
def test_mode(self) -> bool:
return True
@servable_model_registry.register
class LLaMA7BFP16TPUv5e(LLaMA7BFP16):
"""7B model on TPU v5e-4.
"""
BATCH_SIZE = [1]
BUCKET_KEYS = [128]
MAX_DECODE_STEPS = [32]
ICI_MESH_SHAPE = [1, 1, 4]
@property
def test_mode(self) -> bool:
return False
@servable_model_registry.register
@quantization.for_transformer(quantize_on_the_fly=False)
class LLaMA7B(BaseLLaMA):
"""7B model on a A100-40GB.
April 12, 2023
Latency = 2.337s with 128 decoded tokens. 17ms per output token.
"""
NUM_LAYERS = 32
VOCAB_SIZE = 32000
DIMS_PER_HEAD = 128
NUM_HEADS = 32
MODEL_DIMS = 4096
HIDDEN_DIMS = 11008
ICI_MESH_SHAPE = [1, 1, 1]
@property
def test_mode(self) -> bool:
return True
@servable_model_registry.register
@quantization.for_transformer(quantize_on_the_fly=False)
class LLaMA13B(BaseLLaMA):
"""13B model on a A100-40GB.
April 12, 2023
Latency = 5.06s with 128 decoded tokens. 38ms per output token.
"""
NUM_LAYERS = 40
VOCAB_SIZE = 32000
DIMS_PER_HEAD = 128
NUM_HEADS = 40
MODEL_DIMS = 5120
HIDDEN_DIMS = 13824
ICI_MESH_SHAPE = [1, 1, 1]
@property
def test_mode(self) -> bool:
return True
@servable_model_registry.register
@quantization.for_transformer(quantize_on_the_fly=False)
class LLaMA33B(BaseLLaMA):
"""33B model on TPU v4-8.
April 12, 2023
Latency = 3.35s with 128 decoded tokens. 25ms per output token.
"""
NUM_LAYERS = 60
VOCAB_SIZE = 32000
DIMS_PER_HEAD = 128
NUM_HEADS = 52
MODEL_DIMS = 6656
HIDDEN_DIMS = 17920
ICI_MESH_SHAPE = [1, 1, 4]
@property
def test_mode(self) -> bool:
return True
@servable_model_registry.register
@quantization.for_transformer(quantize_on_the_fly=False)
class LLaMA65B(BaseLLaMA):
"""65B model on TPUv4-8.
April 12, 2023
Latency = 5.9s with 128 decoded tokens. 45ms per output token.
"""
NUM_LAYERS = 80
VOCAB_SIZE = 32000
DIMS_PER_HEAD = 128
NUM_HEADS = 64
MODEL_DIMS = 8192
HIDDEN_DIMS = 22016
ICI_MESH_SHAPE = [1, 1, 4]
@property
def test_mode(self) -> bool:
return True
# LlaMa2 70B models (use grouped query attention)
@servable_model_registry.register
class LLaMA70BFP16TPUv5e(BaseLLaMA):
"""LlaMA-2 70B model on TPUv5-16."""
NUM_LAYERS = 80
VOCAB_SIZE = 32000
DIMS_PER_HEAD = 128
NUM_HEADS = 64
MODEL_DIMS = 8192
HIDDEN_DIMS = 28672
USE_MQA = True
NUM_KV_HEADS = 8
ICI_MESH_SHAPE = [1, 1, 16]
MAX_DECODE_STEPS = 128
ENABLE_GENERATE_STREAM = False
@property
def test_mode(self) -> bool:
return True
@servable_model_registry.register
class LLaMA70BFP16TPUv5e32(LLaMA70BFP16TPUv5e):
"""LlaMA-2 70B model on TPUv5-32."""
ICI_MESH_SHAPE = [1, 1, 32]
@servable_model_registry.register
class LLaMA70BFP16TPUv5e64(LLaMA70BFP16TPUv5e):
"""LlaMA-2 70B model on TPUv5-64."""
ICI_MESH_SHAPE = [1, 1, 64]
# GPT-J/NeoX family
@template.make_servable()
class BaseNeoX(base_experiment.BaseExperiment):
"""Base GPTJ/NeoX Transformer LM configuration."""
SPM_MODEL = '/cns/mf-d/home/huangyp/ulm/pax-gptj/tokenizer.model'
SOS_ID = 0
EOS_ID = 2
# architecture related
NUM_LAYERS = 32
VOCAB_SIZE = 32000
DIMS_PER_HEAD = 128
NUM_HEADS = 32
MODEL_DIMS = 4096
HIDDEN_DIMS = MODEL_DIMS * 4
NORM_POLICY = 'pre-hybrid'
FPROP_DTYPE = jnp.bfloat16
MODEL_DTYPE = jnp.bfloat16
ACTIVATION_CLS = activations.GELU
RMS_NORM_EPSILON = 1.0e-05
# Sub-class has to specify a mesh.
ICI_MESH_SHAPE = [1, 1, 1]
DCN_MESH_SHAPE = None
DECODE_MESH_TRANSPOSE = None
BATCH_SIZE = 1
NUM_SAMPLES = 1
ENABLE_GENERATE_STREAM = True
STREAM_INTERVAL_STEPS = 16
FPROP_FOR_PREFIX = True
INPUT_SEQ_LEN = 4096
BUCKET_KEYS = [128, 1024, 4096]
MAX_DECODE_STEPS = [128, 512, 1024]
EXTRA_INPUTS = {
'temperature': 0.5,
'per_example_max_decode_steps': 128,
'per_example_top_k': 200,
'per_example_top_p': 0.95,
}
def datasets(self) -> List[pax_fiddle.Config[base_input.BaseInput]]:
return []
def task(self) -> pax_fiddle.Config[tasks_lib.SingleTask]:
"""Returns the task parameters."""
task_p = pax_fiddle.Config(tasks_lib.SingleTask, name='xformer_task')
task_p.model = pax_fiddle.Config(layers.LanguageModel, name='xformer_lm')
model_p = task_p.model
model_p.lm_tpl.packed_input = False
model_p.lm_tpl.model_dims = self.MODEL_DIMS
model_p.lm_tpl.vocab_size = self.VOCAB_SIZE
model_p.lm_tpl.position_emb_tpl = None
model_p.lm_tpl.softmax_tpl = pax_fiddle.Config(
layers.FullSoftmax,
name='output',
input_dims=self.MODEL_DIMS,
num_classes=self.VOCAB_SIZE,
)
model_p.lm_tpl.softmax_tpl.feed_forward_tpl.has_bias = False
model_p.lm_tpl.separate_embedding_tpl = pax_fiddle.Config(
layers.Embedding,
name='tok_embeddings',
input_dims=self.MODEL_DIMS,
num_classes=self.VOCAB_SIZE,
)
model_p.lm_tpl.final_ln_tpl.epsilon = self.RMS_NORM_EPSILON
stacked_transformer_tpl = pax_fiddle.Config(layers.StackedTransformer)
stacked_transformer_tpl.model_dims = self.MODEL_DIMS
stacked_transformer_tpl.hidden_dims = self.HIDDEN_DIMS
stacked_transformer_tpl.num_layers = self.NUM_LAYERS
stacked_transformer_tpl.num_heads = self.NUM_HEADS
stacked_transformer_tpl.dim_per_head = self.DIMS_PER_HEAD
transformer_layer_p = cast(
pax_fiddle.Config[ParallelTransformer],
stacked_transformer_tpl.transformer_layer_params_tpl,
)
transformer_layer_p.norm_policy = self.NORM_POLICY
transformer_layer_p.ln_tpl.epsilon = self.RMS_NORM_EPSILON
transformer_layer_p.tr_atten_tpl.internal_enable_per_dim_scale = False
transformer_layer_p.tr_atten_tpl.internal_enable_query_scale = True
transformer_layer_p.tr_atten_tpl.use_bias = True
transformer_layer_p.tr_atten_tpl.combine_qkv = True
transformer_layer_p.tr_atten_tpl.rotary_position_emb_tpl = (
pax_fiddle.Config(LLaMARotaryEmbedding)
)
transformer_layer_p.tr_atten_tpl.use_rotary_position_emb = True
transformer_layer_p.tr_fflayer_tpl.has_bias = True
transformer_layer_p.tr_fflayer_tpl.activation_tpl = pax_fiddle.Config(
self.ACTIVATION_CLS
)
transformer_layer_p.tr_fflayer_tpl.use_gated_activation = False
model_p.lm_tpl.stacked_transformer_tpl = stacked_transformer_tpl
model_p.fprop_dtype = self.FPROP_DTYPE
model_p.dtype = self.MODEL_DTYPE
# Set sharding
task_p = template.set_decoding_sharding_hparams(
task_p,
mesh_shape=self.ICI_MESH_SHAPE,
decode_mesh_transpose=self.DECODE_MESH_TRANSPOSE,
)
# Unused.
lp = task_p.train.learner
lp.loss_name = 'total_loss'
lp.optimizer = pax_fiddle.Config(
optimizers.ShardedSgd,
learning_rate=1e-3,
lr_schedule=pax_fiddle.Config(schedules.Constant)
)
return task_p
@servable_model_registry.register
@template.make_servable()
class LmCloudSpmd2B(lm_cloud.LmCloudSpmd2B):
# pylint: disable=line-too-long
"""Servable config on 1x1x4.
Checkpoint:
gs://sax-data/lm_cloud_2b_mesh_3/1/checkpoints/checkpoint_00000000
"""
# pylint: enable=line-too-long
SPM_MODEL = os.path.join(os.path.dirname(__file__), 'test_model.model')
ICI_MESH_SHAPE = [1, 1, 4]
FPROP_FOR_PREFIX = True
BATCH_SIZE = 1
TRAINING_OPTIMIZED_SHARDING = False
USE_REPEATED_LAYER = True
def task(self) -> pax_fiddle.Config[tasks_lib.SingleTask]:
task_p = super().task()
task_p = template.set_decoding_sharding_hparams(
task_p,
mesh_shape=self.ICI_MESH_SHAPE,
)
return task_p
@servable_model_registry.register
class LmCloudSpmd2BTest(LmCloudSpmd2B):
"""2B Servable config on 1x1x1 in test mode."""
ICI_MESH_SHAPE = [1, 1, 1]
@property
def test_mode(self) -> bool:
return True
@servable_model_registry.register
class LmCloudSpmd2B4Test(LmCloudSpmd2BTest):
"""2B Servable config on 1x1x4 in test mode."""
ICI_MESH_SHAPE = [1, 1, 4]
@servable_model_registry.register
class LmCloudSpmd2B8Test(LmCloudSpmd2BTest):
"""2B Servable config on 1x1x8 in test mode."""
ICI_MESH_SHAPE = [1, 1, 8]
@servable_model_registry.register
class LmCloudSpmd2B16Test(LmCloudSpmd2BTest):
"""2B Servable config on 1x1x16 in test mode."""
ICI_MESH_SHAPE = [1, 1, 16]
@servable_model_registry.register
class LmCloudSpmd2B32Test(LmCloudSpmd2BTest):
"""2B Servable config on 1x1x32 in test mode."""
ICI_MESH_SHAPE = [1, 1, 32]
@servable_model_registry.register
@quantization.for_transformer(quantize_on_the_fly=False)
class LmCloudSpmd175B(LmCloudSpmd2B):
"""175B on TPU v4-32.
April 14, 2023
Latency = 2.337s with 128 decoded tokens. 17ms per output token
"""
NUM_LAYERS = 96
MODEL_DIMS = 12288
NUM_HEADS = 96
DIMS_PER_HEAD = 128
HIDDEN_DIMS = MODEL_DIMS * 4
ICI_MESH_SHAPE = [1, 1, 16]
BATCH_SIZE = 1
NUM_SAMPLES = 1
FPROP_FOR_PREFIX = True
INPUT_SEQ_LEN = 128 # 4096
BUCKET_KEYS = None # [128, 1024, 4096]
MAX_DECODE_STEPS = 128 # [128, 512, 1024]
EXTRA_INPUTS = {
'temperature': 0.5,
'per_example_max_decode_steps': 128,
'per_example_top_k': 200,
'per_example_top_p': 0.95,
}
@servable_model_registry.register
class LmCloudSpmd175BTest(LmCloudSpmd175B):
"""175B on TPU v4-32 in test mode."""
@property
def test_mode(self) -> bool:
return True
@servable_model_registry.register
class LmCloudSpmd175B32Test(LmCloudSpmd175BTest):
"""175B Servable config on 1x1x32 in test mode."""
ICI_MESH_SHAPE = [1, 1, 32]
@servable_model_registry.register
class LmCloudSpmd175B64Test(LmCloudSpmd175BTest):
"""175B Servable config on 1x1x64 in test mode."""
ICI_MESH_SHAPE = [1, 1, 64]
@servable_model_registry.register
class LmCloudSpmd175B128Test(LmCloudSpmd175BTest):
"""175B Servable config on 1x1x128 in test mode."""
ICI_MESH_SHAPE = [1, 1, 128]
@servable_model_registry.register
class LmCloudSpmd175B256Test(LmCloudSpmd175BTest):
"""175B Servable config on 1x1x256 in test mode."""
ICI_MESH_SHAPE = [1, 1, 256]