Skip to content

Configure Ascend worker CPU affinity ​

Use automatic CPU affinity to assign each local Ascend NPU worker an equal number of physical cores and logical CPUs within one NUMA node. Each worker receives all allowed SMT siblings of its assigned cores, and workers never share a physical core within the same launch.

NPU affinity is enabled by default. TokenSpeed reserves at least 20% of the allowed physical cores outside worker masks. Rounding or NUMA locality can leave more cores unused. This reserve is global, not 20% on every NUMA node.

Prerequisites ​

Before you configure worker affinity, prepare the deployment environment:

  • Install the TokenSpeed runtime, tokenspeed-kernel, and tokenspeed-kernel-npu from the same revision. Older NPU wheels do not contain this planner.
  • Make the ACL and DCMI libraries and valid device mappings available to the launcher.
  • Expose /sys/devices/system/node/node*/cpulist and /sys/devices/system/cpu/cpu*/topology/thread_siblings_list in the container.
  • Use a cpuset with equal SMT visibility per physical core so that workers receive equal CPU counts without splitting a core. TokenSpeed partitions a restricted taskset or container cpuset; it never expands the allowed CPUs.
  • If you run independent launchers on the same host, assign them disjoint CPU sets as described in Multiple engines on one host.

TokenSpeed derives CPU IDs from the effective scheduler mask, not cpu_count() or a virtualized cpu/online file.

Configure worker affinity ​

Configure the launcher environment before starting the server:

  1. Unset PTA's CPU_AFFINITY_CONF so that PTA cannot override the worker masks planned by TokenSpeed:

    bash
    unset CPU_AFFINITY_CONF

    TokenSpeed rejects startup when this variable is set while automatic NPU affinity is enabled.

  2. Enable NPU affinity and set the CPU reserve percentage:

    bash
    export TOKENSPEED_NPU_NUMA_AWARE_WORKER_AFFINITY=true
    export TOKENSPEED_NPU_CPU_RESERVE_PERCENT=20

    This example explicitly sets both defaults. Replace 20 with the global percentage of allowed physical cores to reserve, from 0 through 99.

  3. Launch TokenSpeed with your model and parallelism arguments. This example starts a single-host server with tensor parallelism:

    bash
    tokenspeed serve MODEL_PATH \
      --device npu \
      --tensor-parallel-size LOCAL_WORKER_COUNT

    Replace the following:

    • MODEL_PATH: the model ID or local checkpoint path.
    • LOCAL_WORKER_COUNT: the local tensor-parallel worker count.

    Add the other model and parallelism arguments that your deployment uses. Startup logs report each worker's NPU ordinal, NUMA node, and complete CPU mask. Continue with Verify worker CPU affinity.

Optional: Disable automatic worker affinity ​

To keep the inherited CPU mask or use PTA's CPU_AFFINITY_CONF, disable the NPU switch before launch:

bash
export TOKENSPEED_NPU_NUMA_AWARE_WORKER_AFFINITY=false

The separate TOKENSPEED_NUMA_AWARE_WORKER_AFFINITY switch controls NVIDIA binding. CUDA and ROCm retain their existing affinity behavior and do not load the NPU backend.

CPU placement behavior ​

The NPU implementation lives in tokenspeed-kernel-npu. The launcher accesses it through tokenspeed-kernel and plans all local workers together before the native host and PTA create threads:

text
Allowed CPUs (sched_getaffinity) + sysfs NUMA/SMT relationships
                          +
Torch-visible NPU IDs -> ACL physical IDs -> DCMI card/chip CPU locality
                          |
           equal, disjoint, single-NUMA worker masks
                          |
  narrow spawning thread -> start worker -> restore launcher mask
                          |
        worker and subsequently created threads inherit mask

The launcher looks up each rank's device ID and applies the corresponding mask inside the existing process-start context. The worker, native host, and their subsequently created threads inherit the assignment. ACL discovery runs in a short-lived child process, which preserves visible-device mapping without creating an NPU context in the launcher.

Missing libraries, invalid device mappings, and incomplete topology fail startup with the missing detail. Only DCMI's explicit NOT_SUPPORT response selects host-NUMA balancing, as on fully connected A3 supernodes. Ascend 910B ARM and x86 systems use driver-reported locality; the planner requires no architecture-specific core counts or SMT assumptions.

Automatic affinity sets only CPU masks. It does not set NUMA memory policy or exclude operating-system threads, interrupts, or other applications from worker CPUs. A CPU bandwidth quota remains a separate limit; binding does not increase it.

Multiple engines on one host ​

Independent launchers, including prefill and decode roles, must receive disjoint CPU sets from the deployment system. Device visibility alone does not reserve host CPUs. TokenSpeed partitions each role's cpuset; it is not a host-wide CPU allocator.

To configure multiple engines on one host, follow these steps:

  1. Split whole physical cores between roles, keeping SMT siblings together.
  2. Match each role's CPU budget to its worker count, and apply the same reserve percentage inside each role's cpuset.
  3. Start each launcher with its assigned cpuset and the configuration in Configure worker affinity.

Each launcher partitions only its own allowed CPUs. Verify every role separately.

Verify worker CPU affinity ​

After startup, verify both the planned masks and the masks inherited by worker threads:

  1. Check the startup logs for one entry per local worker. Each entry has the following form, with host-specific IDs and counts:

    text
    NPU 0: NUMA 0, 4 physical cores, 8 logical CPUs (0, 1, 2, 3, 4, 5, 6, 7); 2 physical cores remain outside this launch's worker masks

    Confirm that workers have equal physical-core and logical-CPU counts, each worker belongs to one NUMA node, and worker masks do not share a physical core.

  2. Inspect the native control, forward, and runtime threads for a worker. Replace WORKER_PID with the worker's process ID:

    bash
    grep -H -E '^(Name|Cpus_allowed_list):' \
      /proc/WORKER_PID/task/*/status

    The output includes the name and Cpus_allowed_list value in every thread's status file. Compare these values with the complete CPU mask in that worker's startup log entry. The worker and threads created after the assignment must inherit the planned mask.

Compare performance with affinity disabled ​

CPU affinity alone does not guarantee a performance improvement. To measure its effect, follow these steps:

  1. Compare cold starts with TOKENSPEED_NPU_NUMA_AWARE_WORKER_AFFINITY set to false and true. Keep model weights, concurrency, request inputs, prefix-cache state, and runtime settings identical.
  2. Warm every actual batch shape before measuring.
  3. Compare throughput, TTFT, TPOT, and total elapsed time over repeated, alternating runs.

Vendor references ​

TokenSpeed documentation