"""Fix UnslothGRPOTrainer line 3295: guard the unconditional for_training() call.""" path = "/tmp/unsloth_compiled_cache/UnslothGRPOTrainer.py" with open(path) as f: lines = f.readlines() target_idx = 3294 # 0-indexed line 3295 line = lines[target_idx] print(f"Line 3295: {repr(line)}") stripped = line.strip() if stripped == "self.model.for_training()": indent = line[: len(line) - len(line.lstrip())] lines[target_idx] = ( f'{indent}if hasattr(self.model, "for_training"): self.model.for_training()\n' ) with open(path, "w") as f: f.writelines(lines) print("FIXED line 3295") elif 'hasattr' in stripped: print("Already patched or different content — no change needed") else: # Search nearby lines for the call print("Searching nearby lines...") for i in range(max(0, 3290), min(len(lines), 3305)): if "for_training" in lines[i]: print(f" Line {i+1}: {repr(lines[i])}") stripped2 = lines[i].strip() if stripped2 == "self.model.for_training()": indent = lines[i][: len(lines[i]) - len(lines[i].lstrip())] lines[i] = ( f'{indent}if hasattr(self.model, "for_training"): self.model.for_training()\n' ) with open(path, "w") as f: f.writelines(lines) print(f"FIXED at line {i+1}") break