Zenn
🧠

視覚言語モデルの脳内🧠を見てみる。 (感度行列 for Phi4-mm)

に公開

なぜこんなことをする?

  • Microsoft製の視覚言語モデルであるPhi-4 multimodalのDecoder(下図のPhi4 miniの部分)は、24×32個のAttentionから構成されています。そんなにAttentionが潤沢にあるなら無くてもよいものもあるんじゃないのか? というのが本検証の動機です。

https://huggingface.co/microsoft/Phi-4-multimodal-instruct

補足です。
Attention数に出てきた数字の24はHead数、32はLayer数です。
さらにPhi-4では、Attention内のQKVについてKとVが独立なパラメタはAttention全体の1/3しかなく、複数のAttention間で共用されているようです。様々な努力が行われているようです。

何をする?

無くても良さそうなAttentionを探すべく、感度行列というものを導入しPhi-4 multimodalの脳内を見てみます。

感度行列

感度行列というもの見ることによってAttentionを削った場合の出力への寄与を計算できます。

感度行列では、縦軸をLayer数で横軸をHead数としたヒートマップを作成します。各要素には、該当のAttentionのみを無効化(出力を0)したときの出力の変化の値(感度)が入ります。出力の変化とは、通常状態の出力を基準としたときの差分を指します。差分の定義としては、logitsのうちTop確率のものがどれぐらい変わった割合を指します。

入力データ


Outdoor-Scene

Indoor-Scene

上の2つの画像に対して以下の6個のプロンプトを入力し、それぞれについて感度行列を計算します。

左脳系プロンプト

A. Based on the outdoor scene image with a bicycle, river, and bridge, how many arches does the bridge have?
「自転車、川、橋が写る屋外風景の画像に基づいて、その橋は何個のアーチを持っていますか?」
B. Based on the outdoor scene image with a bicycle, river, and bridge, what type of bicycle can be inferred from its parts (tire size, frame type)?
「自転車、川、橋が写る屋外風景の画像に基づいて、その自転車の部品(タイヤのサイズ、フレームの種類)からどのタイプの自転車であると推測されますか?」
C. Based on the outdoor scene image with a bicycle, river, and bridge, what is the estimated width of the river and length of the bridge?
「自転車、川、橋が写る屋外風景の画像に基づいて、川の幅と橋の長さはおおよそどれくらいと推定されますか?」

右脳系プロンプト

A. Based on the outdoor scene image with a bicycle, river, and bridge, what kind of wind or sounds might you experience while riding the bicycle?
「自転車、川、橋が写る屋外風景の画像に基づいて、その自転車に乗るときにどのような風や音を感じるかもしれませんか?」
B. Based on the outdoor scene image with a bicycle, river, and bridge, if you were to paint this scene, what colors and brushstrokes would you use?
「自転車、川、橋が写る屋外風景の画像に基づいて、その自転車に乗るときにどのような風や音を感じるかもしれませんか?」
C. Based on the outdoor scene image with a bicycle, river, and bridge, what memories or stories does the image evoke?
「自転車、川、橋が写る屋外風景の画像に基づいて、その画像はどのような思い出や物語を呼び起こしますか?」

結果

感想

  • 全てのサンプルに共通して出力への影響が極めて低いAttetionが10%ぐらいある?削れそう。。
  • テキストに比べて画像の違いのほうが感度行列への寄与が大きい。画像トークン数がテキストトークン数よりもの極めて大きいことを考慮すれば、単純に入力トークンの違いがそのまま感度へ影響している?
  • 右脳・左脳は見つからなかった。(n=12)
  • Attentionの無効化の方法として、恒等関数を使う方法だと結果が変わりそうかも。

参考

ヘッドウォータース

Discussion

ログインするとコメントできます