Skip to content

Treat only a standalone baseline as a relative outcome constraint - #5337

Open
uditstocks wants to merge 2 commits into
meta:mainfrom
uditstocks:fix-baseline-metric-name
Open

uditstocks wants to merge 2 commits into
meta:mainfrom
uditstocks:fix-baseline-metric-name

Conversation

@uditstocks

Copy link
Copy Markdown

An outcome constraint is treated as relative whenever the text baseline appears anywhere in its expression, also inside a metric name. So baseline_latency <= 10 becomes a relative constraint of +900% instead of an absolute bound of 10, and latency_vs_baseline >= 2 becomes +100%. The constraint still prints as OutcomeConstraint(baseline_latency <= 10), so nothing shows the change. Without a status quo arm, the optimization then stops at the first Bayesian optimization trial with DataRequiredError: Optimization config has relative constraint, but model was not fit with status quo. With a status quo arm (client.attach_baseline, baseline_latency of 10), the first Bayesian optimization trial fails instead with ValueError: StandardizeY transform does not support relative constraint OutcomeConstraint(baseline_latency <= 100): Derelativize turns +900% into 100, and its rebuilt expression is again read as relative. On this branch the same run completes its Bayesian optimization trials.

The same check has two more effects:

  • cost + baseline_cost <= 5 is rejected by Client.configure_optimization with "Scalarized outcome constraints cannot be relative".
  • model.baseline <= 10 (or model/baseline) becomes the metric model.1 with a bound of +900%, because the standalone replacement \bbaseline\b -> 1 also matches after a dot or a slash.

Cause

_parse_constraint_expression in ax/core/outcome_constraint.py starts with if "baseline" in expression_str:. It came with the expression refactor in #5000. Before that, parse_outcome_constraint in from_string.py treated only a SymPy symbol named exactly baseline as the relative marker, so these metric names worked.

Fix

Match baseline only as a standalone token: not next to a word character or one of the separators that sanitize_name keeps inside metric names (., :, /, |, ~, @, -, and ( after it). A - before baseline counts as part of a name only after a word character, as in sanitize_name, so qps-baseline is a metric name while qps >= -baseline stays relative. The same pattern is used for the check, for stripping * baseline, and for the standalone baseline -> 1 replacement. Expressions that use baseline as intended (qps >= 0.95 * baseline, qps >= baseline) parse exactly as before. The library change is 10 lines added and 3 removed in one file; the rest is tests.

flowchart TD
    A[Constraint baseline_latency at most 10] --> B{Parse the expression}
    B -->|Before| C[Text baseline found inside the metric name]
    C --> D[Relative bound of 900 percent]
    B -->|After| E[Only a standalone baseline counts]
    E --> F[Absolute bound of 10]
Loading

Narrated recording, before then after (sound on):

evidence.mp4

Repro, as typed in the recording

from ax import Client, RangeParameterConfig
from ax.analysis import MetricSummary
client = Client(random_seed=0)
client.configure_experiment(name="demo", parameters=[RangeParameterConfig(name="x", parameter_type="float", bounds=(0, 1))])
client.configure_optimization(objective="accuracy", outcome_constraints=["baseline_latency <= 10"])
for _ in range(6):
    for i, p in client.get_next_trials(max_trials=1).items():
        client.complete_trial(i, raw_data={"accuracy": p["x"], "baseline_latency": 20 * p["x"]})

client.compute_analyses([MetricSummary()], display=False)[0].df[["Name", "Bound"]]

On main (08aa942), five trials complete, then:

ax.exceptions.core.DataRequiredError: Optimization config has relative constraint, but model was not fit with status quo.
>>> client.compute_analyses([MetricSummary()], display=False)[0].df[["Name", "Bound"]]
               Name      Bound
0          accuracy       None
1  baseline_latency  <= 900.0%

On this branch, all six trials complete (the sixth is the first Bayesian optimization trial), then:

               Name    Bound
0          accuracy     None
1  baseline_latency  <= 10.0

The recording is a plain Python REPL with this branch's Ax. Before the session starts, Ax's INFO logs are turned off and sys.tracebacklimit is set to 0, so an error shows as its last line only. Repeated typing is cut; nothing else is edited.

Edge cases and unchanged behaviour checked

Shown in the third part of the recording, in a fresh Client on this branch with these constraints:

constraints = ["latency_vs_baseline >= 2", "model.baseline <= 10", "cost + baseline_cost <= 5",
               "qps >= 0.95 * baseline", "cpu <= baseline", "latency <= 10"]
client.configure_optimization(objective="accuracy", outcome_constraints=constraints)
client.configure_generation_strategy()
client.compute_analyses([MetricSummary()], display=False)[0].df[["Name", "Bound"]]
                  Name                      Bound
0             accuracy                       None
1        baseline_cost  cost + baseline_cost <= 5
2                 cost  cost + baseline_cost <= 5
3                  cpu                    <= 0.0%
4              latency                    <= 10.0
5  latency_vs_baseline                     >= 2.0
6       model.baseline                    <= 10.0
7                  qps                   >= -5.0%
  • Edge case: latency_vs_baseline >= 2 stays absolute, >= 2.0 (on main: >= 100.0%).
  • Edge case: model.baseline <= 10 keeps its name and its bound, <= 10.0 (on main: metric model.1, <= 900.0%).
  • Edge case: cost + baseline_cost <= 5 is accepted as an absolute two-metric constraint (on main: UserInputError: Scalarized outcome constraints cannot be relative).
  • Unchanged: qps >= 0.95 * baseline is still relative, >= -5.0%.
  • Unchanged: cpu <= baseline is still relative, <= 0.0%.
  • Unchanged: latency <= 10 is still absolute, <= 10.0.

Checked by the new unit test only: model/baseline <= 3 and qps-baseline <= 3 (absolute), baseline_qps >= 0.95 * baseline (relative, -5%), model.baseline >= baseline (relative, 0%) and qps >= -baseline (still relative, as on main).

Not covered: a multi-word metric name with baseline as a separate word, such as latency vs baseline, still counts as relative, as on main.

Tests

  • New test_BaselineInMetricName in ax/core/tests/test_outcome_constraint.py, and one more case in test_relative_scalarized_constraints (a + 2 * baseline_b <= 1.0) in ax/api/utils/instantiation/tests/test_from_string.py. On main they fail (900.0 != 10.0, 100.0 != 2.0, ['model.1'] != ['model.baseline'], ['model/1'] != ['model/baseline'], ['qps-1'] != ['qps-baseline'], 400.0 != 5.0, and UserInputError: Scalarized outcome constraints cannot be relative). On this branch they pass.
  • pytest ax/core/tests/test_outcome_constraint.py ax/api/utils/instantiation/tests/test_from_string.py ax/core/tests/test_optimization_config.py ax/utils/common/tests/test_sympy.py ax/utils/common/tests/test_string_utils.py ax/adapter/transforms/tests/test_relativize_transform.py ax/adapter/transforms/tests/test_derelativize_transform.py: 86 passed, 77 subtests passed.
  • pytest ax/api/tests/test_client.py -k "configure or constraint or baseline": 13 passed.
  • pytest ax/storage/json_store/tests/test_json_store.py -k "constraint or Constraint or relative": 2 passed, 5 subtests passed.
  • ufmt check (black 25.11.0 and usort 1.1.0 as in .pre-commit-config.yaml, and again with the ruff-api formatter set in pyproject.toml) and flake8 7.0.0 on the changed files: clean.
  • Run on Windows with Python 3.12. Ax's TestCase installs a SIGALRM handler, which Windows does not have, so pytest ran through a small wrapper that maps SIGALRM to SIGTERM and makes signal.alarm a no-op. Pyre was not run.

An outcome constraint was marked relative whenever the text "baseline"
appeared anywhere in its expression, including inside a metric name.
So "baseline_latency <= 10" was parsed as a relative constraint with a
bound of +900% instead of an absolute bound of 10, and names such as
"model.baseline" were rewritten to "model.1".

Match "baseline" only as a standalone token, as the parser did before
the expression refactor, where only a SymPy symbol named exactly
"baseline" made a constraint relative.

Signed-off-by: Udit Sharma <uditsharma9981@gmail.com>
The standalone-token check treated any hyphen before "baseline" as part
of a metric name, so "qps >= -baseline" lost its relative marker and
"baseline" became a second metric. sanitize_name joins a hyphen into a
name only after a word character, so match that: "qps-baseline" stays a
metric name and "-baseline" stays a sign.

Signed-off-by: Udit Sharma <uditsharma9981@gmail.com>
@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Dec 6, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant