2022-09-25 02:42:54 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2022-09-15 02:40:50 +00:00
|
|
|
import sys
|
2022-10-15 00:21:38 +00:00
|
|
|
from functools import partialmethod
|
2022-10-16 23:42:46 +00:00
|
|
|
from shutil import rmtree
|
2022-09-15 02:40:50 +00:00
|
|
|
|
|
|
|
import pytest
|
2022-10-11 04:43:32 +00:00
|
|
|
import responses
|
2022-10-15 00:21:38 +00:00
|
|
|
from tqdm import tqdm
|
2022-09-25 02:42:54 +00:00
|
|
|
from urllib3 import HTTPConnectionPool
|
2022-09-15 02:40:50 +00:00
|
|
|
|
2022-09-16 06:06:59 +00:00
|
|
|
from imaginairy import api
|
2022-10-11 02:50:11 +00:00
|
|
|
from imaginairy.log_utils import suppress_annoying_logs_and_warnings
|
2022-11-26 22:52:28 +00:00
|
|
|
from imaginairy.samplers import SAMPLER_TYPE_OPTIONS
|
2022-09-22 05:38:44 +00:00
|
|
|
from imaginairy.utils import (
|
|
|
|
fix_torch_group_norm,
|
|
|
|
fix_torch_nn_layer_norm,
|
2022-09-28 00:04:16 +00:00
|
|
|
get_device,
|
2022-09-22 05:38:44 +00:00
|
|
|
platform_appropriate_autocast,
|
|
|
|
)
|
2022-09-25 02:42:54 +00:00
|
|
|
from tests import TESTS_FOLDER
|
2022-09-15 02:40:50 +00:00
|
|
|
|
|
|
|
if "pytest" in str(sys.argv):
|
|
|
|
suppress_annoying_logs_and_warnings()
|
|
|
|
|
2022-09-25 02:42:54 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-10-16 23:42:46 +00:00
|
|
|
SAMPLERS_FOR_TESTING = SAMPLER_TYPE_OPTIONS
|
|
|
|
if get_device() == "mps:0":
|
|
|
|
SAMPLERS_FOR_TESTING = ["plms", "k_euler_a"]
|
|
|
|
elif get_device() == "cpu":
|
|
|
|
SAMPLERS_FOR_TESTING = []
|
|
|
|
|
2022-09-15 02:40:50 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
|
|
def pre_setup():
|
|
|
|
api.IMAGINAIRY_SAFETY_MODE = "disabled"
|
2022-09-22 05:38:44 +00:00
|
|
|
suppress_annoying_logs_and_warnings()
|
2022-10-16 23:42:46 +00:00
|
|
|
test_output_folder = f"{TESTS_FOLDER}/test_output"
|
2022-09-28 00:04:16 +00:00
|
|
|
|
|
|
|
# delete the testoutput folder and recreate it
|
2022-10-16 23:42:46 +00:00
|
|
|
try:
|
|
|
|
rmtree(test_output_folder)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
os.makedirs(test_output_folder, exist_ok=True)
|
2022-09-25 02:42:54 +00:00
|
|
|
|
|
|
|
orig_urlopen = HTTPConnectionPool.urlopen
|
|
|
|
|
|
|
|
def urlopen_tattle(self, method, url, *args, **kwargs):
|
|
|
|
# traceback.print_stack()
|
2022-10-16 23:42:46 +00:00
|
|
|
# current_test = os.environ.get("PYTEST_CURRENT_TEST", "")
|
|
|
|
# print(f"{current_test} {method} {self.host}{url}")
|
2022-09-28 00:04:16 +00:00
|
|
|
result = orig_urlopen(self, method, url, *args, **kwargs)
|
2022-10-16 23:42:46 +00:00
|
|
|
|
2022-10-11 04:43:32 +00:00
|
|
|
# raise HTTPError("NO NETWORK CALLS")
|
2022-09-28 00:04:16 +00:00
|
|
|
return result
|
2022-09-25 02:42:54 +00:00
|
|
|
|
|
|
|
HTTPConnectionPool.urlopen = urlopen_tattle
|
2022-10-15 00:21:38 +00:00
|
|
|
tqdm.__init__ = partialmethod(tqdm.__init__, disable=True)
|
2022-10-16 23:42:46 +00:00
|
|
|
|
|
|
|
# real_randn = torch.randn
|
|
|
|
# def randn_tattle(*args, **kwargs):
|
|
|
|
# print("RANDN CALL RANDN CALL")
|
|
|
|
# traceback.print_stack()
|
|
|
|
# return real_randn(*args, **kwargs)
|
|
|
|
#
|
|
|
|
# torch.randn = randn_tattle
|
2022-09-25 02:42:54 +00:00
|
|
|
|
2022-09-22 05:38:44 +00:00
|
|
|
with fix_torch_nn_layer_norm(), fix_torch_group_norm(), platform_appropriate_autocast():
|
2022-09-17 05:21:20 +00:00
|
|
|
yield
|
2022-09-28 00:04:16 +00:00
|
|
|
|
|
|
|
|
2022-10-06 04:43:00 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def reset_get_device():
|
|
|
|
get_device.cache_clear()
|
|
|
|
|
|
|
|
|
2022-09-28 00:04:16 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def filename_base_for_outputs(request):
|
2022-10-16 23:42:46 +00:00
|
|
|
filename_base = f"{TESTS_FOLDER}/test_output/{request.node.name}_"
|
|
|
|
return filename_base
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def filename_base_for_orig_outputs(request):
|
|
|
|
filename_base = f"{TESTS_FOLDER}/test_output/{request.node.originalname}_"
|
2022-09-28 00:04:16 +00:00
|
|
|
return filename_base
|
2022-10-11 04:43:32 +00:00
|
|
|
|
|
|
|
|
2022-10-16 23:42:46 +00:00
|
|
|
@pytest.fixture(params=SAMPLERS_FOR_TESTING)
|
|
|
|
def sampler_type(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
|
2022-10-11 04:43:32 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mocked_responses():
|
|
|
|
with responses.RequestsMock() as rsps:
|
|
|
|
yield rsps
|
2022-10-16 23:42:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption(
|
|
|
|
"--subset",
|
|
|
|
action="store",
|
|
|
|
default=None,
|
|
|
|
help="Runs an exclusive subset of tests: '1/3', '2/3', '3/3'. Useful for distributed testing",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.hookimpl()
|
|
|
|
def pytest_collection_modifyitems(config, items):
|
|
|
|
"""Only select a subset of tests to run, based on the --subset option."""
|
|
|
|
filtered_node_ids = set()
|
|
|
|
node_ids = [f.nodeid for f in items]
|
|
|
|
node_ids.sort()
|
|
|
|
subset = config.getoption("--subset")
|
|
|
|
if subset:
|
|
|
|
partition_no, total_partitions = subset.split("/")
|
|
|
|
partition_no, total_partitions = int(partition_no), int(total_partitions)
|
|
|
|
if partition_no < 1 or partition_no > total_partitions:
|
|
|
|
raise ValueError("Invalid subset")
|
|
|
|
for i, node_id in enumerate(node_ids):
|
|
|
|
if i % total_partitions == partition_no - 1:
|
|
|
|
filtered_node_ids.add(node_id)
|
|
|
|
|
|
|
|
items[:] = [i for i in items if i.nodeid in filtered_node_ids]
|
|
|
|
|
|
|
|
print(
|
|
|
|
f"Running subset {partition_no}/{total_partitions} {len(filtered_node_ids)} tests:"
|
|
|
|
)
|
|
|
|
filtered_node_ids = list(filtered_node_ids)
|
|
|
|
filtered_node_ids.sort()
|
|
|
|
for n in filtered_node_ids:
|
|
|
|
print(f" {n}")
|