Skip to content
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

previous_dtype is now inferred from F.linear's result output type. #1010

Merged
merged 16 commits into from
Feb 19, 2024

Conversation

MFajcik
Copy link
Contributor

@MFajcik MFajcik commented Oct 10, 2023

Fixes the issue from #971

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

@BenjaminBossan
Copy link
Member

Thanks for providing the fix. Could you please run make style on your code? This should make the quality test pass, after which the full CI should run.

@MFajcik
Copy link
Contributor Author

MFajcik commented Oct 11, 2023

Done. Also, I tested it, successfully running my experiment with GPT-2 LORA fine-tuning, with original model computation running in bf16 as intended.

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for providing this fix and for testing it. I ran the GPU tests locally and they also passed (but I can't test with bf16). Do you happen to have a quick test at the ready to show that it works with bf16 and that doesn't involve inspecting intermediate results (as in the original issue)?

Regarding the PR, could you please remove the empty line 243, it's not required anymore. Also, should the forward methods of Embedding and Conv2d also be changed accordingly?

@BenjaminBossan
Copy link
Member

@MFajcik Are you still working on this?

@BenjaminBossan
Copy link
Member

@MFajcik Friendly reminder about this PR :)

There are some merge conflicts now due to a recent change. Let me know if you need help resolving it.

@MFajcik
Copy link
Contributor Author

MFajcik commented Nov 16, 2023

@BenjaminBossan Yeah I didn't forget about this. I just don't have time right now.
I will try to resolve these next week.

@BenjaminBossan
Copy link
Member

Fantastic, thanks. I don't want to put any pressure on you, take the time you need, I just wanted to ping you because sometimes people just forget :)

@MFajcik
Copy link
Contributor Author

MFajcik commented Dec 6, 2023

@BenjaminBossan I merged changes, and reapplied fix for Conv2D, Linear and Embeddings. I also ran make format, and added 1 test covering the fix for LoRA. Is that ok?

@MFajcik MFajcik mentioned this pull request Dec 6, 2023
4 tasks
@BenjaminBossan
Copy link
Member

@MFajcik Thanks for making the updates. The quality check still fails, could you please re-run make style? If that doesn't report back anything, maybe some package versions are outdated and need upgrading.

added 1 test covering the fix for LoRA

Thanks for adding the test. Probably we can move it to some of the existing files, but I'll take a closer look later and make a suggestion.

@MFajcik
Copy link
Contributor Author

MFajcik commented Dec 6, 2023

@MFajcik Thanks for making the updates. The quality check still fails, could you please re-run make style? If that doesn't report back anything, maybe some package versions are outdated and need upgrading.

added 1 test covering the fix for LoRA

Thanks for adding the test. Probably we can move it to some of the existing files, but I'll take a closer look later and make a suggestion.

Yeah, I forgot to run make style after writing the test again... Should be okay now.

@BenjaminBossan
Copy link
Member

@MFajcik Code quality checks are still failing:

tests/test_autocast_torchcompatibility_lora.py:141:20: F841 Local variable labels is assigned to but never used

@BenjaminBossan
Copy link
Member

Friendly ping @MFajcik

@MFajcik
Copy link
Contributor Author

MFajcik commented Dec 15, 2023

now? @BenjaminBossan

Copy link
Contributor

@younesbelkada younesbelkada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I left a single comment with respect to failing tests

return conv_output


class TestAutoCast(unittest.TestCase):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test seems to require a GPU, can you add the require_torch_gpu decorator here?

def require_torch_gpu(test_case):

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @MFajcik for making LoRA work correctly with AMP. This looks pretty good already. I have a couple of change requests and suggestions for improvement, please check them out.

In terms of the general approach, now that we use

result = self.base_layer(x, *args, **kwargs)

I think it's much safer to make this change than at the time the issue was originally discussed. This way, we should have a guarantee that LoRA does not change the dtype of the output.

@@ -251,7 +251,8 @@ def __init__(
r: int = 0,
lora_alpha: int = 1,
lora_dropout: float = 0.0,
fan_in_fan_out: bool = False, # Set this to True if the layer to replace stores weight like (fan_in, fan_out)
fan_in_fan_out: bool = False,
# Set this to True if the layer to replace stores weight like (fan_in, fan_out)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we undo this change, or at least move the comment above 254, where it belongs?

@@ -0,0 +1,159 @@
import unittest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add the comment header that we have in all our files? Also, please move this test to test_gpu_examples.py, otherwise it won't be run on CI (since our normal CI has no GPUs and only selected test files are run with GPU in our nightly tests).


class SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
super(SimpleModel, self).__init__()
super().__init__()

Same for other classes.

self.embedding_layer = nn.Embedding(1000, 768)
self.layer_norm = nn.LayerNorm(768)
self.linear_transform_base = nn.Linear(768, 256)
self.linear_transform = LoraLinear(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of wrapping the layer explicitly, let's create a LoraConfig and call get_peft_model with SimpleModel as input. test_simple_lora_linear_model could be responsible for initializing the class and passes the instance to _test_model. This way, we can be 100% that this generates a model the same way that users typically do.

super(SimpleLorAEmbeddingModel, self).__init__()

self.embedding_layer_base = nn.Embedding(1000, 768)
self.embedding_layer = LoraEmbedding(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same argument as above.

self.embedding_layer = nn.Embedding(1000, 768)
self.layer_norm = nn.LayerNorm(768)
self.conv2d_transform_base = nn.Conv2d(1, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
self.conv2d_transform = LoraConv2d(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same argument as above.


@require_torch_gpu
class TestAutoCast(unittest.TestCase):
def test_simple_model(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about parametrizing the test (using parameterize, see other tests) over the dtype? That way, we can run a single test per test case, which is usually preferable.

# Prepare dummy inputs
input_ids = torch.randint(0, 1000, (2, 10)).cuda()

# Forward pass with torch.bfloat16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the bf16 case, can we please run self.skipTest if not torch.cuda.is_bf16_supported()?

@BenjaminBossan
Copy link
Member

@MFajcik Do you still plan to work on this? :)

@MFajcik
Copy link
Contributor Author

MFajcik commented Jan 4, 2024

@MFajcik Do you still plan to work on this? :)

Hopefully this month sometimes :)

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@MFajcik
Copy link
Contributor Author

MFajcik commented Jan 24, 2024

@BenjaminBossan I think I adressed all your comments now.

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @MFajcik for providing the fix and adding tests for it. From my point of view, this looks good, but I'd like to have another review by @younesbelkada or @pacman100 to be 100% sure.

I saw that other methods probably need to be changed in the same way as done in this PR (LoHa, LoKr, poly, oft, IA³, lora.tp_layer.LoraParallelLinear), but that can be done in a subsequent PR.

tests/test_gpu_examples.py Show resolved Hide resolved
Copy link
Contributor

@pacman100 pacman100 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @MFajcik for the fixes and @BenjaminBossan for all the discussions and suggestions!

@BenjaminBossan
Copy link
Member

@MFajcik I think we should be good to merge this PR once the merge conflict is resolved. It should be pretty straightforward, let me know if there are any questions. Please note that we now use normal asserts everywhere instead of unittest's self.assertEqual etc.

Martin Fajcik added 2 commits February 19, 2024 13:08
@BenjaminBossan
Copy link
Member

Could you please run make style too :)

@BenjaminBossan BenjaminBossan merged commit 7b7e4b2 into huggingface:main Feb 19, 2024
14 checks passed
BenjaminBossan pushed a commit to BenjaminBossan/peft that referenced this pull request Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants