Merged
feat(fallback 2stages) #740
esok11Add on Aug 14
feat(fallback 2stages) #740
Merged
esok11Add on Aug 14
Total: 3 files changed+181-58
@@ -20,66 +20,106 @@
20 20 
21namespace fallback {21namespace fallback {
22 22 
23-#define CANN_OPS_OPB_SYN_EXEC_ACLNN(ctx, aclnnApi, ...) \23+#define CANN_OPS_OPB_SYN_EXEC_ACLNN(ctx, aclnnApi, ...) \
24- ({ \24+ ({ \
25- static auto ret = GRAPH_SUCCESS; \25+ static auto ret = GRAPH_SUCCESS; \
26- do { \26+ do { \
27- static const auto ResetCacheThreadLocalAddr = GetOpApiFuncAddr("ResetCacheThreadLocal"); \27+ static const auto ResetCacheThreadLocalAddr = GetOpApiFuncAddr("ResetCacheThreadLocal"); \
28- static const auto getWorkspaceSizeFuncAddr = GetOpApiFuncAddr(#aclnnApi "GetWorkspaceSize"); \28+ static const auto getWorkspaceSizeFuncAddr = GetOpApiFuncAddr(#aclnnApi "GetWorkspaceSize"); \
29- static const auto opApiFuncAddr = GetOpApiFuncAddr(#aclnnApi); \29+ static const auto opApiFuncAddr = GetOpApiFuncAddr(#aclnnApi); \
30- if (getWorkspaceSizeFuncAddr == nullptr || opApiFuncAddr == nullptr || \30+ if (getWorkspaceSizeFuncAddr == nullptr || opApiFuncAddr == nullptr || \
31- ResetCacheThreadLocalAddr == nullptr) { \31+ ResetCacheThreadLocalAddr == nullptr) { \
32- OP_LOGE("aclnnfallback", "%s or %s not in %s or %s or ResetCacheThreadLocal not found.", \32+ OP_LOGE("aclnnfallback", "%s or %s not in %s or %s or ResetCacheThreadLocal not found.", \
33- #aclnnApi "GetWorkspaceSize", #aclnnApi, GetOpApiLibName(), GetOpApiLibName()); \33+ #aclnnApi "GetWorkspaceSize", #aclnnApi, GetOpApiLibName(), GetOpApiLibName()); \
34- ret = GRAPH_FAILED; \34+ ret = GRAPH_FAILED; \
35- break; \35+ break; \
36- } \36+ } \
37- auto ResetCacheThreadLocalFunc = reinterpret_cast<ResetCacheThreadLocal>(ResetCacheThreadLocalAddr); \37+ auto ResetCacheThreadLocalFunc = reinterpret_cast<ResetCacheThreadLocal>(ResetCacheThreadLocalAddr); \
38- ResetCacheThreadLocalFunc(); \38+ ResetCacheThreadLocalFunc(); \
39- uint64_t workspace_size = 0; \39+ uint64_t workspace_size = 0; \
40- uint64_t* workspace_size_addr = &workspace_size; \40+ uint64_t* workspace_size_addr = &workspace_size; \
41- aclOpExecutor* executor = nullptr; \41+ aclOpExecutor* executor = nullptr; \
42- aclOpExecutor** executor_addr = &executor; \42+ aclOpExecutor** executor_addr = &executor; \
43- auto converted_params = ConvertTypes(__VA_ARGS__, workspace_size_addr, executor_addr); \43+ auto converted_params = ConvertTypes(__VA_ARGS__, workspace_size_addr, executor_addr); \
44- static auto getWorkspaceSizeFunc = ConvertToOpApiFunc(converted_params, getWorkspaceSizeFuncAddr); \44+ static auto getWorkspaceSizeFunc = ConvertToOpApiFunc(converted_params, getWorkspaceSizeFuncAddr); \
45- auto workspace_status = call(getWorkspaceSizeFunc, converted_params); \45+ auto workspace_status = call(getWorkspaceSizeFunc, converted_params); \
46- if (workspace_status != 0) { \46+ if (workspace_status != 0) { \
47- OP_LOGE("aclnnfallback", "call %s failed:", #aclnnApi); \47+ OP_LOGE("aclnnfallback", "call %s failed:", #aclnnApi); \
48- ret = GRAPH_FAILED; \48+ ret = GRAPH_FAILED; \
49- break; \49+ break; \
50- } \50+ } \
51- void* workspace_addr = nullptr; \51+ void* workspace_addr = nullptr; \
52- if (workspace_size > 0) { \52+ if (workspace_size > 0) { \
53- workspace_addr = ctx->MallocWorkspace(workspace_size); \53+ workspace_addr = ctx->MallocWorkspace(workspace_size); \
54- if (workspace_addr == nullptr) { \54+ if (workspace_addr == nullptr) { \
55- OP_LOGE("aclnnfallback", "call %s allocate workspace failed", #aclnnApi); \55+ OP_LOGE("aclnnfallback", "call %s allocate workspace failed", #aclnnApi); \
56- ret = GRAPH_FAILED; \56+ ret = GRAPH_FAILED; \
57- break; \57+ break; \
58- } \58+ } \
59- } \59+ } \
60- auto acl_stream = ctx->GetStream(); \60+ auto acl_stream = ctx->GetStream(); \
61- auto acl_call = [converted_params, workspace_addr, workspace_size, ctx, acl_stream, \61+ auto acl_call = [converted_params, workspace_addr, workspace_size, ctx, acl_stream, executor]() -> int { \
62- executor]() -> int { \62+ using OpApiFunc = int (*)(void*, uint64_t, aclOpExecutor*, const aclrtStream); \
63- using OpApiFunc = int (*)(void*, uint64_t, aclOpExecutor*, const aclrtStream); \63+ OpApiFunc opApiFunc = reinterpret_cast<OpApiFunc>(opApiFuncAddr); \
64- OpApiFunc opApiFunc = reinterpret_cast<OpApiFunc>(opApiFuncAddr); \64+ auto op_api_ret = opApiFunc(workspace_addr, workspace_size, executor, acl_stream); \
65- auto op_api_ret = opApiFunc(workspace_addr, workspace_size, executor, acl_stream); \65+ ReleaseConvertTypes(converted_params); \
66- ReleaseConvertTypes(converted_params); \66+ ctx->FreeWorkspace(); \
67- ctx->FreeWorkspace(); \67+ if (op_api_ret != 0) { \
68- if (op_api_ret != 0) { \68+ OP_LOGE("aclnnfallback", "call %s allocate workspace failed op_api_ret: %d", #aclnnApi, \
69- OP_LOGE("aclnnfallback", "call %s allocate workspace failed op_api_ret: %d", #aclnnApi, \69+ op_api_ret); \
70- op_api_ret); \70+ return GRAPH_FAILED; \
71- return GRAPH_FAILED; \71+ } \
72- } \72+ return op_api_ret; \
73- return op_api_ret; \73+ }; \
74- }; \74+ \
75- \75+ ret = acl_call(); \
76- ret = acl_call(); \76+ } while (false); \
77- } while (false); \77+ (ret); \
78- (ret); \
79 })78 })
80 79 
81-#define EXEC_OPAPI_CMD(aclnn_api, ...) \80+#define EXEC_OPAPI_CMD(aclnn_api, ...) CANN_OPS_OPB_SYN_EXEC_ACLNN(host_api_ctx, aclnn_api, ##__VA_ARGS__)
82- CANN_OPS_OPB_SYN_EXEC_ACLNN(host_api_ctx, aclnn_api, ##__VA_ARGS__)81+ 
82+#define CANN_OPS_OPB_ASYN_EXEC_ACLNN(ctx, aclnn_api, ...) \
83+ ({ \
84+ static auto ret = GRAPH_SUCCESS; \
85+ do { \
86+ static const auto ResetCacheThreadLocalAddr = GetOpApiFuncAddr("ResetCacheThreadLocal"); \
87+ static const auto opApiFuncAddr = GetOpApiFuncAddr(#aclnn_api); \
88+ static const auto getWorkspaceSizeFuncAddr = GetOpApiFuncAddr(#aclnn_api "GetWorkspaceSize"); \
89+ if (getWorkspaceSizeFuncAddr == nullptr || opApiFuncAddr == nullptr || \
90+ ResetCacheThreadLocalAddr == nullptr) { \
91+ OP_LOGE("aclnnfallback", "%s or %s not in %s or %s or ResetCacheThreadLocal not found.", \
92+ #aclnn_api "GetWorkspaceSize", #aclnn_api, GetOpApiLibName(), GetOpApiLibName()); \
93+ ret = GRAPH_FAILED; \
94+ break; \
95+ } \
96+ auto* op_api_params = new (std::nothrow) OpApiParams(); \
97+ auto ResetCacheThreadLocalFunc = reinterpret_cast<ResetCacheThreadLocal>(ResetCacheThreadLocalAddr); \
98+ ResetCacheThreadLocalFunc(); \
99+ op_api_params->op_api_func = reinterpret_cast<OpApiFunc2Stage>(opApiFuncAddr); \
100+ uint64_t workspace_size = 0; \
101+ uint64_t* workspace_size_addr = &workspace_size; \
102+ aclOpExecutor** executor_addr = &op_api_params->executor; \
103+ auto converted_params = ConvertTypes(__VA_ARGS__, workspace_size_addr, executor_addr); \
104+ using TupleT = decltype(converted_params); \
105+ constexpr size_t tuple_size = std::tuple_size<TupleT>::value; \
106+ op_api_params->converted_params.reserve(tuple_size); \
107+ CollectConvertedTypes(converted_params, op_api_params->converted_params); \
108+ ctx->SetOpApiParamsWithDefaultDeleter<OpApiParams>(op_api_params); \
109+ static auto getWorkspaceSizeFunc = ConvertToOpApiFunc(converted_params, getWorkspaceSizeFuncAddr); \
110+ auto workspace_status = call(getWorkspaceSizeFunc, converted_params); \
111+ if (workspace_status != 0) { \
112+ OP_LOGE("aclnnfallback", "call %s failed:", #aclnn_api); \
113+ ret = GRAPH_FAILED; \
114+ break; \
115+ } \
116+ auto set_workspace_status = ctx->SetWorkspaceSizes({workspace_size}); \
117+ if (set_workspace_status != GRAPH_SUCCESS) { \
118+ ret = GRAPH_FAILED; \
119+ } \
120+ } while (false); \
121+ (ret); \
122+ })
83 123 
84} // namespace fallback124} // namespace fallback
85 125 
@@ -19,6 +19,8 @@
19#include <vector>19#include <vector>
20 20 
21#include "aclnn/aclnn_base.h"21#include "aclnn/aclnn_base.h"
22+#include "exe_graph/runtime/op_execute_prepare_context.h"
23+#include "exe_graph/runtime/op_execute_launch_context.h"
22#include "exe_graph/runtime/tensor.h"24#include "exe_graph/runtime/tensor.h"
23#include "op_common/log/log.h"25#include "op_common/log/log.h"
24 26 
@@ -36,10 +38,16 @@ using _aclCreateTensor = aclTensor* (*)(const int64_t* view_dims, uint64_t view_
36 const int64_t* storage_dims, uint64_t storage_dims_num, void* tensor_data);38 const int64_t* storage_dims, uint64_t storage_dims_num, void* tensor_data);
37 39 
38using _aclCreateScalar = aclScalar* (*)(void* value, aclDataType data_type);40using _aclCreateScalar = aclScalar* (*)(void* value, aclDataType data_type);
41+using _aclCreateIntArray = aclIntArray* (*)(const int64_t* value, uint64_t size);
42+using _aclCreateFloatArray = aclFloatArray* (*)(const float* value, uint64_t size);
43+using _aclCreateBoolArray = aclBoolArray* (*)(const bool* value, uint64_t size);
39using _aclCreateTensorList = aclTensorList* (*)(const aclTensor* const* value, uint64_t size);44using _aclCreateTensorList = aclTensorList* (*)(const aclTensor* const* value, uint64_t size);
40 45 
41using _aclDestroyTensor = int (*)(const aclTensor* tensor);46using _aclDestroyTensor = int (*)(const aclTensor* tensor);
42using _aclDestroyScalar = int (*)(const aclScalar* scalar);47using _aclDestroyScalar = int (*)(const aclScalar* scalar);
48+using _aclDestroyIntArray = int (*)(const aclIntArray* array);
49+using _aclDestroyFloatArray = int (*)(const aclFloatArray* array);
50+using _aclDestroyBoolArray = int (*)(const aclBoolArray* array);
43using _aclDestroyTensorList = int (*)(const aclTensorList* array);51using _aclDestroyTensorList = int (*)(const aclTensorList* array);
44 52 
45using ResetCacheThreadLocal = void (*)();53using ResetCacheThreadLocal = void (*)();
@@ -342,6 +350,77 @@ auto ConvertToOpApiFunc(const Tuple& params, void* opApiAddr) -> typename std::e
342 return ConvertToOpApiFunc(params, opApiAddr, std_utils::make_index_sequence<size>{});350 return ConvertToOpApiFunc(params, opApiAddr, std_utils::make_index_sequence<size>{});
343}351}
344 352 
353+// ---- 2stage fallback 内部实现 ----
354+ 
355+using OpApiAnyValueDeleter = void (*)(void*);
356+typedef struct {
357+ void* pointer;
358+ OpApiAnyValueDeleter deleter;
359+} OpApiAnyValue;
360+ 
361+using OpApiFunc2Stage = int (*)(void*, uint64_t, aclOpExecutor*, const aclrtStream);
362+struct OpApiParams {
363+ std::vector<OpApiAnyValue> converted_params;
364+ aclOpExecutor* executor = nullptr;
365+ OpApiFunc2Stage op_api_func = nullptr;
366+};
367+ 
368+inline void Collect(aclTensor* p, std::vector<OpApiAnyValue>& params)
369+{
370+ static auto aclDestroyTensor = GET_OP_API_FUNC(aclDestroyTensor);
371+ OP_CHECK_IF(aclDestroyTensor == nullptr, OP_LOGE("aclnnfallback", "aclDestroyTensor is null"), return);
372+ params.emplace_back(OpApiAnyValue{p, [](void* param) { aclDestroyTensor(static_cast<aclTensor*>(param)); }});
373+}
374+ 
375+inline void Collect(aclScalar* p, std::vector<OpApiAnyValue>& params)
376+{
377+ static auto aclDestroyScalar = GET_OP_API_FUNC(aclDestroyScalar);
378+ OP_CHECK_IF(aclDestroyScalar == nullptr, OP_LOGE("aclnnfallback", "aclDestroyScalar is null"), return);
379+ params.emplace_back(OpApiAnyValue{p, [](void* param) { aclDestroyScalar(static_cast<aclScalar*>(param)); }});
380+}
381+ 
382+inline void Collect(aclIntArray* p, std::vector<OpApiAnyValue>& params)
383+{
384+ static auto aclDestroyIntArray = GET_OP_API_FUNC(aclDestroyIntArray);
385+ OP_CHECK_IF(aclDestroyIntArray == nullptr, OP_LOGE("aclnnfallback", "aclDestroyIntArray is null"), return);
386+ params.emplace_back(OpApiAnyValue{p, [](void* param) { aclDestroyIntArray(static_cast<aclIntArray*>(param)); }});
387+}
388+ 
389+inline void Collect(aclBoolArray* p, std::vector<OpApiAnyValue>& params)
390+{
391+ static auto aclDestroyBoolArray = GET_OP_API_FUNC(aclDestroyBoolArray);
392+ OP_CHECK_IF(aclDestroyBoolArray == nullptr, OP_LOGE("aclnnfallback", "aclDestroyBoolArray is null"), return);
393+ params.emplace_back(OpApiAnyValue{p, [](void* param) { aclDestroyBoolArray(static_cast<aclBoolArray*>(param)); }});
394+}
395+ 
396+inline void Collect(aclTensorList* p, std::vector<OpApiAnyValue>& params)
397+{
398+ static auto aclDestroyTensorList = GET_OP_API_FUNC(aclDestroyTensorList);
399+ OP_CHECK_IF(aclDestroyTensorList == nullptr, OP_LOGE("aclnnfallback", "aclDestroyTensorList is null"), return);
400+ params.emplace_back(
401+ OpApiAnyValue{p, [](void* param) { aclDestroyTensorList(static_cast<aclTensorList*>(param)); }});
402+}
403+ 
404+template <typename T>
405+void Collect(T value, std::vector<OpApiAnyValue>& params)
406+{
407+ (void)value;
408+ params.emplace_back(OpApiAnyValue{nullptr, nullptr});
409+}
atomgit-bot

🟡 Medium Priority

变更行:op_fallback_internal.h 新增了 _aclCreateFloatArray/_aclDestroyFloatArray 别名(第 42、49 行),并新增了 Collect 重载集(第 368-402 行)覆盖 aclTensor*/aclScalar*/aclIntArray*/aclBoolArray*/aclTensorList*,但遗漏了 aclFloatArray* 的重载。

受影响的行为:当算子参数中包含 float 数组(对应 op 框架的 OPARG_FLOAT_LIST/aclFloatArray*,经 ConvertType 恒等模板原样保留为 aclFloatArray*)时,重载决议会落入通用模板 Collect(T value, ...)(第 404-409 行)。

失败模式:通用模板把该参数丢弃并写入 OpApiAnyValue{nullptr, nullptr},既不保存指针也不绑定 aclDestroyFloatArray 析构器。两阶段分离后,该 aclFloatArray 对象失去所有权、永远不被销毁 → 内存泄漏。这与 PR 描述中“补齐 aclIntArray/aclFloatArray/aclBoolArray 类型支持”的意图相矛盾(float 数组类型别名已加,但析构收集缺失)。

likedislike
Incorrect?
esok11
esok11
Aug 17 Comments:
likedislike
410+ 
411+template <typename Tuple, size_t... I>
412+void CallCollect(Tuple t, std_utils::index_sequence<I...>, std::vector<OpApiAnyValue>& params)
413+{
414+ (void)std::initializer_list<int>{(Collect(std::get<I>(t), params), 0)...};
415+}
416+ 
417+template <typename Tuple>
418+void CollectConvertedTypes(Tuple& t, std::vector<OpApiAnyValue>& params)
419+{
420+ static constexpr auto size = std::tuple_size<Tuple>::value;
421+ CallCollect(t, std_utils::make_index_sequence<size>{}, params);
422+}
423+ 
345} // namespace fallback424} // namespace fallback
346 425 
347#endif // INC_OP_GRAPH_OP_FALLBACK_INTERNAL_H_426#endif // INC_OP_GRAPH_OP_FALLBACK_INTERNAL_H_
@@ -27,6 +27,10 @@
27#include "lib_path.h"27#include "lib_path.h"
28#include "nlohmann/json.hpp"28#include "nlohmann/json.hpp"
29 29 
30+namespace Adx {
31+int32_t AdumpSaveToFile(const char* data, size_t dataLen, const char* filename, SaveType type) { return 0; }
32+} // namespace Adx
33+ 
30class OpInfoRecordUtest : public testing::Test {34class OpInfoRecordUtest : public testing::Test {
31protected:35protected:
32 virtual void SetUp()36 virtual void SetUp()