---
title: 函数：load_from_mem_with_mem
description: "从内存加载离线模型数据，由用户自行管理模型运行的内存。"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/ascendgraphapi/aclpythondevg_01_0142.html
sourcePath: /source/zh/canncommercial/900/API/ascendgraphapi/aclpythondevg_01_0142.html
indexId: e322c0c4b4f3a3e0faaeb513f4adf9647403fdc5dc4a0ba62a994afbac583be575
---
# 函数：load_from_mem_with_mem

#### 功能说明

从内存加载离线模型数据，由用户自行管理模型运行的内存。

系统完成模型加载后，返回的模型ID，作为后续操作时用于识别模型的标志。


#### 函数原型

- C函数原型
  1 aclError aclmdlLoadFromMemWithMem(const void *model, size_t modelSize, uint32_t *modelId, void *workPtr, size_t workSize, void *weightPtr, size_t weightSize)

- python函数
  1 model_id, ret = acl.mdl.load_from_mem_with_mem(model, model_size, model_id, work_ptr, work_size, weight_ptr, weight_size)


#### 参数说明

| 参数名 | 说明 |
| --- | --- |
| model | int，模型数据的内存地址。 应用运行在Host时，此处需申请Host上的内存。 应用运行在Device时，此处需申请Device上的内存。 应用的运行模式可使用acl.rt.get\_run\_mode(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg\_01\_0065.html)接口获取。 |
| model\_size | int，模型数据长度，单位Byte。 |
| model\_id | int，系统完成模型加载后生成的模型ID。 |
| work\_ptr | int，Device上模型所需工作内存（存放模型输入/输出等数据）的指针地址，由用户自行管理，模型执行过程中不能释放该内存。如果在该参数处传入0，表示由系统管理内存。 说明： 由用户自行管理工作内存时，如果多个模型串行执行，可共用同一个工作内存，但用户需确保模型的串行执行顺序，工作内存的大小需按多个模型中最大工作内存的大小来申请，例如通过以下方式保证串行： 同步模型执行时，加锁，保证执行任务串行。 异步模型执行时，使用同一个Stream，保证执行任务串行。 |
| work\_size | int，模型所需工作内存的大小，单位Byte。“work\_ptr”为0时无效。 |
| weight\_ptr | int，Device上模型权值内存（存放权值数据）的指针地址，由用户自行管理，模型执行过程中不能释放该内存。如果在“weight\_ptr”参数处传入0，表示由系统管理内存。 说明： 使用用户自行管理权值内存时，在多线程场景下，对于同一个模型，如果在每个线程中都加载了一次，可选择共用“weight\_ptr”的方式，因为“weight\_ptr”内存在推理过程中是只读的。 此处需注意，在共用“weight\_ptr”期间，不能释放“weight\_ptr”。 |
| weight\_size | int，模型所需权值内存的大小，单位Byte。“weight\_ptr”为0时无效。 |


#### 返回值说明

| 返回值 | 说明 |
| --- | --- |
| model\_id | int，系统完成模型加载后生成的模型ID。 |
| ret | int，错误码，返回0表示成功，返回其它值表示失败。 |


#### 约束说明

模型加载、模型执行、模型卸载的操作必须在同一个Context下（关于Context的创建请参见acl.rt.set_device(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0062.html)、acl.rt.create_context(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0072.html)）。


#### 资源参考

接口调用流程，参见模型加载(https://www.hiascend.comdocument/detail/zh/canncommercial/900/programug/acldevg/aclpythondevg_0031.html)。
