huggingface--transformers
e06fe8e8c6
Secret Leaks / trufflehog (push) Failing after 1s
Build documentation / build (push) Failing after 1s
Build documentation / build_other_lang (push) Failing after 0s
CodeQL Security Analysis / CodeQL Analysis (push) Failing after 0s
PR CI / pr-ci (push) Failing after 1s
Slow tests on important models (on Push - A10) / Get all modified files (push) Failing after 1s
Slow tests on important models (on Push - A10) / Model CI (push) Has been skipped
Self-hosted runner (benchmark) / Benchmark (aws-g5-4xlarge-cache) (push) Has been cancelled
New model PR merged notification / Notify new model (push) Has been cancelled
Update Transformers metadata / build_and_package (push) Has been cancelled
308 ่ก
15 KiB
Markdown
308 ่ก
15 KiB
Markdown
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
|
the License. You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
|
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
specific language governing permissions and limitations under the License.
|
|
|
|
โ ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
|
rendered properly in your Markdown viewer.
|
|
|
|
-->
|
|
|
|
# ๋๋ฒ๊น
[[debugging]]
|
|
|
|
## Multi-GPU ๋คํธ์ํฌ ๋ฌธ์ ๋๋ฒ๊ทธ [[multigpu-network-issues-debug]]
|
|
|
|
`DistributedDataParallel` ๋ฐ ๋ค์ค GPU๋ฅผ ์ฌ์ฉํ์ฌ ํ๋ จํ๊ฑฐ๋ ์ถ๋ก ํ ๋, ํ๋ก์ธ์ค ๋ฐ/๋๋ ๋
ธ๋ ๊ฐ์ ์ํธ ํต์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ ๊ฒฝ์ฐ, ๋ค์ ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ์ฌ ๋คํธ์ํฌ ๋ฌธ์ ๋ฅผ ์ง๋จํ ์ ์์ต๋๋ค.
|
|
|
|
```bash
|
|
wget https://raw.githubusercontent.com/huggingface/transformers/main/scripts/distributed/torch-distributed-gpu-test.py
|
|
```
|
|
|
|
์๋ฅผ ๋ค์ด, 2๊ฐ์ GPU๊ฐ ์ํธ ์์ฉํ๋ ๋ฐฉ์์ ํ
์คํธํ๋ ค๋ฉด ๋ค์์ ์คํํ์ธ์:
|
|
|
|
```bash
|
|
python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py
|
|
```
|
|
๋ ํ๋ก์ธ์ค๊ฐ ์๋ก ํต์ ํ๊ณ GPU ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ๋ ๊ฒฝ์ฐ, ๊ฐ๊ฐ "OK" ์ํ๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
|
|
|
|
๋ ๋ง์ GPU ๋๋ ๋
ธ๋์ ๊ฒฝ์ฐ ์คํฌ๋ฆฝํธ์ ์ธ์๋ฅผ ์กฐ์ ํ๋ฉด ๋ฉ๋๋ค.
|
|
|
|
์ง๋จ ์คํฌ๋ฆฝํธ ๋ด์์ ๋ ๋ง์ ์ธ๋ถ ์ ๋ณด์ SLURM ํ๊ฒฝ์์ ์คํํ๋ ๋ฐฉ๋ฒ์ ๋ํ ๋ ์ํผ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.
|
|
|
|
์ถ๊ฐ์ ์ธ ๋๋ฒ๊ทธ ์์ค์ ๋ค์๊ณผ ๊ฐ์ด `NCCL_DEBUG=INFO` ํ๊ฒฝ ๋ณ์๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์
๋๋ค:
|
|
|
|
```bash
|
|
NCCL_DEBUG=INFO python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py
|
|
```
|
|
|
|
์ด๋ ๊ฒ ํ๋ฉด NCCL ๊ด๋ จ ๋๋ฒ๊ทธ ์ ๋ณด๊ฐ ๋ง์ด ์ถ๋ ฅ๋๋ฉฐ, ๋ฌธ์ ๊ฐ ๋ณด๊ณ ๋ ๊ฒฝ์ฐ์๋ ์ธํฐ๋ท์์ ๊ฒ์ํ ์ ์์ต๋๋ค. ๋๋ ์ถ๋ ฅ์ ํด์ํ๋ ๋ฐฉ๋ฒ์ ์ ๋ชจ๋ฅด๋ ๊ฒฝ์ฐ ๋ก๊ทธ ํ์ผ์ ์ด์์ ๊ณต์ ํ ์ ์์ต๋๋ค.
|
|
|
|
|
|
|
|
## ์ธ๋ํ๋ก ๋ฐ ์ค๋ฒํ๋ก ๊ฐ์ง [[underflow-and-overflow-detection]]
|
|
|
|
|
|
<Tip>
|
|
|
|
์ด ๊ธฐ๋ฅ์ ํ์ฌ PyTorch์์๋ง ์ฌ์ฉํ ์ ์์ต๋๋ค.
|
|
|
|
</Tip>
|
|
|
|
<Tip>
|
|
|
|
๋ค์ค GPU ํ๋ จ์ ์ํด์๋ DDP (`torch.distributed.launch`)๊ฐ ํ์ํฉ๋๋ค.
|
|
|
|
</Tip>
|
|
|
|
<Tip>
|
|
|
|
์ด ๊ธฐ๋ฅ์ `nn.Module`์ ๊ธฐ๋ฐ์ผ๋ก ํ๋ ๋ชจ๋ธ๊ณผ ํจ๊ป ์ฌ์ฉํ ์ ์์ต๋๋ค.
|
|
|
|
</Tip>
|
|
|
|
`loss=NaN`์ด ๋ํ๋๊ฑฐ๋ ๋ชจ๋ธ์ด `inf` ๋๋ `nan`์ผ๋ก ์ธํด ๋ค๋ฅธ ์ด์ํ ๋์์ ํ๋ ๊ฒฝ์ฐ, ์ธ๋ํ๋ก ๋๋ ์ค๋ฒํ๋ก์ ์ฒซ ๋ฒ์งธ ๋ฐ์ ์์น์ ๊ทธ ์์ธ์ ํ์
ํด์ผ ํฉ๋๋ค. ๋คํํ๋ ์ด๋ฅผ ์๋์ผ๋ก ๊ฐ์งํ๋ ํน์ ๋ชจ๋์ ํ์ฑํํ์ฌ ์ฝ๊ฒ ์์๋ผ ์ ์์ต๋๋ค.
|
|
|
|
[`Trainer`]๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, ๋ค์์ ๊ธฐ์กด์ ๋ช
๋ น์ค ์ธ์์ ์ถ๊ฐํ๋ฉด ๋ฉ๋๋ค.
|
|
|
|
```bash
|
|
--debug underflow_overflow
|
|
```
|
|
๋๋ [`TrainingArguments`] ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ `debug="underflow_overflow"`๋ฅผ ์ ๋ฌํฉ๋๋ค.
|
|
|
|
์์ฒด ํ๋ จ ๋ฃจํ๋ ๋ค๋ฅธ Trainer๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, ๋ค์๊ณผ ๊ฐ์ด ์ํํ ์ ์์ต๋๋ค.
|
|
|
|
```python
|
|
from transformers.debug_utils import DebugUnderflowOverflow
|
|
|
|
debug_overflow = DebugUnderflowOverflow(model)
|
|
```
|
|
|
|
[`~debug_utils.DebugUnderflowOverflow`]๋ ๋ชจ๋ธ์ ํํฌ๋ฅผ ์ฝ์
ํ์ฌ ๊ฐ forward ํธ์ถ ์งํ์ ์
๋ ฅ ๋ฐ ์ถ๋ ฅ ๋ณ์ ๋ฐ ํด๋น ๋ชจ๋์ ๊ฐ์ค์น๋ฅผ ํ
์คํธํฉ๋๋ค. ํ์ฑํ๋ ๊ฐ์ค์น์ ์ต์ํ ํ๋์ ์์์์ `inf` ๋๋ `nan`์ด ๊ฐ์ง๋๋ฉด ํ๋ก๊ทธ๋จ์ด ์ด์คํธ๋๊ณ ๋ค์๊ณผ ๊ฐ์ ๋ณด๊ณ ์๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค. (์ด ์์ ๋ fp16 ํผํฉ ์ ๋ฐ๋์์ `google/mt5-small`์์ ์บก์ฒ๋ ๊ฒ์
๋๋ค):
|
|
|
|
```
|
|
Detected inf/nan during batch_number=0
|
|
Last 21 forward frames:
|
|
abs min abs max metadata
|
|
encoder.block.1.layer.1.DenseReluDense.dropout Dropout
|
|
0.00e+00 2.57e+02 input[0]
|
|
0.00e+00 2.85e+02 output
|
|
[...]
|
|
encoder.block.2.layer.0 T5LayerSelfAttention
|
|
6.78e-04 3.15e+03 input[0]
|
|
2.65e-04 3.42e+03 output[0]
|
|
None output[1]
|
|
2.25e-01 1.00e+04 output[2]
|
|
encoder.block.2.layer.1.layer_norm T5LayerNorm
|
|
8.69e-02 4.18e-01 weight
|
|
2.65e-04 3.42e+03 input[0]
|
|
1.79e-06 4.65e+00 output
|
|
encoder.block.2.layer.1.DenseReluDense.wi_0 Linear
|
|
2.17e-07 4.50e+00 weight
|
|
1.79e-06 4.65e+00 input[0]
|
|
2.68e-06 3.70e+01 output
|
|
encoder.block.2.layer.1.DenseReluDense.wi_1 Linear
|
|
8.08e-07 2.66e+01 weight
|
|
1.79e-06 4.65e+00 input[0]
|
|
1.27e-04 2.37e+02 output
|
|
encoder.block.2.layer.1.DenseReluDense.dropout Dropout
|
|
0.00e+00 8.76e+03 input[0]
|
|
0.00e+00 9.74e+03 output
|
|
encoder.block.2.layer.1.DenseReluDense.wo Linear
|
|
1.01e-06 6.44e+00 weight
|
|
0.00e+00 9.74e+03 input[0]
|
|
3.18e-04 6.27e+04 output
|
|
encoder.block.2.layer.1.DenseReluDense T5DenseGatedGeluDense
|
|
1.79e-06 4.65e+00 input[0]
|
|
3.18e-04 6.27e+04 output
|
|
encoder.block.2.layer.1.dropout Dropout
|
|
3.18e-04 6.27e+04 input[0]
|
|
0.00e+00 inf output
|
|
```
|
|
|
|
์์ ์ถ๋ ฅ์ ๊ฐ๋ต์ฑ์ ์ํด ์ค๊ฐ ๋ถ๋ถ์ด ์๋ ค ์์ต๋๋ค.
|
|
|
|
๋ ๋ฒ์งธ ์ด์ ์ ๋์ ์ผ๋ก ๊ฐ์ฅ ํฐ ์์์ ๊ฐ์ด๋ฉฐ, ๋ฐ๋ผ์ ๋ง์ง๋ง ๋ช ๊ฐ์ ํ๋ ์์ ์์ธํ ์ดํด๋ณด๋ฉด ์
๋ ฅ๊ณผ ์ถ๋ ฅ์ด `1e4` ๋ฒ์์ ์์์ ์ ์ ์์ต๋๋ค. ๋ฐ๋ผ์ ์ด ํ๋ จ์ `fp16` ํผํฉ ์ ๋ฐ๋๋ก ์ํ๋ ๋ ๊ฐ์ฅ ๋ง์ง๋ง ๋จ๊ณ์์ ์ค๋ฒํ๋ก์ฐ๊ฐ ๋ฐ์ํ์ต๋๋ค (`fp16`์์ `inf` ์ด์ ์ ๊ฐ์ฅ ํฐ ์ซ์๋ `64e3`์
๋๋ค). `fp16` ์๋์์ ์ค๋ฒํ๋ก์ฐ๋ฅผ ํผํ๊ธฐ ์ํด์๋ ํ์ฑํ๋ `1e4`๋ณด๋ค ํจ์ฌ ์์์ผ ํฉ๋๋ค. ์๋ํ๋ฉด `1e4 * 1e4 = 1e8`์ด๊ธฐ ๋๋ฌธ์ ํฐ ํ์ฑํ์์ ํ๋ ฌ ๊ณฑ์ ์์น์ ์ธ ์ค๋ฒํ๋ก์ฐ ์กฐ๊ฑด์ผ๋ก ์ด์ด์ง ๊ฒ์
๋๋ค.
|
|
|
|
์ถ์ ์ ๋งจ ์ฒ์์์ ์ด๋ ๋ฐฐ์น ๋ฒํธ์์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋์ง ์ ์ ์์ต๋๋ค (์ฌ๊ธฐ์ `Detected inf/nan during batch_number=0`์ ๋ฌธ์ ๊ฐ ์ฒซ ๋ฒ์งธ ๋ฐฐ์น์์ ๋ฐ์ํ์์ ์๋ฏธํฉ๋๋ค).
|
|
|
|
๊ฐ ๋ณด๊ณ ๋ ํ๋ ์์ ํด๋น ํ๋ ์์ด ๋ณด๊ณ ํ๋ ํด๋น ๋ชจ๋์ ๋ํ ์์ ํ ํญ๋ชฉ์ ์ ์ธํ๋ฉฐ, ์ด ํ๋ ์๋ง ์ดํด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
|
|
|
|
```
|
|
encoder.block.2.layer.1.layer_norm T5LayerNorm
|
|
8.69e-02 4.18e-01 weight
|
|
2.65e-04 3.42e+03 input[0]
|
|
1.79e-06 4.65e+00 output
|
|
```
|
|
|
|
์ฌ๊ธฐ์ `encoder.block.2.layer.1.layer_norm`์ ์ธ์ฝ๋์ ๋ ๋ฒ์งธ ๋ธ๋ก์ ์ฒซ ๋ฒ์งธ ๋ ์ด์ด์ ๋ํ ๋ ์ด์ด ์ ๊ทํ๋ฅผ ์๋ฏธํ๋ฉฐ, `forward`์ ํน์ ํธ์ถ์ `T5LayerNorm`์
๋๋ค.
|
|
|
|
์ด ๋ณด๊ณ ์์ ๋ง์ง๋ง ๋ช ๊ฐ ํ๋ ์์ ์ดํด๋ณด๊ฒ ์ต๋๋ค:
|
|
|
|
```
|
|
Detected inf/nan during batch_number=0
|
|
Last 21 forward frames:
|
|
abs min abs max metadata
|
|
[...]
|
|
encoder.block.2.layer.1.DenseReluDense.wi_0 Linear
|
|
2.17e-07 4.50e+00 weight
|
|
1.79e-06 4.65e+00 input[0]
|
|
2.68e-06 3.70e+01 output
|
|
encoder.block.2.layer.1.DenseReluDense.wi_1 Linear
|
|
8.08e-07 2.66e+01 weight
|
|
1.79e-06 4.65e+00 input[0]
|
|
1.27e-04 2.37e+02 output
|
|
encoder.block.2.layer.1.DenseReluDense.wo Linear
|
|
1.01e-06 6.44e+00 weight
|
|
0.00e+00 9.74e+03 input[0]
|
|
3.18e-04 6.27e+04 output
|
|
encoder.block.2.layer.1.DenseReluDense T5DenseGatedGeluDense
|
|
1.79e-06 4.65e+00 input[0]
|
|
3.18e-04 6.27e+04 output
|
|
encoder.block.2.layer.1.dropout Dropout
|
|
3.18e-04 6.27e+04 input[0]
|
|
0.00e+00 inf output
|
|
```
|
|
|
|
๋ง์ง๋ง ํ๋ ์์ `Dropout.forward` ํจ์์ ๋ํ ๋ณด๊ณ ์
๋๋ค. ์ฒซ ๋ฒ์งธ ํญ๋ชฉ์ ์ ์ผํ ์
๋ ฅ์ ๋ํ๋ด๊ณ ๋ ๋ฒ์งธ ํญ๋ชฉ์ ์ ์ผํ ์ถ๋ ฅ์ ๋ํ๋
๋๋ค. ์ด ํจ์๊ฐ `DenseReluDense` ํด๋์ค ๋ด๋ถ์ `dropout` ์์ฑ์์ ํธ์ถ๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค. ์ด๋ ์ฒซ ๋ฒ์งธ ๋ ์ด์ด์ ๋ ๋ฒ์งธ ๋ธ๋ก์์ ์ฒซ ๋ฒ์งธ ๋ฐฐ์น ์ค์ ๋ฐ์ํ๋ค๋ ๊ฒ์ ์ ์ ์์ต๋๋ค. ๋ง์ง๋ง์ผ๋ก, ์ ๋์ ์ผ๋ก ๊ฐ์ฅ ํฐ ์
๋ ฅ ์์๋ `6.27e+04`์ด๊ณ ์ถ๋ ฅ๋ ๋ง์ฐฌ๊ฐ์ง๋ก `inf`์
๋๋ค.
|
|
|
|
์ฌ๊ธฐ์์๋ `T5DenseGatedGeluDense.forward`๊ฐ ์ถ๋ ฅ ํ์ฑํ๋ฅผ ์์ฑํ๋๋ฐ, ์ ๋์ ์ผ๋ก ๊ฐ์ฅ ํฐ ๊ฐ์ด ์ฝ 62.7K์ธ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค. ์ด ๊ฐ์ fp16์ ์ต๋ ์ ํ์ธ 64K์ ๋งค์ฐ ๊ทผ์ ํฉ๋๋ค. ๋ค์ ํ๋ ์์์๋ ์ผ๋ถ ์์๋ฅผ 0์ผ๋ก ๋ง๋ ํ ๊ฐ์ค์น๋ฅผ ์ฌ์ ๊ทํํ๋ `Dropout`์ด ์์ต๋๋ค. ์ด๋ก ์ธํด ์ ๋ ์ต๋๊ฐ์ด 64K๋ฅผ ์ด๊ณผํ๊ณ ์ค๋ฒํ๋ก์ฐ(`inf`)๊ฐ ๋ฐ์ํฉ๋๋ค.
|
|
|
|
๋ณด์๋ค์ํผ, fp16 ์ซ์์ ๊ฒฝ์ฐ ์ซ์๊ฐ ๋งค์ฐ ์ปค์ง ๋ ์ด์ ํ๋ ์์ ์ดํด๋ณด์์ผ ํฉ๋๋ค.
|
|
|
|
๋ณด๊ณ ์๋ฅผ `models/t5/modeling_t5.py`์ ์ฝ๋์ ์ผ์น์์ผ ๋ณด๊ฒ ์ต๋๋ค.
|
|
|
|
```python
|
|
class T5DenseGatedGeluDense(nn.Module):
|
|
def __init__(self, config):
|
|
super().__init__()
|
|
self.wi_0 = nn.Linear(config.d_model, config.d_ff, bias=False)
|
|
self.wi_1 = nn.Linear(config.d_model, config.d_ff, bias=False)
|
|
self.wo = nn.Linear(config.d_ff, config.d_model, bias=False)
|
|
self.dropout = nn.Dropout(config.dropout_rate)
|
|
self.gelu_act = ACT2FN["gelu_new"]
|
|
|
|
def forward(self, hidden_states):
|
|
hidden_gelu = self.gelu_act(self.wi_0(hidden_states))
|
|
hidden_linear = self.wi_1(hidden_states)
|
|
hidden_states = hidden_gelu * hidden_linear
|
|
hidden_states = self.dropout(hidden_states)
|
|
hidden_states = self.wo(hidden_states)
|
|
return hidden_states
|
|
```
|
|
|
|
์ด์ `dropout` ํธ์ถ๊ณผ ์ด์ ์ ๋ชจ๋ ํธ์ถ์ ์ฝ๊ฒ ํ์ธํ ์ ์์ต๋๋ค.
|
|
|
|
๊ฐ์ง๋ `forward` ํํฌ์์ ๋ฐ์ํ๋ฏ๋ก, ์ด๋ฌํ ๋ณด๊ณ ์๋ ๊ฐ `forward`๊ฐ ๋ฐํ๋ ์งํ์ ์ฆ์ ์ถ๋ ฅ๋ฉ๋๋ค.
|
|
|
|
์ ์ฒด ๋ณด๊ณ ์๋ก ๋์๊ฐ์ ๋ฌธ์ ์ ๋ํ ์กฐ์น ๋ฐ ์์ ์ ํ๋ ค๋ฉด, ์ซ์๊ฐ ์ฆ๊ฐํ๊ธฐ ์์ํ ๋ช ๊ฐ์ ํ๋ ์ ์๋ก ์ด๋ํด์ ์ฌ๊ธฐ์ `fp32` ๋ชจ๋๋ก ์ ํํด์ผ ํฉ๋๋ค. ์ด๋ ๊ฒ ํด์ผ ์ซ์๊ฐ ๊ณฑํด์ง๊ฑฐ๋ ํฉ์ณ์ง ๋ ์ค๋ฒํ๋ก์ฐ๋์ง ์์ ๊ฐ๋ฅ์ฑ์ด ๋์ต๋๋ค. ๋ฌผ๋ก ๋ค๋ฅธ ํด๊ฒฐ์ฑ
๋ ์์ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, `amp`๊ฐ ํ์ฑํ๋ ๊ฒฝ์ฐ ์ผ์์ ์ผ๋ก ๋๊ณ ์๋์ `forward`๋ฅผ ๋์ฐ๋ฏธ ๋ํผ๋ก ์ด๋ํ ํ ๋ค์๊ณผ ๊ฐ์ด ํ ์ ์์ต๋๋ค:
|
|
|
|
```python
|
|
def _forward(self, hidden_states):
|
|
hidden_gelu = self.gelu_act(self.wi_0(hidden_states))
|
|
hidden_linear = self.wi_1(hidden_states)
|
|
hidden_states = hidden_gelu * hidden_linear
|
|
hidden_states = self.dropout(hidden_states)
|
|
hidden_states = self.wo(hidden_states)
|
|
return hidden_states
|
|
|
|
|
|
import torch
|
|
|
|
|
|
def forward(self, hidden_states):
|
|
device_type = hidden_states.device.type
|
|
if torch.is_autocast_enabled(device_type):
|
|
with torch.amp.autocast(device_type, enabled=False):
|
|
return self._forward(hidden_states)
|
|
else:
|
|
return self._forward(hidden_states)
|
|
```
|
|
|
|
์๋ ๊ฐ์ง๊ธฐ๋ ์ ์ฒด ํ๋ ์์ ์
๋ ฅ๊ณผ ์ถ๋ ฅ์ ๋ํด์๋ง ๋ณด๊ณ ํ๋ฏ๋ก, ์ด๋๋ฅผ ์ดํด๋ด์ผ ํ๋์ง ์๋ฉด ํน์ `forward` ํจ์์ ์ค๊ฐ ๋จ๊ณ๋ ๋ถ์ํ ์ ์์ต๋๋ค. ์ด ๊ฒฝ์ฐ์๋ `detect_overflow` ๋์ฐ๋ฏธ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ํ๋ ์์น์ ๊ฐ์ง๊ธฐ๋ฅผ ์ฝ์
ํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด:
|
|
|
|
```python
|
|
from debug_utils import detect_overflow
|
|
|
|
|
|
class T5LayerFF(nn.Module):
|
|
[...]
|
|
|
|
def forward(self, hidden_states):
|
|
forwarded_states = self.layer_norm(hidden_states)
|
|
detect_overflow(forwarded_states, "after layer_norm")
|
|
forwarded_states = self.DenseReluDense(forwarded_states)
|
|
detect_overflow(forwarded_states, "after DenseReluDense")
|
|
return hidden_states + self.dropout(forwarded_states)
|
|
```
|
|
|
|
์ฌ๊ธฐ์๋ ์ด๋ฅผ ์ถ๊ฐํ์ฌ 2๊ฐ์ ๊ฒ์ ์ถ์ ํ๊ณ ์ด์ `forwarded_states`์ `inf` ๋๋ `nan`์ด ์ค๊ฐ์ ๊ฐ์ง๋์๋์ง๋ฅผ ์ถ์ ํฉ๋๋ค.
|
|
|
|
์ค์ ๋ก ์์ ์์ ์์ ๊ฐ ํธ์ถ์ด `nn.Module`์ด๊ธฐ ๋๋ฌธ์ ํ์ง๊ธฐ๊ฐ ์ด๋ฏธ ์ด๋ฅผ ๋ณด๊ณ ํฉ๋๋ค. ๋ก์ปฌ์์ ์ง์ ๊ณ์ฐํ๋ ๊ฒฝ์ฐ ์ด๋ ๊ฒ ์ํํ๋ค๊ณ ๊ฐ์ ํด ๋ด
์๋ค.
|
|
|
|
๋ํ, ์์ฒด ์ฝ๋์์ ๋๋ฒ๊ฑฐ๋ฅผ ์ธ์คํด์คํํ๋ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ์์ ์ถ๋ ฅ๋๋ ํ๋ ์ ์๋ฅผ ์กฐ์ ํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด:
|
|
|
|
```python
|
|
from transformers.debug_utils import DebugUnderflowOverflow
|
|
|
|
debug_overflow = DebugUnderflowOverflow(model, max_frames_to_save=100)
|
|
```
|
|
|
|
### ํน์ ๋ฐฐ์น์ ์ ๋๊ฐ ์ต์ ๋ฐ ์ต๋ ๊ฐ ์ถ์ [[specific-batch-absolute-min-and-max-value-tracing]]
|
|
|
|
๋์ผํ ๋๋ฒ๊น
ํด๋์ค๋ ์ธ๋ํ๋ก์ฐ/์ค๋ฒํ๋ก์ฐ ๊ฐ์ง ๊ธฐ๋ฅ์ด ๊บผ์ง ์ํ์์ ๋ฐฐ์น๋ณ ์ถ์ ์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
|
|
|
|
์๋ฅผ ๋ค์ด, ํน์ ๋ฐฐ์น์ ๊ฐ `forward` ํธ์ถ์ ๋ชจ๋ ๊ตฌ์ฑ ์ฑ๋ถ์ ๋ํ ์ ๋ ์ต์๊ฐ๊ณผ ์ต๋๊ฐ์ ํ์ธํ๊ณ , ์ด๋ฅผ ๋ฐฐ์น 1๊ณผ 3์ ๋ํด์๋ง ์ํํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์ด ํด๋์ค๋ฅผ ์ธ์คํด์คํํฉ๋๋ค:
|
|
|
|
```python
|
|
debug_overflow = DebugUnderflowOverflow(model, trace_batch_nums=[1, 3])
|
|
```
|
|
|
|
๊ทธ๋ฌ๋ฉด ์ด์ ๋ฐฐ์น 1๊ณผ 3 ์ ์ฒด๊ฐ ์ธ๋ํ๋ก์ฐ/์ค๋ฒํ๋ก์ฐ ๊ฐ์ง๊ธฐ์ ๋์ผํ ํ์์ผ๋ก ์ถ์ ๋ฉ๋๋ค.
|
|
|
|
๋ฐฐ์น๋ 0๋ถํฐ ์์ํฉ๋๋ค.
|
|
|
|
์ด๋ ํ๋ก๊ทธ๋จ์ด ํน์ ๋ฐฐ์น ๋ฒํธ ์ดํ์ ์ค์๋ํ๊ธฐ ์์ํ๋ ๊ฒ์ ์๊ณ ์๋ ๊ฒฝ์ฐ์ ์ ์ฉํฉ๋๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ํด๋น ์์ญ์ผ๋ก ๋ฐ๋ก ์ด๋ํ ์ ์์ต๋๋ค. ์ด๋ฐ ๊ตฌ์ฑ์ ๋ํ ์ํ ์ถ์๋ ์ถ๋ ฅ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
|
|
|
|
```
|
|
*** Starting batch number=1 ***
|
|
abs min abs max metadata
|
|
shared Embedding
|
|
1.01e-06 7.92e+02 weight
|
|
0.00e+00 2.47e+04 input[0]
|
|
5.36e-05 7.92e+02 output
|
|
[...]
|
|
decoder.dropout Dropout
|
|
1.60e-07 2.27e+01 input[0]
|
|
0.00e+00 2.52e+01 output
|
|
decoder T5Stack
|
|
not a tensor output
|
|
lm_head Linear
|
|
1.01e-06 7.92e+02 weight
|
|
0.00e+00 1.11e+00 input[0]
|
|
6.06e-02 8.39e+01 output
|
|
T5ForConditionalGeneration
|
|
not a tensor output
|
|
|
|
*** Starting batch number=3 ***
|
|
abs min abs max metadata
|
|
shared Embedding
|
|
1.01e-06 7.92e+02 weight
|
|
0.00e+00 2.78e+04 input[0]
|
|
5.36e-05 7.92e+02 output
|
|
[...]
|
|
```
|
|
|
|
์ฌ๊ธฐ์์๋ ๋ชจ๋ธ์ forward ํธ์ถ ์์ ๋์ผํ ์์ ํ๋ ์์ด ๋คํ๋๋ฏ๋ก ๋ง์ ์์ ํ๋ ์์ด ์์ฑ๋ฉ๋๋ค. ๋ฐ๋ผ์ ์ํ๋ ๊ฒ์ผ ์๋ ์๊ณ ์๋ ์๋ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ๋๋ก๋ ์ผ๋ฐ ๋๋ฒ๊ฑฐ๋ณด๋ค ๋๋ฒ๊น
๋ชฉ์ ์ผ๋ก ๋ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ๋ฌธ์ ๊ฐ ๋ฐฐ์น ๋ฒํธ 150์์ ์์ํ๋ ๊ฒฝ์ฐ 149์ 150์ ์ถ์ ์ ๋คํํ๊ณ ์ซ์๊ฐ ์ด๋์๋ถํฐ ๋ค๋ฅด๊ฒ ๋์๋์ง ๋น๊ตํ ์ ์์ต๋๋ค.
|
|
|
|
๋ํ, ํ๋ จ์ ์ค์งํ ๋ฐฐ์น ๋ฒํธ๋ฅผ ์ง์ ํ ์๋ ์์ต๋๋ค. ๋ค์๊ณผ ๊ฐ์ด ์ง์ ํ ์ ์์ต๋๋ค.
|
|
|
|
```python
|
|
debug_overflow = DebugUnderflowOverflow(model, trace_batch_nums=[1, 3], abort_after_batch_num=3)
|
|
```
|