---
title: SelfAttentionParam
description: "| 属性 | 类型 | 默认值 | 描述 |"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/ascendtb/ascendtb_01_0337.html
sourcePath: /source/zh/canncommercial/900/API/ascendtb/ascendtb_01_0337.html
indexId: 6a571ceab41cd8577f4ed4216434b4f603df80c99f6076680bfc44a260cc247e64
---
# SelfAttentionParam

| 属性 | 类型 | 默认值 | 描述 |
| --- | --- | --- | --- |
| quant\_type | torch\_atb.SelfAttentionParam.QuantType | torch\_atb.QuantType.TYPE\_QUANT\_UNQUANT | 表示不进行量化操作。 |
| out\_data\_type | torch\_atb.AclDataType | torch\_atb.AclDataType.ACL\_DT\_UNDEFINED | 根据输入tensors自动推导输出tensors数据类型。 |
| head\_num | int | 0 | 此默认值不可用，用户须配置此项参数。 |
| kv\_head\_num | int | 0 | \- |
| q\_scale | float | 1.0 | \- |
| qk\_scale | float | 1.0 | \- |
| batch\_run\_status\_enable | bool | False | \- |
| is\_triu\_mask | int | 0 | \- |
| calc\_type | torch\_atb.SelfAttentionParam.CalcType | torch\_atb.SelfAttentionParam.CalcType.UNDEFINED | decoder&encoder for flashAttention。 |
| kernel\_type | torch\_atb.SelfAttentionParam.KernelType | torch\_atb.SelfAttentionParam.KernelType.KERNELTYPE\_DEFAULT | \- |
| clamp\_type | torch\_atb.SelfAttentionParam.ClampType | torch\_atb.SelfAttentionParam.ClampType.CLAMP\_TYPE\_UNDEFINED | 不做clamp。 |
| clamp\_min | float | 0.0 | \- |
| clamp\_max | float | 0.0 | \- |
| mask\_type | torch\_atb.SelfAttentionParam.MaskType | torch\_atb.SelfAttentionParam.MaskType.MASK\_TYPE\_UNDEFINED | 全0mask。 |
| kvcache\_cfg | torch\_atb.SelfAttentionParam.KvCacheCfg | torch\_atb.SelfAttentionParam.KvCacheCfg.K\_CACHE\_V\_CACHE | \- |
| scale\_type | torch\_atb.SelfAttentionParam.ScaleType | torch\_atb.SelfAttentionParam.ScaleType.SCALE\_TYPE\_TOR | \- |
| input\_layout | torch\_atb.InputLayout | torch\_atb.InputLayout.TYPE\_BSND | \- |
| mla\_v\_head\_size | int | 0 | \- |
| cache\_type | torch\_atb.SelfAttentionParam.CacheType | torch\_atb.SelfAttentionParam.CacheType.CACHE\_TYPE\_NORM | \- |
| window\_size | int | 0 | \- |


#### SelfAttentionParam.QuantType

枚举项：

- TYPE_QUANT_UNQUANT
- TYPE_DEQUANT_FUSION
- TYPE_QUANT_QKV_OFFLINE
- TYPE_QUANT_QKV_ONLINE


#### SelfAttentionParam.CalcType

枚举项：

- UNDEFINED
- ENCODER
- DECODER
- PA_ENCODER
- PREFIX_ENCODER


#### SelfAttentionParam.KernelType

枚举项：

- KERNELTYPE_DEFAULT
- KERNELTYPE_HIGH_PRECISION


#### SelfAttentionParam.ClampType

枚举项：

- CLAMP_TYPE_UNDEFINED
- CLAMP_TYPE_MIN_MAX


#### SelfAttentionParam.MaskType

枚举项：

- MASK_TYPE_UNDEFINED
- MASK_TYPE_NORM
- MASK_TYPE_ALIBI
- MASK_TYPE_NORM_COMPRESS
- MASK_TYPE_ALIBI_COMPRESS
- MASK_TYPE_ALIBI_COMPRESS_SQRT
- MASK_TYPE_ALIBI_COMPRESS_LEFT_ALIGN
- MASK_TYPE_SLIDING_WINDOW_NORM
- MASK_TYPE_SLIDING_WINDOW_COMPRESS


#### SelfAttentionParam.KvCacheCfg

枚举项：

- K_CACHE_V_CACHE
- K_BYPASS_V_BYPASS


#### SelfAttentionParam.ScaleType

枚举项：

- SCALE_TYPE_TOR
- SCALE_TYPE_LOGN
- SCALE_TYPE_MAX


#### SelfAttentionParam.CacheType

枚举项：

- CACHE_TYPE_NORM
- CACHE_TYPE_SWA


#### 调用示例

```
import torch
import torch_atb  

def self_attention():
    self_attention_param = torch_atb.SelfAttentionParam(head_num = 24, kv_head_num = 24)
    self_attention_param.calc_type = torch_atb.SelfAttentionParam.CalcType.PA_ENCODER
    self_attention = torch_atb.Operation(self_attention_param)
    q = torch.ones(4096, 24, 64, dtype=torch.float16).npu()
    k = torch.ones(4096, 24, 64, dtype=torch.float16).npu()
    v = torch.ones(4096, 24, 64, dtype=torch.float16).npu()
    seqlen = torch.tensor([4096], dtype=torch.int32)
    intensors = [q,k,v,seqlen]
    print("intensors: ", intensors)

    def self_attention_run():
        outputs = self_attention.forward([q,k,v,seqlen])
        return [outputs]

    outputs = self_attention_run()
    print("outputs: ", outputs)

if __name__ == "__main__":
    self_attention()
```
