MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / print_tensor_test

Function print_tensor_test

diffusers/src/diffusers/utils/testing_utils.py:109–132  ·  view source on GitHub ↗
(
    tensor,
    limit_to_slices=None,
    max_torch_print=None,
    filename="test_corrections.txt",
    expected_tensor_name="expected_slice",
)

Source from the content-addressed store, hash-verified

107
108
109def print_tensor_test(
110 tensor,
111 limit_to_slices=None,
112 max_torch_print=None,
113 filename="test_corrections.txt",
114 expected_tensor_name="expected_slice",
115):
116 if max_torch_print:
117 torch.set_printoptions(threshold=10_000)
118
119 test_name = os.environ.get("PYTEST_CURRENT_TEST")
120 if not torch.is_tensor(tensor):
121 tensor = torch.from_numpy(tensor)
122 if limit_to_slices:
123 tensor = tensor[0, -3:, -3:, -1]
124
125 tensor_str = str(tensor.detach().cpu().flatten().to(torch.float32)).replace("\n", "")
126 # format is usually:
127 # expected_slice = np.array([-0.5713, -0.3018, -0.9814, 0.04663, -0.879, 0.76, -1.734, 0.1044, 1.161])
128 output_str = tensor_str.replace("tensor", f"{expected_tensor_name} = np.array")
129 test_file, test_class, test_fn = test_name.split("::")
130 test_fn = test_fn.split()[0]
131 with open(filename, "a") as f:
132 print("::".join([test_file, test_class, test_fn, output_str]), file=f)
133
134
135def get_tests_dir(append_path=None):

Callers

nothing calls this directly

Calls 1

toMethod · 0.45

Tested by

no test coverage detected