Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: eval schedule string #125

Merged
merged 1 commit into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions discoart/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,17 +692,18 @@ def _version_check(package: str = None, github_repo: str = None):

def _eval_scheduling_str(val) -> List[float]:
if isinstance(val, str):
r = eval(val)
elif isinstance(val, (int, float, bool)):
r = [val] * _MAX_DIFFUSION_STEPS
val = eval(val)

if isinstance(val, (int, float, bool)):
val = [val] * _MAX_DIFFUSION_STEPS
elif isinstance(val, (list, tuple)):
if len(val) != _MAX_DIFFUSION_STEPS:
raise ValueError(
f'invalid scheduling string: {val} the schedule steps should be exactly {_MAX_DIFFUSION_STEPS}'
)
else:
raise ValueError(f'unsupported scheduling type: {val}: {type(val)}')

if len(r) != _MAX_DIFFUSION_STEPS:
raise ValueError(
f'invalid scheduling string: {val} the schedule steps should be exactly {_MAX_DIFFUSION_STEPS}'
)
return r
return val


def _get_current_schedule(schedule_table: Dict, t: int) -> 'SimpleNamespace':
Expand Down
11 changes: 11 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from discoart.config import default_args, save_config, load_config, export_python
from discoart.helper import _eval_scheduling_str


def test_export_load_config(tmpfile):
Expand All @@ -23,3 +24,13 @@ def test_format_config():

def test_export_python():
assert export_python(default_args)


def test_eval_schedule_string():
assert _eval_scheduling_str('1') == [1] * 1000
assert _eval_scheduling_str('[1] * 1000') == [1] * 1000
assert _eval_scheduling_str(1) == [1] * 1000
assert _eval_scheduling_str('1.') == [1] * 1000
assert _eval_scheduling_str('True') == [True] * 1000
assert _eval_scheduling_str('False') == [False] * 1000
assert _eval_scheduling_str(True) == [True] * 1000