Facebook開源大模型可視分析工具:Transparency Tool ,將Transformer扒的一干二凈 原創 精華
Transparency Tool是facebook開源的大語言模型可視分析工具,用于分析基于Transformer架構的語言模型。
源碼:https://github.com/facebookresearch/llm-transparency-tool
技術報告:https://arxiv.org/pdf/2404.07004.pdf
一、淺談原理
Transformer是由多個注意力塊堆疊而成,每個注意力塊視為一層,每個層包含一個多頭注意力層和一個前饋網絡。token向量在注意力層中,向量之間能夠相互交流,并根據彼此信息更新自身的值;在前饋網絡中,向量的值會被修改。
Transparency Tool將模型的前向推理過程構建成一個信息流圖,圖的節點表示token向量,圖的邊表示操作。以次來追蹤和可視化模型內部的信息流動路徑,同時允許檢查單個注意力頭和神經元的貢獻。
二、在線體驗
https://huggingface.co/spaces/facebook/llm-transparency-tool-demo
在huggingface上可以體驗一下這個在線demo,在左側選擇模型,默認gpt2,在上方選擇輸入提示文本,默認“When Mary and John went to the store, John gave a drink to”。
Graph就是構建的信息流圖,在Graph中圓圈代表節點,線代表邊。點擊token“to”最后一層的節點,右側Top Tokens中排在第一個的是token“Mary”。
同樣,通過點擊連接節點的注意力邊,您可以探索形成相關連接的頭的模式。
如果想了解圖的詳細構建過程以及如何通過圖進行分析,請參考下面這篇論文。
https://arxiv.org/pdf/2403.00824.pdf
三、私人定制
huggingface只提供了gpt2、distilgpt2、facebook/opt-125m三個模型,如何加載自己的模型呢?
Transparency Tool是基于TransformerLens開發的,TransformerLens是一個專注于生成語言模型(如GPT-2風格的模型)的可解釋性的庫。其核心目標是利用訓練好的模型,通過分析模型的內部工作機制,來提供對模型行為的深入理解。
https://github.com/neelnanda-io/TransformerLens
所以,凡是TransformerLens支持的模型,Transparency Tool都能支持。
對于TransformerLens不支持的模型,需要實現自己的TransparentLlm類。
首先要搭建本地環境。
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
}
啟動
streamlit run llm_transparency_tool/server/app.py -- config/local.json
本文轉載自公眾號人工智能大講堂
原文鏈接:??https://mp.weixin.qq.com/s/TSOkh5LEnE0sraE6yGRaCw??
