Repository navigation
[Data] Remove safe_round from ExecutionResources hot path - #63680
Conversation
`safe_round` in `ExecutionResources.__init__` was the largest leaf in the scheduler thread (~8% of wall time in the worker_scaling_5000_tasks release test): every `add`/`subtract`/`max`/`min`/`copy`/`scale`/`zero`/ `inf`/`for_limits` constructed a new instance and paid four rounds. - Drop rounding from `__init__`; store values at native precision. - Move the quantize-for-Ray-Core boundary to `to_resource_dict()` (the only place ExecutionResources actually feeds back to Ray Core's `.options(...)` is at the boundary, not via internal bookkeeping). - `__eq__` / `__hash__` / `is_zero` / `is_non_negative` quantize on access so accumulated float drift (cpu/gpu ~1e-15/op; memory ~1e-4/op on byte-magnitude floats) doesn't flip results. Preserves legacy semantics, just paid at comparison time rather than per construction. Microbenchmark (1M loops of add+sub+max+min): 22.9us -> 7.9us per loop (~2.9x). Signed-off-by: xgui <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request optimizes ExecutionResources by deferring resource quantization from initialization to access boundaries (such as dictionary conversion, comparison, hashing, and zero/non-negative checks). This prevents rounding from being a performance hotspot during arithmetic operations. The review feedback suggests resolving a type mismatch in to_resource_dict where safe_round returns an optional float, and adding a type check in __eq__ to prevent potential AttributeErrors when comparing with non-ExecutionResources objects.
Per review: factor the four `safe_round` calls used by `__eq__`, `__hash__`, `is_zero`, `is_non_negative`, and `to_resource_dict` into a single `_quantized_key()` helper that caches the resulting tuple on the instance after the first call. Subsequent calls reuse the cached tuple instead of re-rounding. Microbenchmarks (single-thread): - realistic update_budgets-like loop (200K iters, fresh instances): 17.8 us/iter -> 20.1 us/iter (+13%; cache populates but never hits). - same-instance is_zero/is_non_negative loop (200K pairs): 5.6s -> 0.15s (~37x). - pure arithmetic loop (1M add+sub+max+min): unchanged. Still ~2.6x faster than the original (which paid safe_round 4 times per construction). Signed-off-by: xgui <[email protected]>
satisfies_limit still compared raw unquantized values while __eq__, is_zero, and is_non_negative were updated to quantize via _quantized_key(). Two ExecutionResources equal under __eq__ could then disagree under satisfies_limit, letting can_submit_new_task spuriously reject a task whose usage drifted ~1e-15 above its budget. Quantize on access so satisfies_limit matches the other comparison methods. Also update test_reservation_accounts_for_completed_ops_complex_graph to native precision (212.5 / 381.25); the rounding removal already reflected in the other reservation-allocator tests was missed here. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Signed-off-by: xgui <[email protected]>
zero() and inf() are called all over the scheduler hot path (e.g. `.max(zero())`). Instances are immutable in practice -- every arithmetic op returns a new object and there are no setters -- so return a single shared, lazily-cached instance instead of allocating one per call. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Signed-off-by: xgui <[email protected]>
Drop the temporary `key` local in `_quantized_key` and assign the cache directly. Tighten the bare `tuple` annotation (and the `_quantized` field) to `Tuple[float, float, float, float]` -- it stays a tuple rather than a dict because it is used as a hash key and unpacked positionally. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Signed-off-by: xgui <[email protected]>
- Make ExecutionResources immutable via __setattr__: resource fields are set once in __init__ and never reassigned (only the `_quantized` cache slot may transition). This is what makes the lazy quantization cache and the shared zero()/inf() singletons safe (reviewer ask). - Coalesce None -> 0.0 in __init__ so `_cpu/_gpu/...` are always plain floats; the property getters drop their `or 0` fallback (reviewer ask). - Add an isinstance guard to __eq__, returning NotImplemented for foreign types instead of raising AttributeError on `other.cpu` (gemini). The satisfies_limit quantization the bots flagged is already handled in an earlier commit on this branch. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Signed-off-by: xgui <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 69d9cd1. Configure here.
Coalescing unspecified fields to 0.0 broke ExecutionOptions' resource_limits setter: it reads the raw _cpu/_gpu/... fields and passes them to for_limits(), which maps None (not 0) to an unlimited (inf) limit. With coalescing, ExecutionResources(cpu=4) as a limit produced (4, 0, 0, 0) -- zeroing the GPU/object_store/memory limits and blocking all work (test_execution_options_resource_limit would fail). Restore the Optional[float] fields and the `or 0` property fallbacks. The freeze (__setattr__) and __eq__ isinstance guard from the prior commit stay; only the coalescing is reverted. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Signed-off-by: xgui <[email protected]>
The safe_round removal and epsilon-tolerant ExecutionResources comparisons are now owned by ray-project#63680. Revert execution_options.py to master here so this PR is limited to the incremental resource accounting changes. Signed-off-by: xgui <[email protected]>
…ts() The resource_limits setter reached into ExecutionResources privates (value._cpu, value._gpu, ...) to map unspecified (None) fields to inf. Add a public `to_limits()` method that does this, and have the setter call it -- ExecutionOptions no longer touches private attributes. Kept the property/setter rather than making resource_limits a plain instance variable: the setter's None->inf normalization runs on every assignment, and many call sites assign a bare ExecutionResources and rely on it (e.g. `options.resource_limits = ExecutionResources(cpu=2)` expects gpu/object_store/memory to stay unlimited; see test_executor_resource_management.py and test_resource_manager.py). A plain attribute would silently turn those into zero-resource limits. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Signed-off-by: xgui <[email protected]>
…t#63680) ## Summary - `safe_round` in `ExecutionResources.__init__` was the largest leaf in the scheduler thread (~8% of wall time in the worker_scaling_5000_tasks release test); every `add`/`subtract`/`max`/`min`/`copy`/`scale`/`zero`/`inf`/`for_limits` paid four rounds per construction. - Drop rounding from `__init__`; store values at native precision. The boundary that feeds Ray Core (`to_resource_dict()`) now does the quantization (5 digits for cpu/gpu, integer bytes for memory). - `__eq__` / `__hash__` / `is_zero` / `is_non_negative` / `satisfies_limit` quantize on access so accumulated float drift (~1e-15 per cpu/gpu op, up to ~1e-4 per memory op on byte-magnitude floats) doesn't flip results. Microbenchmark (1M iterations of `add + subtract + max + min`): **22.9µs → 7.9µs per loop (~2.9×)**. ## Scheduling-loop benchmark Scheduling-loop duration from the worker-scaling release test, master vs this PR, across actor/task counts (seconds): | Row Name | p50_scheduling_loop_duration_s | p90_scheduling_loop_duration_s | max_scheduling_loop_duration_s | | :--- | :--- | :--- | :--- | | master_500_actors | 0.612625923 | 0.665133368 | 0.722738731 | | remove_safe_round_500_actors | 0.601569453 | 0.640638011 | 0.740870783 | | master_500_tasks | 0.130088642 | 1.37959566 | 2.648603352 | | remove_safe_round_500_tasks | 0.110020964 | 1.160841493 | 2.169771337 | | master_1000_actors | 1.305331189 | 1.427004707 | 1.558304391 | | remove_safe_round_1000_actors | 1.213747345 | 1.375785287 | 1.496900667 | | master_1000_tasks | 0.196409489 | 2.933294435 | 5.940470687 | | remove_safe_round_1000_tasks | 0.199019694 | 2.894881022 | 6.349671409 | | master_2000_actors | 2.635618952 | 2.854903978 | 3.065606266 | | remove_safe_round_2000_actors | 2.431840577 | 2.738552787 | 2.981410969 | | master_2000_tasks | 6.467886639 | 6.96452463 | 15.59612749 | | remove_safe_round_2000_tasks | 4.14211371 | 6.906086244 | 14.25746993 | | master_5000_actors | 7.535207658 | 8.594098917 | 8.973420257 | | remove_safe_round_5000_actors | 6.220295621 | 6.859902615 | 7.413166343 | | master_5000_tasks | 11.25641787 | 17.33123665 | 32.68019135 | | remove_safe_round_5000_tasks | 9.844983811 | 13.24368429 | 30.86245854 | p50/p90 improve consistently at scale — e.g. 5000_actors p50 7.54s → 6.22s (~17%) and p90 8.59s → 6.86s (~20%); 2000_tasks p50 6.47s → 4.14s; 5000_tasks p90 17.33s → 13.24s. --------- Signed-off-by: xgui <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
…3680)" (#64296) Reverts #63680 for #64291 . Unblocking the release Signed-off-by: xgui <[email protected]>
…3680)" (#64296) Reverts #63680 for #64291 . Unblocking the release Signed-off-by: xgui <[email protected]> Signed-off-by: elliot-barn <[email protected]>
Rebased onto master after the ray-project#63680 revert (ray-project#64291). ExecutionResources keeps safe_round in __init__ (eager rounding); the perf work avoids allocations without changing rounding semantics: - Add __slots__ and cached zero()/inf() singletons to ExecutionResources. - Add combine_sum() to fold per-op usages in a single pass (one allocation instead of O(num_ops) intermediates); use it for the global/usage rollups. - Rewrite the reservation/budget loops to compute on raw per-dimension floats (no ExecutionResources construction in the hot loop), applying safe_round at each point the object-based code materialized an ExecutionResources, so results match it bit-for-bit (per-step rounding conserves split totals). - Add a regression test for ray-project#64291 (float drift in _get_max_scale_up). Co-Authored-By: Claude Opus 4.8 <[email protected]> Signed-off-by: xgui <[email protected]>
Rebased onto master after the ray-project#63680 revert (ray-project#64291). ExecutionResources keeps safe_round in __init__ (eager rounding); the perf work avoids allocations without reintroducing manual rounding: - Add __slots__ and cached zero()/inf() singletons to ExecutionResources. - Add combine_sum() to fold per-op usages in a single pass (one allocation instead of O(num_ops) intermediates); use it for the global/usage rollups. - Compute the reservation/budget loops on raw per-dimension floats (no ExecutionResources construction per intermediate). Rounding happens only at the constructor when a value is materialized, and drift-sensitive checks go through ExecutionResources methods. This rounds once per stored value rather than per step, which can differ from the object-based code by <=1 byte; the affected reservation-allocator test expectations are updated accordingly. - Add a regression test for ray-project#64291 (float drift in _get_max_scale_up). Co-Authored-By: Claude Opus 4.8 <[email protected]> Signed-off-by: xgui <[email protected]>
Rebased onto master after the ray-project#63680 revert (ray-project#64291). ExecutionResources keeps safe_round in __init__ (eager rounding); the perf work avoids allocations without reintroducing manual rounding: - Add __slots__ and cached zero()/inf() singletons to ExecutionResources. - Add combine(resources, fn): fold an iterable per dimension with a binary float op in a single pass (one allocation instead of O(num_ops) intermediates). combine_sum is a thin wrapper (operator.add) that returns the zero() singleton for an empty input; used for the global/usage rollups. - Compute the reservation/budget loops on raw per-dimension floats (no ExecutionResources construction per intermediate). Rounding happens only at the constructor when a value is materialized, and drift-sensitive checks go through ExecutionResources methods. This rounds once per stored value rather than per step, which can differ from the object-based code by <=1 byte; the affected reservation-allocator test expectations are updated accordingly. - Add a regression test for ray-project#64291 (float drift in _get_max_scale_up). Co-Authored-By: Claude Opus 4.8 <[email protected]> Signed-off-by: xgui <[email protected]>
Rebased onto master after the ray-project#63680 revert (ray-project#64291). ExecutionResources keeps safe_round in __init__ (eager rounding). This is a focused, low-risk set of allocation/accessor wins that produce the same results as before: - Add __slots__ and cached zero()/inf() singletons to ExecutionResources. - Add combine(resources, fn): fold an iterable per dimension with a binary float op in a single pass (one allocation instead of O(num_ops) intermediates). combine_sum is a thin wrapper (operator.add) returning the zero() singleton for an empty input; used for the global/usage rollups (replacing the per-element reduce/add folds). - Add a regression test for ray-project#64291 (float drift in _get_max_scale_up). The reservation/budget loops are left as-is (ExecutionResources methods): an earlier raw-float rewrite added per-step rounding complexity and could differ by <=1 byte, so it was dropped in favor of identical results and simpler maintenance. Co-Authored-By: Claude Opus 4.8 <[email protected]> Signed-off-by: xgui <[email protected]>
Rebased onto master after the ray-project#63680 revert (ray-project#64291). ExecutionResources keeps safe_round in __init__ (eager rounding). This is a focused, low-risk set of allocation/accessor wins that produce the same results as before: - Add __slots__ and cached zero()/inf() singletons to ExecutionResources. - Add combine(resources, fn): fold an iterable per dimension with a binary float op in a single pass (one allocation instead of O(num_ops) intermediates). combine_sum is a thin wrapper (operator.add) returning the zero() singleton for an empty input; used for the global/usage rollups (replacing the per-element reduce/add folds). - Add a regression test for ray-project#64291 (float drift in _get_max_scale_up). The reservation/budget loops are left as-is (ExecutionResources methods): an earlier raw-float rewrite added per-step rounding complexity and could differ by <=1 byte, so it was dropped in favor of identical results and simpler maintenance. Co-Authored-By: Claude Opus 4.8 <[email protected]> Signed-off-by: xgui <[email protected]>
…t#63680) ## Summary - `safe_round` in `ExecutionResources.__init__` was the largest leaf in the scheduler thread (~8% of wall time in the worker_scaling_5000_tasks release test); every `add`/`subtract`/`max`/`min`/`copy`/`scale`/`zero`/`inf`/`for_limits` paid four rounds per construction. - Drop rounding from `__init__`; store values at native precision. The boundary that feeds Ray Core (`to_resource_dict()`) now does the quantization (5 digits for cpu/gpu, integer bytes for memory). - `__eq__` / `__hash__` / `is_zero` / `is_non_negative` / `satisfies_limit` quantize on access so accumulated float drift (~1e-15 per cpu/gpu op, up to ~1e-4 per memory op on byte-magnitude floats) doesn't flip results. Microbenchmark (1M iterations of `add + subtract + max + min`): **22.9µs → 7.9µs per loop (~2.9×)**. ## Scheduling-loop benchmark Scheduling-loop duration from the worker-scaling release test, master vs this PR, across actor/task counts (seconds): | Row Name | p50_scheduling_loop_duration_s | p90_scheduling_loop_duration_s | max_scheduling_loop_duration_s | | :--- | :--- | :--- | :--- | | master_500_actors | 0.612625923 | 0.665133368 | 0.722738731 | | remove_safe_round_500_actors | 0.601569453 | 0.640638011 | 0.740870783 | | master_500_tasks | 0.130088642 | 1.37959566 | 2.648603352 | | remove_safe_round_500_tasks | 0.110020964 | 1.160841493 | 2.169771337 | | master_1000_actors | 1.305331189 | 1.427004707 | 1.558304391 | | remove_safe_round_1000_actors | 1.213747345 | 1.375785287 | 1.496900667 | | master_1000_tasks | 0.196409489 | 2.933294435 | 5.940470687 | | remove_safe_round_1000_tasks | 0.199019694 | 2.894881022 | 6.349671409 | | master_2000_actors | 2.635618952 | 2.854903978 | 3.065606266 | | remove_safe_round_2000_actors | 2.431840577 | 2.738552787 | 2.981410969 | | master_2000_tasks | 6.467886639 | 6.96452463 | 15.59612749 | | remove_safe_round_2000_tasks | 4.14211371 | 6.906086244 | 14.25746993 | | master_5000_actors | 7.535207658 | 8.594098917 | 8.973420257 | | remove_safe_round_5000_actors | 6.220295621 | 6.859902615 | 7.413166343 | | master_5000_tasks | 11.25641787 | 17.33123665 | 32.68019135 | | remove_safe_round_5000_tasks | 9.844983811 | 13.24368429 | 30.86245854 | p50/p90 improve consistently at scale — e.g. 5000_actors p50 7.54s → 6.22s (~17%) and p90 8.59s → 6.86s (~20%); 2000_tasks p50 6.47s → 4.14s; 5000_tasks p90 17.33s → 13.24s. --------- Signed-off-by: xgui <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
…y-project#63680)" (ray-project#64296) Reverts ray-project#63680 for ray-project#64291 . Unblocking the release Signed-off-by: xgui <[email protected]>
…t#63680) ## Summary - `safe_round` in `ExecutionResources.__init__` was the largest leaf in the scheduler thread (~8% of wall time in the worker_scaling_5000_tasks release test); every `add`/`subtract`/`max`/`min`/`copy`/`scale`/`zero`/`inf`/`for_limits` paid four rounds per construction. - Drop rounding from `__init__`; store values at native precision. The boundary that feeds Ray Core (`to_resource_dict()`) now does the quantization (5 digits for cpu/gpu, integer bytes for memory). - `__eq__` / `__hash__` / `is_zero` / `is_non_negative` / `satisfies_limit` quantize on access so accumulated float drift (~1e-15 per cpu/gpu op, up to ~1e-4 per memory op on byte-magnitude floats) doesn't flip results. Microbenchmark (1M iterations of `add + subtract + max + min`): **22.9µs → 7.9µs per loop (~2.9×)**. ## Scheduling-loop benchmark Scheduling-loop duration from the worker-scaling release test, master vs this PR, across actor/task counts (seconds): | Row Name | p50_scheduling_loop_duration_s | p90_scheduling_loop_duration_s | max_scheduling_loop_duration_s | | :--- | :--- | :--- | :--- | | master_500_actors | 0.612625923 | 0.665133368 | 0.722738731 | | remove_safe_round_500_actors | 0.601569453 | 0.640638011 | 0.740870783 | | master_500_tasks | 0.130088642 | 1.37959566 | 2.648603352 | | remove_safe_round_500_tasks | 0.110020964 | 1.160841493 | 2.169771337 | | master_1000_actors | 1.305331189 | 1.427004707 | 1.558304391 | | remove_safe_round_1000_actors | 1.213747345 | 1.375785287 | 1.496900667 | | master_1000_tasks | 0.196409489 | 2.933294435 | 5.940470687 | | remove_safe_round_1000_tasks | 0.199019694 | 2.894881022 | 6.349671409 | | master_2000_actors | 2.635618952 | 2.854903978 | 3.065606266 | | remove_safe_round_2000_actors | 2.431840577 | 2.738552787 | 2.981410969 | | master_2000_tasks | 6.467886639 | 6.96452463 | 15.59612749 | | remove_safe_round_2000_tasks | 4.14211371 | 6.906086244 | 14.25746993 | | master_5000_actors | 7.535207658 | 8.594098917 | 8.973420257 | | remove_safe_round_5000_actors | 6.220295621 | 6.859902615 | 7.413166343 | | master_5000_tasks | 11.25641787 | 17.33123665 | 32.68019135 | | remove_safe_round_5000_tasks | 9.844983811 | 13.24368429 | 30.86245854 | p50/p90 improve consistently at scale — e.g. 5000_actors p50 7.54s → 6.22s (~17%) and p90 8.59s → 6.86s (~20%); 2000_tasks p50 6.47s → 4.14s; 5000_tasks p90 17.33s → 13.24s. --------- Signed-off-by: xgui <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> Signed-off-by: 400Ping <[email protected]>
…y-project#63680)" (ray-project#64296) Reverts ray-project#63680 for ray-project#64291 . Unblocking the release Signed-off-by: xgui <[email protected]> Signed-off-by: 400Ping <[email protected]>

Summary
safe_roundinExecutionResources.__init__was the largest leaf in the scheduler thread (~8% of wall time in the worker_scaling_5000_tasks release test); everyadd/subtract/max/min/copy/scale/zero/inf/for_limitspaid four rounds per construction.__init__; store values at native precision. The boundary that feeds Ray Core (to_resource_dict()) now does the quantization (5 digits for cpu/gpu, integer bytes for memory).__eq__/__hash__/is_zero/is_non_negative/satisfies_limitquantize on access so accumulated float drift (~1e-15 per cpu/gpu op, up to ~1e-4 per memory op on byte-magnitude floats) doesn't flip results.Microbenchmark (1M iterations of
add + subtract + max + min): 22.9µs → 7.9µs per loop (~2.9×).Scheduling-loop benchmark
Scheduling-loop duration from the worker-scaling release test, master vs this PR, across actor/task counts (seconds):
p50/p90 improve consistently at scale — e.g. 5000_actors p50 7.54s → 6.22s (~17%) and p90 8.59s → 6.86s (~20%); 2000_tasks p50 6.47s → 4.14s; 5000_tasks p90 17.33s → 13.24s.