latest (dev)
Copy
Latest development documentation · Updated 2026-11-18
Adding a device backend
TensorPlay reserves one device slot, PrivateUse1, for a backend that lives
outside this repository: a new accelerator, a simulator, a remote device. The
slot is a full backend. It has its own dispatch key and its own autograd,
autocast and vmap layers, and it can take a name of its own, so
tp.ones(3, device="npu") reads like any other device.
The reference backend under test/cpp_extensions/openreg/ is about 200 lines
and implements everything below. Read it alongside this note.
What a backend provides
Piece |
Where it is registered |
What it answers |
|---|---|---|
Allocator |
|
where every tensor on the device gets its memory |
Device hooks |
|
device count, current device, synchronization, raw byte copies |
Kernels |
|
the operators themselves |
Name |
|
how the device is spelled in Python |
The C++ pieces register from static initializers, so loading the library is
all it takes. tensorplay.utils.cpp_extension.load compiles sources against
the p10 headers, links libp10, caches the build and loads it:
import tensorplay as tp
from tensorplay.utils import cpp_extension
cpp_extension.load("npu_backend", ["npu_backend.cpp"])
tp.utils.rename_privateuse1_backend("npu")
x = tp.randn(4, 4).to("npu")
Kernels
Every backend implements four operators natively, because everything else is built on them:
emptyandempty.memory_formatallocate from the backend’s allocator;copy_moves data between the device and the host in both directions, and between two devices of the backend;itemreads a single value back to the host.
Views (view, as_strided, t, slicing, …) are composite kernels: they
only rewrite sizes and strides, so they serve the new device with no work.
So do composite operators built from other operators (_to_copy,
empty_strided, ones, …), once the operators they call have kernels.
A kernel is called through a pointer cast to the operator’s kernel
signature, so a kernel of a different type is undefined behaviour.
tensorplay/ops/KernelSignatures.h spells the signature of every operator
as a C++ function type. Casting to it makes a mismatch a compile error:
#include "tensorplay/ops/KernelSignatures.h"
namespace sig = tensorplay::kernel_signatures;
TENSORPLAY_LIBRARY_IMPL(PrivateUse1, NpuKernels) {
m.impl("empty", static_cast<sig::empty*>(&npu_empty));
m.impl("copy_", static_cast<sig::copy_*>(&npu_copy));
m.impl("mm", static_cast<sig::mm*>(&npu_mm));
}
Names replace the overload dot with two underscores: add.Tensor becomes
sig::add__Tensor.
Bringing a model up with host fallbacks
A backend rarely starts with a kernel for every operator. CpuFallback.h
turns any operator’s CPU kernel into a kernel for the device. It copies the
device arguments to the host, runs the CPU kernel, then copies the results
and the written arguments back:
#include "CpuFallback.h"
TENSORPLAY_LIBRARY_IMPL(PrivateUse1, NpuHostFallbacks) {
m.impl("addmm", cpu_fallback::kernel<"addmm", sig::addmm>);
m.impl("add_.Tensor", cpu_fallback::kernel<"add_.Tensor", sig::add___Tensor>);
}
The fallback moves data only with the backend’s own empty and copy_, so
it works on hardware the host cannot address. Every call pays two transfers
per tensor. Use it to get a model running, then replace fallbacks with
device kernels one operator at a time. It cannot serve operators whose
result aliases an input (views); the composite view kernels already cover
those.
To see which operators a workload still runs this way, count them:
with tp.utils.count_host_fallbacks() as calls:
train_step()
print(calls) # {"pow.Tensor_Scalar": 2, "ones_like": 1, ...}; {} when none
An operator with no kernel for the device fails loudly with Kernel not found for op: <name> on backend: PrivateUse1. Set
TP_TRACE_KERNEL_MISS=1 to also print the call stack that reached it.
Autograd, autocast and vmap come with the slot
Derivative formulas are written in terms of operators, and the generated
autograd kernels are registered for AutogradPrivateUse1 like for every
in-tree backend. Once the forward and backward operators a model uses have
kernels, loss.backward() works on the new device. Autocast uses the CUDA
policy (float16 by default) under tp.autocast("npu"). tp.func.vmap
batches device tensors through the same batch rules as the host.
Device hooks
The hooks object answers device-level questions the runtime cannot answer by itself. Every method has a default that fits one synchronous device, so a backend overrides only what differs:
struct NpuHooks final : tensorplay::PrivateUse1HooksInterface {
int64_t device_count() const override { return npu_device_count(); }
int64_t current_device() const override { return npu_get_device(); }
void set_device(int64_t i) const override { npu_set_device(i); }
void synchronize(int64_t i) const override { npu_synchronize(i); }
void copy_bytes(void* dst, const tensorplay::Device&, const void* src,
const tensorplay::Device&, size_t n) const override {
npu_memcpy(dst, src, n);
}
};
NpuHooks hooks;
REGISTER_PRIVATEUSE1_HOOKS(&hooks);
A named backend with hooks becomes the process’s accelerator, ahead of any
in-tree one. tp.accelerator.current_accelerator(), device_count(),
set_device_index(), device_index(...) and synchronize() then answer
for it.
Profiling
tensorplay.profiler reports device time for the backend’s operations once
the backend registers timer hooks: an event recorded on the device before
and after each operation, read back when the session stops.
#include "Profiler.h"
struct NpuTimer final : tensorplay::prof::DeviceTimerHooks {
int current_device() const override { return npu_get_device(); }
void* record(int device) const override { return npu_event_record(device); }
bool elapsed_ms(void* start, void* end, float* ms) const override {
return npu_event_elapsed(start, end, ms); // waits for ``end``
}
void release(void* event) const override { npu_event_free(event); }
};
const bool npu_timer = [] {
tensorplay::prof::register_privateuse1_timer_hooks(new NpuTimer);
return true;
}();
profile(activities=[ProfilerActivity.CPU, ProfilerActivity.PrivateUse1])
or profile(use_device="npu") then fills the device-time columns, titled
with the device’s name. A backend whose vendor has an activity-tracing
library can also register DeviceActivityHooks: the session then collects
the kernels, copies and fills the device ran, each joined to the operation
that launched it through the operation’s slot, pushed as the collector’s
external correlation id while the operation dispatches. They appear in
prof.kineto_results(), in the kernel counts and in exported traces.
Python side
tp.utils.rename_privateuse1_backend(name)names the device. The name is fixed for the process. It must be an identifier and may not be an in-tree device type."privateuseone"keeps parsing afterwards.tp.utils.generate_methods_for_privateuse1_backend()addsTensor.npu(),Tensor.is_npu,Module.npu()and thePackedSequencecounterparts.tp._register_device_module("npu", module)installs a runtime module astp.npu. Without one,tp.npuis a default module built on the hooks:is_available,device_count,current_device,set_device,synchronize.Custom operators take device kernels under the slot’s names:
op.register_kernel("PrivateUse1")(or"privateuseone", or the registered name) serves tensors on the device. The registration can come before or after the rename.
Packaging
A backend shipped as a wheel declares an entry point in the
tensorplay.backends group. import tensorplay calls every such entry
point, so the backend loads its library and names its device without the
user importing anything:
[project.entry-points."tensorplay.backends"]
npu = "tensorplay_npu:_autoload"
Set TENSORPLAY_DEVICE_BACKEND_AUTOLOAD=0 to skip autoloading.
Help improve this page
Found an error, an unclear step, or a missing example?
Autograd mechanics
This note will present an overview of how autograd works and records the operations. It’s not strictly necessary to understand all this, but we recommend getting familiar with it, as it will help you write more efficient
Broadcasting semantics
Many TensorPlay operations support NumPy’s broadcasting semantics. See Numpy Documentation for details.

