(module, clip_results)
| 191 | |
| 192 | @torch.no_grad() |
| 193 | def apply_clip(module, clip_results): |
| 194 | if not isinstance(clip_results, list): |
| 195 | clip_list = clip_results["clip"] |
| 196 | else: |
| 197 | clip_list = clip_results |
| 198 | if len(clip_list) == 0: |
| 199 | return |
| 200 | if len(clip_list[0]) == 3: |
| 201 | for name, max_val, min_val in clip_list: |
| 202 | layer = get_op_by_name(module, name) |
| 203 | layer.cuda() |
| 204 | max_val = max_val.to(layer.weight.device) |
| 205 | min_val = min_val.to(layer.weight.device) |
| 206 | org_shape = layer.weight.shape |
| 207 | layer.weight.data = layer.weight.data.reshape(*max_val.shape[:2], -1) |
| 208 | layer.weight.data = torch.clamp(layer.weight.data, min_val, max_val) |
| 209 | layer.weight.data = layer.weight.data.reshape(org_shape) |
| 210 | else: |
| 211 | raise 1 |
no test coverage detected