Skip to content

Commit cd2cc1a

Browse files
committed
Merge branch 'feat/600-C-grid-owner' into develop (#627)
2 parents 802ac42 + c2019f7 commit cd2cc1a

5 files changed

Lines changed: 75 additions & 26 deletions

File tree

‎docs/ARCHITECTURE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ one to open.
8383
| Validation registry | `dw/validation.py`: `ERROR_CHECKS`, `WARNING_CHECKS`, `run_checks` | A check is one `Check` in a registry, and a check that raises becomes one `internal` finding while the rest still run. | `tests/test_validation.py::TestExceptionPolicy::test_a_raising_check_is_one_internal_error_and_the_rest_still_run` |
8484
| Step-value checks | `dw/step_value_checks.py`: `fps_errors`, `null_media_errors`, `select_errors`, `chain_prompts_errors` | The only caller of these checks is the error registry. | `dw/validation.py`: `ERROR_CHECKS` |
8585
| Admission | `dw/server/admission.py`: `admit` | Validate, submit, rerun and enhance load and check a request once, and `JobManager.submit` does not check it again. | `tests/test_admission.py::test_a_submit_expands_once` |
86-
| Variable constraints | `dw/variable_constraints.py`: `apply_constraints` | A model's rule about a value is declared in the workflow's `variable_constraints`, and it is checked at validation and again before anything loads. | `tests/test_variable_constraints.py::TestTheGrid::test_without_snap_an_off_grid_value_is_refused_not_rounded`, `tests/test_variable_constraints.py::TestTheRunTimePass::test_an_illegal_value_is_refused_before_anything_loads` |
86+
| Variable constraints | `dw/variable_constraints.py`: `apply_constraints`; the grid arithmetic `aligned` / `aligned_down` | A model's rule about a value is declared in the workflow's `variable_constraints`, and it is checked at validation and again before anything loads. `plan_cuts` (and its static check) rounds render lengths through `aligned` / `aligned_down`, never its own copy, so a planned `num_frames` is one the template's constraint accepts. | `tests/test_variable_constraints.py::TestTheGrid::test_without_snap_an_off_grid_value_is_refused_not_rounded`, `tests/test_variable_constraints.py::TestTheGrid::test_plan_cuts_rounds_through_the_constraint_owner`, `tests/test_variable_constraints.py::TestTheRunTimePass::test_an_illegal_value_is_refused_before_anything_loads` |
8787
| Bound acknowledgement | `dw/plan.py`: `fingerprint`, `dw/server/admission.py`: `check_bound_acknowledgement` | A bound `acknowledged_cost` is refused with 409 when the plan's fingerprint or its required downloads changed. | `tests/test_server.py::TestBoundRerun::test_a_rerun_bound_to_a_stale_plan_is_refused`, `tests/test_server.py::TestBoundAcknowledgement::test_a_matching_fingerprint_queues` |
8888
| Per-repo gating | `dw/plan.py`: `build_plan` | Each gated repo, including a `loras` entry's repo, is probed and reported on its own. | `tests/test_plan.py::TestDownloadsRequired::test_a_gated_repo_this_token_lacks_access_to_is_blocked` |
8989
| Observed cost | `dw/server/observed_cost.py`: `declared_drivers` | `observed` comes only from this box's finished jobs and never writes `cost`. Cold and warm runs are reported separately. | `tests/test_observed_cost.py::TestColdIsNotWarm::test_the_two_are_reported_separately_each_with_its_runs` |

‎dw/task_domains.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,9 +715,12 @@ def grid_problems(modulus, remainder, min_frames, max_frames):
715715
and (remainder or 0) >= 0
716716
and (low or 1) > 0
717717
):
718-
from .tasks.cuts import grid_up
718+
from .variable_constraints import aligned
719719

720-
smallest = grid_up(max(low or 0, 1), modulus, remainder or 0)
720+
floor = max(low or 0, 1)
721+
smallest = aligned(floor, {"modulus": modulus, "remainder": remainder or 0})
722+
if smallest is None:
723+
smallest = floor
721724
if smallest > high:
722725
problems.append(
723726
(

‎dw/tasks/cuts.py‎

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import math
3535
import re
3636

37+
from ..variable_constraints import aligned, aligned_down
3738
from .registry import register_command
3839

3940
logger = logging.getLogger("dw")
@@ -79,23 +80,21 @@ def _integer(value, name):
7980
return int(number)
8081

8182

82-
def grid_up(frames, modulus, remainder):
83-
"""The smallest render length on the grid (modulus * n + remainder, n >= 0)
84-
at or above `frames`; `frames` itself when there is no modulus."""
85-
if modulus is None:
86-
return frames
87-
steps = max(0, -(-(frames - remainder) // modulus))
88-
return modulus * steps + remainder
83+
def _grid_up(frames, grid):
84+
"""The smallest render length on the grid at or above `frames`; `frames`
85+
itself when there is no modulus. The arithmetic is the template
86+
constraint's own (`variable_constraints.aligned`), so a planned
87+
`num_frames` is one the template's constraint accepts."""
88+
target = aligned(frames, grid)
89+
return frames if target is None else target
8990

9091

91-
def grid_down(frames, modulus, remainder):
92-
"""The largest render length on the grid at or below `frames`, or None
93-
when none fits."""
94-
if modulus is None:
92+
def _grid_down(frames, grid):
93+
"""The largest render length on the grid at or below `frames` (`frames`
94+
itself when there is no modulus), or None when none fits."""
95+
if not grid["modulus"]:
9596
return frames
96-
if frames < remainder:
97-
return None
98-
return modulus * ((frames - remainder) // modulus) + remainder
97+
return aligned_down(frames, grid)
9998

10099

101100
def transcript_problem(transcript):
@@ -588,9 +587,7 @@ def _frames(scenes, fps, total_frames, warnings):
588587
def _render_length(grid, lead, cut):
589588
"""Frames to render for a cut with this lead: lead + cut, no fewer than
590589
min_frames, raised to the grid."""
591-
return grid_up(
592-
max(grid["min_frames"] or 0, lead + cut), grid["modulus"], grid["remainder"]
593-
)
590+
return _grid_up(max(grid["min_frames"] or 0, lead + cut), grid)
594591

595592

596593
def _split_over_max(framed, grid, beat_frames, warnings):
@@ -602,7 +599,7 @@ def _split_over_max(framed, grid, beat_frames, warnings):
602599
is warned about, since the same words then carry several shots."""
603600
if grid["max_frames"] is None:
604601
return framed
605-
ceiling = grid_down(grid["max_frames"], grid["modulus"], grid["remainder"])
602+
ceiling = _grid_down(grid["max_frames"], grid)
606603

607604
def reach(start):
608605
return ceiling - min(grid["lead_frames"], start)
@@ -829,8 +826,8 @@ def plan_cuts(
829826
"max_frames": args.max_frames,
830827
"lead_frames": int(round((args.lead_s or 0.0) * args.fps)),
831828
}
832-
if args.max_frames is not None and grid["lead_frames"] >= grid_down(
833-
args.max_frames, args.modulus, grid["remainder"]
829+
if args.max_frames is not None and grid["lead_frames"] >= _grid_down(
830+
args.max_frames, grid
834831
):
835832
raise ValueError(
836833
f"{COMMAND}'s 'lead_s' ({args.lead_s:g} s, {grid['lead_frames']} "

‎dw/variable_constraints.py‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ def _as_integer(value):
141141
return None
142142

143143

144+
def _grid(constraint):
145+
"""The constraint's (modulus, remainder) with the remainder brought into
146+
[0, modulus), or None when there is no grid."""
147+
modulus = constraint.get("modulus")
148+
if not modulus:
149+
return None
150+
return modulus, constraint.get("remainder", 0) % modulus
151+
152+
144153
def aligned(value, constraint):
145154
"""The smallest value on the constraint's grid that is not below `value`,
146155
ignoring the range, or None when there is no grid to align to.
@@ -152,17 +161,32 @@ def aligned(value, constraint):
152161
108 is accepted (it becomes 124).
153162
"""
154163
number = _as_integer(value)
155-
modulus = constraint.get("modulus")
156-
if number is None or not modulus:
164+
grid = _grid(constraint)
165+
if number is None or grid is None:
157166
return None
158-
remainder = constraint.get("remainder", 0) % modulus
167+
modulus, remainder = grid
159168
steps = math.ceil((number - remainder) / modulus)
160169
target = steps * modulus + remainder
161170
while target < number:
162171
target += modulus
163172
return target
164173

165174

175+
def aligned_down(value, constraint):
176+
"""The largest value on the constraint's grid that is not above `value`
177+
and not below the remainder (n >= 0), or None when there is no grid or no
178+
grid value fits. The floor that matches `aligned`: `plan_cuts` uses it for
179+
the longest render a shot may have under `max_frames`."""
180+
number = _as_integer(value)
181+
grid = _grid(constraint)
182+
if number is None or grid is None:
183+
return None
184+
modulus, remainder = grid
185+
if number < remainder:
186+
return None
187+
return modulus * ((number - remainder) // modulus) + remainder
188+
189+
166190
def effective(value, constraint):
167191
"""The number the run will actually use - `value` itself, or what
168192
`snap: "up"` rounds it to. None when this is not a number to check."""

‎tests/test_variable_constraints.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from dw.variable_constraints import (
2323
aligned,
24+
aligned_down,
2425
entry_constraint_fields,
2526
apply_constraints,
2627
constraint_errors,
@@ -63,6 +64,30 @@ def test_a_value_off_the_grid_rounds_up(self):
6364
assert snapped(130, H3) == 141
6465
assert violations(130, H3) == []
6566

67+
def test_the_floor_is_the_largest_grid_value_at_or_below(self):
68+
"""`aligned_down` is `aligned`'s floor - the longest render that
69+
fits under a ceiling, which `plan_cuts` reads `max_frames` through."""
70+
assert aligned_down(345, H3) == 345
71+
assert aligned_down(344, H3) == 328
72+
assert aligned_down(5, H3) == 5
73+
assert aligned_down(4, H3) is None
74+
assert aligned_down(100, {}) is None
75+
# the remainder is normalised the same way `aligned` normalises it
76+
odd = {"modulus": 17, "remainder": 22}
77+
assert aligned(130, odd) == aligned(130, H3)
78+
assert aligned_down(344, odd) == aligned_down(344, H3)
79+
80+
def test_plan_cuts_rounds_through_the_constraint_owner(self):
81+
"""`plan_cuts` gets its grid arithmetic from here, so a planned
82+
`num_frames` is one the template's own constraint accepts."""
83+
from dw.tasks import cuts
84+
85+
grid = {"modulus": 17, "remainder": 5, "min_frames": 124}
86+
for lead, cut in ((0, 48), (12, 48), (12, 130), (0, 1)):
87+
planned = cuts._render_length(grid, lead, cut)
88+
assert planned == aligned(max(124, lead + cut), H3)
89+
assert violations(planned, {**H3, "snap": None}) == []
90+
6691
def test_the_range_holds_for_the_rounded_value(self):
6792
"""What the pipeline does: `align_num_frames` snaps first and the
6893
duration bound then holds for the aligned count. 61 becomes 73 and

0 commit comments

Comments
 (0)