mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-08 20:40:34 +00:00
[test] Hotfix/fix some model test and refactor check util api (#4369)
* fix llama test * fix test bug of bert, blip2, bloom, gpt2 * fix llama test * fix opt test * fix sam test * fix sam test * fix t5 test * fix vit test * fix whisper test * fix whisper test * polish code * adjust allclose parameter * Add mistakenly deleted code * addjust allclose * change loss function for some base model
This commit is contained in:
@@ -3,7 +3,6 @@ import torch
|
||||
|
||||
import colossalai
|
||||
from colossalai.logging import disable_existing_loggers
|
||||
from colossalai.tensor.d_tensor.api import is_customized_distributed_tensor, is_distributed_tensor
|
||||
from colossalai.testing import (
|
||||
assert_hf_output_close,
|
||||
clear_cache_before_run,
|
||||
@@ -12,14 +11,14 @@ from colossalai.testing import (
|
||||
spawn,
|
||||
)
|
||||
from tests.kit.model_zoo import model_zoo
|
||||
from tests.test_shardformer.test_model._utils import build_model, run_forward
|
||||
from tests.test_shardformer.test_model._utils import build_model, check_grad, run_forward
|
||||
|
||||
|
||||
def check_forward_backward(org_model, sharded_model, data_gen_fn, output_transform_fn, loss_fn):
|
||||
# check forward
|
||||
org_output, org_loss, shard_output, shard_loss = run_forward(org_model, sharded_model, data_gen_fn,
|
||||
output_transform_fn, loss_fn)
|
||||
assert_hf_output_close(org_output, shard_output, ignore_keys='past_key_values')
|
||||
assert_hf_output_close(org_output, shard_output, ignore_keys='past_key_values', atol=1e-5)
|
||||
|
||||
# do backward
|
||||
org_loss.backward()
|
||||
@@ -28,8 +27,7 @@ def check_forward_backward(org_model, sharded_model, data_gen_fn, output_transfo
|
||||
assert torch.allclose(org_loss, shard_loss,
|
||||
atol=1e-5), f"shard model loss is not equal to orgin model loss\n{org_loss}\n{shard_loss}"
|
||||
|
||||
# check grad
|
||||
|
||||
# unwarp the model
|
||||
if org_model.__class__.__name__ == 'WhisperForConditionalGeneration':
|
||||
whisper = org_model.model
|
||||
sharded_whisper = sharded_model.model
|
||||
@@ -37,38 +35,15 @@ def check_forward_backward(org_model, sharded_model, data_gen_fn, output_transfo
|
||||
whisper = org_model
|
||||
sharded_whisper = sharded_model
|
||||
|
||||
# compare self attention grad
|
||||
org_grad = whisper.encoder.layers[0].self_attn.q_proj.weight.grad
|
||||
shard_grad = sharded_whisper.encoder.layers[0].self_attn.q_proj.weight.grad
|
||||
shard_weight = sharded_whisper.encoder.layers[0].self_attn.q_proj.weight
|
||||
|
||||
if is_distributed_tensor(shard_weight) or is_customized_distributed_tensor(shard_weight):
|
||||
shard_grad_list = [torch.zeros([*shard_grad.shape]).to('cuda') for _ in range(2)]
|
||||
shard_grad = torch.distributed.all_gather(shard_grad_list, shard_grad)
|
||||
all_shard_grad = torch.cat(shard_grad_list, dim=0)
|
||||
else:
|
||||
all_shard_grad = shard_grad
|
||||
assert torch.allclose(org_grad, all_shard_grad,
|
||||
atol=1e-5), f"shard model grad is not equal to orgin model grad\n{org_grad}\n{all_shard_grad}"
|
||||
|
||||
# WhisperForAudioClassification does not have decoder and embedding layer
|
||||
# check grad
|
||||
if org_model.__class__.__name__ == 'WhisperForAudioClassification':
|
||||
return
|
||||
|
||||
# compare embedding grad
|
||||
org_grad = whisper.decoder.embed_tokens.weight.grad
|
||||
shard_grad = sharded_whisper.decoder.embed_tokens.weight.grad
|
||||
shard_weight = sharded_whisper.decoder.embed_tokens.weight
|
||||
|
||||
if is_distributed_tensor(shard_weight) or is_customized_distributed_tensor(shard_weight):
|
||||
shard_grad_list = [torch.zeros([*shard_grad.shape]).to('cuda') for _ in range(2)]
|
||||
shard_grad = torch.distributed.all_gather(shard_grad_list, shard_grad)
|
||||
all_shard_grad = torch.cat(shard_grad_list, dim=0)
|
||||
col_layer_for_check = ['encoder.layers[0].self_attn.q_proj']
|
||||
row_layer_for_check = ['encoder.layers[0].self_attn.out_proj']
|
||||
else:
|
||||
all_shard_grad = shard_grad
|
||||
|
||||
assert torch.allclose(org_grad, all_shard_grad,
|
||||
atol=1e-5), f"shard model grad is not equal to orgin model grad\n{org_grad}\n{all_shard_grad}"
|
||||
col_layer_for_check = ['encoder.layers[0].self_attn.q_proj', 'decoder.layers[0].self_attn.q_proj']
|
||||
row_layer_for_check = ['encoder.layers[0].self_attn.out_proj', 'decoder.layers[0].self_attn.out_proj']
|
||||
check_grad(whisper, sharded_whisper, col_layer_for_check, atol=1e-6, rtol=1e-5, dim=0, verbose=False)
|
||||
check_grad(whisper, sharded_whisper, row_layer_for_check, atol=1e-6, rtol=1e-5, dim=1, verbose=False)
|
||||
|
||||
|
||||
@parameterize('enable_fused_normalization', [True, False])
|
||||
|
Reference in New Issue
Block a user