-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Implement rest of the test cases (LoRA tests) #2824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6cdb47c
inital commit for lora test cases
aandyw 2b9887f
Merge branch 'main' into lora-test-cases
aandyw 5a208ce
help a bit with lora for 3d
patrickvonplaten fb4f2f0
Merge branch 'huggingface:main' into lora-test-cases
aandyw c3373a1
fixed lora tests
aandyw 85a4846
replaced redundant code
aandyw 2cb926e
Merge branch 'main' into lora-test-cases
patrickvonplaten f94705c
Merge branch 'main' into lora-test-cases
sayakpaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ | |
torch.backends.cuda.matmul.allow_tf32 = False | ||
|
||
|
||
def create_lora_layers(model): | ||
def create_lora_layers(model, mock_weights: bool = True): | ||
lora_attn_procs = {} | ||
for name in model.attn_processors.keys(): | ||
cross_attention_dim = None if name.endswith("attn1.processor") else model.config.cross_attention_dim | ||
|
@@ -57,12 +57,13 @@ def create_lora_layers(model): | |
lora_attn_procs[name] = LoRAAttnProcessor(hidden_size=hidden_size, cross_attention_dim=cross_attention_dim) | ||
lora_attn_procs[name] = lora_attn_procs[name].to(model.device) | ||
|
||
# add 1 to weights to mock trained weights | ||
with torch.no_grad(): | ||
lora_attn_procs[name].to_q_lora.up.weight += 1 | ||
lora_attn_procs[name].to_k_lora.up.weight += 1 | ||
lora_attn_procs[name].to_v_lora.up.weight += 1 | ||
lora_attn_procs[name].to_out_lora.up.weight += 1 | ||
if mock_weights: | ||
# add 1 to weights to mock trained weights | ||
with torch.no_grad(): | ||
lora_attn_procs[name].to_q_lora.up.weight += 1 | ||
lora_attn_procs[name].to_k_lora.up.weight += 1 | ||
lora_attn_procs[name].to_v_lora.up.weight += 1 | ||
lora_attn_procs[name].to_out_lora.up.weight += 1 | ||
|
||
return lora_attn_procs | ||
|
||
|
@@ -378,26 +379,7 @@ def test_lora_processors(self): | |
with torch.no_grad(): | ||
sample1 = model(**inputs_dict).sample | ||
|
||
lora_attn_procs = {} | ||
for name in model.attn_processors.keys(): | ||
cross_attention_dim = None if name.endswith("attn1.processor") else model.config.cross_attention_dim | ||
if name.startswith("mid_block"): | ||
hidden_size = model.config.block_out_channels[-1] | ||
elif name.startswith("up_blocks"): | ||
block_id = int(name[len("up_blocks.")]) | ||
hidden_size = list(reversed(model.config.block_out_channels))[block_id] | ||
elif name.startswith("down_blocks"): | ||
block_id = int(name[len("down_blocks.")]) | ||
hidden_size = model.config.block_out_channels[block_id] | ||
|
||
lora_attn_procs[name] = LoRAAttnProcessor(hidden_size=hidden_size, cross_attention_dim=cross_attention_dim) | ||
|
||
# add 1 to weights to mock trained weights | ||
with torch.no_grad(): | ||
lora_attn_procs[name].to_q_lora.up.weight += 1 | ||
lora_attn_procs[name].to_k_lora.up.weight += 1 | ||
lora_attn_procs[name].to_v_lora.up.weight += 1 | ||
lora_attn_procs[name].to_out_lora.up.weight += 1 | ||
lora_attn_procs = create_lora_layers(model) | ||
|
||
# make sure we can set a list of attention processors | ||
model.set_attn_processor(lora_attn_procs) | ||
|
@@ -465,28 +447,7 @@ def test_lora_save_load_safetensors(self): | |
with torch.no_grad(): | ||
old_sample = model(**inputs_dict).sample | ||
|
||
lora_attn_procs = {} | ||
for name in model.attn_processors.keys(): | ||
cross_attention_dim = None if name.endswith("attn1.processor") else model.config.cross_attention_dim | ||
if name.startswith("mid_block"): | ||
hidden_size = model.config.block_out_channels[-1] | ||
elif name.startswith("up_blocks"): | ||
block_id = int(name[len("up_blocks.")]) | ||
hidden_size = list(reversed(model.config.block_out_channels))[block_id] | ||
elif name.startswith("down_blocks"): | ||
block_id = int(name[len("down_blocks.")]) | ||
hidden_size = model.config.block_out_channels[block_id] | ||
|
||
lora_attn_procs[name] = LoRAAttnProcessor(hidden_size=hidden_size, cross_attention_dim=cross_attention_dim) | ||
lora_attn_procs[name] = lora_attn_procs[name].to(model.device) | ||
|
||
# add 1 to weights to mock trained weights | ||
with torch.no_grad(): | ||
lora_attn_procs[name].to_q_lora.up.weight += 1 | ||
lora_attn_procs[name].to_k_lora.up.weight += 1 | ||
lora_attn_procs[name].to_v_lora.up.weight += 1 | ||
lora_attn_procs[name].to_out_lora.up.weight += 1 | ||
|
||
lora_attn_procs = create_lora_layers(model) | ||
model.set_attn_processor(lora_attn_procs) | ||
|
||
with torch.no_grad(): | ||
|
@@ -518,21 +479,7 @@ def test_lora_save_safetensors_load_torch(self): | |
model = self.model_class(**init_dict) | ||
model.to(torch_device) | ||
|
||
lora_attn_procs = {} | ||
for name in model.attn_processors.keys(): | ||
cross_attention_dim = None if name.endswith("attn1.processor") else model.config.cross_attention_dim | ||
if name.startswith("mid_block"): | ||
hidden_size = model.config.block_out_channels[-1] | ||
elif name.startswith("up_blocks"): | ||
block_id = int(name[len("up_blocks.")]) | ||
hidden_size = list(reversed(model.config.block_out_channels))[block_id] | ||
elif name.startswith("down_blocks"): | ||
block_id = int(name[len("down_blocks.")]) | ||
hidden_size = model.config.block_out_channels[block_id] | ||
|
||
lora_attn_procs[name] = LoRAAttnProcessor(hidden_size=hidden_size, cross_attention_dim=cross_attention_dim) | ||
lora_attn_procs[name] = lora_attn_procs[name].to(model.device) | ||
|
||
lora_attn_procs = create_lora_layers(model, mock_weights=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we not mocking weights here? |
||
model.set_attn_processor(lora_attn_procs) | ||
# Saving as torch, properly reloads with directly filename | ||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
|
@@ -553,21 +500,7 @@ def test_lora_save_torch_force_load_safetensors_error(self): | |
model = self.model_class(**init_dict) | ||
model.to(torch_device) | ||
|
||
lora_attn_procs = {} | ||
for name in model.attn_processors.keys(): | ||
cross_attention_dim = None if name.endswith("attn1.processor") else model.config.cross_attention_dim | ||
if name.startswith("mid_block"): | ||
hidden_size = model.config.block_out_channels[-1] | ||
elif name.startswith("up_blocks"): | ||
block_id = int(name[len("up_blocks.")]) | ||
hidden_size = list(reversed(model.config.block_out_channels))[block_id] | ||
elif name.startswith("down_blocks"): | ||
block_id = int(name[len("down_blocks.")]) | ||
hidden_size = model.config.block_out_channels[block_id] | ||
|
||
lora_attn_procs[name] = LoRAAttnProcessor(hidden_size=hidden_size, cross_attention_dim=cross_attention_dim) | ||
lora_attn_procs[name] = lora_attn_procs[name].to(model.device) | ||
|
||
lora_attn_procs = create_lora_layers(model, mock_weights=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question. |
||
model.set_attn_processor(lora_attn_procs) | ||
# Saving as torch, properly reloads with directly filename | ||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.