Atomically write binary data to a file. Writes to a temporary file in the same directory, then atomically replaces the target path. This prevents race conditions when multiple parallel workers write to the same ``op_tests/`` tree.
(path: Path, data: bytes)
| 132 | |
| 133 | |
| 134 | def _atomic_write_binary(path: Path, data: bytes) -> None: |
| 135 | """ |
| 136 | Atomically write binary data to a file. |
| 137 | |
| 138 | Writes to a temporary file in the same directory, then atomically replaces |
| 139 | the target path. This prevents race conditions when multiple parallel |
| 140 | workers write to the same ``op_tests/`` tree. |
| 141 | """ |
| 142 | path.parent.mkdir(parents=True, exist_ok=True) |
| 143 | fd, tmp = tempfile.mkstemp(dir=path.parent, suffix=".tmp") |
| 144 | closed = False |
| 145 | try: |
| 146 | os.write(fd, data) |
| 147 | os.close(fd) |
| 148 | closed = True |
| 149 | os.replace(tmp, path) |
| 150 | except BaseException: |
| 151 | if not closed: |
| 152 | os.close(fd) |
| 153 | if os.path.exists(tmp): |
| 154 | os.unlink(tmp) |
| 155 | raise |
| 156 | |
| 157 | |
| 158 | def save_tensors_to_bin(tensors: List[torch.Tensor], path: Union[str, Path]) -> None: |
no test coverage detected