MCPcopy Create free account
hub / github.com/TencentARC/BrushNet / get_piecewise_constant_schedule

Function get_piecewise_constant_schedule

src/diffusers/optimization.py:81–120  ·  view source on GitHub ↗

Create a schedule with a constant learning rate, using the learning rate set in optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. step_rules (`string`): The rules for the learning rate. ex

(optimizer: Optimizer, step_rules: str, last_epoch: int = -1)

Source from the content-addressed store, hash-verified

79
80
81def get_piecewise_constant_schedule(optimizer: Optimizer, step_rules: str, last_epoch: int = -1) -> LambdaLR:
82 """
83 Create a schedule with a constant learning rate, using the learning rate set in optimizer.
84
85 Args:
86 optimizer ([`~torch.optim.Optimizer`]):
87 The optimizer for which to schedule the learning rate.
88 step_rules (`string`):
89 The rules for the learning rate. ex: rule_steps="1:10,0.1:20,0.01:30,0.005" it means that the learning rate
90 if multiple 1 for the first 10 steps, mutiple 0.1 for the next 20 steps, multiple 0.01 for the next 30
91 steps and multiple 0.005 for the other steps.
92 last_epoch (`int`, *optional*, defaults to -1):
93 The index of the last epoch when resuming training.
94
95 Return:
96 `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule.
97 """
98
99 rules_dict = {}
100 rule_list = step_rules.split(",")
101 for rule_str in rule_list[:-1]:
102 value_str, steps_str = rule_str.split(":")
103 steps = int(steps_str)
104 value = float(value_str)
105 rules_dict[steps] = value
106 last_lr_multiple = float(rule_list[-1])
107
108 def create_rules_function(rules_dict, last_lr_multiple):
109 def rule_func(steps: int) -> float:
110 sorted_steps = sorted(rules_dict.keys())
111 for i, sorted_step in enumerate(sorted_steps):
112 if steps < sorted_step:
113 return rules_dict[sorted_steps[i]]
114 return last_lr_multiple
115
116 return rule_func
117
118 rules_func = create_rules_function(rules_dict, last_lr_multiple)
119
120 return LambdaLR(optimizer, rules_func, last_epoch=last_epoch)
121
122
123def get_linear_schedule_with_warmup(

Callers

nothing calls this directly

Calls 1

create_rules_functionFunction · 0.85

Tested by

no test coverage detected