LLM 大模型學(xué)習(xí)必知必會系列(三):LLM和多模態(tài)模型高效推理實踐 1.多模態(tài)大模型推理 LLM 的推理流程: 多模態(tài)的 LLM 的原理: 代碼演示:使用 ModelScope NoteBook 完成語言大模型,視覺大模型,音頻大模型的推理 環(huán)境配置與安裝 以下主要演示的模型推理代碼可在魔搭社區(qū)免
LLM 的推理流程:
多模態(tài)的 LLM 的原理:
代碼演示:使用 ModelScope NoteBook 完成語言大模型,視覺大模型,音頻大模型的推理
環(huán)境配置與安裝
以下主要演示的模型推理代碼可在魔搭社區(qū)免費實例 PAI-DSW 的配置下運行(顯存 24G) :
#通義千問1_8B LLM大模型的推理代碼示例
#通義千問1_8B:https://modelscope.cn/models/qwen/Qwen-1_8B-Chat/summary
from modelscope import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
#Note: The default behavior now has injection attack prevention off.
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen-1_8B-Chat", revision='master', trust_remote_code=True)
#use bf16
#model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-1_8B-Chat", device_map="auto", trust_remote_code=True, bf16=True).eval()
#use fp16
#model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-1_8B-Chat", device_map="auto", trust_remote_code=True, fp16=True).eval()
#use cpu only
#model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-1_8B-Chat", device_map="cpu", trust_remote_code=True).eval()
#use auto mode, automatically select precision based on the device.
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-1_8B-Chat", revision='master', device_map="auto", trust_remote_code=True).eval()
#Specify hyperparameters for generation. But if you use transformers>=4.32.0, there is no need to do this.
#model.generation_config = GenerationConfig.from_pretrained("Qwen/Qwen-1_8B-Chat", trust_remote_code=True) # 可指定不同的生成長度、top_p等相關(guān)超參
#第一輪對話 1st dialogue turn
response, history = model.chat(tokenizer, "你好", history=None)
print(response)
# 你好!很高興為你提供幫助。
#第二輪對話 2nd dialogue turn
response, history = model.chat(tokenizer, "給我講一個年輕人奮斗創(chuàng)業(yè)最終取得成功的故事。", history=history)
print(response)
#這是一個關(guān)于一個年輕人奮斗創(chuàng)業(yè)最終取得成功的故事。
#故事的主人公叫李明,他來自一個普通的家庭,父母都是普通的工人。從小,李明就立下了一個目標(biāo):要成為一名成功的企業(yè)家。
#為了實現(xiàn)這個目標(biāo),李明勤奮學(xué)習(xí),考上了大學(xué)。在大學(xué)期間,他積極參加各種創(chuàng)業(yè)比賽,獲得了不少獎項。他還利用課余時間去實習(xí),積累了寶貴的經(jīng)驗。
#畢業(yè)后,李明決定開始自己的創(chuàng)業(yè)之路。他開始尋找投資機會,但多次都被拒絕了。然而,他并沒有放棄。他繼續(xù)努力,不斷改進(jìn)自己的創(chuàng)業(yè)計劃,并尋找新的投資機會。
#最終,李明成功地獲得了一筆投資,開始了自己的創(chuàng)業(yè)之路。他成立了一家科技公司,專注于開發(fā)新型軟件。在他的領(lǐng)導(dǎo)下,公司迅速發(fā)展起來,成為了一家成功的科技企業(yè)。
#李明的成功并不是偶然的。他勤奮、堅韌、勇于冒險,不斷學(xué)習(xí)和改進(jìn)自己。他的成功也證明了,只要努力奮斗,任何人都有可能取得成功。
#第三輪對話 3rd dialogue turn
response, history = model.chat(tokenizer, "給這個故事起一個標(biāo)題", history=history)
print(response)
#《奮斗創(chuàng)業(yè):一個年輕人的成功之路》
#Qwen-1.8B-Chat現(xiàn)在可以通過調(diào)整系統(tǒng)指令(System Prompt),實現(xiàn)角色扮演,語言風(fēng)格遷移,任務(wù)設(shè)定,行為設(shè)定等能力。
#Qwen-1.8B-Chat can realize roly playing, language style transfer, task setting, and behavior setting by system prompt.
response, _ = model.chat(tokenizer, "你好呀", history=None, system="請用二次元可愛語氣和我說話")
print(response)
#你好啊!我是一只可愛的二次元貓咪哦,不知道你有什么問題需要我?guī)兔獯饐幔?
response, _ = model.chat(tokenizer, "My colleague works diligently", history=None, system="You will write beautiful compliments according to needs")
print(response)
#Your colleague is an outstanding worker! Their dedication and hard work are truly inspiring. They always go above and beyond to ensure that
#their tasks are completed on time and to the highest standard. I am lucky to have them as a colleague, and I know I can count on them to handle any challenge that comes their way.
輸出結(jié)果:
#Qwen-VL 是阿里云研發(fā)的大規(guī)模視覺語言模型(Large Vision Language Model, LVLM)。Qwen-VL 可以以圖像、文本、檢測框作為輸入,并以文本和檢測框作為輸出。Qwen-VL 系列模型性能強大,具備多語言對話、多圖交錯對話等能力,并支持中文開放域定位和細(xì)粒度圖像識別與理解。
from modelscope import (
snapshot_download, AutoModelForCausalLM, AutoTokenizer, GenerationConfig
)
from auto_gptq import AutoGPTQForCausalLM
model_dir = snapshot_download("qwen/Qwen-VL-Chat-Int4", revision='v1.0.0')
import torch
torch.manual_seed(1234)
# Note: The default behavior now has injection attack prevention off.
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
# use cuda device
model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="cuda", trust_remote_code=True,use_safetensors=True).eval()
# 1st dialogue turn
query = tokenizer.from_list_format([
{'image': 'https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg'},
{'text': '這是什么'},
])
response, history = model.chat(tokenizer, query=query, history=None)
print(response)
# 圖中是一名年輕女子在沙灘上和她的狗玩耍,狗的品種可能是拉布拉多。她們坐在沙灘上,狗的前腿抬起來,似乎在和人類擊掌。兩人之間充滿了信任和愛。
# 2nd dialogue turn
response, history = model.chat(tokenizer, '輸出"狗"的檢測框', history=history)
print(response)
image = tokenizer.draw_bbox_on_latest_picture(response, history)
if image:
image.save('1.jpg')
else:
print("no box")
輸出結(jié)果:
from modelscope import (
snapshot_download, AutoModelForCausalLM, AutoTokenizer, GenerationConfig
)
import torch
model_id = 'qwen/Qwen-Audio-Chat'
revision = 'master'
model_dir = snapshot_download(model_id, revision=revision)
torch.manual_seed(1234)
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
if not hasattr(tokenizer, 'model_dir'):
tokenizer.model_dir = model_dir
# Note: The default behavior now has injection attack prevention off.
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
# use bf16
# model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", trust_remote_code=True, bf16=True).eval()
# use fp16
# model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", trust_remote_code=True, fp16=True).eval()
# use cpu only
# model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="cpu", trust_remote_code=True).eval()
# use cuda device
model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="cuda", trust_remote_code=True).eval()
# 1st dialogue turn
query = tokenizer.from_list_format([
{'audio': 'https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-Audio/1272-128104-0000.flac'}, # Either a local path or an url
{'text': 'what does the person say?'},
])
response, history = model.chat(tokenizer, query=query, history=None)
print(response)
# The person says: "mister quilter is the apostle of the middle classes and we are glad to welcome his gospel".
# 2nd dialogue turn
response, history = model.chat(tokenizer, 'Find the start time and end time of the word "middle classes"', history=history)
print(response)
# The word "middle classes" starts at <|2.33|> seconds and ends at <|3.26|> seconds.
輸出結(jié)果:
FastChat 是一個開放平臺,用于訓(xùn)練、服務(wù)和評估基于 LLM 的 ChatBot。
FastChat 的核心功能包括:
●優(yōu)秀的大語言模型訓(xùn)練和評估代碼。
●具有 Web UI 和 OpenAI 兼容的 RESTful API 的分布式多模型服務(wù)系統(tǒng)。
vLLM 是一個由加州伯克利分校、斯坦福大學(xué)和加州大學(xué)圣迭戈分校的研究人員基于操作系統(tǒng)中經(jīng)典的虛擬緩存和分頁技術(shù)開發(fā)的 LLM 服務(wù)系統(tǒng)。他實現(xiàn)了幾乎零浪費的 KV 緩存,并且可以在請求內(nèi)部和請求之間靈活共享 KV 高速緩存,從而減少內(nèi)存使用量。
FastChat 開源鏈接: https://github.com/lm-sys/FastChat
vLLM 開源鏈接: https://github.com/vllm-project/vllm
實戰(zhàn)演示:
git clone https://github.com/lm-sys/FastChat.git
cd FastChat
pip install .
在 vLLM 和 FastChat 上使用魔搭的模型需要設(shè)置兩個環(huán)境變量:1
export VLLM_USE_MODELSCOPE=True
export FASTCHAT_USE_MODELSCOPE=True
可以結(jié)合 FastChat 和 vLLM 搭建一個網(wǎng)頁 Demo 或者類 OpenAI API 服務(wù)器,
python -m fastchat.serve.controller
#以qwen-1.8B為例,在A10運行
python -m fastchat.serve.vllm_worker --model-path qwen/Qwen-1_8B-Chat --trust-remote-code --dtype bfloat16
python -m fastchat.serve.gradio_web_server --host 0.0.0.0 --port 8000
LLM 會產(chǎn)生誤導(dǎo)性的 “幻覺”,依賴的信息可能過時,處理特定知識時效率不高,缺乏專業(yè)領(lǐng)域的深度洞察,同時在推理能力上也有所欠缺。?正是在這樣的背景下,檢索增強生成技術(shù)(Retrieval-Augmented Generation,RAG)應(yīng)時而生,成為 AI 時代的一大趨勢。
RAG 通過在語言模型生成答案之前,先從廣泛的文檔數(shù)據(jù)庫中檢索相關(guān)信息,然后利用這些信息來引導(dǎo)生成過程,極大地提升了內(nèi)容的準(zhǔn)確性和相關(guān)性。RAG 有效地緩解了幻覺問題,提高了知識更新的速度,并增強了內(nèi)容生成的可追溯性,使得大型語言模型在實際應(yīng)用中變得更加實用和可信。
一個典型的 RAG 的例子:
這里面主要包括包括三個基本步驟:
RAG(開卷考試)VS. Finetune(專業(yè)課程學(xué)習(xí))
機器學(xué)習(xí):神經(jīng)網(wǎng)絡(luò)構(gòu)建(下)
閱讀華為Mate品牌盛典:HarmonyOS NEXT加持下游戲性能得到充分釋放
閱讀實現(xiàn)對象集合與DataTable的相互轉(zhuǎn)換
閱讀算法與數(shù)據(jù)結(jié)構(gòu) 1 - 模擬
閱讀5. Spring Cloud OpenFeign 聲明式 WebService 客戶端的超詳細(xì)使用
閱讀Java代理模式:靜態(tài)代理和動態(tài)代理的對比分析
閱讀Win11筆記本“自動管理應(yīng)用的顏色”顯示規(guī)則
閱讀本站所有軟件,都由網(wǎng)友上傳,如有侵犯你的版權(quán),請發(fā)郵件[email protected]
湘ICP備2022002427號-10 湘公網(wǎng)安備:43070202000427號© 2013~2025 haote.com 好特網(wǎng)