MCPcopy
hub / github.com/dmlc/dgl / save_data

Function save_data

python/dgl/graphbolt/internal/utils.py:50–80  ·  view source on GitHub ↗

Save data into disk.

(data, path, fmt)

Source from the content-addressed store, hash-verified

48
49
50def save_data(data, path, fmt):
51 """Save data into disk."""
52 # Make sure the directory exists.
53 os.makedirs(os.path.dirname(path), exist_ok=True)
54
55 if fmt not in ["numpy", "torch"]:
56 raise RuntimeError(f"Unsupported format: {fmt}")
57
58 # Perform necessary conversion.
59 if fmt == "numpy" and isinstance(data, torch.Tensor):
60 data = data.cpu().numpy()
61 elif fmt == "torch" and isinstance(data, np.ndarray):
62 data = torch.from_numpy(data).cpu()
63
64 # Save the data.
65 if fmt == "numpy":
66 if not data.flags["C_CONTIGUOUS"]:
67 Warning(
68 "The ndarray saved to disk is not contiguous, "
69 "so it will be copied to contiguous memory."
70 )
71 data = np.ascontiguousarray(data)
72 numpy_save_aligned(path, data)
73 elif fmt == "torch":
74 if not data.is_contiguous():
75 Warning(
76 "The tensor saved to disk is not contiguous, "
77 "so it will be copied to contiguous memory."
78 )
79 data = data.contiguous()
80 torch.save(data, path)
81
82
83def get_npy_dim(npy_path):

Callers 1

copy_or_convert_dataFunction · 0.85

Calls 3

numpy_save_alignedFunction · 0.85
cpuMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected