---
title: 简介
description: "OpInferDataTypeContextBuilder用于构建InferDataTypeContext 。构造出的Context在算子数据类型推导过程中作为入参，用于获取必要的算子输入输出数据。推导完成后，结果会被写入回上下文中。"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/maintenref/basicdataapi/atlasopapi_07_00607.html
sourcePath: /source/zh/canncommercial/900/maintenref/basicdataapi/atlasopapi_07_00607.html
indexId: f4e9165e5990a8efbe46d034df426425e44c4762625cad298226fe5e38ac7e6a78
---
# 简介

OpInferDataTypeContextBuilder用于构建InferDataTypeContext 。构造出的Context在算子数据类型推导过程中作为入参，用于获取必要的算子输入输出数据。推导完成后，结果会被写入回上下文中。

OpInferDataTypeContextBuilder继承关系图如下：


使用步骤如下：

1. 构造ContextHolder。
  调用OpInferDataTypeContextBuilder类的接口，传入输入的TensorDesc描述等信息，调用Build()接口构造一个ContextHolder<InferDataTypeContext>对象。

2. 获取InferDataTypeContext。
  通过ContextHolder的GetContext接口，获取InferDataTypeContext。

3. 调用算子数据类型推导函数InferDataTypeKernelFunc，将InferDataTypeContext作为函数入参，完成数据类型推导，并写入推导的输出结果。
4. 通过InferDataTypeContext的GetOutputDataType接口可以获取到推导的输出数据类型。
5. 根据需要释放ContextHolder，释放完成后，此时由Build构造出来的InferDataTypeContext中的数据指针均无效。

该类继承自OpContextBuilderBase类，在Build构建ContextHolder对象之前，需要调用OpContextBuilderBase的OpType、OpName、IONum或IOInstanceNum，以及AppendAttr接口，分别设置算子的类型、名称、输入输出个数、以及算子的属性。


#### 需要包含的头文件

```
#include "base/context_builder/op_infer_datatype_context_builder.h"
```


#### Public成员函数

```
OpInferDataTypeContextBuilder()
~
OpInferDataTypeContextBuilder() override
OpInferDataTypeContextBuilder &InputTensorDesc(size_t index, ge::DataType dtype, ge::Format origin_format,
ge::Format storage_format,
const gert::ExpandDimsType &expand_dims_type = {})
OpInferDataTypeContextBuilder &OutputTensorDesc(size_t index, ge::Format origin_format, ge::Format storage_format,
const gert::ExpandDimsType &expand_dims_type = {})
ContextHolder<InferDataTypeContext> Build()
```
