키 잠금 순위 1 편집 구현. 프로젝트 페이지
이 문서의 장점은 추가된 개념당 추가 매개변수가 100kb까지 매우 적다는 것입니다.
LLM용 메모리 편집 논문의 Rank-1 편집 기술을 몇 가지 개선하여 성공적으로 적용한 것으로 보입니다. 그들은 또한 키가 새로운 개념의 "어디"를 결정하고 값이 "무엇"을 결정하는지 확인하고 (값을 학습하는 동안) 슈퍼클래스 개념에 대한 로컬/글로벌 키 잠금을 제안했습니다.
연구자들에게 이 문서가 확인된다면 이 저장소의 도구는 교차 주의 조건을 사용하는 다른 텍스트 대 <insert modality>
네트워크에서 작동해야 합니다. 그냥 생각
넉넉한 후원을 위한 StabilityAI와 저의 다른 후원자들
여러 코드 검토 및 명확한 이메일을 제공한 Yoad Tewel
Stable Diffusion 1.5에 사용된 CLIP의 공분산 행렬을 미리 계산해 준 Brad Vidler!
SOTA 오픈 소스 대조 학습 텍스트-이미지 모델을 위한 OpenClip의 모든 관리자
$ pip install perfusion-pytorch
import torch
from torch import nn
from perfusion_pytorch import Rank1EditModule
to_keys = nn . Linear ( 768 , 320 , bias = False )
to_values = nn . Linear ( 768 , 320 , bias = False )
wrapped_to_keys = Rank1EditModule (
to_keys ,
is_key_proj = True
)
wrapped_to_values = Rank1EditModule (
to_values
)
text_enc = torch . randn ( 4 , 77 , 768 ) # regular input
text_enc_with_superclass = torch . randn ( 4 , 77 , 768 ) # init_input in algorithm 1, for key-locking
concept_indices = torch . randint ( 0 , 77 , ( 4 ,)) # index where the concept or superclass concept token is in the sequence
key_pad_mask = torch . ones ( 4 , 77 ). bool ()
keys = wrapped_to_keys (
text_enc ,
concept_indices = concept_indices ,
text_enc_with_superclass = text_enc_with_superclass ,
)
values = wrapped_to_values (
text_enc ,
concept_indices = concept_indices ,
text_enc_with_superclass = text_enc_with_superclass ,
)
# after much training ...
wrapped_to_keys . eval ()
wrapped_to_values . eval ()
keys = wrapped_to_keys ( text_enc )
values = wrapped_to_values ( text_enc )
또한 저장소에는 새로운 개념에 대한 교육(및 여러 개념에 대한 최종 추론)을 쉽게 해주는 EmbeddingWrapper
도 포함되어 있습니다.
import torch
from torch import nn
from perfusion_pytorch import EmbeddingWrapper
embed = nn . Embedding ( 49408 , 512 ) # open clip embedding, somewhere in the module tree of stable diffusion
# wrap it, and will automatically create a new concept for learning, based on the superclass embed string
wrapped_embed = EmbeddingWrapper (
embed ,
superclass_string = 'dog'
)
# now just pass in your prompts with the superclass id
embeds_with_new_concept , embeds_with_superclass , embed_mask , concept_indices = wrapped_embed ([
'a portrait of dog' ,
'dog running through a green field' ,
'a man walking his dog'
]) # (3, 77, 512), (3, 77, 512), (3, 77), (3,)
# now pass both embeds through clip text transformer
# the embed_mask needs to be passed to the cross attention as key padding mask
안정적인 확산 인스턴스 내에서 CLIP
인스턴스를 식별할 수 있는 경우 이를 OpenClipEmbedWrapper
에 직접 전달하여 교차 주의 레이어에 필요한 모든 것을 얻을 수도 있습니다.
전.
from perfusion_pytorch import OpenClipEmbedWrapper
texts = [
'a portrait of dog' ,
'dog running through a green field' ,
'a man walking his dog'
]
wrapped_clip_with_new_concept = OpenClipEmbedWrapper (
stable_diffusion . path . to . clip ,
superclass_string = 'dog'
)
text_enc , superclass_enc , mask , indices = wrapped_clip_with_new_concept ( texts )
# (3, 77, 512), (3, 77, 512), (3, 77), (3,)
xiao의 dreambooth-sd로 시작하여 SD 1.5로 연결
여러 개념을 추론하기 위한 Readme의 예 표시
make_key_value_proj_rank1_edit_modules_
함수에 대해 지정되지 않은 경우 키 및 값 투영 위치를 자동으로 추론합니다.
임베딩 래퍼는 슈퍼 클래스 토큰 ID로 대체하고 슈퍼 클래스로 임베딩을 반환해야 합니다.
다양한 컨셉 검토 - Yoad 덕분에
교차 주의를 연결하는 기능을 제공합니다.
추론 시 하나의 프롬프트에서 여러 개념 처리 - 시그모이드 항 + 출력의 합
추론을 위해 여러 Rank1EditModule
에서 별도로 학습된 개념을 하나로 결합하는 방법을 제공합니다.
Rank1EditModule
병합 기능 제공 논문에서 제안한 컨셉의 제로샷 마스킹 추가
데이터세트와 텍스트 인코더를 가져와 순위 1 업데이트에 필요한 공분산 행렬을 미리 계산하는 함수를 관리하세요.
연구자가 다른 학습 속도에 대해 걱정하도록 하는 대신 다른 논문의 분수 기울기 트릭을 제공합니다(임베딩 개념을 배우기 위해).
@article { Tewel2023KeyLockedRO ,
title = { Key-Locked Rank One Editing for Text-to-Image Personalization } ,
author = { Yoad Tewel and Rinon Gal and Gal Chechik and Yuval Atzmon } ,
journal = { ACM SIGGRAPH 2023 Conference Proceedings } ,
year = { 2023 } ,
url = { https://api.semanticscholar.org/CorpusID:258436985 }
}
@inproceedings { Meng2022LocatingAE ,
title = { Locating and Editing Factual Associations in GPT } ,
author = { Kevin Meng and David Bau and Alex Andonian and Yonatan Belinkov } ,
booktitle = { Neural Information Processing Systems } ,
year = { 2022 } ,
url = { https://api.semanticscholar.org/CorpusID:255825985 }
}