chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# DeepSpeed Team
|
||||
|
||||
import os
|
||||
import json
|
||||
import argparse
|
||||
import torch
|
||||
import deepspeed
|
||||
from torch.utils.data.distributed import DistributedSampler
|
||||
import deepspeed.comm as dist
|
||||
|
||||
|
||||
class SimpleModel(torch.nn.Module):
|
||||
|
||||
def __init__(self, hidden_dim, empty_grad=False):
|
||||
super(SimpleModel, self).__init__()
|
||||
self.linear = torch.nn.Linear(hidden_dim, hidden_dim)
|
||||
self.linear2 = torch.nn.Linear(hidden_dim, hidden_dim)
|
||||
self.linear3 = torch.nn.Linear(hidden_dim, hidden_dim)
|
||||
self.linear4 = torch.nn.Linear(hidden_dim, hidden_dim)
|
||||
if empty_grad:
|
||||
self.layers2 = torch.nn.ModuleList([torch.nn.Linear(hidden_dim, hidden_dim)])
|
||||
self.cross_entropy_loss = torch.nn.CrossEntropyLoss()
|
||||
|
||||
def forward(self, x, y):
|
||||
hidden = x
|
||||
hidden = self.linear(hidden)
|
||||
hidden = self.linear2(hidden)
|
||||
hidden = self.linear3(hidden)
|
||||
hidden = self.linear4(hidden)
|
||||
return self.cross_entropy_loss(hidden, y)
|
||||
|
||||
|
||||
def create_config_from_dict(tmpdir, config_dict):
|
||||
config_path = os.path.join(tmpdir, 'temp_config.json')
|
||||
with open(config_path, 'w') as fd:
|
||||
json.dump(config_dict, fd)
|
||||
return config_path
|
||||
|
||||
|
||||
def get_data_loader(model, total_samples, hidden_dim, device):
|
||||
batch_size = model.train_micro_batch_size_per_gpu()
|
||||
train_data = torch.randn(total_samples, hidden_dim, device=device, dtype=torch.half)
|
||||
train_label = torch.empty(total_samples, dtype=torch.long, device=device).random_(hidden_dim)
|
||||
train_dataset = torch.utils.data.TensorDataset(train_data, train_label)
|
||||
sampler = DistributedSampler(train_dataset)
|
||||
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, sampler=sampler)
|
||||
return train_loader
|
||||
|
||||
|
||||
def get_args(tmpdir, config_dict):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--local_rank", type=int, default=0)
|
||||
parser.add_argument('--zero', type=int, default=0)
|
||||
args = parser.parse_args() #args=''
|
||||
|
||||
config_dict["zero_optimization"]["stage"] = args.zero
|
||||
print('config_dict["zero_optimization"]', config_dict["zero_optimization"])
|
||||
config_path = create_config_from_dict(tmpdir, config_dict)
|
||||
|
||||
args.deepspeed_config = config_path
|
||||
return args
|
||||
|
||||
|
||||
def print0(msg):
|
||||
if dist.get_rank() == 0:
|
||||
print(msg, flush=True)
|
||||
|
||||
|
||||
rank = int(os.environ['RANK'])
|
||||
print('seed:', 2222 + rank)
|
||||
torch.random.manual_seed(2222 + rank)
|
||||
|
||||
config_dict = {
|
||||
"train_batch_size": 256,
|
||||
"steps_per_print": 1,
|
||||
"optimizer": {
|
||||
"type": "Adam",
|
||||
"params": {
|
||||
"lr": 0.00015,
|
||||
}
|
||||
},
|
||||
"fp16": {
|
||||
"enabled": True,
|
||||
"initial_scale_power": 15
|
||||
},
|
||||
"zero_optimization": {
|
||||
"stage": 0,
|
||||
"sub_group_size": 8,
|
||||
"reduce_bucket_size": 20,
|
||||
"offload_optimizer": {
|
||||
"device": "cpu",
|
||||
"pin_memory": True,
|
||||
"ratio": 0.3
|
||||
}
|
||||
}
|
||||
}
|
||||
# "initial_scale_power": 15
|
||||
args = get_args('/tmp/', config_dict)
|
||||
hidden_dim = 4 * 1024
|
||||
|
||||
model = SimpleModel(hidden_dim, empty_grad=False)
|
||||
|
||||
model, _, _, _ = deepspeed.initialize(args=args,
|
||||
model=model,
|
||||
model_parameters=model.parameters(),
|
||||
dist_init_required=True)
|
||||
|
||||
|
||||
def print_params(tag, model):
|
||||
if dist.get_rank() == 0:
|
||||
for n, p in model.named_parameters():
|
||||
print0("{} {}:{}".format(tag, n, p))
|
||||
|
||||
|
||||
data_loader = get_data_loader(model=model, total_samples=1000, hidden_dim=hidden_dim, device=model.device)
|
||||
#print_params('pre-train', model)
|
||||
#while True:
|
||||
for n, batch in enumerate(data_loader):
|
||||
loss = model(batch[0], batch[1])
|
||||
if dist.get_rank() == 0:
|
||||
print("LOSS:", loss.item())
|
||||
model.backward(loss)
|
||||
model.step()
|
||||
#print_params('step={}'.format(n), model)
|
||||
if n == 2: break
|
||||
@@ -0,0 +1,90 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# DeepSpeed Team
|
||||
|
||||
import torch
|
||||
|
||||
import deepspeed
|
||||
|
||||
###################################
|
||||
# Setup
|
||||
###################################
|
||||
|
||||
|
||||
class VerboseLinear(torch.nn.Linear):
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
print('Begin VerboseLinear.__init__')
|
||||
super().__init__(**kwargs)
|
||||
print('End VerboseLinear.__init__')
|
||||
|
||||
|
||||
class LinearStack(torch.nn.Module):
|
||||
|
||||
def __init__(self, input_dim=2, hidden_dim=4, output_dim=4, num_layers=2):
|
||||
super().__init__()
|
||||
self.input_dim = input_dim
|
||||
self.output_dim = output_dim
|
||||
self.hidden_dim = hidden_dim
|
||||
|
||||
self.input_layer = VerboseLinear(in_features=self.input_dim, out_features=self.hidden_dim)
|
||||
self.layers = torch.nn.ModuleList([
|
||||
torch.nn.Linear(in_features=self.hidden_dim, out_features=self.hidden_dim, bias=False)
|
||||
for x in range(num_layers)
|
||||
])
|
||||
self.output_layer = torch.nn.Linear(in_features=self.hidden_dim, out_features=self.output_dim)
|
||||
self.identity = torch.nn.Identity()
|
||||
|
||||
def forward(self, x):
|
||||
x = self.input_layer(x)
|
||||
for layer in self.layers:
|
||||
x = layer(x)
|
||||
x = self.output_layer(x)
|
||||
x = self.identity(x)
|
||||
return x
|
||||
|
||||
|
||||
###################################
|
||||
# DRIVER
|
||||
###################################
|
||||
|
||||
|
||||
def test_driver():
|
||||
print()
|
||||
print('BUILDING MODEL')
|
||||
with deepspeed.zero.Init():
|
||||
model = LinearStack()
|
||||
print()
|
||||
|
||||
# parted = [name for (name, p) in model.named_parameters() if p._partitioned]
|
||||
# not_parted = [name for (name, p) in model.named_parameters() if not p._partitioned]
|
||||
# print('partitioned: ', parted)
|
||||
# print('full: ', not_parted)
|
||||
# print()
|
||||
|
||||
model.train()
|
||||
|
||||
test_input = torch.rand(1, model.input_dim)
|
||||
grad_output = torch.rand(1, model.output_dim)
|
||||
|
||||
grad_output.requires_grad = False
|
||||
test_input.requires_grad = False
|
||||
|
||||
print()
|
||||
print('BEGINNING FORWARD')
|
||||
print()
|
||||
|
||||
output = model(test_input)
|
||||
output.backward(grad_output)
|
||||
|
||||
# parted = [name for (name, p) in model.named_parameters() if p._partitioned]
|
||||
# not_parted = [name for (name, p) in model.named_parameters() if not p._partitioned]
|
||||
# print('partitioned: ', parted)
|
||||
# print('full:' , not_parted)
|
||||
# print()
|
||||
|
||||
#samyamspeed.disable()
|
||||
|
||||
|
||||
test_driver()
|
||||
@@ -0,0 +1,52 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# DeepSpeed Team
|
||||
|
||||
import torch
|
||||
from deepspeed.pt.deepspeed_linear import LinearModuleForZeroStage3
|
||||
from deepspeed.pt.log_utils import logger
|
||||
from deepspeed.accelerator import get_accelerator
|
||||
|
||||
|
||||
def see_memory_usage(message):
|
||||
|
||||
# Print message except when distributed but not rank 0
|
||||
logger.info(message)
|
||||
logger.info(
|
||||
"Memory Allocated %s GigaBytes ",
|
||||
get_accelerator().memory_allocated() / (1024 * 1024 * 1024),
|
||||
)
|
||||
logger.info(
|
||||
"Max Memory Allocated %s GigaBytes",
|
||||
get_accelerator().max_memory_allocated() / (1024 * 1024 * 1024),
|
||||
)
|
||||
logger.info(
|
||||
"Cache Allocated %s GigaBytes",
|
||||
get_accelerator().memory_cached() / (1024 * 1024 * 1024),
|
||||
)
|
||||
logger.info(
|
||||
"Max cache Allocated %s GigaBytes",
|
||||
get_accelerator().max_memory_cached() / (1024 * 1024 * 1024),
|
||||
)
|
||||
|
||||
|
||||
tens = torch.rand(1024, 16384, dtype=torch.half, device=torch.device(get_accelerator().device_name()))
|
||||
tens_back = tens.detach().clone()
|
||||
|
||||
#linear_bk = torch.nn.functional.linear
|
||||
#torch.nn.functional.linear = deepspeed.pt.deepspeed_linear.LinearFunctionForZeroStage3.apply
|
||||
model = LinearModuleForZeroStage3(16384, 16384)
|
||||
|
||||
model.to(get_accelerator().device_name()).half()
|
||||
|
||||
see_memory_usage("Before forward")
|
||||
y = model(tens)
|
||||
|
||||
see_memory_usage("After forward")
|
||||
|
||||
model.weight.data = torch.zeros(1, dtype=torch.half, device=torch.device(get_accelerator().device_name()))
|
||||
|
||||
see_memory_usage("After weight zero")
|
||||
|
||||
y.backward(tens_back)
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
deepspeed test_mics_config.py --mics_shard_size=1
|
||||
|
||||
deepspeed test_mics_config.py --mics_shard_size=2
|
||||
|
||||
# for debugging the hierarchical params gathering
|
||||
export NDEV_PER_NODE=2
|
||||
deepspeed test_mics_config.py --mics_shard_size=4 --mics_hierarchical_params_gather
|
||||
@@ -0,0 +1,133 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# DeepSpeed Team
|
||||
|
||||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
"""
|
||||
Testing on a 8 GPUs node
|
||||
NDEV_PER_NODE=2 torchrun --nnodes 1 --nproc-per-node 8 test_mics_config.py
|
||||
"""
|
||||
|
||||
import os
|
||||
import json
|
||||
import argparse
|
||||
import torch
|
||||
import deepspeed
|
||||
from torch.utils.data.distributed import DistributedSampler
|
||||
import deepspeed.comm as dist
|
||||
|
||||
|
||||
class SimpleModel(torch.nn.Module):
|
||||
|
||||
def __init__(self, hidden_dim, empty_grad=False):
|
||||
super(SimpleModel, self).__init__()
|
||||
self.linear = torch.nn.Linear(hidden_dim, hidden_dim)
|
||||
if empty_grad:
|
||||
self.layers2 = torch.nn.ModuleList([torch.nn.Linear(hidden_dim, hidden_dim)])
|
||||
self.cross_entropy_loss = torch.nn.CrossEntropyLoss()
|
||||
|
||||
def forward(self, x, y):
|
||||
hidden = x
|
||||
hidden = self.linear(hidden)
|
||||
return self.cross_entropy_loss(hidden, y)
|
||||
|
||||
|
||||
def create_config_from_dict(tmpdir, config_dict):
|
||||
config_path = os.path.join(tmpdir, 'temp_config.json')
|
||||
with open(config_path, 'w') as fd:
|
||||
json.dump(config_dict, fd)
|
||||
return config_path
|
||||
|
||||
|
||||
def get_data_loader(model, total_samples, hidden_dim, device):
|
||||
batch_size = model.train_micro_batch_size_per_gpu()
|
||||
train_data = torch.randn(total_samples, hidden_dim, device=device, dtype=torch.float)
|
||||
train_label = torch.empty(total_samples, dtype=torch.long, device=device).random_(hidden_dim)
|
||||
train_dataset = torch.utils.data.TensorDataset(train_data, train_label)
|
||||
sampler = DistributedSampler(train_dataset)
|
||||
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, sampler=sampler)
|
||||
return train_loader
|
||||
|
||||
|
||||
def get_args(tmpdir, config_dict):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--zero', type=int, default=3)
|
||||
parser.add_argument('--local_rank', type=int)
|
||||
|
||||
parser.add_argument('--mics_shard_size', default=2, type=int)
|
||||
parser.add_argument('--mics_hierarchical_params_gather', default=False, action='store_true')
|
||||
args = parser.parse_args() #args=''
|
||||
|
||||
config_dict["zero_optimization"]["stage"] = args.zero
|
||||
config_dict["zero_optimization"]["mics_shard_size"] = args.mics_shard_size
|
||||
config_dict["zero_optimization"]["mics_hierarchical_params_gather"] = args.mics_hierarchical_params_gather
|
||||
|
||||
# print('config_dict["zero_optimization"]', config_dict["zero_optimization"])
|
||||
config_path = create_config_from_dict(tmpdir, config_dict)
|
||||
|
||||
args.deepspeed_config = config_path
|
||||
return args
|
||||
|
||||
|
||||
def print0(msg):
|
||||
if dist.get_rank() == 0:
|
||||
print(msg, flush=True)
|
||||
|
||||
|
||||
rank = int(os.environ['RANK'])
|
||||
print('seed:', 2222 + rank)
|
||||
torch.random.manual_seed(2222 + rank)
|
||||
|
||||
config_dict = {
|
||||
"train_batch_size": 8,
|
||||
"steps_per_print": 1,
|
||||
"optimizer": {
|
||||
"type": "Adam",
|
||||
"params": {
|
||||
"lr": 0.00015,
|
||||
}
|
||||
},
|
||||
"fp16": {
|
||||
"enabled": False,
|
||||
"initial_scale_power": 15
|
||||
},
|
||||
"zero_optimization": {
|
||||
"stage": 3,
|
||||
"reduce_bucket_size": 20,
|
||||
"mics_shard_size": 4,
|
||||
"mics_hierarchical_params_gather": True,
|
||||
"stage3_model_persistence_threshold": 10
|
||||
}
|
||||
}
|
||||
# "initial_scale_power": 15
|
||||
args = get_args('/tmp/', config_dict)
|
||||
hidden_dim = 32
|
||||
|
||||
with deepspeed.zero.MiCS_Init(config_dict_or_path=config_dict):
|
||||
model = SimpleModel(hidden_dim, empty_grad=False)
|
||||
# print('------> init model with deepspeed.zero.Init()')
|
||||
|
||||
model, _, _, _ = deepspeed.initialize(args=args,
|
||||
model=model,
|
||||
model_parameters=model.parameters(),
|
||||
dist_init_required=True)
|
||||
|
||||
|
||||
def print_params(tag, model):
|
||||
if dist.get_rank() == 0:
|
||||
for n, p in model.named_parameters():
|
||||
print0("{} {}:{}".format(tag, n, p))
|
||||
|
||||
|
||||
data_loader = get_data_loader(model=model, total_samples=1000, hidden_dim=hidden_dim, device=model.device)
|
||||
#print_params('pre-train', model)
|
||||
for n, batch in enumerate(data_loader):
|
||||
loss = model(batch[0], batch[1])
|
||||
if dist.get_rank() == 0:
|
||||
print("LOSS:", loss.item())
|
||||
model.backward(loss)
|
||||
model.step()
|
||||
#print_params('step={}'.format(n), model)
|
||||
if n == 5: break
|
||||
Executable
+125
@@ -0,0 +1,125 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# DeepSpeed Team
|
||||
|
||||
import os
|
||||
import json
|
||||
import argparse
|
||||
import torch
|
||||
import deepspeed
|
||||
from torch.utils.data.distributed import DistributedSampler
|
||||
import deepspeed.comm as dist
|
||||
|
||||
|
||||
class SimpleModel(torch.nn.Module):
|
||||
|
||||
def __init__(self, hidden_dim, empty_grad=False):
|
||||
super(SimpleModel, self).__init__()
|
||||
self.linear = torch.nn.Linear(hidden_dim, hidden_dim, bias=True)
|
||||
self.linear = torch.nn.Linear(hidden_dim, hidden_dim, bias=False)
|
||||
if empty_grad:
|
||||
self.layers2 = torch.nn.ModuleList([torch.nn.Linear(hidden_dim,
|
||||
hidden_dim)]) #QuantizeLinear(hidden_dim, hidden_dim)
|
||||
self.cross_entropy_loss = torch.nn.CrossEntropyLoss()
|
||||
|
||||
def forward(self, x, y):
|
||||
hidden = x
|
||||
hidden1 = self.linear(hidden)
|
||||
hidden2 = self.linear(hidden1)
|
||||
return self.cross_entropy_loss(hidden2, y)
|
||||
|
||||
|
||||
def create_config_from_dict(tmpdir, config_dict):
|
||||
config_path = os.path.join(tmpdir, 'temp_config.json')
|
||||
with open(config_path, 'w') as fd:
|
||||
json.dump(config_dict, fd)
|
||||
return config_path
|
||||
|
||||
|
||||
def get_data_loader(model, total_samples, hidden_dim, device):
|
||||
batch_size = model.train_micro_batch_size_per_gpu()
|
||||
train_data = torch.randn(total_samples, hidden_dim, device=device, dtype=torch.half)
|
||||
train_label = torch.empty(total_samples, dtype=torch.long, device=device).random_(hidden_dim)
|
||||
train_dataset = torch.utils.data.TensorDataset(train_data, train_label)
|
||||
sampler = DistributedSampler(train_dataset)
|
||||
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, sampler=sampler)
|
||||
return train_loader
|
||||
|
||||
|
||||
def get_args(tmpdir, config_dict):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--local_rank", type=int, default=0)
|
||||
parser.add_argument('--zero', type=int, default=0)
|
||||
parser.add_argument('--zero_hpz_partition_size', type=int, default=1)
|
||||
args = parser.parse_args() #args=''
|
||||
|
||||
config_dict["zero_optimization"]["stage"] = args.zero
|
||||
config_dict["zero_optimization"]["zero_hpz_partition_size"] = args.zero_hpz_partition_size
|
||||
print('config_dict["zero_optimization"]', config_dict["zero_optimization"])
|
||||
config_path = create_config_from_dict(tmpdir, config_dict)
|
||||
|
||||
args.deepspeed_config = config_path
|
||||
return args
|
||||
|
||||
|
||||
def print0(msg):
|
||||
if dist.get_rank() == 0:
|
||||
print(msg, flush=True)
|
||||
|
||||
|
||||
rank = int(os.environ['RANK'])
|
||||
print('seed:', 2222 + rank)
|
||||
torch.random.manual_seed(2222 + rank)
|
||||
|
||||
config_dict = {
|
||||
"train_batch_size": 256,
|
||||
"steps_per_print": 1,
|
||||
"optimizer": {
|
||||
"type": "Adam",
|
||||
"params": {
|
||||
"lr": 0.00015,
|
||||
}
|
||||
},
|
||||
"fp16": {
|
||||
"enabled": True,
|
||||
"initial_scale_power": 8
|
||||
},
|
||||
"zero_optimization": {
|
||||
"stage": 0,
|
||||
"reduce_bucket_size": 20,
|
||||
"zero_hpz_partition_size": 1,
|
||||
"reduce_scatter": True,
|
||||
"zero_quantized_weights": False,
|
||||
"zero_quantized_gradients": False
|
||||
}
|
||||
}
|
||||
# "initial_scale_power": 15
|
||||
args = get_args('/tmp/', config_dict)
|
||||
hidden_dim = 4 * 1024
|
||||
|
||||
model = SimpleModel(hidden_dim, empty_grad=False)
|
||||
|
||||
model, _, _, _ = deepspeed.initialize(args=args,
|
||||
model=model,
|
||||
model_parameters=model.parameters(),
|
||||
dist_init_required=True)
|
||||
|
||||
|
||||
def print_params(tag, model):
|
||||
if dist.get_rank() == 0:
|
||||
for n, p in model.named_parameters():
|
||||
print0("{} {}:{}".format(tag, n, p))
|
||||
|
||||
|
||||
data_loader = get_data_loader(model=model, total_samples=256, hidden_dim=hidden_dim, device=model.device)
|
||||
#print_params('pre-train', model)
|
||||
|
||||
for n, batch in enumerate(data_loader):
|
||||
loss = model(batch[0], batch[1])
|
||||
if dist.get_rank() == 0:
|
||||
print("LOSS:", loss.item())
|
||||
model.backward(loss)
|
||||
model.step()
|
||||
#print_params('step={}'.format(n), model)
|
||||
#if n == 5: break
|
||||
Reference in New Issue
Block a user