Skip to content

Add support for Module Freezing #8468

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ finetune_program = your_dspy_program
ckpt_path = "saved_checkpoint_path_from_finetuning"
LM = dspy.HFModel(checkpoint=ckpt_path, model=model_to_finetune)

for p in finetune_program.predictors():
for p in finetune_program.predictors(return_trainable=False):
p.lm = LM
p.activated = False
```
Expand Down
19 changes: 17 additions & 2 deletions dspy/primitives/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(self, callbacks=None):
self._compiled = False
# LM calling history of the module.
self.history = []
self.trainable = True

@with_callbacks
def __call__(self, *args, **kwargs):
Expand Down Expand Up @@ -78,13 +79,27 @@ async def acall(self, *args, **kwargs):

return await self.aforward(*args, **kwargs)

def freeze(self):
self.trainable = False
# Propagate freeze to all sub-modules and predictors
for _, module in self.named_sub_modules():
if module is not self and hasattr(module, 'trainable'):
module.trainable = False

def unfreeze(self):
self.trainable = True
# Propagate unfreeze to all sub-modules and predictors
for _, module in self.named_sub_modules():
if module is not self and hasattr(module, 'trainable'):
module.trainable = True

def named_predictors(self):
from dspy.predict.predict import Predict

return [(name, param) for name, param in self.named_parameters() if isinstance(param, Predict)]

def predictors(self):
return [param for _, param in self.named_predictors()]
def predictors(self, return_trainable: bool = True):
return [param for _, param in self.named_predictors() if param.trainable or not return_trainable]

def set_lm(self, lm):
for _, param in self.named_predictors():
Expand Down
2 changes: 1 addition & 1 deletion dspy/propose/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def parse_list_of_instructions(instruction_string):

def get_program_instruction_set_string(program):
instruction_list = []
for _, pred in enumerate(program.predictors()):
for _, pred in enumerate(program.predictors(return_trainable=False)):
pred_instructions = get_signature(pred).instructions
instruction_list.append(f'"{pred_instructions}"')
# Joining the list into a single string that looks like a list
Expand Down
Loading
Loading