Skip to content

Conversation

@xunyoyo
Copy link

@xunyoyo xunyoyo commented Nov 15, 2025

This test file implements various unit tests for the ResourceManagerV1 class, including functionality for managing requests, handling multimodal inputs, and testing resource allocation and scheduling behaviors.

Motivation

NO.39 功能模块 fastdeploy/engine/sched/resource_manager_v1.py 单测补充

Modifications

add tests/engine/test_resource_manager_v1.py

Usage or Command

tests/engine/test_resource_manager_v1.py:

python -m coverage run -m unittest tests.engine.test_resource_manager_v1 \
&& python -m coverage report -m --include='fastdeploy/engine/sched/resource_manager_v1.py'

Accuracy Tests

tests/engine/test_resource_manager_v1.py:

Name                                             Stmts   Miss  Cover   Missing
------------------------------------------------------------------------------
fastdeploy/engine/sched/resource_manager_v1.py     590    114    81%   207, 256-257, 285-286, 295, 342-349, 370, 380, 409-428, 4
39, 442-443, 451, 462, 479, 485-488, 519-520, 539-549, 591-626, 634, 653, 656-657, 684-686, 700, 703-704, 722-726, 748, 751-755,
 792-794, 811, 815-819, 830, 841, 844-845, 858-873, 884, 886, 904, 975, 981-983
------------------------------------------------------------------------------
TOTAL                                              590    114    81%

Checklist

  • Add at least a tag in the PR title.
    • Tag list: [[FDConfig],[APIServer],[Engine], [Scheduler], [PD Disaggregation], [Executor], [Graph Optimization], [Speculative Decoding], [RL], [Models], [Quantization], [Loader], [OP], [KVCache], [DataProcessor], [BugFix], [Docs], [CI], [Optimization], [Feature], [Benchmark], [Others], [XPU], [HPU], [GCU], [DCU], [Iluvatar], [Metax]]
    • You can add new tags based on the PR content, but the semantics must be clear.
  • Format your code, run pre-commit before commit.
  • Add unit tests. Please write the reason in this PR if no unit tests.
  • Provide accuracy results.
  • If the current PR is submitting to the release branch, make sure the PR has been submitted to the develop branch, then cherry-pick it to the release branch with the [Cherry-Pick] PR tag.

This test file implements various unit tests for the ResourceManagerV1 class, including functionality for managing requests, handling multimodal inputs, and testing resource allocation and scheduling behaviors.
Copilot AI review requested due to automatic review settings November 15, 2025 10:44
@paddle-bot
Copy link

paddle-bot bot commented Nov 15, 2025

Thanks for your contribution!

@paddle-bot paddle-bot bot added the contributor External developers label Nov 15, 2025
Copilot finished reviewing on behalf of xunyoyo November 15, 2025 10:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds comprehensive unit tests for the ResourceManagerV1 class in the FastDeploy engine scheduler module, achieving 81% test coverage. The test suite includes various scenarios such as request scheduling, resource allocation, multimodal input handling, cache management, and preemption behaviors.

  • Adds extensive test coverage for ResourceManagerV1 functionality
  • Implements mock objects and stubs to isolate tests from external dependencies
  • Tests core scheduling behaviors including prefill, decode, and preemption flows

self.calls.append(("inc", value))


class _FakePrefixCacheManager:
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

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

The _FakePrefixCacheManager class lacks documentation. Add a docstring to explain its purpose:

class _FakePrefixCacheManager:
    """
    A mock implementation of PrefixCacheManager for testing.
    
    Simulates cache block allocation and management without requiring
    actual GPU resources, making tests faster and more isolated.
    """
Suggested change
class _FakePrefixCacheManager:
class _FakePrefixCacheManager:
"""
A mock implementation of PrefixCacheManager for testing.
Simulates cache block allocation and management without requiring
actual GPU resources, making tests faster and more isolated.
"""

Copilot uses AI. Check for mistakes.
pass


class _FakeSignal:
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

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

The _FakeSignal class lacks documentation. Add a docstring to explain its purpose:

class _FakeSignal:
    """
    A mock implementation of IPCSignal for testing.
    
    Provides a numpy array-based signal without actual inter-process communication.
    """
Suggested change
class _FakeSignal:
class _FakeSignal:
"""
A mock implementation of IPCSignal for testing.
Provides a numpy array-based signal without actual inter-process communication.
"""

Copilot uses AI. Check for mistakes.

num_tokens = manager._get_num_new_tokens(request, token_budget)

assert num_tokens == 4 # Modal boundary extended the budget
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider adding a comment to explain why the modal boundary extends the budget from 3 to 4 tokens. This would make the test's intent clearer:

assert num_tokens == 4  # Modal boundary at patch_map[2] requires extending budget from 3 to 4
Suggested change
assert num_tokens == 4 # Modal boundary extended the budget
assert num_tokens == 4 # Modal boundary at patch_map[2] requires extending budget from 3 to 4

Copilot uses AI. Check for mistakes.
Comment on lines +377 to +390
def test_schedule_allocates_extend_blocks(resource_manager_factory):
manager = resource_manager_factory(enable_prefix=False, max_num_seqs=1)
request = _make_request("extend-flow", list(range(8)))
request.idx = 0
request.block_tables = manager.cache_manager.allocate_gpu_blocks(4)
request.num_computed_tokens = request.need_prefill_tokens
request.output_token_ids = [10, 11, 12, 13]
request.use_extend_tables = True
manager.running.append(request)
manager.requests[request.request_id] = request
manager.req_dict[request.request_id] = 0
manager.tasks_list[0] = request
manager.stop_flags[0] = False
manager.need_block_num_signal.value[0] = 2
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

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

This test manually sets up many internal state variables (lines 380-390). Consider extracting this setup into a helper function to avoid duplication and improve maintainability:

def _setup_running_request(manager, request, idx=0, need_block_num=0):
    """Setup a request in running state with proper internal bookkeeping."""
    request.idx = idx
    manager.running.append(request)
    manager.requests[request.request_id] = request
    manager.req_dict[request.request_id] = idx
    manager.tasks_list[idx] = request
    manager.stop_flags[idx] = False
    if need_block_num:
        manager.need_block_num_signal.value[idx] = need_block_num

This pattern is repeated in multiple tests (e.g., lines 441-448, 491-496).

Copilot uses AI. Check for mistakes.
import pytest


def _install_required_stubs():
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

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

The _install_required_stubs function lacks documentation. Consider adding a docstring to explain its purpose:

def _install_required_stubs():
    """
    Install stub modules for paddle and paddleformers to enable testing without actual dependencies.
    
    This function creates mock modules that simulate the behavior of paddle and paddleformers
    libraries, allowing tests to run in isolation without requiring the full framework installation.
    """
Suggested change
def _install_required_stubs():
def _install_required_stubs():
"""
Install stub modules for paddle and paddleformers to enable testing without actual dependencies.
This function creates mock modules that simulate the behavior of paddle and paddleformers
libraries, allowing tests to run in isolation without requiring the full framework installation.
"""

Copilot uses AI. Check for mistakes.
)


class _MetricRecorder:
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

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

The _MetricRecorder helper class lacks documentation. Add a docstring to explain its purpose:

class _MetricRecorder:
    """
    A test helper class that records metric operations for verification.
    
    Tracks both the current value and a history of all set/inc operations
    to facilitate assertions in unit tests.
    """
Suggested change
class _MetricRecorder:
class _MetricRecorder:
"""
A test helper class that records metric operations for verification.
Tracks both the current value and a history of all set/inc operations
to facilitate assertions in unit tests.
"""

Copilot uses AI. Check for mistakes.
return _factory


def _make_request(request_id, prompt_token_ids, **kwargs):
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

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

The _make_request helper function lacks documentation. Add a docstring to explain its purpose and parameters:

def _make_request(request_id, prompt_token_ids, **kwargs):
    """
    Create a test Request object with specified parameters.
    
    Args:
        request_id: Unique identifier for the request
        prompt_token_ids: List of token IDs for the prompt
        **kwargs: Optional attributes like disaggregate_info, cached_block_num, etc.
    
    Returns:
        Request: A configured Request object for testing
    """
Suggested change
def _make_request(request_id, prompt_token_ids, **kwargs):
def _make_request(request_id, prompt_token_ids, **kwargs):
"""
Create a test Request object with specified parameters.
Args:
request_id: Unique identifier for the request.
prompt_token_ids: List of token IDs for the prompt.
**kwargs: Optional attributes to set on the Request object, such as:
- disaggregate_info (dict): Additional info for disaggregation.
- cached_block_num (int): Number of cached blocks.
- multimodal_inputs (dict): Multimodal input data.
- output_token_ids (list): Output token IDs.
- reasoning_max_tokens (int): Max tokens for reasoning.
- use_extend_tables (bool): Whether to use extended tables.
- extend_block_tables (list): Extended block tables.
Returns:
Request: A configured Request object for testing.
"""

Copilot uses AI. Check for mistakes.
manager.add_request(req1)
manager.add_request(req2)

monkeypatch.setattr(rm_v1.paddle, "is_compiled_with_xpu", lambda: True, raising=False)
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

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

The raising=False parameter is incorrect for monkeypatch.setattr. This parameter doesn't exist in pytest's monkeypatch API. The correct usage is either:

  1. Just omit this parameter: monkeypatch.setattr(rm_v1.paddle, "is_compiled_with_xpu", lambda: True)
  2. Or use raising=True (the default) to raise an error if the attribute doesn't exist

However, since paddle is already stubbed in line 37, and the test is trying to override it, this should work without raising=False.

Suggested change
monkeypatch.setattr(rm_v1.paddle, "is_compiled_with_xpu", lambda: True, raising=False)
monkeypatch.setattr(rm_v1.paddle, "is_compiled_with_xpu", lambda: True)

Copilot uses AI. Check for mistakes.

_install_required_stubs()

import fastdeploy.engine.sched.resource_manager_v1 as rm_v1
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

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

Module 'fastdeploy.engine.sched.resource_manager_v1' is imported with both 'import' and 'import from'.

Suggested change
import fastdeploy.engine.sched.resource_manager_v1 as rm_v1

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant