mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-10-28 20:30:42 +00:00
[legacy] clean up legacy code (#4743)
* [legacy] remove outdated codes of pipeline (#4692) * [legacy] remove cli of benchmark and update optim (#4690) * [legacy] remove cli of benchmark and update optim * [doc] fix cli doc test * [legacy] fix engine clip grad norm * [legacy] remove outdated colo tensor (#4694) * [legacy] remove outdated colo tensor * [test] fix test import * [legacy] move outdated zero to legacy (#4696) * [legacy] clean up utils (#4700) * [legacy] clean up utils * [example] update examples * [legacy] clean up amp * [legacy] fix amp module * [legacy] clean up gpc (#4742) * [legacy] clean up context * [legacy] clean core, constants and global vars * [legacy] refactor initialize * [example] fix examples ci * [example] fix examples ci * [legacy] fix tests * [example] fix gpt example * [example] fix examples ci * [devops] fix ci installation * [example] fix examples ci
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
from colossalai.context.parallel_context import ParallelContext
|
||||
from colossalai.core import global_context as gpc
|
||||
from colossalai.logging import get_dist_logger
|
||||
from colossalai.context import ParallelMode
|
||||
from .datasets.data_samplers import build_pretraining_data_loader
|
||||
from .datasets.builder import build_train_valid_test_datasets
|
||||
import torch
|
||||
|
||||
from colossalai.legacy.context import ParallelMode
|
||||
from colossalai.legacy.context.parallel_context import ParallelContext
|
||||
from colossalai.legacy.core import global_context as gpc
|
||||
from colossalai.logging import get_dist_logger
|
||||
|
||||
from .datasets.builder import build_train_valid_test_datasets
|
||||
from .datasets.data_samplers import build_pretraining_data_loader
|
||||
|
||||
|
||||
def cyclic_iter(iter):
|
||||
while True:
|
||||
@@ -18,8 +20,7 @@ def build_train_valid_test_data_iterators(train_iters,
|
||||
eval_interval,
|
||||
eval_iters,
|
||||
dataloader_type='single',
|
||||
**kwargs
|
||||
):
|
||||
**kwargs):
|
||||
(train_dataloader, valid_dataloader, test_dataloader) = (None, None, None)
|
||||
|
||||
logger = get_dist_logger()
|
||||
@@ -42,9 +43,7 @@ def build_train_valid_test_data_iterators(train_iters,
|
||||
train_samples = train_iters * global_batch_size
|
||||
eval_iters_ = (train_iters // eval_interval + 1) * eval_iters
|
||||
test_iters = eval_iters
|
||||
train_val_test_num_samples = [train_samples,
|
||||
eval_iters_ * global_batch_size,
|
||||
test_iters * global_batch_size]
|
||||
train_val_test_num_samples = [train_samples, eval_iters_ * global_batch_size, test_iters * global_batch_size]
|
||||
logger.info(' > datasets target sizes (minimum size):')
|
||||
logger.info(' train: {}'.format(train_val_test_num_samples[0]), ranks=[0])
|
||||
logger.info(' validation: {}'.format(train_val_test_num_samples[1]), ranks=[0])
|
||||
@@ -56,19 +55,20 @@ def build_train_valid_test_data_iterators(train_iters,
|
||||
|
||||
# Build dataloaders.
|
||||
dp_size = gpc.get_world_size(ParallelMode.DATA)
|
||||
train_dataloader = build_pretraining_data_loader(
|
||||
train_ds, consumed_samples=0, micro_batch_size=global_batch_size//dp_size)
|
||||
valid_dataloader = build_pretraining_data_loader(
|
||||
valid_ds, consumed_samples=0, micro_batch_size=global_batch_size//dp_size)
|
||||
test_dataloader = build_pretraining_data_loader(test_ds, 0, micro_batch_size=global_batch_size//dp_size)
|
||||
train_dataloader = build_pretraining_data_loader(train_ds,
|
||||
consumed_samples=0,
|
||||
micro_batch_size=global_batch_size // dp_size)
|
||||
valid_dataloader = build_pretraining_data_loader(valid_ds,
|
||||
consumed_samples=0,
|
||||
micro_batch_size=global_batch_size // dp_size)
|
||||
test_dataloader = build_pretraining_data_loader(test_ds, 0, micro_batch_size=global_batch_size // dp_size)
|
||||
|
||||
# Flags to know if we need to do training/validation/testing.
|
||||
do_train = train_dataloader is not None and train_iters > 0
|
||||
do_valid = valid_dataloader is not None and eval_iters > 0
|
||||
do_test = test_dataloader is not None and eval_iters > 0
|
||||
# Need to broadcast num_tokens and num_type_tokens.
|
||||
flags = torch.cuda.LongTensor(
|
||||
[int(do_train), int(do_valid), int(do_test)])
|
||||
flags = torch.cuda.LongTensor([int(do_train), int(do_valid), int(do_test)])
|
||||
else:
|
||||
flags = torch.cuda.LongTensor([0, 0, 0])
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
from colossalai.core import global_context as gpc
|
||||
from colossalai.context import ParallelMode
|
||||
import torch
|
||||
|
||||
from colossalai.legacy.context import ParallelMode
|
||||
from colossalai.legacy.core import global_context as gpc
|
||||
|
||||
_MAX_DATA_DIM = 5
|
||||
|
||||
|
||||
@@ -22,7 +23,8 @@ def _build_key_size_numel_dictionaries(keys, data):
|
||||
|
||||
# Move to GPU and broadcast.
|
||||
sizes_cuda = torch.cuda.LongTensor(sizes)
|
||||
torch.distributed.broadcast(sizes_cuda, gpc.get_ranks_in_group(ParallelMode.TENSOR)[0],
|
||||
torch.distributed.broadcast(sizes_cuda,
|
||||
gpc.get_ranks_in_group(ParallelMode.TENSOR)[0],
|
||||
group=gpc.get_group(ParallelMode.TENSOR))
|
||||
|
||||
# Move back to cpu and unpack.
|
||||
@@ -60,19 +62,15 @@ def broadcast_data(keys, data, datatype):
|
||||
"""
|
||||
# Build (key, size) and (key, number of elements) dictionaries along
|
||||
# with the total number of elements on all ranks.
|
||||
key_size, key_numel, total_numel = _build_key_size_numel_dictionaries(keys,
|
||||
data)
|
||||
key_size, key_numel, total_numel = _build_key_size_numel_dictionaries(keys, data)
|
||||
|
||||
# Pack on rank zero.
|
||||
if not gpc.is_initialized(ParallelMode.TENSOR) or gpc.get_local_rank(ParallelMode.TENSOR) == 0:
|
||||
# Check that all keys have the same data type.
|
||||
# Flatten the data associated with the keys
|
||||
flatten_data = torch.cat(
|
||||
[data[key].contiguous().view(-1) for key in keys], dim=0).cuda()
|
||||
flatten_data = torch.cat([data[key].contiguous().view(-1) for key in keys], dim=0).cuda()
|
||||
else:
|
||||
flatten_data = torch.empty(total_numel,
|
||||
device=torch.cuda.current_device(),
|
||||
dtype=datatype)
|
||||
flatten_data = torch.empty(total_numel, device=torch.cuda.current_device(), dtype=datatype)
|
||||
|
||||
# Broadcast
|
||||
torch.distributed.broadcast(flatten_data,
|
||||
@@ -139,7 +137,7 @@ def get_batch_for_sequence_parallel(data_iterator):
|
||||
seq_length = data_b['text'].size(1)
|
||||
sub_seq_length = seq_length // local_world_size
|
||||
sub_seq_start = local_rank * sub_seq_length
|
||||
sub_seq_end = (local_rank+1) * sub_seq_length
|
||||
sub_seq_end = (local_rank + 1) * sub_seq_length
|
||||
#
|
||||
# # Unpack.
|
||||
tokens = data_b['text'][:, sub_seq_start:sub_seq_end].long()
|
||||
@@ -156,10 +154,9 @@ class SequenceParallelDataIterator:
|
||||
|
||||
def __init__(self, data_iter):
|
||||
self.data_iter = data_iter
|
||||
|
||||
|
||||
def __iter__(self):
|
||||
return self.data_iter
|
||||
|
||||
def __next__(self):
|
||||
return get_batch_for_sequence_parallel(self.data_iter)
|
||||
return get_batch_for_sequence_parallel(self.data_iter)
|
||||
|
||||
@@ -21,8 +21,8 @@ import numpy as np
|
||||
import torch
|
||||
from torch.utils.data import Dataset
|
||||
|
||||
from colossalai.context import ParallelMode
|
||||
from colossalai.core import global_context as gpc
|
||||
from colossalai.legacy.context import ParallelMode
|
||||
from colossalai.legacy.core import global_context as gpc
|
||||
from colossalai.logging import get_dist_logger
|
||||
|
||||
from ..tokenizer import get_tokenizer
|
||||
|
||||
@@ -14,10 +14,12 @@
|
||||
# limitations under the License.
|
||||
"""Dataloaders."""
|
||||
|
||||
import torch
|
||||
import random
|
||||
from colossalai.core import global_context as gpc
|
||||
from colossalai.context import ParallelMode
|
||||
|
||||
import torch
|
||||
|
||||
from colossalai.legacy.context import ParallelMode
|
||||
from colossalai.legacy.core import global_context as gpc
|
||||
|
||||
|
||||
def build_pretraining_data_loader(dataset, consumed_samples, micro_batch_size, dataloader_type='single', num_workers=0):
|
||||
|
||||
@@ -12,13 +12,12 @@
|
||||
# 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.
|
||||
|
||||
"""Megatron tokenizers."""
|
||||
|
||||
from abc import ABC
|
||||
from abc import abstractmethod
|
||||
from colossalai.core import global_context as gpc
|
||||
from colossalai.context import ParallelMode
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from colossalai.legacy.context import ParallelMode
|
||||
from colossalai.legacy.core import global_context as gpc
|
||||
|
||||
from .bert_tokenization import FullTokenizer as FullBertTokenizer
|
||||
|
||||
@@ -26,18 +25,13 @@ from .bert_tokenization import FullTokenizer as FullBertTokenizer
|
||||
def build_tokenizer(vocab_file, tokenizer_type, vocab_extra_ids=0):
|
||||
"""Initialize tokenizer."""
|
||||
if not gpc.is_initialized(ParallelMode.GLOBAL) or gpc.get_global_rank() == 0:
|
||||
print('> building {} tokenizer ...'.format(tokenizer_type),
|
||||
flush=True)
|
||||
print('> building {} tokenizer ...'.format(tokenizer_type), flush=True)
|
||||
|
||||
# Select and instantiate the tokenizer.
|
||||
if tokenizer_type == 'BertWordPieceLowerCase':
|
||||
tokenizer = _BertWordPieceTokenizer(vocab_file=vocab_file,
|
||||
lower_case=True,
|
||||
vocab_extra_ids=vocab_extra_ids)
|
||||
tokenizer = _BertWordPieceTokenizer(vocab_file=vocab_file, lower_case=True, vocab_extra_ids=vocab_extra_ids)
|
||||
elif tokenizer_type == 'BertWordPieceCase':
|
||||
tokenizer = _BertWordPieceTokenizer(vocab_file=vocab_file,
|
||||
lower_case=False,
|
||||
vocab_extra_ids=vocab_extra_ids)
|
||||
tokenizer = _BertWordPieceTokenizer(vocab_file=vocab_file, lower_case=False, vocab_extra_ids=vocab_extra_ids)
|
||||
else:
|
||||
raise NotImplementedError('{} tokenizer is not '
|
||||
'implemented.'.format(tokenizer_type))
|
||||
@@ -62,8 +56,8 @@ def _vocab_size_with_padding(orig_vocab_size, make_vocab_size_divisible_by=128):
|
||||
after += 1
|
||||
if not gpc.is_initialized(ParallelMode.GLOBAL) or gpc.get_global_rank() == 0:
|
||||
print(' > padded vocab (size: {}) with {} dummy tokens '
|
||||
'(new size: {})'.format(
|
||||
orig_vocab_size, after - orig_vocab_size, after), flush=True)
|
||||
'(new size: {})'.format(orig_vocab_size, after - orig_vocab_size, after),
|
||||
flush=True)
|
||||
return after
|
||||
|
||||
|
||||
@@ -142,8 +136,7 @@ class _BertWordPieceTokenizer(AbstractTokenizer):
|
||||
self._additional_special_tokens = []
|
||||
|
||||
# (dsachan) Add BOS and EOS tokens
|
||||
SPECIAL_TOKENS = {'eos_token': '[EOS]',
|
||||
'bos_token': '[BOS]'}
|
||||
SPECIAL_TOKENS = {'eos_token': '[EOS]', 'bos_token': '[BOS]'}
|
||||
self._bos_token = '[BOS]'
|
||||
self.add_token(self._bos_token)
|
||||
self._bos_token_id = self.vocab.get(self._bos_token)
|
||||
@@ -155,8 +148,7 @@ class _BertWordPieceTokenizer(AbstractTokenizer):
|
||||
# (dsachan) Add additional special tokens
|
||||
# These can be used as sentinel tokens in T5 model inputs
|
||||
additional_special_tokens = []
|
||||
additional_special_tokens.extend(
|
||||
["<extra_id_{}>".format(i) for i in range(vocab_extra_ids)])
|
||||
additional_special_tokens.extend(["<extra_id_{}>".format(i) for i in range(vocab_extra_ids)])
|
||||
self.add_additional_special_tokens(additional_special_tokens)
|
||||
|
||||
def add_token(self, token):
|
||||
|
||||
Reference in New Issue
Block a user