函数:cast
函数:execute_v2
| C函数原型 | aclError aclopExecuteV2(const char *opType, int numInputs, aclTensorDesc *inputDesc[], aclDataBuffer *inputs[], int numOutputs, aclTensorDesc *outputDesc[], aclDataBuffer *outputs[], aclopAttr *attr, aclrtStream stream); |
|---|---|
| Python函数 | ret = acl.op.execute_v2(op_type, input_desc, inputs, output_desc, outputs, attr, stream) |
| 函数功能 | 同步或者异步执行指定的算子。 |
| 输入说明 | op_type:str,指定算子类型名称。 input_desc:list,表示算子输入tensor的描述, 整形列表,包含多个acl的tensor描述地址对象。 inputs:list,表示算子输入tensor,整形列表,包含多个aclDataBuffer数据地址对象。 output_desc:list,表示算子输出tensor的描述,整形列表,包含多个acl的tensor描述地址对象。 outputs:list,表示算子输出tensor,整形列表,包含多个aclDataBuffer数据地址对象。 attr:int,算子的属性地址对象。 stream:int,该算子需要加载的Stream对象。 |
| 返回值说明 | ret:int,错误码。 - 返回0表示成功。 - 返回其它值表示失败。 |
| 约束说明 | 多线程场景下,不支持调用本接口时指定同一个Stream或使用默认Stream,否则可能任务执行异常。 每个算子的输入、输出组织不同,需要应用在调用时严格按照算子输入、输出参数来组织算子。用户在调用acl.op.execute_v2接口时,pyACL根据optype、输入tensor的描述、输出tensor的描述、attr等信息查找对应的任务,并下发执行。 对于支持动态Shape的算子,无法明确算子输出Shape时,可调用acl.op.infer_shape接口获取算子的输出Shape: - 若可获取到准确的输出Shape,则使用准确的输出Shape来构造outputDesc参数,作为acl.op.execute_v2的输入。该场景下,acl.op.execute_v2接口是异步接口。 - 若无法获取准确的输出Shape,仅能获取输出Shape范围,则使用Shape最大值来构造outputDesc参数,作为acl.op.execute_v2的输入。该场景下,在调用acl.op.execute_v2接口执行算子后,系统内部会计算出准确的输出Shape,通过acl.op.execute_v2接口的outputDesc参数输出,此时acl.op.execute_v2接口是同步接口。 - (该场景预留)若无法获取准确的输出Shape以及Shape范围,则需由用户预估一个最大的Shape来构造outputDesc参数,作为acl.op.execute_v2的输入。该场景下,在调用acl.op.execute_v2接口执行算子后,系统内部会计算出准确的输出Shape,通过acl.op.execute_v2接口的outputDesc参数输出,此时acl.op.execute_v2接口是同步接口。 执行有可选输入的算子时,如果可选输入不使用: - 则需按此种方式创建aclTensorDesc类型的数据:acl.create_tensor_desc(ACL_DT_UNDEFINED, 0, [], ACL_FORMAT_UNDEFINED),表示数据类型设置为ACL_DT_UNDEFINED,Format设置为ACL_FORMAT_UNDEFINED,Shape信息为[]。 - 则需按此种方式创建aclDataBuffer类型的数据:acl.create_data_buffer([], 0),同时aclDataBuffer中的数据不需要释放。 执行有constant输入的算子时,需要先调用acl.set_tensor_const接口设置constant输入。 调用acl.op.execute_v2接口时,设置的constant输入数据必须保持一致。 对于算子有constant输入的场景,如果未调用acl.set_tensor_const接口设置constant输入,则需调用acl.set_tensor_place_ment设置TensorDesc的placement属性,将memType设置为Host内存。 在执行单算子时,一般要求用户传入的输入/输出tensor数据是存放在Device内存的,比如两个tensor相加的场景。但是,也存在部分算子,除了将featureMap、weight等Device内存中的tensor数据作为输入外,还需tensor shape信息、learningRate、tensor的dims信息等通常在Host内存中的tensor数据也作为输入,此时,用户不需要额外将这些Host内存中的tensor数据拷贝到Device上作为输入,只需要调用acl.set_tensor_place_ment接口设置对应TensorDesc的placement属性为Host内存,在执行单算子时,设置了placement属性为Host内存的TensorDesc,其对应的tensor数据必须存放在Host内存中,AscendCL内部会自动将Host上的tensor数据拷贝到Device上。 |
| 参考资源 | 接口调用流程、示例,参见单算子调用。 |
父主题: 算子加载与执行(op)
函数:execute_with_handle
| C函数原型 | aclError **aclopExecWithHandle(**aclopHandle *handle, int numInputs, const aclDataBuffer *const inputs[], int numOutputs, aclDataBuffer *const outputs[], aclrtStream stream); |
|---|---|
| Python函数 | **ret = acl.op.execute_with_handle(****handle,**inputs,outputs,stream) |
| 函数功能 | 以Handle方式调用一个算子,不支持动态Shape算子,动态Shape算子请使用acl.op.execute_v2。异步接口。 |
| 输入说明 | handle:int,指定执行算子的handle,指针地址。 需提前调用acl.op.create_handle接口创建aclopHandle类型的数据。 inputs:list,算子输入tensor, 整形列表,包含多个aclDataBuffer数据地址对象。 需提前调用acl.create_data_buffer接口创建aclDataBuffer类型的数据。 outputs:list,算子输出tensor,整形列表,包含多个aclDataBuffer数据地址对象。 需提前调用acl.create_data_buffer接口创建aclDataBuffer类型的数据。 stream:int,执行算子所在的Stream,stream地址对象。 |
| 返回值说明 | ret:int,错误码。 - 返回0表示成功。 - 返回其它值表示失败。 |
| 约束说明 | - 多线程场景下,不支持调用本接口时指定同一个Stream或使用默认Stream,否则可能任务执行异常。 - 执行有可选输入的算子时,如果可选输入不使用,则需按此种方式创建aclDataBuffer类型的数据:acl.create_data_buffer(nullptr, 0),同时aclDataBuffer中的数据不需要释放,因为是空的数据类型。 |
父主题: 算子加载与执行(op)
函数:cast
| C函数原型 | aclError aclopCast(const aclTensorDesc *srcDesc, const aclDataBuffer *srcBuffer, const aclTensorDesc *dstDesc, aclDataBuffer *dstBuffer, uint8_t truncate, aclrtStream stream) |
|---|---|
| Python函数 | ret = acl.op.cast(src_desc, src_buffer, dst_desc, dst_buffer, truncate, stream) |
| 函数功能 | 转换输入数据的数据类型,异步接口。 |
| 输入说明 | src_desc: int,输入tensor描述指针地址。 src_buffer:int,输入tensor,调用acl.create_data_buffer创建的aclDataBuffer指针地址。 dst_desc:int, 输出tensor的描述指针地址。 dst_buffer:int,输出tensor,调用acl.create_data_buffer创建的aclDataBuffer指针地址。 truncate:int,预留。 stream:int,执行算子所在的Stream,Stream地址对象。 |
| 返回值说明 | ret:int,错误码。 - 返回0表示成功。 - 返回其它值表示失败。 |
| 约束说明 | 无 |
父主题: 算子加载与执行(op)
函数:create_handle_for_cast
| C函数原型 | aclError aclopCreateHandleForCast(const aclTensorDesc *srcDesc, const aclTensorDesc *dstDesc, uint8_t truncate, aclopHandle **handle) |
|---|---|
| Python函数 | handle, ret = acl.op.create_handle_for_cast(src_desc, dst_desc, truncate) |
| 函数功能 | 创建数据类型转换的handle,同步接口。 创建handle成功后,需调用acl.op.execute_with_handle接口执行算子。 |
| 输入说明 | src_desc:int,输入tensor描述指针地址。 dst_desc:int,输出tensor描述指针地址。 truncate:预留。 |
| 返回值说明 | handle:int,输出的handle的指针地址。 ret:int,错误码。 - 返回0表示成功。 - 返回其它值表示失败 |
| 约束说明 | 无 |
父主题: 算子加载与执行(op)
函数:create_attr
| C函数原型 | aclopAttr *aclopCreateAttr() |
|---|---|
| Python函数 | attr = acl.op.create_attr() |
| 函数功能 | 创建acl算子属性类型的数据,同步接口。 |
| 输入说明 | 无 |
| 返回值说明 | attr:int,acl算子属性地址对象。 |
| 约束说明 | 无 |
父主题: 算子加载与执行(op)
函数:destroy_attr
| C函数原型 | void aclopDestroyAttr(const aclopAttr *attr) |
|---|---|
| Python函数 | acl.op.destroy_attr(attr) |
| 函数功能 | 销毁acl算子属性类型的数据,同步接口。 |
| 输入说明 | attr:int,acl算子属性地址对象, 此对象使用调用acl.op.create_attr创建。 |
| 返回值说明 | 无 |
| 约束说明 | 销毁类接口:调用该接口后,不能继续使用已释放或销毁的资源,建议调用该接口后,将相关资源设置为无效值(例如,置为0或None)。 |
父主题: 算子加载与执行(op)
在线提单