---
title: 函数：execute
description: "执行模型推理，直到返回推理结果。"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/ascendgraphapi/aclpythondevg_01_0145.html
sourcePath: /source/zh/canncommercial/900/API/ascendgraphapi/aclpythondevg_01_0145.html
indexId: 06071c5d629d812a1a219fe9dc26ff9ffc75445d459266b0674e4264fca2d48775
---
# 函数：execute

#### 功能说明

执行模型推理，直到返回推理结果。


#### 函数原型

- C函数原型
  1 aclError aclmdlExecute(uint32_t modelId, const aclmdlDataset *input, aclmdlDataset *output)

- python函数
  1 ret = acl.mdl.execute(model_id, input, output)


#### 参数说明

| 参数名 | 说明 |
| --- | --- |
| model\_id | int，指定需要执行推理的模型的ID。 可在以下接口成功加载模型后获取到模型ID。 acl.mdl.load\_from\_file acl.mdl.load\_from\_mem acl.mdl.load\_from\_file\_with\_mem acl.mdl.load\_from\_mem\_with\_mem |
| input | int，模型推理的输入数据对应的指针地址，具体请参见aclmdlDataset。 |
| output | int，模型推理的输出数据对应的指针地址，具体请参见aclmdlDataset。 调用acl.create\_data\_buffer接口创建存放对应index输出数据的aclDataBuffer类型时，可通过“data”参数处传入0，同时设置“size”为0，创建一个空的aclDataBuffer类型，在模型执行过程中，系统内部自行计算并申请该index输出的内存。 使用该方式可节省内存，但在内存数据使用结束后，需由用户释放内存并重置aclDataBuffer，并且系统内部在申请内存时涉及内存拷贝，可能涉及性能损耗。 释放内存并重置aclDataBuffer的示例代码如下： data\_buffer = acl.mdl.get\_dataset\_buffer(output, 0) \# 根据index获取对应的dataBuffer data\_addr = acl.get\_data\_buffer\_addr(data\_buffer) \# 获取data的Device指针地址 acl.rt.free(data\_addr) // 释放Device内存 acl.update\_data\_buffer(data\_buffer, 0, 0) \# 重置dataBuffer，以便下次推理 |


#### 返回值说明

| 返回值 | 说明 |
| --- | --- |
| ret | int，错误码，返回0表示成功，返回其它值表示失败。 |


#### 约束说明

- 若由于业务需求，必须在多线程中使用同一个“model_id”，则用户线程间需加锁，保证刷新输入输出内存、保证执行是连续操作，例如：

```
# 线程A的接口调用顺序：
lock(handle1) -> acl.rt.memcpy刷新输入输出内存 -> acl.mdl.execute执行推理 -> unlock(handle1)
# 线程B的接口调用顺序：
lock(handle1) -> acl.rt.memcpy刷新输入输出内存 -> acl.mdl.execute执行推理 -> unlock(handle1)
```


- 模型加载、模型执行、模型卸载的操作必须在同一个Context下（关于Context的创建请参见  acl.rt.create_context(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0072.html)
）。
- 存放模型输入/输出数据的内存，可以使用以下接口申请：acl.rt.malloc(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0110.html)、acl.rt.malloc_host(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0116.html)、acl.rt.malloc_cached(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0111.html)接口、acl.media.dvpp_malloc(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/dvpp/aclpythondevg_01_0223.html)接口或acl.himpi.dvpp_malloc(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/dvpp/aclpythondevg_01_0416.html)接口。
其中：

  - 内存申请接口的具体使用场景及约束请参见相关接口的详细说明。
  - 当应用程序在Device上调用    acl.rt.malloc_host(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0116.html)
接口申请内存时，该内存为Device内存。
  -     acl.media.dvpp_malloc(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/dvpp/aclpythondevg_01_0223.html)
接口和    acl.himpi.dvpp_malloc(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/dvpp/aclpythondevg_01_0416.html)
接口是媒体数据处理功能专用的内存申请接口，一般从性能角度，为了减少拷贝，媒体数据处理的输出作为模型推理的输入，实现内存复用。
  - 但由于媒体数据处理访问的地址空间有限，为确保媒体数据处理时内存足够，除媒体数据处理功能外的其它功能（例如，模型加载），建议调用其它接口申请内存，例如    acl.rt.malloc(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0110.html)
接口。


#### 资源参考

接口调用流程及示例代码，参见模型执行(https://www.hiascend.comdocument/detail/zh/canncommercial/900/programug/acldevg/aclpythondevg_0032.html)。
