对于 Transformers 系列模型,由于这些模型在自然语言处理(NLP)领域内非常流行,很多深度学习框架都提供了对它们的支持和实现,比如 TensorFlow 的 transformers
库和 PyTorch 的 transformers
库(由 Hugging Face 提供)。这些库中的模型通常包括 BERT、GPT、RoBERTa、Transformer-XL、T5 等。
这里使用 ai21labs/Jamba-v0.1 用作测试模型
1 |
https://huggingface.co/ai21labs/Jamba-v0.1/ |
1. 下载模型,查看模型的大小
模型大概有96GB大小,单个GPU可能无法加载,需要使用多个GPU进行加载
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 |
ls -l ai21labs/Jamba-v0.1/ total 100728956 -rwxrwxrwx 1 tony tony 1310 Mar 29 13:11 config.json -rwxrwxrwx 1 tony tony 11238 Mar 29 13:11 configuration_jamba.py -rwxrwxrwx 1 tony tony 137 Mar 29 13:11 generation_config.json -rwxrwxrwx 1 tony tony 1519 Mar 29 13:11 gitattributes -rwxrwxrwx 1 tony tony 4951236864 Mar 29 13:46 model-00001-of-00021.safetensors -rwxrwxrwx 1 tony tony 4884145024 Mar 29 13:48 model-00002-of-00021.safetensors -rwxrwxrwx 1 tony tony 4992294632 Mar 29 13:49 model-00003-of-00021.safetensors -rwxrwxrwx 1 tony tony 4958591040 Mar 29 13:50 model-00004-of-00021.safetensors -rwxrwxrwx 1 tony tony 4975501296 Mar 29 13:50 model-00005-of-00021.safetensors -rwxrwxrwx 1 tony tony 4884145016 Mar 29 14:01 model-00006-of-00021.safetensors -rwxrwxrwx 1 tony tony 4884144968 Mar 29 14:39 model-00007-of-00021.safetensors -rwxrwxrwx 1 tony tony 4992294696 Mar 29 14:43 model-00008-of-00021.safetensors -rwxrwxrwx 1 tony tony 4932506800 Mar 29 14:41 model-00009-of-00021.safetensors -rwxrwxrwx 1 tony tony 4884145056 Mar 29 14:48 model-00010-of-00021.safetensors -rwxrwxrwx 1 tony tony 4884145088 Mar 29 15:08 model-00011-of-00021.safetensors -rwxrwxrwx 1 tony tony 4884145088 Mar 29 15:09 model-00012-of-00021.safetensors -rwxrwxrwx 1 tony tony 4932506800 Mar 29 15:10 model-00013-of-00021.safetensors -rwxrwxrwx 1 tony tony 4992294648 Mar 29 15:11 model-00014-of-00021.safetensors -rwxrwxrwx 1 tony tony 4884145088 Mar 29 15:11 model-00015-of-00021.safetensors -rwxrwxrwx 1 tony tony 4884145088 Mar 29 15:32 model-00016-of-00021.safetensors -rwxrwxrwx 1 tony tony 4908260352 Mar 29 15:34 model-00017-of-00021.safetensors -rwxrwxrwx 1 tony tony 4908391496 Mar 29 15:34 model-00018-of-00021.safetensors -rwxrwxrwx 1 tony tony 4992294688 Mar 29 15:43 model-00019-of-00021.safetensors -rwxrwxrwx 1 tony tony 4884145088 Mar 29 15:43 model-00020-of-00021.safetensors -rwxrwxrwx 1 tony tony 4647318256 Mar 29 15:43 model-00021-of-00021.safetensors -rwxrwxrwx 1 tony tony 107400 Mar 29 13:11 model.safetensors.index.json -rwxrwxrwx 1 tony tony 99785 Mar 29 13:11 modeling_jamba.py -rwxrwxrwx 1 tony tony 121 Mar 29 13:11 special_tokens_map.json -rwxrwxrwx 1 tony tony 4242082 Mar 29 13:12 tokenizer.json -rwxrwxrwx 1 tony tony 1124742 Mar 29 13:12 tokenizer.model -rwxrwxrwx 1 tony tony 1109 Mar 29 13:12 tokenizer_config.json |
2. 使用 AutoConfig 获取模型参数
1 2 3 4 5 6 7 |
from transformers import AutoConfig # 加载特定模型的配置 config = AutoConfig.from_pretrained('ai21labs/Jamba-v0.1', trust_remote_code=True) # 显示配置信息 print(config) |
这个时候可以放心运行,不加载模型,只加载模型的配置文件,运行输出结果
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 |
python test01.py JambaConfig { "_name_or_path": "ai21labs/Jamba-v0.1", "architectures": [ "JambaForCausalLM" ], "attention_dropout": 0.0, "attn_layer_offset": 4, "attn_layer_period": 8, "auto_map": { "AutoConfig": "configuration_jamba.JambaConfig", "AutoModel": "modeling_jamba.JambaModel", "AutoModelForCausalLM": "modeling_jamba.JambaForCausalLM", "AutoModelForSequenceClassification": "model.JambaForSequenceClassification" }, "bos_token_id": 1, "calc_logits_for_entire_prompt": false, "eos_token_id": 2, "expert_layer_offset": 1, "expert_layer_period": 2, "hidden_act": "silu", "hidden_size": 4096, "initializer_range": 0.02, "intermediate_size": 14336, "mamba_conv_bias": true, "mamba_d_conv": 4, "mamba_d_state": 16, "mamba_dt_rank": 256, "mamba_expand": 2, "mamba_inner_layernorms": true, "mamba_proj_bias": false, "model_type": "jamba", "n_ctx": 262144, "num_attention_heads": 32, "num_experts": 16, "num_experts_per_tok": 2, "num_hidden_layers": 32, "num_key_value_heads": 8, "output_router_logits": false, "pad_token_id": 0, "rms_norm_eps": 1e-06, "router_aux_loss_coef": 0.001, "sliding_window": null, "tie_word_embeddings": false, "torch_dtype": "bfloat16", "transformers_version": "4.39.2", "use_cache": true, "use_mamba_kernels": true, "vocab_size": 65536 } |
从结果来看,其实是模型目录下的 config.json 文件里面的内容
从提供的输出来看,test01.py
脚本很可能是调用了 Hugging Face transformers
库中的 AutoConfig.from_pretrained
方法,用于加载一个名为 ai21labs/Jamba-v0.1
的模型配置。这个配置属于 JambaConfig
类,这是一个定制化的模型配置,可能是为 Jamba
模型特别设计的。
输出中包含了大量的配置信息,其中一些关键字段说明如下:
"architectures"
: 显示了模型的架构,这里是JambaForCausalLM
,表明这个配置是为因果语言模型设计的。"hidden_size"
: 隐藏层的大小是 4096。"num_hidden_layers"
: 表明模型有 32 层隐藏层。"num_attention_heads"
: 模型使用了 32 个注意力头。"vocab_size"
: 词汇表的大小是 65536。"transformers_version"
: 指明了transformers
库的版本是4.39.2
。
此外,还包含了一些针对 Jamba
模型特有的配置项,如 mamba_conv_bias
、mamba_d_conv
、mamba_d_state
等,这些配置项可能与模型中特殊的卷积或状态处理机制有关。
num_hidden_layers
指的是该模型架构中的隐藏层总数。这个参数就代表了模型中相应部分的层数。
了解这个参数 num_hidden_layers
对后面的模型加载有用。
3. 使用 CPU 加载模型
当你的 GPU 内存不够使用的时候,使用 CPU 加载模型,如果内存不够(此模型大概需要 212GB),可能需要增大交换区(linux),可以获取模型参数
1 2 3 4 5 6 7 |
from transformers import AutoModelForCausalLM import torch MODEL_NAME = "ai21labs/Jamba-v0.1" device = torch.device("cpu") model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, trust_remote_code=True).to(device) print(model) |
运行结果如下:
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 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 |
python test02.py Loading checkpoint shards: 100%|████████████| 21/21 [27:07<00:00, 77.52s/it] JambaForCausalLM( (model): JambaModel( (embed_tokens): Embedding(65536, 4096, padding_idx=0) (layers): ModuleList( (0): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (1): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (2): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (3): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (4): JambaAttentionDecoderLayer( (self_attn): JambaSdpaAttention( (q_proj): Linear(in_features=4096, out_features=4096, bias=False) (k_proj): Linear(in_features=4096, out_features=1024, bias=False) (v_proj): Linear(in_features=4096, out_features=1024, bias=False) (o_proj): Linear(in_features=4096, out_features=4096, bias=False) ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (5): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (6): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (7): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (8): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (9): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (10): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (11): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (12): JambaAttentionDecoderLayer( (self_attn): JambaSdpaAttention( (q_proj): Linear(in_features=4096, out_features=4096, bias=False) (k_proj): Linear(in_features=4096, out_features=1024, bias=False) (v_proj): Linear(in_features=4096, out_features=1024, bias=False) (o_proj): Linear(in_features=4096, out_features=4096, bias=False) ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (13): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (14): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (15): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (16): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (17): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (18): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (19): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (20): JambaAttentionDecoderLayer( (self_attn): JambaSdpaAttention( (q_proj): Linear(in_features=4096, out_features=4096, bias=False) (k_proj): Linear(in_features=4096, out_features=1024, bias=False) (v_proj): Linear(in_features=4096, out_features=1024, bias=False) (o_proj): Linear(in_features=4096, out_features=4096, bias=False) ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (21): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (22): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (23): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (24): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (25): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (26): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (27): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (28): JambaAttentionDecoderLayer( (self_attn): JambaSdpaAttention( (q_proj): Linear(in_features=4096, out_features=4096, bias=False) (k_proj): Linear(in_features=4096, out_features=1024, bias=False) (v_proj): Linear(in_features=4096, out_features=1024, bias=False) (o_proj): Linear(in_features=4096, out_features=4096, bias=False) ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (29): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (30): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (experts): ModuleList( (0): JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) (31): JambaMambaDecoderLayer( (mamba): JambaMambaMixer( (conv1d): Conv1d(8192, 8192, kernel_size=(4,), stride=(1,), padding=(3,), groups=8192) (act): SiLU() (in_proj): Linear(in_features=4096, out_features=16384, bias=False) (x_proj): Linear(in_features=8192, out_features=288, bias=False) (dt_proj): Linear(in_features=256, out_features=8192, bias=True) (out_proj): Linear(in_features=8192, out_features=4096, bias=False) (dt_layernorm): JambaRMSNorm() (B_layernorm): JambaRMSNorm() (C_layernorm): JambaRMSNorm() ) (moe): JambaSparseMoeBlock( (router): Linear(in_features=4096, out_features=16, bias=False) (experts): ModuleList( (0-15): 16 x JambaMLP( (gate_proj): Linear(in_features=4096, out_features=14336, bias=False) (down_proj): Linear(in_features=14336, out_features=4096, bias=False) (up_proj): Linear(in_features=4096, out_features=14336, bias=False) (act_fn): SiLU() ) ) ) (input_layernorm): JambaRMSNorm() (pre_moe_layernorm): JambaRMSNorm() ) ) (final_layernorm): JambaRMSNorm() ) (lm_head): Linear(in_features=4096, out_features=65536, bias=False) ) |
4. 使用多个 GPU 加载模型
当你的单个 GPU 内存不够使用的时候,可能需要多个GPU,这里我们使用了 8 个 RTX 4090 的卡, 实际上对于这个模型,不需要这么多。
从CPU的参数来看,可以得知,有如下权重参数:
- model.embed_tokens
- model.norm
- model.final_layernorm
- lm_head
- model.layers (总共32层)
我们可以将Transformer模型的每一层分配给一个GPU,每个GPU可能是多层
代码如下:
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 |
from transformers import AutoTokenizer, AutoModelForCausalLM import torch # 定义一个函数来自动配置在多GPU环境下模型各部分的设备分布 def auto_configure_device_map(num_gpus: int): num_trans_layers = 32 # 定义Transformer模型的层数 per_gpu_layers = num_trans_layers / num_gpus # 计算每个GPU应承担的层数 # 初始化设备映射字典,指定一些特定模块应该放置的GPU编号 device_map = { 'model.embed_tokens': 0, # 嵌入层放在第一个GPU上 'model.norm': num_gpus-1, # 最后一个正则化层放在最后一个GPU上 'lm_head': num_gpus-1, # 语言模型头(用于预测下一个词的层)放在最后一个GPU上 'model.final_layernorm': num_gpus-1 # 最后一个LayerNorm层也放在最后一个GPU上 } # 将Transformer模型的每一层分配给一个GPU for i in range(num_trans_layers): device_map[f'model.layers.{i}'] = int(i//per_gpu_layers) return device_map MODEL_NAME = "ai21labs/Jamba-v0.1" # 检测可用的GPU数量 NUM_GPUS = torch.cuda.device_count() MAX_TOKENS = 512 # 定义最大令牌数 # 如果有可用的GPU,则基于GPU数量自动配置设备映射;否则不使用设备映射 device_map = auto_configure_device_map(NUM_GPUS) if NUM_GPUS > 0 else None # 如果有可用的GPU,则使用第一个GPU;否则使用CPU device = torch.device("cuda") if NUM_GPUS > 0 else torch.device("cpu") # 根据是否使用GPU设置数据类型(半精度或全精度) device_dtype = torch.half if NUM_GPUS > 0 else torch.float # 加载分词器和模型,指定设备映射和数据类型 tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, trust_remote_code=True, device_map=device_map, torch_dtype=device_dtype) # 将模型设置为评估模式 model = model.eval() # 对输入文本进行分词,并将分词结果移到模型所在设备 input_ids = tokenizer("In the recent Super Bowl LVIII,", return_tensors='pt').to(device)["input_ids"] # 使用模型生成文本 outputs = model.generate(input_ids, max_new_tokens=MAX_TOKENS) # 打印生成的文本 print(tokenizer.batch_decode(outputs)) |
输出的结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
python test03.py Loading checkpoint shards: 100%|█████████| 21/21 [27:41<00:00, 79.11s/it] The `seen_tokens` attribute is deprecated and will be removed in v4.41. Use the `cache_position` model input instead. ["<|startoftext|>In the recent Super Bowl LVIII, the Kansas City Chiefs emerged victorious, defeating the San Francisco 49ers in a thrilling overtime showdown. The game was a nail-biter, with both teams showcasing their skills and determination. The Chiefs, led by their star quarterback Patrick Mahomes, displayed their offensive prowess, while the 49ers, led by their defense, put up a strong fight. The game went into overtime, with the Chiefs ultimately securing the win with a touchdown. The victory marked the Chiefs' second Super Bowl win in four years, solidifying their status as one of the top teams in the NFL. The game was a testament to the skill and determination of both teams, and it will be remembered as one of the most exciting Super Bowls in recent history. The Kansas City Chiefs' victory in Super Bowl LVIII was a thrilling and historic moment for the team and its fans. The game was a testament to the skill and determination of both teams, and it will be remembered as one of the most exciting Super Bowls in recent history. The Chiefs' victory was a result of their offensive prowess, led by their star quarterback Patrick Mahomes, and their ability to come back from a 10-point deficit in the fourth quarter. The 49ers, led by their defense, put up a strong fight, but ultimately fell short in overtime. The game was a showcase of the best of the NFL, with both teams displaying their skills and determination. The Chiefs' victory was a culmination of their hard work and dedication throughout the season, and it will be remembered as a historic moment in the team's history. The game was a testament to the skill and determination of both teams, and it will be remembered as one of the most exciting Super Bowls in recent history. The Chiefs' victory was a result of their offensive prowess, led by their star quarterback Patrick Mahomes, and their ability to come back from a 10-point deficit in the fourth quarter. The 49ers, led by their defense, put up a strong fight, but ultimately fell short in overtime. The game was a showcase of the best of the NFL, with both teams displaying their skills and determination. The Chiefs' victory was a culmination of their hard work and dedication throughout the season, and it will be remembered as a historic moment in the team's history. The game was a testament to the skill and determination of both teams, and it will be remembered"] |
5. 多 GPU int8 和 int4 量化部署
量化后的 GPU 内存,需要的更少。但加载的时间会更长,下面的代码是int4的
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 |
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig import torch # 定义一个函数来自动配置在多GPU环境下模型各部分的设备分布 def auto_configure_device_map(num_gpus: int): num_trans_layers = 32 # 定义Transformer模型的层数 per_gpu_layers = num_trans_layers / num_gpus # 计算每个GPU应承担的层数 # 初始化设备映射字典,指定一些特定模块应该放置的GPU编号 device_map = { 'model.embed_tokens': 0, # 嵌入层放在第一个GPU上 'model.norm': num_gpus-1, # 最后一个正则化层放在最后一个GPU上 'lm_head': num_gpus-1, # 语言模型头(用于预测下一个词的层)放在最后一个GPU上 'model.final_layernorm': num_gpus-1 # 最后一个LayerNorm层也放在最后一个GPU上 } # 将Transformer模型的每一层分配给一个GPU for i in range(num_trans_layers): device_map[f'model.layers.{i}'] = int(i//per_gpu_layers) return device_map MODEL_NAME = "ai21labs/Jamba-v0.1" # 检测可用的GPU数量 NUM_GPUS = torch.cuda.device_count() MAX_TOKENS = 512 # 定义最大令牌数 # 如果有可用的GPU,则基于GPU数量自动配置设备映射;否则不使用设备映射 device_map = auto_configure_device_map(NUM_GPUS) if NUM_GPUS > 0 else None # 如果有可用的GPU,则使用第一个GPU;否则使用CPU device = torch.device("cuda") if NUM_GPUS > 0 else torch.device("cpu") # 根据是否使用GPU设置数据类型(半精度或全精度) device_dtype = torch.float16 if NUM_GPUS > 0 else torch.float # 加载分词器和模型,指定设备映射和数据类型 tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True) quantization_config = BitsAndBytesConfig(load_in_4bit=True, llm_int4_skip_modules=["mamba"]) model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, trust_remote_code=True, torch_dtype=torch.bfloat16, attn_implementation="flash_attention_2", quantization_config=quantization_config, device_map=device_map) #model = dispatch_model(model, device_map=device_map) # 将模型设置为评估模式 model = model.eval() # 对输入文本进行分词,并将分词结果移到模型所在设备 input_ids = tokenizer("In the recent Super Bowl LVIII,", return_tensors='pt').to(device)["input_ids"] # 使用模型生成文本 outputs = model.generate(input_ids, max_new_tokens=MAX_TOKENS) # 打印生成的文本 print(tokenizer.batch_decode(outputs)) |
如果需要int8 的,只需要修改:
1 2 |
quantization_config = BitsAndBytesConfig(load_in_8bit=True, llm_int8_skip_modules=["mamba"]) |
运行结果:
1 2 3 4 5 6 7 8 9 |
python test06.py Loading checkpoint shards: 100%|████████████| 21/21 [1:25:22<00:00, 243.91s/it] The `seen_tokens` attribute is deprecated and will be removed in v4.41. Use the `cache_position` model input instead. ["<|startoftext|>In the recent Super Bowl LVIII, the Kansas City Chiefs emerged victorious, defeating the San Francisco 49ers 25-22 in overtime. The game, which took place at Allegiant Stadium in Las Vegas, Nevada, was a thrilling spectacle for football fans around the world. The Chiefs, led by their star quarterback Patrick Mahomes, showcased their resilience and determination throughout the game. Mahomes, who was named the Super Bowl MVP for the second time in his career, delivered an outstanding performance, throwing for 333 yards and two touchdowns. The 49ers, on the other hand, put up a valiant effort but fell short in the end. Quarterback Brock Purdy, who had a breakout season, struggled to find his rhythm against the Chiefs' formidable defense. Despite a strong start, the 49ers were unable to maintain their momentum and ultimately fell short of their goal. The game was not without its controversies, however. One of the most talked-about moments was a controversial call by the referees in the fourth quarter, which many believed cost the 49ers a crucial touchdown. The call sparked outrage among fans and pundits alike, with many questioning the integrity of the officiating. Despite the controversies, the Super Bowl LVIII was a memorable event for football fans. The halftime show, featuring performances by Usher and Alicia Keys, was a highlight of the evening, with fans praising the energy and excitement of the show. Overall, Super Bowl LVIII was a thrilling and unforgettable event, showcasing the best of American football and entertainment. The Kansas City Chiefs' victory will go down in history as one of the greatest moments in football history, and the controversies surrounding the game will continue to be debated for years to come.<|endoftext|>"] |