Skip to content

feat: Add warnings for invalid tool_choice and UTs - #6582

Merged
zhyncs merged 8 commits into
mainfrom
chang/tool-call-tests
May 27, 2025
Merged

zhyncs merged 8 commits into
mainfrom
chang/tool-call-tests

Conversation

@CatherineSue

@CatherineSue CatherineSue commented May 25, 2025 •

Copy link
Copy Markdown
Collaborator

Motivation

This PR improves error handling and user experience for invalid tool choices in SGLang's function calling system. Previously, when users specified a tool_choice with a function name that doesn't exist in the available tools, the system would silently fail with Skip invalid EBNF strings. This change adds proper validation and logging.
NOTE: the correct way is to add this kind of validation in the ChatCompletionRequest protocol and returns a 400. However, our adapter.py or protocol.py doesn't have this kind of check. And it is better to fix all similar issues in refactor. So right now we use a logging.warning to mitigate it.

The changes also introduce comprehensive unit tests for tool choice functionality across different models and scenarios, ensuring robust validation of the function calling system.

Modifications

  • Add validation in get_ebnf() method to check if the requested function exists in available tools
  • Add test_tool_choice.py to run_suite:
    • tool_choice="auto" in streaming and non-streaming modes
    • tool_choice="required" in streaming and non-streaming modes
    • Specific function selection via tool_choice={"type": "function", "function": {"name": "..."}}
    • Multi-tool scenarios
    • Error handling for invalid function names
    • Support for multiple model types (Llama, Qwen2.5, Mistral), which covers (Llama32Detector, MistralDetector, and Qwen25Detector)

Checklist

- Add checks for invalid tool_choice when tool_choice is
ToolChoice, e.g. function_name is not in tools
- Add test_tool_choice.py to run_suite
@CatherineSue

Copy link
Copy Markdown
Collaborator Author

UT result:
python3 -m unittest test_tool_choice.TestToolChoice

Screenshot 2025-05-24 at 10 24 01 PM

- Replace class-level FLAKY_TESTS with setUpClass configuration
 for better inheritance handling
- Remove warning system in favor of explicit flaky test marking
- Mark `test_multi_tool_scenario_auto` and
`test_multi_tool_scenario_required` as flaky
- Increase max_tokens from 200 to 400 for better tool calling
performance
- Update `test_tool_choice_specific_function_non_streaming` to
handle multiple weather calls for top 5 cities
@CatherineSue

Copy link
Copy Markdown
Collaborator Author

Updated UT with flaky tests instead of print warnings
Screenshot 2025-05-25 at 11 16 44 AM

@zhyncs zhyncs self-assigned this May 26, 2025

@JustinTong0323 JustinTong0323 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@zhyncs zhyncs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verified locally

@zhyncs
zhyncs merged commit 41ba767 into main May 27, 2025
@zhyncs
zhyncs deleted the chang/tool-call-tests branch May 27, 2025 23:53
Layssy pushed a commit to Layssy/sglang-iaas that referenced this pull request Jun 9, 2025
xwu-intel pushed a commit to xwu-intel/sglang that referenced this pull request Jun 17, 2025
0826joyce pushed a commit to 0826joyce/sglang-perf-opt that referenced this pull request May 19, 2026
yyihuang pushed a commit to yyihuang/sglang that referenced this pull request Oct 16, 2026
…merge commit 5c5c44c314a6 (round 6)

Move the FlashInfer contract pin from 36fd76859292 (PR sgl-project#6330 merge)
to 5c5c44c314a6, the merge commit of
PR sgl-project#6582 (round 6).

python/sglang/kernels/cake_kernels/mamba.py: the contract paragraph
now describes the round-6 programs:
- Prescaled decay factor table and single-rounding FMA state update:
  the same algorithm, validated against an fp64 oracle, not bitwise
  the round-5 programs.
- Batched programs issue the operand TMA before the metadata stage.
- The FlashInfer host selects one of three exact-scan forms per call:
  fused single-launch (exactfused) inside the fused window for the
  cu_seqlens form, the batched form, and the triple form with a
  trusted caller cumsum; the round-6 two-kernel form (exactpre) from
  two chunks per work item on; the retained round-5 two-kernel
  programs (exact, byte for byte) for one-chunk items.
- The _noring twin stays for the measured BF16/FP16 windows and is no
  longer selected for FP32 state.
- The chunk-parallel family is a single-pass decoupled look-back
  program (chunkpar); the three-phase program is retained (chunkpar3,
  bitwise round 5). FLASHINFER_CAKE_SSD_CHUNK_PARALLEL forces or
  disables it.
- It also states: any positive seqlen, graph-capturable prepared
  submission, BF16/FP16/FP32 state, token-major out, and varlen
  without initial_states.

test/registered/kernels/ops/mamba/test_cake_ssd_combined.py:
test_route_call_cu_seqlens_at_engine_chunk_size_matches_triple_and_stock
asserted torch.equal(out_cu, out_tr) and torch.equal(final_cu,
final_tr) between the cu_seqlens form and the triple form. Under the
round-6 host route, the one-chunk geometries run different programs
in the two forms. On the old pin with the round-6 FlashInfer tree,
four parametrizations failed on B200 and GB300 (38 passed,
4 failed). The test now checks:
(a) bitwise equality only with both forms pinned to the same
    two-kernel family: fused_selected is monkeypatched to return
    False, so the cu_seqlens form also takes two_kernel_family,
    which reads the same host quantities for both forms (same
    approach as the FlashInfer out-of-range test);
(b) each form on its own within the fixed tolerance of the fp64
    recurrence (_assert_within_fixed_tolerance, stock Triton(256)
    as the comparison arm); the cu_seqlens form already had this
    check, the triple form gains it;
(c) a cross-form sanity check at the state-dtype tolerance for the
    production (unpinned) routes.
The docstring says: bitwise where both forms run the same program;
the one-chunk geometries run different programs under the round-6
host route and are compared under tolerance.

Routing is unchanged. The public contract (supports_ssd_combined,
ssd_combined_fwd, run, prepare) is unchanged.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
yyihuang pushed a commit to yyihuang/sglang that referenced this pull request Oct 19, 2026
…merge commit f41bfa18fcee (rounds 7-8)

Move the FlashInfer contract pin from 5c5c44c314a6 (PR sgl-project#6582 merge,
round 6) to f41bfa18fcee, the merge commit of PR sgl-project#6746 (round 8),
which includes PR sgl-project#6715 (2a2fa514203f, round 7).

python/sglang/kernels/cake_kernels/mamba.py: the contract paragraph
now describes the round-7 and round-8 host routes:
- Round 7 (PR sgl-project#6715): inside the fused shape window the seq_idx
  triple form without a caller seq_chunk_cumsum is routed by compute
  capability -- sm_100a runs a light packed-varlen metadata kernel
  ahead of the fused main kernel (two launches, exactmeta), sm_103a
  runs the fused main kernel deriving its private sequence table in
  a CTA prologue (one launch, exactone) unless the table must be
  published into the caller's buffer, which keeps the two-kernel
  program. Two-kernel calls of at least 16 chunks per item with more
  (sequence, head) items than SMs, a balanced persistent grid that
  leaves a tail wave and a shorter pair makespan run the 2-CTA pair
  programs (exactpre_*_tail, bitwise the same outputs). The device
  programs release the auxiliary ring's empty barrier per reader
  (racecheck-clean; no routing or numerics change).
- Round 8 (PR sgl-project#6746): sm_107a (Rubin) programs in the FlashInfer
  loader -- the triple form routed as on sm_103a, Rubin-calibrated
  chunk-parallel cost constants and a Rubin three-phase rule. This
  adapter keeps admitting sm_100a / sm_103a only (ARCHS unchanged);
  sm_107a is listed with the architectures the adapter leaves to the
  existing SGLang path.

test/registered/kernels/ops/mamba/test_cake_ssd_combined.py:
unchanged. The pinned-family bitwise clause still closes the fused
window through fused_selected; the pair program needs 16 chunks per
item and more items than SMs, which none of the test geometries
reach, so both forms keep taking the two-kernel family from the same
host quantities.

Tests against FlashInfer f41bfa18fcee:
- test/registered/unit/kernels/test_cake_mamba_sp_routes.py:
  47 passed (CPU, on x86_64 and on aarch64).
- test/registered/kernels/ops/mamba/test_cake_ssd_combined.py:
  42 passed on B200 (sm_100a) and 42 passed on GB300 (sm_103a);
  no skips, no xfails.

Routing of this adapter and its public contract
(supports_ssd_combined, ssd_combined_fwd, run, prepare) are
unchanged.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants