从哪里可以查看到PTA(例如:PTAGetExecCache)相关接口的介绍说明
收藏回复举报
从哪里可以查看到PTA(例如:PTAGetExecCache)相关接口的介绍说明
t('forum.solved') 已解决
新人帖
发表于2024-05-21 14:41:01
0 查看

我在op_plugin仓库中看到算子launch前可以通过hit_cache接口,规避掉AclTensor转换等操作。针对这块代码看起来是比较容易理解的,但是存在一系列的接口,没有找到任何说明。请问社区我们可以从那边查看到这些接口的使用说明。谢谢。例如:PTAGetExecCache, SetPTAHashKey,InitPTACacheThreadLocal等

如下是op_plugin仓库op_api_common.h文件中的一段代码:

template <typename... Args> bool hit_cache(aclrtStream acl_stream, const char *aclnn_api, void *phrase2, Args &&...args)
{
    static const auto ptaGetExecCacheAddr = GetOpApiFuncAddr("PTAGetExecCache");
    static const auto initPTACacheThreadLocalAddr = GetOpApiFuncAddr("InitPTACacheThreadLocal");
    static const auto setPTAHashKeyAddr = GetOpApiFuncAddr("SetPTAHashKey");
    static const auto canUsePTACacheAddr = GetOpApiFuncAddr("CanUsePTACache");
    PTAGetExecCache ptaGetExecCacheFunc = reinterpret_cast<PTAGetExecCache>(ptaGetExecCacheAddr);
    InitPTACacheThreadLocal initPTACacheThreadLocalFunc =
        reinterpret_cast<InitPTACacheThreadLocal>(initPTACacheThreadLocalAddr);
    SetPTAHashKey setPTAHashKeyFunc = reinterpret_cast<SetPTAHashKey>(setPTAHashKeyAddr);
    CanUsePTACache canUsePTACacheFunc = reinterpret_cast<CanUsePTACache>(canUsePTACacheAddr);
    bool has_func = ptaGetExecCacheFunc && initPTACacheThreadLocalFunc && setPTAHashKeyFunc;
    bool can_use = canUsePTACacheFunc && canUsePTACacheFunc(aclnn_api);
    if (!has_func || !can_use) {
        return false;
    }
    uint64_t workspace_size = 0;
    uint64_t *workspace_size_addr = &workspace_size;
    initPTACacheThreadLocalFunc();
    g_hash_offset = 0;
    add_param_to_buf(std::string(aclnn_api), args...);
    uint64_t hashId = calc_hash_id();
    setPTAHashKeyFunc(hashId);
    aclOpExecutor *executor = ptaGetExecCacheFunc(hashId, workspace_size_addr);
    if (executor == nullptr) {
        return false;
    }
    void *workspace_addr = nullptr;
    if (workspace_size != 0) {
        auto workspace_tensor = at_npu::native::OpPreparation::unsafe_empty_workspace(workspace_size);
        workspace_addr = const_cast<void *>(workspace_tensor.storage().data());
    }
    auto acl_call = [workspace_addr, workspace_size, acl_stream, executor, phrase2]() -> int {
        OpApiFunc opApiFunc = reinterpret_cast<OpApiFunc>(phrase2);
        auto api_ret = opApiFunc(workspace_addr, workspace_size, executor, acl_stream);
        TORCH_CHECK(api_ret == 0, "call failed, detail:", aclGetRecentErrMsg(), OPS_ERROR(ErrCode::INTERNAL));
        return api_ret;
    };
    at_npu::native::OpCommand cmd;
    cmd.Name(aclnn_api);
    cmd.SetCustomHandler(acl_call);
    cmd.Run();
    UnInitCacheThreadLocal();
    return true;
}

本帖最后由 匿名用户2024/05/21 20:47:12 编辑

我要发帖子