---
title: push_cache
description: "| 产品 | 是否支持 |"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/hixlug/llmprv2_42_0036.html
sourcePath: /source/zh/canncommercial/900/API/hixlug/llmprv2_42_0036.html
indexId: f8824cb31cb23eb09f116ff60295327d31c8ee47b8ea35834e03d83357db6e5861
---
# push_cache

#### 产品支持情况

| 产品 | 是否支持 |
| --- | --- |
| Atlas 350 加速卡 | x |
| Atlas A3 训练系列产品 / Atlas A3 推理系列产品 | √ |
| Atlas A2 训练系列产品 / Atlas A2 推理系列产品 | √ |
| Atlas 200I/500 A2 推理产品 | x |
| Atlas 推理系列产品 | x |
| Atlas 训练系列产品 | x |


说明：针对 Atlas A2 训练系列产品 / Atlas A2 推理系列产品 ，仅支持Atlas 800I A2 推理服务器、A200I A2 Box 异构组件。


#### 函数功能

根据CacheKey，从本地节点推送Cache到远端Cache。


#### 函数原型

```
push_cache(self,
                   dst_cache_key: CacheKeyByIdAndIndex,
                   src_cache: Cache,
                   src_batch_index: int = 0,
                   src_layer_range: range = None,
                   dst_layer_range: range = None,
                   size: int = -1,
                   tensor_num_per_layer = _NUM_TENSORS_PER_LAYER) -> None
```


#### 参数说明

| 参数名称 | 数据类型 | 取值说明 |
| --- | --- | --- |
| dst\_cache\_key | CacheKeyByIdAndIndex | 远端的Cache索引 |
| src\_cache | Cache | 本地的cache |
| src\_batch\_index | int | 本地的batch index，默认为0。 |
| src\_layer\_range | Optional[range] | 可选参数，用于按层pull kv场景。默认值为None。 传输源的layer的范围，step只支持1。不设置时为传输所有layer。需要注意这里是layer的index，而不是tensor的index，即1个layer对应连续N个tensor(K/V)，这里要求分配内存时，必须是KV,...,KV排布，不支持其他场景。N为tensor\_num\_per\_layer的取值，默认为2。 |
| dst\_layer\_range | Optional[range] | 可选参数，用于按层pull kv场景。默认值为None。 传输目标的layer的范围，step只支持1。不设置时为传输所有layer。需要注意这里是layer的index，而不是tensor的index，即1个layer对应连续N个tensor(K/V)，这里要求分配内存时，必须是KV,...,KV排布，不支持其他场景。N为tensor\_num\_per\_layer的取值，默认为2。 |
| tensor\_num\_per\_layer | Optional[int] | 可选参数，表示每层的tensor的数量，默认值是2，取值范围是[1,cache的tensor总数]。当src\_layer\_range或dst\_layer\_range取值为非默认值时， tensor\_num\_per\_layer可以保持默认值，也可以输入其他值，输入其他值时，tensor\_num\_per\_layer的取值还需要被当前cache的tensor总数整除。 |
| size | int | 一个tensor传输的大小，默认值\-1表示本地单个KV的大小，暂不支持其他设置。 |


#### 调用示例

请单击Gitcode(https://gitcode.com/cann/hixl)，选择配套版本，从“examples/python”目录中获取样例。


#### 返回值

正常情况下无返回值。

传入数据类型错误情况下会抛出TypeError或ValueError异常。

执行时间超过sync_kv_timeout配置会抛出LLMException异常。

layer_range参数异常会抛出LLMException异常。


#### 约束说明

- 当src_cache是HOST设备时，dst_cache是DEVICE设备时，仅支持src_cache与dst_cache都为连续cache的场景。
- 开启  enable_remote_cache_accessible
时，只支持cache_key类型为  CacheKeyByIdAndIndex
。
- 在D2H和H2D传输场景，需要在Host端初始化时配置Device内存池。
- 当前仅支持src_layer_range和dst_layer_range取值一致。
