Skip to content

Fix duplicate custom op registration error when adding new operations#2

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-b89da1d1-5a74-4b5f-972f-7e807c90e23c
Draft

Fix duplicate custom op registration error when adding new operations#2
Copilot wants to merge 3 commits intomainfrom
copilot/fix-b89da1d1-5a74-4b5f-972f-7e807c90e23c

Conversation

Copy link

Copilot AI commented Sep 22, 2025

Problem

Users encountered a RuntimeError when adding new custom operations to vLLM modules:

RuntimeError: Tried to register an operator (vllm::unified_attention(...)) with the same name and overload name multiple times. Each overload's schema should only be registered with a single call to def().

This error occurred when users added new custom operations like:

direct_register_custom_op(
    op_name="creat_attn_output_buffer",
    op_func=creat_attn_output_buffer,
    mutates_args=[],
    fake_impl=creat_attn_output_buffer_fake,
    dispatch_key=current_platform.dispatch_key,
)

The issue was that adding new custom operations could trigger module re-imports in complex scenarios, causing existing operations to be registered multiple times, which PyTorch's library system doesn't allow.

Solution

1. Graceful Duplicate Registration Handling

Modified direct_register_custom_op in vllm/utils/__init__.py to handle duplicate registrations gracefully:

try:
    my_lib.define(op_name + schema_str, tags=tags)
    my_lib.impl(op_name, op_func, dispatch_key=dispatch_key)
    if fake_impl is not None:
        my_lib._register_fake(op_name, fake_impl)
except RuntimeError as e:
    # Handle duplicate registration gracefully
    if "Tried to register an operator" in str(e) and "multiple times" in str(e):
        # The operation is already registered, which is fine
        # This can happen when modules are imported multiple times
        pass
    else:
        # Re-raise other RuntimeErrors as they indicate real problems
        raise

2. Type Annotation Compatibility

Fixed type annotation issues in custom function signatures. Changed torch.Size to torch.Tensor as torch.Size is not compatible with PyTorch's schema inference system:

def creat_attn_output_buffer(
    output_shape: torch.Tensor,  # Changed from torch.Size
    device: torch.device,
    dtype: torch.dtype,
    need_init: bool,
) -> torch.Tensor:
    # Convert tensor to tuple for shape
    shape = tuple(output_shape.tolist())
    # ... rest of implementation

Benefits

  • ✅ Users can safely add custom operations without registration conflicts
  • ✅ Modules can be imported multiple times without errors
  • ✅ Backward compatibility is maintained
  • ✅ All existing functionality is preserved
  • ✅ Prevents the specific "multiple times" registration error

Testing

Added comprehensive tests that validate:

  • Original operations register successfully
  • New user operations register successfully
  • Duplicate registrations are handled gracefully
  • Module re-imports work without errors

This fix resolves the registration conflicts that prevented users from extending vLLM with custom operations.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits September 22, 2025 15:30
Co-authored-by: izhuhaoran <43847754+izhuhaoran@users.noreply.github.com>
…direct_register_custom_op

Co-authored-by: izhuhaoran <43847754+izhuhaoran@users.noreply.github.com>
Copilot AI changed the title [WIP] 我在vllm/attention/layer.py这个文件下添加了: def creat_attn_output_buffer( output_shape: torch.Size, device: torch.device, dtype: torch.dtype, need_init: bool, ) -> torch.Tensor: if need_init: # Avoid output con... Fix duplicate custom op registration error when adding new operations Sep 22, 2025
Copilot AI requested a review from izhuhaoran September 22, 2025 15:40
izhuhaoran pushed a commit that referenced this pull request Oct 13, 2025
izhuhaoran pushed a commit that referenced this pull request Jan 28, 2026
…3058)

Signed-off-by: ramos <49182011+nemoramo@users.noreply.github.com>
Signed-off-by: mayufeng <mayufeng@example.com>
Co-authored-by: mayufeng <mayufeng@example.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants