Skip to content

[Compat] Update DispatchKey usage to c10::DispatchKey and replace dispatch_key_to_string with c10::toString#78525

Merged
SigureMo merged 5 commits intoPaddlePaddle:developfrom
gouzil:feat/update_DispatchKey_namespace
Apr 2, 2026
Merged

[Compat] Update DispatchKey usage to c10::DispatchKey and replace dispatch_key_to_string with c10::toString#78525
SigureMo merged 5 commits intoPaddlePaddle:developfrom
gouzil:feat/update_DispatchKey_namespace

Conversation

@gouzil
Copy link
Copy Markdown
Member

@gouzil gouzil commented Mar 30, 2026

PR Category

Execute Infrastructure

PR Types

Devs

Description

移除 paddle/phi/api/include/compat/torch/library.h 下的 DispatchKey, 改用 c10::DispatchKey

是否引起精度变化

Copilot AI review requested due to automatic review settings March 30, 2026 06:22
@paddle-bot
Copy link
Copy Markdown

paddle-bot Bot commented Mar 30, 2026

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot Bot added the contributor External developers label Mar 30, 2026
@gouzil gouzil requested review from SigureMo and Copilot and removed request for Copilot March 30, 2026 06:23
Copy link
Copy Markdown
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 aims to align the compat Torch library registration API with the upstream-style dispatch key type by switching from a local DispatchKey enum to c10::DispatchKey, and replacing the local dispatch_key_to_string() helper with c10::toString() for logging/printing.

Changes:

  • Add c10/core/DispatchKey.h include and switch operator implementation dispatch keys to c10::DispatchKey.
  • Replace dispatch_key_to_string(...) usages with c10::toString(...).
  • Remove the local DispatchKey enum and string conversion helper.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
paddle/phi/api/include/compat/torch/library.h Switch operator registry dispatch-key storage to c10::DispatchKey and remove local DispatchKey enum/helper.
paddle/phi/api/include/compat/torch/library.cpp Update logging/printing from dispatch_key_to_string to c10::toString.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 662 to 684
@@ -696,7 +680,7 @@ class PADDLE_API OperatorRegistry {
const std::string& schema);

void register_implementation(const std::string& qualified_name,
DispatchKey key,
c10::DispatchKey key,
CppFunction&& func);
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

DispatchKey enum and dispatch_key_to_string() were removed, but this header still uses DispatchKey in multiple places (e.g., Library constructor/member, value_or(DispatchKey::CPU), and the TORCH_LIBRARY_IMPL macro uses torch::DispatchKey::k). As-is, this will not compile. Please either switch all remaining uses to c10::DispatchKey (and update DispatchKey::CPU etc), or introduce using DispatchKey = c10::DispatchKey; in namespace torch to keep the existing torch::DispatchKey references working (and update the .cpp definitions and tests accordingly).

Copilot uses AI. Check for mistakes.
#include <type_traits>
#include <unordered_map>
#include <vector>
#include "c10/core/DispatchKey.h"
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

Include style is inconsistent here: other external headers in this file use angle brackets (e.g., <c10/macros/Macros.h>), but DispatchKey.h is included with quotes. Prefer #include <c10/core/DispatchKey.h> to match the rest of the compat headers and avoid accidental relative-include resolution.

Suggested change
#include "c10/core/DispatchKey.h"
#include <c10/core/DispatchKey.h>

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

这个人家说的有道理,我们 compat 的统一用 <>

Comment on lines 236 to 242
void OperatorRegistry::register_implementation(
const std::string& qualified_name, DispatchKey key, CppFunction&& func) {
auto& op = get_or_create_operator(qualified_name);
op.implementations[key] = std::move(func);
VLOG(3) << "Registered implementation: " << qualified_name << " for "
<< dispatch_key_to_string(key);
<< c10::toString(key);
}
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

OperatorRegistry::register_implementation definition still takes DispatchKey key, but the header now declares c10::DispatchKey key. This is an ODR/compile error. Update the .cpp signature (and any remaining DispatchKey uses in this translation unit) to match the header/type decision.

Copilot uses AI. Check for mistakes.
Comment on lines 268 to 284
// Library
Library::Library(Kind kind,
const std::string& ns,
std::optional<DispatchKey> dispatch_key,
const char* file,
uint32_t line)
: kind_(kind),
ns_(ns),
dispatch_key_(dispatch_key),
file_(file),
line_(line) {
std::stringstream oss;
oss << "Created Library: kind=" << kind_to_string(kind)
<< ", namespace=" << ns;
if (dispatch_key) {
oss << ", dispatch_key=" << dispatch_key_to_string(*dispatch_key);
oss << ", dispatch_key=" << c10::toString(*dispatch_key);
}
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

Library::Library(...) definition still uses std::optional<DispatchKey> for dispatch_key, but the PR is moving toward c10::DispatchKey and the header currently still references DispatchKey in several places. Once DispatchKey is removed/aliased, this will fail to compile. Please align the constructor parameter/member types with the chosen dispatch key type (e.g., std::optional<c10::DispatchKey> or torch::DispatchKey alias).

Copilot uses AI. Check for mistakes.
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 30, 2026

Codecov Report

❌ Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@8309551). Learn more about missing BASE report.

Files with missing lines Patch % Lines
paddle/phi/api/include/compat/torch/library.cpp 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #78525   +/-   ##
==========================================
  Coverage           ?   83.33%           
==========================================
  Files              ?        2           
  Lines              ?        6           
  Branches           ?        0           
==========================================
  Hits               ?        5           
  Misses             ?        1           
  Partials           ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

op.implementations[key] = std::move(func);
VLOG(3) << "Registered implementation: " << qualified_name << " for "
<< dispatch_key_to_string(key);
<< c10::toString(key);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Image

/skip-reason 编译过了就不影响效果

@SigureMo SigureMo merged commit 0ee9000 into PaddlePaddle:develop Apr 2, 2026
111 of 117 checks passed
liuhao2638 pushed a commit to liuhao2638/Paddle that referenced this pull request Apr 7, 2026
YuhanXu pushed a commit to YuhanXu/Paddle that referenced this pull request Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants