Git Product home page Git Product logo

Comments (4)

BenjaminBossan avatar BenjaminBossan commented on June 2, 2024

Note that set_adapter enables requires_grad on the adapter whose name was passed and turns it off on all other adapters. However, since the PEFT model is a PyTorch nn.Module, as always, you can just iterate through the modules and enable or disable gradient according to your wishes:

model.set_adapter("adapterA")
# now only adapter A's weights have gradients enabled
for name, param in model.named_parameters():
    if ...:  # do some check, e.g. `if "lora_" in name`
        param.requires_grad = True

from peft.

bellos1203 avatar bellos1203 commented on June 2, 2024

Thank you for your answer.
However, this isn't quite what I had in mind.

How can I set the different adapters inside the forward path?
For example, does the following code work?
Because setting requires_grad = True inside the forward path seems little bit weird.

# Inside the model
def forward(self, x, y):
  model_a.set_adapter("adapter_x")
  output_x = model_a(x)
  
  model_a.set_adapter("adapter_y")
  output_y = model_a(y)
  
  final_output = model_b(output_x, output_y)
  return final_output

output = model(x, y)
loss = loss_fn(output)
loss.backward()

from peft.

BenjaminBossan avatar BenjaminBossan commented on June 2, 2024

It's really hard to answer your question with this little context. The code you pasted should work. It means that for output_x, only gradients for adapter_x etc. If you try this, do you get an error or does the training work?

from peft.

bellos1203 avatar bellos1203 commented on June 2, 2024

I've tried it, and the code works :)
I misunderstood the set_adapter function.
I thought the line model_a.set_adapter("adapter_y") freezes the other adapters (such as model_a.set_adapter("adapter_x")).
Thanks for your replies :)

from peft.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.