---
title: GetOptionalInputTensor
description: "根据算子原型定义中的输入索引获取对应的可选输入tensor指针。"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/maintenref/basicdataapi/atlasopapi_07_00226.html
sourcePath: /source/zh/canncommercial/900/maintenref/basicdataapi/atlasopapi_07_00226.html
indexId: 3bf76d26cb1c88976650d41cdb0ab8cd6d0000fcdc991560ea163077d864644578
---
# GetOptionalInputTensor

#### 函数功能

根据算子原型定义中的输入索引获取对应的可选输入tensor指针。


#### 函数原型

const Tensor *GetOptionalInputTensor(const size_t ir_index) const


#### 参数说明

| 参数 | 输入/输出 | 说明 |
| --- | --- | --- |
| ir\_index | 输入 | 可选输入在算子IR原型定义中的索引，从0开始计数。 |


#### 返回值说明

指定ir_index的输入tensor指针，当输入ir_index非法或该INPUT没有实例化时，返回空指针。

关于Tensor类型的定义，请参见Tensor。


#### 约束说明

仅在设置数据依赖(https://www.hiascend.comdocument/detail/zh/canncommercial/900/programug/Ascendcopdevg/atlas_ascendc_10_0078.html#ZH-CN_TOPIC_0000002531362232__section0610144611487)时可以获取tensor的数据地址。如果输入没有被设置为数据依赖，调用此接口获取tensor时，只能在tensor中获取到正确的shape、format、datatype信息，无法获取到真实的tensor数据地址（获取到的地址为nullptr）。


#### 调用示例

```
ge::graphStatus Tiling4ReduceCommon(TilingContext* context) {
auto in_shape = context->GetInputShape(0);
GE_ASSERT_NOTNULL(in_shape);
auto axes_tensor = context->GetOptionalInputTensor(1);
}
```
