Build

Function Usage

Constructs KernelContext based on the previous settings and returns a ContextHolder<KernelContext> object.

Prototype

1
ContextHolder<KernelContext> Build()

Parameters

None

Returns

A ContextHolder<KernelContext> object. You can obtain the KernelContext pointer by calling the GetContext() method.

Constraints

  • The caller has the memory ownership of all parameters passed in through pointers. They must ensure that these pointers are valid throughout the lifecycle of the ContextHolder object.
  • When the ContextHolder is destructed, the internal context resources are automatically released. Do not manually release the pointer returned by GetContext().

Examples

 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);