Skip to content

feat!: support polymorphic handles and configs - #919

Merged
voltjia merged 2 commits into
masterfrom
feat/polymorphic-handle-config
Aug 11, 2026
Merged

feat!: support polymorphic handles and configs#919
voltjia merged 2 commits into
masterfrom
feat/polymorphic-handle-config

Conversation

@voltjia

@voltjiavoltjia commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Add a reusable Cloneable<Base, Derived> CRTP helper for concrete Config and Handle subclasses.
  • Give Config and Handle virtual destructors and virtual Clone() methods while keeping both base classes concrete.
  • Replace the two sliced OperatorBase value members with owned std::unique_ptr clones; existing set_config(const Config&) and set_handle(const Handle&) signatures stay unchanged.
  • Add a focused installed-header C++ smoke test covering derived state, base state, clone independence, and source-object lifetime.

Motivation

Operator-specific options currently have to be added to the shared src/config.h, because assigning a derived Config or Handle to OperatorBase slices its dynamic state. This PR provides only the ownership mechanism needed for operator-specific subclasses.

This change is deliberately independent of #800 and contains no Triton/JIT, cache-key, constructor-dispatch, or handle-borrowing optimization changes.

Type of Change

  • feat - polymorphic Config and Handle ownership
  • fix - bug fix
  • perf - performance improvement
  • refactor - code restructuring without behavior change
  • test - focused C++ API coverage
  • docs - documentation only
  • build / ci - build system or CI configuration
  • chore - tooling, formatting, or other non-code changes
  • Breaking change

Platforms Affected

The shared public types and OperatorBase are used by every backend.

  • CPU (WITH_CPU)
  • NVIDIA (WITH_NVIDIA)
  • Iluvatar (WITH_ILUVATAR)
  • MetaX (WITH_METAX)
  • Cambricon (WITH_CAMBRICON)
  • Moore (WITH_MOORE)
  • Ascend (WITH_ASCEND)
  • PyTorch C++ bindings (WITH_TORCH)
  • Build system / CMake / CI
  • Python bindings / user-facing API

Smoke Test Result

Validated locally from commit 1c56d9f5e7ecd5c38c4be984aea1e5f6c00f759f:

wsl g++ -std=c++17 -Wall -Wextra -Werror -Isrc <multilevel Cloneable smoke>
# passed
python3 -m py_compile tests/test_cpp_api.py
# passed
clang-format 21.1.8 --dry-run --Werror \
src/cloneable.h src/config.h src/handle.h src/operator.h
# passed
ruff 0.15.22 format --check tests/test_cpp_api.py
ruff 0.15.22 check tests/test_cpp_api.py
# passed

GitHub Actions provides the installed-library and backend build/run coverage. Checks are rerunning for the current commit; the preceding commit passed clang-format, Ruff, documentation build, and unit plus shadow jobs for NVIDIA, Iluvatar, MetaX, Moore, Cambricon, and Ascend.

Test Results on Supported Platforms

PlatformAffectedBuild / Smoke ResultFull Result / Notes
CPUyeslocal C++17 API syntax probe passedinstalled-library smoke covered by backend CI
NVIDIAyesunit and shadow CI passedpassed
Iluvataryesunit and shadow CI passedpassed
MetaXyesunit and shadow CI passedpassed
Cambriconyesunit and shadow CI passedpassed
Mooreyesunit and shadow CI passedpassed
Ascendyesunit and shadow CI passedpassed

Benchmark / Performance Impact

No performance optimization is included.

Config is cloned when an operator is constructed, which normally happens on a cache miss. The existing invocation path calls set_handle() for every invocation; after this change that performs one virtual Clone(), one derived-object copy, and one heap allocation per call. A borrowed-handle fast path can be evaluated separately without coupling it to the polymorphism API.

Notes for Reviewers

  • This is ABI-breaking: Config and Handle gain virtual tables, and the protected OperatorBase::config_ / handle_ members change from values to std::unique_ptr.
  • Multi-level hierarchies are supported by writing each layer as Cloneable<Parent, Derived>; directly inheriting a cloneable concrete class without rebinding preserves the parent clone behavior.
  • The base Config and Handle classes remain directly constructible and clone themselves by default.
  • There is no production RTTI, dynamic type hash, or config fingerprint in this PR.
  • Cache-key behavior is unchanged. An operator whose derived Config fields affect cached behavior must include those fields explicitly in its existing CacheKeyBuilder<Key> specialization.
  • set_config() and set_handle() remain replaceable, matching the previous setter semantics; no one-time initialization rule is added.

@voltjia
voltjia marked this pull request as ready for review August 11, 2026 07:48
@voltjia
voltjia requested a review from a teamAugust 11, 2026 07:48
@voltjia
voltjia merged commit a540636 into masterAug 11, 2026
20 checks passed
@voltjia
voltjia deleted the feat/polymorphic-handle-config branch August 11, 2026 08:07
Sign up for freeto 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

@voltjia@Ziminli