Deterministic Computation

Introduction

During application development, due to differences such as CANN versions or NPU models, results of multiple runs of the same operator may not be exactly the same; even under the same conditions with the same random seed, results of multiple NPU executions may differ. This difference usually stems from asynchronous multi-threaded execution in operator implementations, which changes the order of floating-point accumulation.

Deterministic computation requires the same running conditions, such as the platform, device, software version, input, and random parameters. It does not guarantee consistent results across CANN versions or NPU models.

In some special use cases (such as experiments, debugging, and regression tests), operators need to produce the same output across multiple runs to locate problems or perform algorithm analysis. This approach is generally called "deterministic computation".

Notes

  • Enabling deterministic computation is generally not recommended, because the same operator generally executes more slowly with deterministic computation than with non-deterministic computation, which may degrade the performance of a single model run. However, in use cases such as experiments, debugging, and regression tests, where consistent results across multiple runs are required to locate problems or analyze algorithms, deterministic computation can improve efficiency.

  • Thread note: Setting determinism multiple times in one thread is currently not recommended. For the configuration scope and validity rules, see the Runtime API documentation of the corresponding version.

Usage

Currently, CANN operators are mainly called through the aclnn API or the PyTorch API (provided by the external TorchNPU framework). Some operator APIs default to a deterministic implementation, and some default to a non-deterministic implementation. For operators with a non-deterministic implementation, some support deterministic computation through manual configuration.

  • Calling the aclnn API

    In this use case, enable determinism through the "Runtime Configuration > aclrtSetSysParamOpt" API (process-level) in Runtime APIs. Specifically, set ACL_OPT_DETERMINISTIC=1 to enable determinism.

  • Calling the PyTorch API

    In this use case, enable deterministic computation through the native PyTorch switch (torch.use_deterministic_algorithms).

For operator APIs of different frameworks, refer to the specific aclnn API documentation or PyTorch API documentation for the default implementation strategy of deterministic computation.