(container_type, source_file, original_source)
| 1073 | return super().find_class(mod_name, name) |
| 1074 | |
| 1075 | def _check_container_source(container_type, source_file, original_source): |
| 1076 | try: |
| 1077 | current_source = ''.join(get_source_lines_and_file(container_type)[0]) |
| 1078 | except Exception: # saving the source is optional, so we can ignore any errors |
| 1079 | warnings.warn("Couldn't retrieve source code for container of " |
| 1080 | "type " + container_type.__name__ + ". It won't be checked " |
| 1081 | "for correctness upon loading.") |
| 1082 | return |
| 1083 | if original_source != current_source: |
| 1084 | if container_type.dump_patches: |
| 1085 | file_name = container_type.__name__ + '.patch' |
| 1086 | diff = difflib.unified_diff(current_source.split('\n'), |
| 1087 | original_source.split('\n'), |
| 1088 | source_file, |
| 1089 | source_file, lineterm="") |
| 1090 | lines = '\n'.join(diff) |
| 1091 | try: |
| 1092 | with open(file_name, 'a+') as f: |
| 1093 | file_size = f.seek(0, 2) |
| 1094 | f.seek(0) |
| 1095 | if file_size == 0: |
| 1096 | f.write(lines) |
| 1097 | elif file_size != len(lines) or f.read() != lines: |
| 1098 | raise OSError |
| 1099 | msg = ("Saved a reverse patch to " + file_name + ". " |
| 1100 | "Run `patch -p0 < " + file_name + "` to revert your " |
| 1101 | "changes.") |
| 1102 | except OSError: |
| 1103 | msg = ("Tried to save a patch, but couldn't create a " |
| 1104 | "writable file " + file_name + ". Make sure it " |
| 1105 | "doesn't exist and your working directory is " |
| 1106 | "writable.") |
| 1107 | else: |
| 1108 | msg = ("you can retrieve the original source code by " |
| 1109 | "accessing the object's source attribute or set " |
| 1110 | "`torch.nn.Module.dump_patches = True` and use the " |
| 1111 | "patch tool to revert the changes.") |
| 1112 | msg = f"source code of class '{torch.typename(container_type)}' has changed. {msg}" |
| 1113 | warnings.warn(msg, SourceChangeWarning) |
| 1114 | |
| 1115 | def legacy_load(f): |
| 1116 | deserialized_objects: Dict[int, Any] = {} |
no test coverage detected
searching dependent graphs…