根据之前的设置,构建KernelContext,返回一个ContextHolder<KernelContext>对象。
1 | ContextHolder<KernelContext> Build() |
无
返回一个ContextHolder<KernelContext>对象。通过调用GetContext()方法可获取KernelContext指针。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include "base/context_builder/op_kernel_run_context_builder.h" OpKernelContextBuilder ctx_builder; gert::StorageShape shape0 = {{10, 20}, {10, 20}}; auto holder = ctx_builder.OpType("Add") .OpName("add_1") .IONum(2, 1) .InputTensorDesc(0, ge::DT_FLOAT16, ge::FORMAT_ND, ge::FORMAT_FRACTAL_NZ) .InputTensorDesc(1, ge::DT_FLOAT16, ge::FORMAT_ND, ge::FORMAT_FRACTAL_NZ) .OutputTensorDesc(0, ge::DT_FLOAT16, ge::FORMAT_ND, ge::FORMAT_FRACTAL_NZ) .Inputs({&shape0, &shape0}) .Outputs({&shape0}) .Build(); auto ctx = reinterpret_cast<KernelContext *>(holder.GetContext()); EXPECT_NE(ctx, nullptr); auto ctx_compute_node_info = static_cast<const gert::ComputeNodeInfo *>(ctx->GetComputeNodeExtend()); EXPECT_NE(ctx_compute_node_info, nullptr); EXPECT_EQ(std::string(ctx_compute_node_info->GetNodeType()), std::string("Add")); EXPECT_EQ(std::string(ctx_compute_node_info->GetNodeName()), std::string("add_1")); EXPECT_EQ(ctx_compute_node_info->GetIrInputsNum(), 2); EXPECT_EQ(ctx_compute_node_info->GetIrOutputsNum(), 1); EXPECT_EQ(ctx_compute_node_info->GetInputsNum(), 2); EXPECT_EQ(ctx_compute_node_info->GetOutputsNum(), 1); const CompileTimeTensorDesc *info_input_0 = ctx_compute_node_info->GetInputTdInfo(0); EXPECT_NE(info_input_0, nullptr); EXPECT_EQ(info_input_0->GetStorageFormat(), ge::FORMAT_FRACTAL_NZ); EXPECT_EQ(info_input_0->GetOriginFormat(), ge::FORMAT_ND); EXPECT_EQ(ctx->GetInput(0)->GetPointer<gert::StorageShape>(), &shape0); EXPECT_EQ(ctx->GetInput(1)->GetPointer<gert::StorageShape>(), &shape0); EXPECT_EQ(ctx->GetOutput(0)->GetPointer<gert::StorageShape>(), &shape0); |