成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用Accelerate庫在多GPU上進行LLM推理

人工智能
大型語言模型(llm)已經徹底改變了自然語言處理領域。隨著這些模型在規模和復雜性上的增長,推理的計算需求也顯著增加。為了應對這一挑戰利用多個gpu變得至關重要。

大型語言模型(llm)已經徹底改變了自然語言處理領域。隨著這些模型在規模和復雜性上的增長,推理的計算需求也顯著增加。為了應對這一挑戰利用多個gpu變得至關重要。

所以本文將在多個gpu上并行執行推理,主要包括:Accelerate庫介紹,簡單的方法與工作代碼示例和使用多個gpu的性能基準測試。

本文將使用多個3090將llama2-7b的推理擴展在多個GPU上

基本示例

我們首先介紹一個簡單的示例來演示使用Accelerate進行多gpu“消息傳遞”。

from accelerate import Accelerator
 from accelerate.utils import gather_object
 
 accelerator = Accelerator()
 
 # each GPU creates a string
 message=[ f"Hello this is GPU {accelerator.process_index}" ] 
 
 # collect the messages from all GPUs
 messages=gather_object(message)
 
 # output the messages only on the main process with accelerator.print() 
 accelerator.print(messages)

輸出如下:

['Hello this is GPU 0', 
  'Hello this is GPU 1', 
  'Hello this is GPU 2', 
  'Hello this is GPU 3', 
  'Hello this is GPU 4']

多GPU推理

下面是一個簡單的、非批處理的推理方法。代碼很簡單,因為Accelerate庫已經幫我們做了很多工作,我們直接使用就可以:

from accelerate import Accelerator
 from accelerate.utils import gather_object
 from transformers import AutoModelForCausalLM, AutoTokenizer
 from statistics import mean
 import torch, time, json
 
 accelerator = Accelerator()
 
 # 10*10 Prompts. Source: https://www.penguin.co.uk/articles/2022/04/best-first-lines-in-books
 prompts_all=[
    "The King is dead. Long live the Queen.",
    "Once there were four children whose names were Peter, Susan, Edmund, and Lucy.",
    "The story so far: in the beginning, the universe was created.",
    "It was a bright cold day in April, and the clocks were striking thirteen.",
    "It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.",
    "The sweat wis lashing oafay Sick Boy; he wis trembling.",
    "124 was spiteful. Full of Baby's venom.",
    "As Gregor Samsa awoke one morning from uneasy dreams he found himself transformed in his bed into a gigantic insect.",
    "I write this sitting in the kitchen sink.",
    "We were somewhere around Barstow on the edge of the desert when the drugs began to take hold.",
 ] * 10
 
 # load a base model and tokenizer
 model_path="models/llama2-7b"
 model = AutoModelForCausalLM.from_pretrained(
    model_path,    
    device_map={"": accelerator.process_index},
    torch_dtype=torch.bfloat16,
 )
 tokenizer = AutoTokenizer.from_pretrained(model_path)   
 
 # sync GPUs and start the timer
 accelerator.wait_for_everyone()
 start=time.time()
 
 # divide the prompt list onto the available GPUs 
 with accelerator.split_between_processes(prompts_all) as prompts:
    # store output of generations in dict
    results=dict(outputs=[], num_tokens=0)
 
    # have each GPU do inference, prompt by prompt
    for prompt in prompts:
        prompt_tokenized=tokenizer(prompt, return_tensors="pt").to("cuda")
        output_tokenized = model.generate(**prompt_tokenized, max_new_tokens=100)[0]
 
        # remove prompt from output 
        output_tokenized=output_tokenized[len(prompt_tokenized["input_ids"][0]):]
 
        # store outputs and number of tokens in result{}
        results["outputs"].append( tokenizer.decode(output_tokenized) )
        results["num_tokens"] += len(output_tokenized)
 
    results=[ results ] # transform to list, otherwise gather_object() will not collect correctly
 
 # collect results from all the GPUs
 results_gathered=gather_object(results)
 
 if accelerator.is_main_process:
    timediff=time.time()-start
    num_tokens=sum([r["num_tokens"] for r in results_gathered ])
 
    print(f"tokens/sec: {num_tokens//timediff}, time {timediff}, total tokens {num_tokens}, total prompts {len(prompts_all)}")

使用多個gpu會導致一些通信開銷:性能在4個gpu時呈線性增長,然后在這種特定設置中趨于穩定。當然這里的性能取決于許多參數,如模型大小和量化、提示長度、生成的令牌數量和采樣策略,所以我們只討論一般的情況

1 GPU: 44個token /秒,時間:225.5s

2 gpu: 88個token /秒,時間:112.9s

3 gpu: 128個token /秒,時間:77.6s

4 gpu: 137個token /秒,時間:72.7s

5 gpu: 119個token /秒,時間:83.8s

在多GPU上進行批處理

現實世界中,我們可以使用批處理推理來加快速度。這會減少GPU之間的通訊,加快推理速度。我們只需要增加prepare_prompts函數將一批數據而不是單條數據輸入到模型即可:

from accelerate import Accelerator
 from accelerate.utils import gather_object
 from transformers import AutoModelForCausalLM, AutoTokenizer
 from statistics import mean
 import torch, time, json
 
 accelerator = Accelerator()
 
 def write_pretty_json(file_path, data):
    import json
    with open(file_path, "w") as write_file:
        json.dump(data, write_file, indent=4)
 
 # 10*10 Prompts. Source: https://www.penguin.co.uk/articles/2022/04/best-first-lines-in-books
 prompts_all=[
    "The King is dead. Long live the Queen.",
    "Once there were four children whose names were Peter, Susan, Edmund, and Lucy.",
    "The story so far: in the beginning, the universe was created.",
    "It was a bright cold day in April, and the clocks were striking thirteen.",
    "It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.",
    "The sweat wis lashing oafay Sick Boy; he wis trembling.",
    "124 was spiteful. Full of Baby's venom.",
    "As Gregor Samsa awoke one morning from uneasy dreams he found himself transformed in his bed into a gigantic insect.",
    "I write this sitting in the kitchen sink.",
    "We were somewhere around Barstow on the edge of the desert when the drugs began to take hold.",
 ] * 10
 
 # load a base model and tokenizer
 model_path="models/llama2-7b"
 model = AutoModelForCausalLM.from_pretrained(
    model_path,    
    device_map={"": accelerator.process_index},
    torch_dtype=torch.bfloat16,
 )
 tokenizer = AutoTokenizer.from_pretrained(model_path)   
 tokenizer.pad_token = tokenizer.eos_token
 
 # batch, left pad (for inference), and tokenize
 def prepare_prompts(prompts, tokenizer, batch_size=16):
    batches=[prompts[i:i + batch_size] for i in range(0, len(prompts), batch_size)]  
    batches_tok=[]
    tokenizer.padding_side="left"     
    for prompt_batch in batches:
        batches_tok.append(
            tokenizer(
                prompt_batch, 
                return_tensors="pt", 
                padding='longest', 
                truncatinotallow=False, 
                pad_to_multiple_of=8,
                add_special_tokens=False).to("cuda") 
            )
    tokenizer.padding_side="right"
    return batches_tok
 
 # sync GPUs and start the timer
 accelerator.wait_for_everyone()    
 start=time.time()
 
 # divide the prompt list onto the available GPUs 
 with accelerator.split_between_processes(prompts_all) as prompts:
    results=dict(outputs=[], num_tokens=0)
 
    # have each GPU do inference in batches
    prompt_batches=prepare_prompts(prompts, tokenizer, batch_size=16)
 
    for prompts_tokenized in prompt_batches:
        outputs_tokenized=model.generate(**prompts_tokenized, max_new_tokens=100)
 
        # remove prompt from gen. tokens
        outputs_tokenized=[ tok_out[len(tok_in):] 
            for tok_in, tok_out in zip(prompts_tokenized["input_ids"], outputs_tokenized) ] 
 
        # count and decode gen. tokens 
        num_tokens=sum([ len(t) for t in outputs_tokenized ])
        outputs=tokenizer.batch_decode(outputs_tokenized)
 
        # store in results{} to be gathered by accelerate
        results["outputs"].extend(outputs)
        results["num_tokens"] += num_tokens
 
    results=[ results ] # transform to list, otherwise gather_object() will not collect correctly
 
 # collect results from all the GPUs
 results_gathered=gather_object(results)
 
 if accelerator.is_main_process:
    timediff=time.time()-start
    num_tokens=sum([r["num_tokens"] for r in results_gathered ])
 
    print(f"tokens/sec: {num_tokens//timediff}, time elapsed: {timediff}, num_tokens {num_tokens}")

可以看到批處理會大大加快速度。

1 GPU: 520 token /sec,時間:19.2s

2 gpu: 900 token /sec,時間:11.1s

3 gpu: 1205個token /秒,時間:8.2s

4 gpu: 1655 token /sec,時間:6.0s

5 gpu: 1658 token /sec,時間:6.0s

總結

截止到本文為止,llama.cpp,ctransformer還不支持多GPU推理,好像llama.cpp在6月有個多GPU的merge,但是我沒看到官方更新,所以這里暫時確定不支持多GPU。如果有小伙伴確認可以支持多GPU請留言。

huggingface的Accelerate包則為我們使用多GPU提供了一個很方便的選擇,使用多個GPU推理可以顯著提高性能,但gpu之間通信的開銷隨著gpu數量的增加而顯著增加。

責任編輯:華軒 來源: DeepHub IMBA
相關推薦

2024-03-25 14:22:07

大型語言模型GaLore

2020-03-07 18:51:11

EclipseFedoraPHP

2009-01-06 10:04:44

CygwinGCCGUI

2024-02-04 00:00:00

Triton格式TensorRT

2022-02-09 15:29:35

Java組件編程語言

2010-02-24 15:19:38

ibmdwLinux

2010-12-09 09:12:28

2020-02-18 09:45:44

云計算云平臺IT

2009-04-14 18:50:55

Nehalem惠普intel

2024-10-16 21:49:24

2023-06-20 08:00:00

2025-04-24 10:26:40

2025-04-23 15:49:37

2023-09-01 15:22:49

人工智能數據

2025-03-18 08:00:00

大語言模型KubeMQOpenAI

2024-01-11 16:24:12

人工智能RAG

2011-10-08 11:05:04

GPUMATLAB

2023-06-21 13:44:57

模型AI

2025-05-09 01:00:00

大語言模型LLMGPU內存

2024-08-28 13:34:13

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91在线免费视频 | 日韩av成人在线 | 日韩欧美在线不卡 | 婷婷开心激情综合五月天 | 91视频一区二区 | 99精品免费久久久久久日本 | 国产精品18久久久久久白浆动漫 | 四虎成人av| 中文字幕一区二区三区精彩视频 | 国产区一区二区三区 | 五月天综合网 | 国产精品毛片久久久久久 | aaaa网站| www国产成人免费观看视频,深夜成人网 | 亚洲视频一区二区三区四区 | 亚洲午夜精品一区二区三区他趣 | 欧美日韩久久 | 涩涩视频在线观看 | 亚洲第一av | 国产一区91精品张津瑜 | 激情六月天 | 天堂网中文字幕在线观看 | 免费视频一区二区 | 亚洲一区精品视频 | 中文字幕国产视频 | 四虎网站在线观看 | 91看片官网| 中文字幕一区二区三区乱码在线 | 日本精品视频 | 一级黄片一级毛片 | 久久久久久久一区二区 | 91天堂| 精品国产一区二区国模嫣然 | 成人免费视频网站在线观看 | 国产精品久久久久av | 极品粉嫩国产48尤物在线播放 | 日本不卡一区 | 99精品热视频| 99re国产精品 | 色又黄又爽网站www久久 | 久久国产日韩欧美 |