Repository: ggerganov/llama.cpp
File: ggml/src/ggml-cann/aclnn_ops.cpp (C++)

Code before:

    int64_t theta_scale_length = ne00 / 2;
    int64_t theta_scale_ne[] = {theta_scale_length, 1, 1, 1};
    size_t theta_scale_nb[] = {sizeof(float_t), sizeof(float_t), sizeof(float_t),
                          theta_scale_length * sizeof(float_t)};

    GGML_ASSERT(src1->type == GGML_TYPE_I32);
    int64_t position_length = src1->ne[0];
    int64_t position_ne[] = {1, 1, position_length, 1};
    size_t position_nb[] = {sizeof(int32_t), sizeof(int32_t), sizeof(int32_t),
                            sizeof(int32_t) * position_length};

    int64_t theta_ne[] = {theta_scale_length, 1, position_length, 1};
    size_t theta_nb[GGML_MAX_DIMS];
    theta_nb[0] = sizeof(float_t);
    for (int i = 1; i < GGML_MAX_DIMS; i++) {
        theta_nb[i] = theta_nb[i - 1] * theta_ne[i - 1];
    }

    bool is_q = (std::strncmp(dst->name, "Qcur-", 5) == 0);
    bool is_k = (std::strncmp(dst->name, "Kcur-", 5) == 0);
    bool is_attention = is_q || is_k;

    if(ctx.init_ptr == nullptr || !is_attention) {
        // theta_scale arange, [0,1,...,ne00/2 - 1]
        if(ctx.init_ptr != nullptr){
            ACL_CHECK(aclrtFree(ctx.init_ptr));
        }
        ACL_CHECK(aclrtMalloc(&ctx.init_ptr,theta_scale_length * sizeof(float_t), ACL_MEM_MALLOC_HUGE_FIRST));
        
        aclTensor* acl_theta_scale_tensor =
            ggml_cann_create_tensor(ctx.init_ptr, ACL_FLOAT, sizeof(float_t),
                                    theta_scale_ne, theta_scale_nb, GGML_MAX_DIMS);
        float start = 0;
        float step = 1;
        float stop = ne00 / 2;
        float n_elements = ne00 / 2;
        aclnn_arange(ctx, acl_theta_scale_tensor, start, stop, step, n_elements);

        // power
        aclScalar* acl_theta_scale = aclCreateScalar(&theta_scale, aclDataType::ACL_FLOAT);
        GGML_CANN_CALL_ACLNN_OP(ctx, PowScalarTensor, acl_theta_scale, acl_theta_scale_tensor,
                                acl_theta_scale_tensor);
        
        // freq_scale
        if (freq_scale != 1) {
            aclnn_muls(ctx, acl_theta_scale_tensor, freq_scale, nullptr, true);
        }

        // freq_factors
        if (src2) {

Review comment (suggestion):
Add a comment indicating that `is_attention` is a flag used for accuracy testing.

Code after:

    int64_t theta_scale_length = ne00 / 2;
    int64_t theta_scale_ne[] = {theta_scale_length, 1, 1, 1};
    size_t theta_scale_nb[] = {sizeof(float_t), sizeof(float_t), sizeof(float_t),
                          theta_scale_length * sizeof(float_t)};

    GGML_ASSERT(src1->type == GGML_TYPE_I32);
    int64_t position_length = src1->ne[0];
    int64_t position_ne[] = {1, 1, position_length, 1};
    size_t position_nb[] = {sizeof(int32_t), sizeof(int32_t), sizeof(int32_t),
                            sizeof(int32_t) * position_length};

    int64_t theta_ne[] = {theta_scale_length, 1, position_length, 1};
    size_t theta_nb[GGML_MAX_DIMS];
    theta_nb[0] = sizeof(float_t);
    for (int i = 1; i < GGML_MAX_DIMS; i++) {
        theta_nb[i] = theta_nb[i - 1] * theta_ne[i - 1];
    }

    bool is_q = (std::strncmp(dst->name, "Qcur-", 5) == 0);
    bool is_k = (std::strncmp(dst->name, "Kcur-", 5) == 0);

    // used for accuracy testing
    bool is_attention = is_q || is_k;

    if(ctx.init_ptr == nullptr || !is_attention) {
        // theta_scale arange, [0,1,...,ne00/2 - 1]
        if(ctx.init_ptr != nullptr){
            ACL_CHECK(aclrtFree(ctx.init_ptr));
        }
        ACL_CHECK(aclrtMalloc(&ctx.init_ptr, theta_scale_length * sizeof(float_t), ACL_MEM_MALLOC_HUGE_FIRST));

        aclTensor* acl_theta_scale_tensor =
            ggml_cann_create_tensor(ctx.init_ptr, ACL_FLOAT, sizeof(float_t),
                                    theta_scale_ne, theta_scale_nb, GGML_MAX_DIMS);
        float start = 0;
        float step = 1;
        float stop = ne00 / 2;
        float n_elements = ne00 / 2;
        aclnn_arange(ctx, acl_theta_scale_tensor, start, stop, step, n_elements);

        // power
        aclScalar* acl_theta_scale = aclCreateScalar(&theta_scale, aclDataType::ACL_FLOAT);
        GGML_CANN_CALL_ACLNN_OP(ctx, PowScalarTensor, acl_theta_scale, acl_theta_scale_tensor,
                                acl_theta_scale_tensor);

        // freq_scale
        if (freq_scale != 1) {
            aclnn_muls(ctx, acl_theta_scale_tensor, freq_scale, nullptr, true);
        }