已合并
fix:BorrowOpCompileContext 入参类型变更 #4646
lfz2812创建于 8 天前
fix:BorrowOpCompileContext 入参类型变更 #4646
已合并
lfz2812创建于 8 天前
2 个文件变更+5-8
@@ -361,13 +361,6 @@ class BorrowedOpCompileContext {
361 std::shared_ptr<bool> active_;361 std::shared_ptr<bool> active_;
362};362};
363 363 
364-BorrowedOpCompileContext BorrowOpCompileContext(uintptr_t ctx_handle) {
365- if (ctx_handle == 0U) {
366- throw std::invalid_argument("ctx_handle is null");
367- }
368- return BorrowedOpCompileContext(reinterpret_cast<gert::OpCompileContext *>(ctx_handle));
369-}
370- 
371template <typename Context>364template <typename Context>
372Context *GetContextFromCapsule(const py::capsule &ctx_handle, const char *expected_name) {365Context *GetContextFromCapsule(const py::capsule &ctx_handle, const char *expected_name) {
373 if ((ctx_handle.get_pointer() == nullptr) || (ctx_handle.name() == nullptr) ||366 if ((ctx_handle.get_pointer() == nullptr) || (ctx_handle.name() == nullptr) ||
@@ -377,6 +370,10 @@ Context *GetContextFromCapsule(const py::capsule &ctx_handle, const char *expect
377 return static_cast<Context *>(ctx_handle.get_pointer());370 return static_cast<Context *>(ctx_handle.get_pointer());
378}371}
379 372 
373+BorrowedOpCompileContext BorrowOpCompileContext(const py::capsule &ctx_handle) {
374+ return BorrowedOpCompileContext(GetContextFromCapsule<gert::OpCompileContext>(ctx_handle, "gert::OpCompileContext"));
375+}
376+ 
380BorrowedEagerOpExecutionContext BorrowEagerOpExecutionContext(const py::capsule &ctx_handle) {377BorrowedEagerOpExecutionContext BorrowEagerOpExecutionContext(const py::capsule &ctx_handle) {
381 return BorrowedEagerOpExecutionContext(378 return BorrowedEagerOpExecutionContext(
382 GetContextFromCapsule<gert::EagerOpExecutionContext>(ctx_handle, "gert::EagerOpExecutionContext"));379 GetContextFromCapsule<gert::EagerOpExecutionContext>(ctx_handle, "gert::EagerOpExecutionContext"));
@@ -552,7 +552,7 @@ class PythonCustomOpPybindBridge {
552 // object that needs cleanup.552 // object that needs cleanup.
553 py::object python_ir_meta = BuildPythonIrMeta(holder->ir_meta.get());553 py::object python_ir_meta = BuildPythonIrMeta(holder->ir_meta.get());
554 py::module_ native_module = py::module_::import(kCustomOpNativeModuleName);554 py::module_ native_module = py::module_::import(kCustomOpNativeModuleName);
555- compile_ctx = native_module.attr("_borrow_op_compile_context")(py::int_(reinterpret_cast<uintptr_t>(ctx)));555+ compile_ctx = native_module.attr("_borrow_op_compile_context")(py::capsule(ctx, "gert::OpCompileContext"));
556 py::object result =556 py::object result =
557 bridge_module_.attr("call_compile")(holder->instance_id, std::move(python_ir_meta), compile_ctx);557 bridge_module_.attr("call_compile")(holder->instance_id, std::move(python_ir_meta), compile_ctx);
558 return TranslateStatusLike(result);558 return TranslateStatusLike(result);