Skip to content

Commit 4899b71

Browse files
CopilotTobiBu
andauthored
style: use jnp.inf and collapse convergence detection to single line
Agent-Logs-Url: https://github.com/AstroAI-Lab/rubix/sessions/48d7ef6c-c096-4eaf-ad70-797404b09044 Co-authored-by: TobiBu <7574273+TobiBu@users.noreply.github.com>
1 parent 2d6de90 commit 4899b71

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

rubix/inference/optimize.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,20 @@ def scan_step(carry, _):
125125
trainable_params,
126126
opt_state,
127127
trainable_params,
128-
jnp.array(float("inf")),
128+
jnp.array(jnp.inf),
129129
)
130130
(final_params, _, final_best_params, best_loss_val), (
131131
loss_arr,
132132
grad_norm_arr,
133133
update_norm_arr,
134134
) = jax.lax.scan(scan_step, init_carry, None, length=max_steps)
135135

136-
# Detect convergence post-hoc; no device->host sync until here
136+
# Detect convergence post-hoc; no device->host sync until here.
137+
# argmax over a boolean array returns the index of the first True; the
138+
# `converged` guard ensures it is only used when at least one True exists.
137139
converged_mask = update_norm_arr < tol
138140
converged = bool(jnp.any(converged_mask))
139-
if converged:
140-
steps_run = int(jnp.argmax(converged_mask)) + 1
141-
else:
142-
steps_run = max_steps
141+
steps_run = (int(jnp.argmax(converged_mask)) + 1) if converged else max_steps
143142

144143
# Materialize history once at the end
145144
loss_history: list[float] = loss_arr[:steps_run].tolist()

0 commit comments

Comments
 (0)