r"""Save an object to a directory. Args: obj: The object to be saved path_or_buffer: a file-like object (has to implement write and flush) or a string or os.PathLike object containing a file name global_dst_rank (int, optional): The destination rank for
(
obj: Any,
path_or_buffer: FILE_LIKE,
global_dst_rank: Optional[int] = None,
save_as_external_data: bool = False,
)
| 682 | |
| 683 | |
| 684 | def save( |
| 685 | obj: Any, |
| 686 | path_or_buffer: FILE_LIKE, |
| 687 | global_dst_rank: Optional[int] = None, |
| 688 | save_as_external_data: bool = False, |
| 689 | ) -> None: |
| 690 | r"""Save an object to a directory. |
| 691 | |
| 692 | Args: |
| 693 | obj: The object to be saved |
| 694 | path_or_buffer: a file-like object (has to implement write and flush) or a string or |
| 695 | os.PathLike object containing a file name |
| 696 | global_dst_rank (int, optional): The destination rank for |
| 697 | saving global tensors. When specified, whole tensors |
| 698 | will be saved by the process whose rank == |
| 699 | global_src_rank, while other processes will not do any |
| 700 | disk I/O. |
| 701 | save_as_external_data (bool): useful only if path_or_buffer is a string or |
| 702 | os.PathLike object containing a file name |
| 703 | """ |
| 704 | if isinstance(path_or_buffer, str): |
| 705 | path_or_buffer = Path(path_or_buffer) |
| 706 | |
| 707 | if isinstance(obj, graph_util.Graph): |
| 708 | if not _is_path(path_or_buffer): |
| 709 | raise ValueError( |
| 710 | "path_or_buffer must be the type of {`str`, `pathlib.Path`} while obj is Graph" |
| 711 | ) |
| 712 | _save_graph(obj, path_or_buffer) |
| 713 | return |
| 714 | |
| 715 | # this `path` is only used for `ContextData` and is set to empty when `path_or_buffer` is IO[bytes] or BinaryIO |
| 716 | path: Path = Path(path_or_buffer if _is_path(path_or_buffer) else "") |
| 717 | obj = {"protocol_version": PROTOCOL_VERSION, ONEFLOW_MAGIC_KEY: None, "data": obj} |
| 718 | |
| 719 | with tensor_pickling_context(path, global_dst_rank, None, save_as_external_data): |
| 720 | pickled_bytes = pickle.dumps(obj) |
| 721 | |
| 722 | if _is_path(path_or_buffer) and save_as_external_data: |
| 723 | path_or_buffer.mkdir(exist_ok=True) |
| 724 | path_or_buffer = path_or_buffer / PICKLE_FILENAME |
| 725 | |
| 726 | def write_file(): |
| 727 | with _open_file_like(path_or_buffer, "wb") as f: |
| 728 | f.write(pickled_bytes) |
| 729 | |
| 730 | if global_dst_rank is not None: |
| 731 | assert isinstance( |
| 732 | global_dst_rank, int |
| 733 | ), f"global_dst_rank expected type int, but got {type(global_dst_rank)}." |
| 734 | assert ( |
| 735 | global_dst_rank >= 0 and global_dst_rank < flow.env.get_world_size() |
| 736 | ), f"out of range (expected to be in range of [0, {flow.env.get_world_size()}), but got {global_dst_rank})." |
| 737 | if flow.env.get_rank() == global_dst_rank: |
| 738 | write_file() |
| 739 | else: |
| 740 | # global_dst_rank is None |
| 741 | write_file() |
nothing calls this directly
no test coverage detected