Cuda custom op to npu custom op

From Fudan-SMI-lab/SEAM · View interactive version

Cuda custom op to npu custom op is an AI agent skill from the GitHub repository Fudan-SMI-lab/SEAM. This directory is an experience reference for converting CUDA or C++ custom

How to use this skill

Skill file

CUDA Custom Op To Ascend Custom Op Migration Notes

This directory is an experience reference for converting CUDA or C++ custom

operators into Ascend C / CANN custom operators. It is documentation only. It is

not a task contract, agent instruction set, completion policy, validation gate,

or automatic execution workflow.

The notes below preserve practical migration lessons from previous projects.

Project scope, priorities, acceptance criteria, reports, and stop conditions live

in the user's current request or in the target project's own plan.

Common Placeholders

The examples use placeholders to avoid binding generic notes to one site or

project.

| Placeholder | Meaning |

|-------------|---------|

| {{PROJECT_NAME}} | Project being migrated |

| {{FRAMEWORK_NAME}} | Host framework or runtime, such as PyTorch, TensorFlow, native C++, or a plugin system |

| {{CANN_INSTALL_PATH}} | Active CANN install root |

| {{VENDOR_NAME}} | Vendor name registered in the custom OPP package |

| {{SOC_VERSION}} | Target SoC string used by CANN build and AddConfig |

| {{BASELINE_MODE}} | Existing reference mode used for accuracy and speed comparison |

| {{CUSTOM_OP_MODE}} | Mode that calls the Ascend custom-op path |

| {{CUSTOM_OPP_PATH}} | Active custom OPP vendor package path |

| {{CUSTOM_OPP_INSTALL_ROOT}} | Install root passed to the generated custom OPP installer |

| {{PROJECT_LOCAL_WORKDIR}} | Project-local scratch directory for generated experiments |

| {{op_name}} | Operator-specific source, host, kernel, and artifact stem |

| {{OP_TYPE}} | Generated CANN op type identifier |

Concrete paths, vendor names, SoCs, operator inventories, benchmark values, and

project-specific module names are better kept in project-local records or explicit

case studies.

Migration Mindset

generated headers, op-info entries, loaded-library paths, runtime probes, and

build logs usually explain the real state.

project, framework, generator, and CANN version.

operator counts look larger than the real installed OPP count.

success claims usually depend on project-defined scope and evidence.

Operator Mapping Experience

A useful migration record often traces each public entry through this path:

source public entry or framework alias
-> semantic operator
-> generated OPP entry
-> adapter callable
-> coverage key
-> parity scope

Several framework aliases can share one semantic operator or one OPP entry. Keeping

aliases visible in the record helps explain accuracy, coverage, and integration

results without confusing aliases with real installed custom ops.

Typical per-operator investigation artifacts:

kernel artifacts, and producer libraries.

Project Analysis Lessons

Common discovery targets include .cu, .cuh, .cpp, and .cc files; build

files for CUDA extensions; framework registries such as REGISTER_OP,

TORCH_LIBRARY, pybind11 modules, plugin tables, or dispatcher branches; backward

or gradient kernels; and device-specific dependencies such as CUDA streams, events,

atomics, texture memory, cooperative groups, CUB, cuBLAS, or cuDNN.

Inventory tables have been useful when they separate the source of truth for each

count:

| Count Type | Source of Truth | Meaning |

|------------|-----------------|---------|

| CUDA source ops | CUDA source and registration files | Semantic work present in the source project |

| Real NPU OPP ops | Generated ACLNN headers and installed op-info entries | Entries visible to the CANN package |

| Adapter functions | Binding module functions, plugin exports, or native symbols | Host-callable surface |

| Framework aliases | Project-specific registry names | Public names that may wrap or alias real ops |

Math And Semantics Lessons

Successful migrations usually started by recovering the public contract before

writing Ascend C code. Useful notes per op include the formula, boundary and mask

rules, shape behavior, dtype expectations, reference outputs, numerical tolerance,

and gradient dependencies.

Recurring observations:

missing the public semantics.

recreating the CUDA optimization schedule.

kernel returning one term of a larger formula.

Strategy Lessons

Common target strategies include Ascend C custom kernels, generated ACLNN APIs,

ACLRT direct launch, hybrid custom/baseline regions, and framework-native reference

paths. Adapter approaches vary across native C++ callers, framework plugins,

Python C extensions, ctypes, and PyTorch pybind11 with torch-npu streams.

Generated ACLNN headers are a useful signature reference because host adapter

signatures can differ from kernel parameter lists.

Ascend C Kernel Lessons

The templates/ascend_custom_op/ directory contains illustrative skeletons. They

are examples, not a prescribed project layout.

Observed kernel-level lessons:

has been useful for simple correctness-first kernels.

changes the feasible movement strategy.

pattern for LocalTensor.

integer variables in index math.

headers and target version.

partitioning or parallel writes; blockDim=1 was useful as a correctness probe.

diagnostics for Ascend device headers.

Package observations:

and binary/dynamic/.

error after a failed rebuild.

actual CANN build/install outputs rather than recreated manually.

Host Tiling And Op Definition Lessons

In op_host/{{op_name}}.cpp, the important migration facts usually include inputs,

outputs, attrs, tiling data, block dimensions, and SoC configuration.

Useful facts to record:

Scalar OpDef attrs worked well only after generated-header inspection and runtime

smoke evidence showed the attr signature, order, type, and lifecycle were stable.

Tiling fields or explicit tensor inputs were simpler in projects where scalar attrs

were unstable through wrappers.

Build, Install, And Path Lessons

Typical build/install records include the active CANN environment, custom-op source

directory, generated run package, install root, vendor package path, generated

headers, kernel artifacts, and libcust_opapi.so identity.

Observed path lessons:

others use opp/vendors.

producer library, but package-level headers, op-info entries, and kernel artifacts

still explain which op package is being observed.

Adapter And Caller Lessons

Adapter records were clearest when they captured the generated ACLNN signature,

adapter library or module path, exported callable, import/link proof, loaded adapter

identity, loaded producer identity, and same-run call observations.

PyTorch pybind11 observations:

indexing assumptions.

cache bugs in previous migrations.

Native or C++ caller observations:

dispatch behavior.

tiling definitions.

Validation And Coverage Lessons

Validation is project-specific. Useful evidence layers have included direct

kernel/reference comparisons, adapter import/link checks, repeated-call tests,

integration tests through the public project entry point, gradient tests, project

test suites, runtime call observations, and comparable baseline/custom timings.

Coverage evidence is easier to interpret when bound to manifest or mapping hash,

run id, workload identity, producer path and sha256, adapter path and sha256,

coverage key, and call count.

Fallback behavior is clearest when documented separately from custom-op coverage

and performance observations.

Benchmark And Reporting Lessons

Comparable benchmark notes usually include the same environment, same input set,

same dtype and shape scope, isolated baseline behavior, custom path evidence, and

measured speed ratio or slowdown. Slower custom paths are still useful information

when reported as measured slowdowns.

Project-local records often include operator inventory, migration manifest, build

attempt notes, runtime coverage JSON, baseline/custom benchmark JSON, speed report,

reproduction guide, and integration status. These documents are navigational and

evidentiary; this shared directory stays generic.

Troubleshooting Lessons

stale installed package artifacts.

handles, or repeated-call state.

libraries were incomplete artifact records.

registration, blockDim, stream, or kernel symbol-name mismatches.

routes rather than successful custom-op migration.

Experience Summary

  1. Separate semantic operators, generated OPP entries, adapter callables, and

framework aliases.

  1. Recover math before translating kernels.
  2. Build references before trusting custom-op call counts.
  3. Treat generated ACLNN headers as valuable adapter-signature evidence.
  4. Recheck artifact hashes after package installs that can replace producer

libraries.

  1. Validate public routes that the current project cares about, including aliases

and gradients when they are in scope.

  1. Keep fallback paths visible but separate from custom-op success metrics.
  2. Bind coverage observations to loaded producer and adapter identities.
  3. Keep shared notes generic and store project-specific results in project-local

reports or explicit examples.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.