

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# 활성화 체크포인트
<a name="model-parallel-core-features-v2-pytorch-activation-checkpointing"></a>

*활성화 체크포인트*는 특정 레이어의 활성화를 지우고 역방향 패스 중에 이를 다시 계산하여 메모리 사용량을 줄이는 기법입니다. 이렇게 하면 추가 계산 시간이 줄어들어 메모리 사용량이 줄어듭니다. 모듈이 체크포인트로 지정된 경우 순방향 패스가 끝날 때 모듈의 최초 입력과 최종 출력만 메모리에 남습니다. PyTorch는 순방향 패스 중에 해당 모듈 내 계산의 일부를 구성한 모든 중간 텐서를 보냅니다. 체크포인트 모듈을 역방향으로 패스하는 동안 PyTorch는 이러한 텐서를 다시 계산합니다. 이 시점에서 이 체크포인트 모듈 뒤의 레이어는 역방향 패스를 완료했으므로 체크포인트의 최대 메모리 사용량을 줄입니다.

SMP v2는 PyTorch 활성화 체크포인트 모듈인 [https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/#activation-checkpointing](https://pytorch.org/blog/scaling-multimodal-foundation-models-in-torchmultimodal-with-pytorch-distributed/#activation-checkpointing)를 지원합니다. 다음은 Hugging Face GPT-NeoX 모델의 활성화 체크포인트의 예입니다.

**Hugging Face GPT-NeoX 모델의 변환기 계층 확인**

```
from transformers.models.gpt_neox import GPTNeoXLayer
from torch.distributed.algorithms._checkpoint.checkpoint_wrapper import (
    apply_activation_checkpointing
)
    
# check_fn receives a module as the arg, 
# and it needs to return whether the module is to be checkpointed
def is_transformer_layer(module):
    from transformers.models.gpt_neox import GPTNeoXLayer
    return isinstance(submodule, GPTNeoXLayer)
    
apply_activation_checkpointing(model, check_fn=is_transformer_layer)
```

**Hugging Face GPT-NeoX 모델의 다른 모든 트랜스포머 계층 확인**

```
# check_fn receives a module as arg, 
# and it needs to return whether the module is to be checkpointed
# here we define that function based on global variable (transformer_layers)
from transformers.models.gpt_neox import GPTNeoXLayer
from torch.distributed.algorithms._checkpoint.checkpoint_wrapper import (
    apply_activation_checkpointing
)

transformer_layers = [
    m for m model.modules() if isinstance(m, GPTNeoXLayer)
]

def is_odd_transformer_layer(module):
    return transformer_layers.index(module) % 2 == 0
    
apply_activation_checkpointing(model, check_fn=is_odd_transformer_layer)
```

또는 PyTorch에는 Hugging Face 트랜스포머 모델의 하위 집합에서 사용하는 체크포인트용 `torch.utils.checkpoint` 모듈도 있습니다. 이 모듈은 SMP v2에서도 작동합니다. 하지만 체크포인트 래퍼를 추가하기 위해 모델 정의에 액세스할 수 있어야 합니다. 따라서 `apply_activation_checkpointing` 메서드를 사용하는 것이 좋습니다.