Facebook開源大模型可視分析工具:Transparency Tool ,將Transformer扒的一干二凈 原創(chuàng) 精華
Transparency Tool是facebook開源的大語(yǔ)言模型可視分析工具,用于分析基于Transformer架構(gòu)的語(yǔ)言模型。
源碼:https://github.com/facebookresearch/llm-transparency-tool
技術(shù)報(bào)告:https://arxiv.org/pdf/2404.07004.pdf
一、淺談原理
Transformer是由多個(gè)注意力塊堆疊而成,每個(gè)注意力塊視為一層,每個(gè)層包含一個(gè)多頭注意力層和一個(gè)前饋網(wǎng)絡(luò)。token向量在注意力層中,向量之間能夠相互交流,并根據(jù)彼此信息更新自身的值;在前饋網(wǎng)絡(luò)中,向量的值會(huì)被修改。
Transparency Tool將模型的前向推理過(guò)程構(gòu)建成一個(gè)信息流圖,圖的節(jié)點(diǎn)表示token向量,圖的邊表示操作。以次來(lái)追蹤和可視化模型內(nèi)部的信息流動(dòng)路徑,同時(shí)允許檢查單個(gè)注意力頭和神經(jīng)元的貢獻(xiàn)。
二、在線體驗(yàn)
https://huggingface.co/spaces/facebook/llm-transparency-tool-demo
在huggingface上可以體驗(yàn)一下這個(gè)在線demo,在左側(cè)選擇模型,默認(rèn)gpt2,在上方選擇輸入提示文本,默認(rèn)“When Mary and John went to the store, John gave a drink to”。
Graph就是構(gòu)建的信息流圖,在Graph中圓圈代表節(jié)點(diǎn),線代表邊。點(diǎn)擊token“to”最后一層的節(jié)點(diǎn),右側(cè)Top Tokens中排在第一個(gè)的是token“Mary”。
同樣,通過(guò)點(diǎn)擊連接節(jié)點(diǎn)的注意力邊,您可以探索形成相關(guān)連接的頭的模式。
如果想了解圖的詳細(xì)構(gòu)建過(guò)程以及如何通過(guò)圖進(jìn)行分析,請(qǐng)參考下面這篇論文。
https://arxiv.org/pdf/2403.00824.pdf
三、私人定制
huggingface只提供了gpt2、distilgpt2、facebook/opt-125m三個(gè)模型,如何加載自己的模型呢?
Transparency Tool是基于TransformerLens開發(fā)的,TransformerLens是一個(gè)專注于生成語(yǔ)言模型(如GPT-2風(fēng)格的模型)的可解釋性的庫(kù)。其核心目標(biāo)是利用訓(xùn)練好的模型,通過(guò)分析模型的內(nèi)部工作機(jī)制,來(lái)提供對(duì)模型行為的深入理解。
https://github.com/neelnanda-io/TransformerLens
所以,凡是TransformerLens支持的模型,Transparency Tool都能支持。
對(duì)于TransformerLens不支持的模型,需要實(shí)現(xiàn)自己的TransparentLlm類。
首先要搭建本地環(huán)境。
Dockerized running
# From the repository root directory
docker build -t llm_transparency_tool .
docker run --rm -p 7860:7860 llm_transparency_tool
Local Installation
# download
git clone git@github.com:facebookresearch/llm-transparency-tool.git
cd llm-transparency-tool
# install the necessary packages
conda env create --name llmtt -f env.yaml
# install the `llm_transparency_tool` package
pip install -e .
# now, we need to build the frontend
# don't worry, even `yarn` comes preinstalled by `env.yaml`
cd llm_transparency_tool/components/frontend
yarn install
yarn build
修改配置文件config/local.json,將模型添加到model中。
{
"allow_loading_dataset_files": true,
"preloaded_dataset_filename": "sample_input.txt",
"debug": true,
"models": {
"": null,
"gpt2": null,
"distilgpt2": null,
"facebook/opt-125m": null,
"facebook/opt-1.3b": null,
"EleutherAI/gpt-neo-125M": null,
"Qwen/Qwen-1_8B": null,
"Qwen/Qwen1.5-0.5B": null,
"Qwen/Qwen1.5-0.5B-Chat": null,
"Qwen/Qwen1.5-1.8B": null,
"Qwen/Qwen1.5-1.8B-Chat": null,
"microsoft/phi-1": null,
"microsoft/phi-1_5": null,
"microsoft/phi-2": null,
"meta-llama/Llama-2-7b-hf": null,
"meta-llama/Llama-2-7b-chat-hf": null,
"meta-llama/Llama-2-13b-hf": null,
"meta-llama/Llama-2-13b-chat-hf": null,
"gpt2-medium": null,
"gpt2-large": null,
"gpt2-xl": null,
"mistralai/Mistral-7B-v0.1": null,
"mistralai/Mistral-7B-Instruct-v0.1": null,
"mistralai/Mistral-7B-Instruct-v0.2": null,
"google/gemma-7b": null,
"google/gemma-2b": null,
"facebook/opt-2.7b": null,
"facebook/opt-6.7b": null,
"facebook/opt-13b": null,
"facebook/opt-30b": null
},
"default_model": "",
"demo_mode": false
}
啟動(dòng)
streamlit run llm_transparency_tool/server/app.py -- config/local.json
本文轉(zhuǎn)載自公眾號(hào)人工智能大講堂
原文鏈接:??https://mp.weixin.qq.com/s/TSOkh5LEnE0sraE6yGRaCw??
