mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-06-29 00:38:02 +00:00
[test] add shard util tests
This commit is contained in:
parent
2d6cc07feb
commit
5fc60a3a04
27
tests/test_shardformer/test_shard_utils.py
Normal file
27
tests/test_shardformer/test_shard_utils.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
|
||||||
|
from colossalai.shardformer.shard.utils import set_tensors_to_none
|
||||||
|
|
||||||
|
|
||||||
|
class Net(nn.Module):
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.layers = nn.Sequential(nn.Linear(1, 2), nn.Linear(2, 3))
|
||||||
|
self.out = nn.Linear(3, 1)
|
||||||
|
|
||||||
|
|
||||||
|
def test_release_layer():
|
||||||
|
orig_cuda_allocated = torch.cuda.memory_allocated()
|
||||||
|
model = Net().cuda()
|
||||||
|
set_tensors_to_none(model, exclude={model.layers[0]})
|
||||||
|
assert model.layers[1].weight is None
|
||||||
|
assert model.layers[1].bias is None
|
||||||
|
assert model.out.weight is None
|
||||||
|
assert model.out.bias is None
|
||||||
|
set_tensors_to_none(model)
|
||||||
|
assert model.layers[0].weight is None
|
||||||
|
assert model.layers[0].bias is None
|
||||||
|
assert len(list(model.parameters())) == 0
|
||||||
|
assert torch.cuda.memory_allocated() == orig_cuda_allocated
|
Loading…
Reference in New Issue
Block a user