aclnnSleep
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Not supported
Atlas A2 products : Not supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description: Puts the NPU device to sleep for a specified number of clock cycles. The operator implements precise delay control through SIMT
clock()busy-waiting, with semantics consistent with CUDA'sspin_kernel/torch.cuda._sleep.Formula:
For the given number of clock cycles, cycles, aclnnSleep performs the following computation:
Obtain the current clock counter value as the start time:
$$ start = clock() $$Busy-wait until the number of elapsed cycles reaches the specified value:
$$ while(clock() - start < cycles): spin $$The actual sleep time (in seconds) depends on the clock frequency:
$$ t_{sleep} = \frac{cycles}{f_{clock}} $$
Function Prototype
Each operator uses a two-phase API. Call the "aclnnSleepGetWorkspaceSize" API first to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call the "aclnnSleep" API to perform the computation.
NOTE: The
cyclesparameter is of theaclIntArray*type. Pass the value directly. The array contains one element, which is the number of sleep cycles.
aclnnStatus aclnnSleepGetWorkspaceSize(
const aclIntArray* cycles,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnSleep(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnSleepGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor cycles (aclIntArray*) Input Number of clock cycles to sleep. Due to the declared value dependency, the aclnn API layer converts it to aclIntArray* for passing. - Must be a positive integer (cycles > 0).
- Empty arrays are not supported.
- Limited by the AI Core timeout, the maximum value of cycles is about 1.782e12 (at the main frequency of 1.65 GHz on
Ascend 950PR&950DT products , about 18 minutes). For details, see Constraints.
INT64 - [1] - workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the Device. The current implementation returns 0. - - - - executor (aclOpExecutor**) Output Returns the op executor containing the operator computation flow. - - - - - Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Value Error Code Description ACLNN_ERR_PARAM_INVALID 161002 The input data type is not in the supported range (not INT64). ACLNN_ERR_INNER_TILING_ERROR 561002 cycles is an empty array, and data retrieval fails in the tiling phase. The cycles parameter is non-positive (cycles <= 0).
aclnnSleep
Parameters
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device. The current implementation does not require a workspace, so nullptr can be passed. workspaceSize Input Workspace size allocated on the Device, obtained by the first-phase API aclnnSleepGetWorkspaceSize. executor Input Operator executor that contains the operator computation flow. stream Input Stream where the task is executed. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Determinism: aclnnSleep defaults to a deterministic implementation.
- Input restriction: cycles must be a positive integer (cycles > 0); otherwise, ACLNN_ERR_INNER_TILING_ERROR (561002) is returned.
- cycles is passed in as aclIntArray*, and the array contains 1 element, which specifies the number of sleep cycles.
- Empty arrays are not supported: if cycles is an empty array, ACLNN_ERR_INNER_TILING_ERROR (561002) is returned.
- Inputs containing ±inf or nan are not supported: cycles is of the INT64 type and does not have inf/nan itself. If an upstream passes in a value converted from a floating-point type (such as PyTorch's
.to(torch.int64)),+infis converted to INT64_MAX; the cycles check passes, but an AICore timeout is triggered at runtime.-infandnanare converted to non-positive values, which are rejected in the tiling phase and return ACLNN_ERR_INNER_TILING_ERROR (561002). - AI Core timeout limit: The default AI Core execution timeout is 18 minutes. On the
Ascend 950PR&950DT products (main frequency 1.65GHz), the maximum value of cycles is about 1.782e12 (= 1080s × 1.65GHz). If the value exceeds this limit, the operator may be forcibly terminated. Set cycles based on the actual use case. For a longer sleep time, adjust this limit by changing the AI Core timeout configuration through theaclrtSetOpExecuteTimeOutAPI.
Example
The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include "acl/acl.h"
#include "aclnnop/aclnn_sleep.h"
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define LOG_PRINT(message, ...) \
do { \
printf(message, ##__VA_ARGS__); \
} while (0)
int Init(int32_t deviceId, aclrtStream* stream) {
auto ret = aclInit(nullptr);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetDevice(deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
ret = aclrtCreateStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
return 0;
}
int main() {
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
int64_t cyclesValue = 1000000;
LOG_PRINT("Testing aclnnSleep with cycles = %ld\n", cyclesValue);
aclIntArray* cycles = aclCreateIntArray(&cyclesValue, 1);
CHECK_RET(cycles != nullptr, LOG_PRINT("aclCreateIntArray failed.\n"); return ACL_ERROR_BAD_ALLOC);
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
ret = aclnnSleepGetWorkspaceSize(cycles, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSleepGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
void* workspaceAddr = nullptr;
if (workspaceSize > 0) {
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
}
ret = aclnnSleep(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSleep failed. ERROR: %d\n", ret); return ret);
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
LOG_PRINT("aclnnSleep test passed.\n");
aclDestroyIntArray(cycles);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}