MCPcopy Create free account
hub / github.com/MegEngine/MegCC / compare_file

Function compare_file

script/test_model.py:89–121  ·  view source on GitHub ↗
(file_path_0, file_path_1, eps, out_dtype)

Source from the content-addressed store, hash-verified

87
88
89def compare_file(file_path_0, file_path_1, eps, out_dtype):
90 print("compare ", file_path_0, file_path_1)
91 with open(file_path_0, "rb") as f:
92 d0 = np.frombuffer(f.read(), dtype=out_dtype)
93 with open(file_path_1, "rb") as f:
94 d1 = np.frombuffer(f.read(), dtype=out_dtype)
95 assert d0.size == d1.size, "{} == {}".format(d0.size, d1.size)
96 diff = (np.maximum(d0, d1) - np.minimum(d0, d1)) / np.maximum(1.0, np.minimum(
97 np.abs(d0), np.abs(d1)))
98 abs_diff = np.maximum(d0, d1) - np.minimum(d0, d1)
99 max_idx = np.argmax(diff.flatten())
100 print(d0.shape)
101 print(
102 "max diff ",
103 np.max(diff.flatten()),
104 "\nabs_diff",
105 np.max(abs_diff.flatten()[max_idx]),
106 "\n",
107 d0[max_idx:max_idx + 10],
108 " vs ",
109 d1[max_idx:max_idx + 10],
110 " at ",
111 max_idx,
112 )
113 #! FIXME: the max abs diff should be zero when dtype is int?
114 if out_dtype == np.int8 or out_dtype == np.uint8 or out_dtype == np.int32:
115 assert np.all(abs_diff <= 1), "failed {} != {}, max : {}".format(
116 d0, d1, np.max(abs_diff.flatten()))
117 if np.max(abs_diff.flatten()) == 1:
118 print("\033[31mWarning\033[0m: the max abs diff is 1")
119 else:
120 assert np.all(diff < eps), "failed {} != {}, max: {} ".format(
121 d0, d1, np.max(diff.flatten()))
122
123
124def compare_file_or_dir(path_0, path_1, eps, output_name_2_dtype):

Callers 1

compare_file_or_dirFunction · 0.70

Calls 1

maxMethod · 0.45

Tested by

no test coverage detected