2022-04-24 08:43:44 +00:00
|
|
|
import torch
|
|
|
|
import torch.nn as nn
|
2022-11-29 05:42:06 +00:00
|
|
|
|
2023-09-11 08:24:28 +00:00
|
|
|
from colossalai.legacy.nn import CheckpointModule
|
2022-04-24 10:30:20 +00:00
|
|
|
from colossalai.utils.cuda import get_current_device
|
2022-04-24 08:43:44 +00:00
|
|
|
|
2022-11-29 05:42:06 +00:00
|
|
|
from .registry import non_distributed_component_funcs
|
|
|
|
from .utils.dummy_data_generator import DummyDataGenerator
|
|
|
|
|
|
|
|
|
2022-04-24 08:43:44 +00:00
|
|
|
class SimpleNet(CheckpointModule):
|
|
|
|
"""
|
|
|
|
In this no-leaf module, it has subordinate nn.modules and a nn.Parameter.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, checkpoint=False) -> None:
|
|
|
|
super().__init__(checkpoint=checkpoint)
|
2022-04-29 06:10:05 +00:00
|
|
|
self.embed = nn.Embedding(20, 4)
|
2022-04-24 08:43:44 +00:00
|
|
|
self.proj1 = nn.Linear(4, 8)
|
2022-04-25 08:01:52 +00:00
|
|
|
self.ln1 = nn.LayerNorm(8)
|
2022-04-24 08:43:44 +00:00
|
|
|
self.proj2 = nn.Linear(8, 4)
|
2022-04-25 08:01:52 +00:00
|
|
|
self.ln2 = nn.LayerNorm(4)
|
2022-05-07 07:49:14 +00:00
|
|
|
self.classifier = nn.Linear(4, 4)
|
2022-04-24 08:43:44 +00:00
|
|
|
|
|
|
|
def forward(self, x):
|
2022-04-29 06:10:05 +00:00
|
|
|
x = self.embed(x)
|
2022-04-24 08:43:44 +00:00
|
|
|
x = self.proj1(x)
|
2022-04-25 08:01:52 +00:00
|
|
|
x = self.ln1(x)
|
2022-04-24 08:43:44 +00:00
|
|
|
x = self.proj2(x)
|
2022-04-25 08:01:52 +00:00
|
|
|
x = self.ln2(x)
|
2022-05-07 07:49:14 +00:00
|
|
|
x = self.classifier(x)
|
2022-04-24 08:43:44 +00:00
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
class DummyDataLoader(DummyDataGenerator):
|
|
|
|
def generate(self):
|
2022-05-07 07:49:14 +00:00
|
|
|
data = torch.randint(low=0, high=20, size=(16,), device=get_current_device())
|
|
|
|
label = torch.randint(low=0, high=2, size=(16,), device=get_current_device())
|
2022-04-24 08:43:44 +00:00
|
|
|
return data, label
|
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
@non_distributed_component_funcs.register(name="simple_net")
|
2022-04-24 08:43:44 +00:00
|
|
|
def get_training_components():
|
2022-11-29 05:42:06 +00:00
|
|
|
def model_builder(checkpoint=False):
|
2022-04-24 08:43:44 +00:00
|
|
|
return SimpleNet(checkpoint)
|
|
|
|
|
|
|
|
trainloader = DummyDataLoader()
|
|
|
|
testloader = DummyDataLoader()
|
|
|
|
|
|
|
|
criterion = torch.nn.CrossEntropyLoss()
|
|
|
|
from colossalai.nn.optimizer import HybridAdam
|
2023-09-19 06:20:26 +00:00
|
|
|
|
2022-04-24 08:43:44 +00:00
|
|
|
return model_builder, trainloader, testloader, HybridAdam, criterion
|