Skip to content
Merged

L #11

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
cde3c6d
Merge pull request #3 from jamesthesnake/co
jamesthesnake Mar 23, 2023
bc7663e
Merge pull request #6 from jamesthesnake/co
jamesthesnake Mar 28, 2023
9fd12a8
Merge pull request #9 from jamesthesnake/co
jamesthesnake Apr 2, 2023
3041286
[chatgpt] add pre-trained model RoBERTa for RLHF stage 2 & 3 (#3223)
Camille7777 Apr 3, 2023
638a07a
[test] fixed gemini plugin test (#3411)
FrankLeeeee Apr 3, 2023
b09adff
[chat]fix sft training for bloom, gpt and opt (#3418)
chengeharrison Apr 4, 2023
26b7aac
[zero] reorganize zero/gemini folder structure (#3424)
ver217 Apr 4, 2023
1beb85c
[checkpoint] refactored the API and added safetensors support (#3427)
FrankLeeeee Apr 4, 2023
773955a
fix save_model inin naive and ddp strategy (#3436)
chengeharrison Apr 4, 2023
573af84
[example] update examples related to zero/gemini (#3431)
ver217 Apr 4, 2023
ffcdbf0
[autoparallel]integrate auto parallel feature with new tracer (#3408)
YuliangLiu0306 Apr 4, 2023
b923139
fix save_model indent error in ppo trainer (#3450)
chengeharrison Apr 5, 2023
46c009d
[format] Run lint on colossalai.engine (#3367)
nijkah Apr 5, 2023
72cb4dd
[Chat] fix the tokenizer "int too big to convert" error in SFT traini…
Camille7777 Apr 6, 2023
933048a
[test] reorganize zero/gemini tests (#3445)
ver217 Apr 6, 2023
8f740de
Fix typo (#3448)
yhna940 Apr 6, 2023
7d8d825
[booster] fixed the torch ddp plugin with the new checkpoint api (#3442)
FrankLeeeee Apr 6, 2023
57a3c4d
[chat]fix readme (#3429)
kingkingofall Apr 6, 2023
73afb63
[chat]fix save_model(#3377)
Dr-Corgi Apr 6, 2023
62f4e2e
[Chat]Add Peft support & fix the ptx bug (#3433)
yynil Apr 6, 2023
80eba05
[test] refactor tests with spawn (#3452)
FrankLeeeee Apr 6, 2023
6afeb12
add community example dictionary (#3465)
Fazziekey Apr 6, 2023
52a933e
[checkpoint] support huggingface style sharded checkpoint (#3461)
flybird11111 Apr 6, 2023
4e99893
[doc] updated contributor list (#3474)
FrankLeeeee Apr 6, 2023
c701b77
[dreambooth] fixing the incompatibity in requirements.txt (#3190) (#3…
NatalieC323 Apr 6, 2023
891b8e7
[chat] fix stage3 PPO sample sh command (#3477)
binmakeswell Apr 6, 2023
fb8fae6
Revert "[dreambooth] fixing the incompatibity in requirements.txt (#3…
NatalieC323 Apr 6, 2023
ab5fd12
[example] update roberta with newer ColossalAI (#3472)
mandoxzhang Apr 7, 2023
8f2c55f
[example] remove redundant texts & update roberta (#3493)
mandoxzhang Apr 7, 2023
a7ca297
[coati] Fix LlamaCritic (#3475)
Apr 7, 2023
634f32f
Merge pull request #10 from hpcaitech/main
jamesthesnake Apr 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
19 changes: 9 additions & 10 deletions .github/workflows/build_on_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ jobs:
detect:
name: Detect file change
if: |
github.event.pull_request.draft == false &&
github.base_ref == 'main' &&
github.event.pull_request.base.repo.full_name == 'hpcaitech/ColossalAI' &&
contains( github.event.pull_request.labels.*.name, 'Run Build and Test')
github.event.pull_request.draft == false &&
github.base_ref == 'main' &&
github.event.pull_request.base.repo.full_name == 'hpcaitech/ColossalAI' &&
contains( github.event.pull_request.labels.*.name, 'Run Build and Test')
outputs:
changedExtenisonFiles: ${{ steps.find-extension-change.outputs.all_changed_files }}
anyExtensionFileChanged: ${{ steps.find-extension-change.outputs.any_changed }}
Expand All @@ -27,10 +27,10 @@ jobs:
- name: Locate base commit
id: locate-base-sha
run: |
curBranch=$(git rev-parse --abbrev-ref HEAD)
commonCommit=$(git merge-base origin/main $curBranch)
echo $commonCommit
echo "baseSHA=$commonCommit" >> $GITHUB_OUTPUT
curBranch=$(git rev-parse --abbrev-ref HEAD)
commonCommit=$(git merge-base origin/main $curBranch)
echo $commonCommit
echo "baseSHA=$commonCommit" >> $GITHUB_OUTPUT

- name: Find the changed extension-related files
id: find-extension-change
Expand Down Expand Up @@ -63,7 +63,6 @@ jobs:
echo "$file was changed"
done


build:
name: Build and Test Colossal-AI
needs: detect
Expand Down Expand Up @@ -124,7 +123,7 @@ jobs:
- name: Execute Unit Testing
if: needs.detect.outputs.anyLibraryFileChanged == 'true'
run: |
PYTHONPATH=$PWD pytest --cov=. --cov-report xml tests/
CURL_CA_BUNDLE="" PYTHONPATH=$PWD pytest --cov=. --cov-report xml tests/
env:
DATA: /data/scratch/cifar-10
NCCL_SHM_DISABLE: 1
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,10 @@ You may contact us or participate in the following ways:

Thanks so much to all of our amazing contributors!

<a href="https://github.com/hpcaitech/ColossalAI/graphs/contributors"><img src="https://raw.githubusercontent.com/hpcaitech/public_assets/main/colossalai/img/contributor_avatar.png" width="800px"></a>
<a href="https://github.com/hpcaitech/ColossalAI/graphs/contributors">
<img src="https://contrib.rocks/image?repo=hpcaitech/ColossalAI" width="800px"/>
</a>

*The order of contributor avatars is randomly shuffled.*

<p align="right">(<a href="#top">back to top</a>)</p>

Expand Down
11 changes: 6 additions & 5 deletions applications/Chat/coati/dataset/sft_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ def __getitem__(self, idx):
# return dict(self.prompts[idx], self.prompts[idx])


def _tokenize_fn(strings: Sequence[str], tokenizer: transformers.PreTrainedTokenizer) -> Dict:
def _tokenize_fn(strings: Sequence[str], tokenizer: transformers.PreTrainedTokenizer, max_length: int) -> Dict:
"""Tokenize a list of strings."""
tokenized_list = [
tokenizer(
text,
return_tensors="pt",
padding="longest",
max_length=tokenizer.model_max_length,
max_length=max_length,
truncation=True,
) for text in strings
]
Expand All @@ -105,10 +105,11 @@ def preprocess(
sources: Sequence[str],
targets: Sequence[str],
tokenizer: transformers.PreTrainedTokenizer,
max_length: int,
) -> Dict:
"""Preprocess the data by tokenizing."""
examples = [s + t for s, t in zip(sources, targets)]
examples_tokenized, sources_tokenized = [_tokenize_fn(strings, tokenizer) for strings in (examples, sources)]
examples_tokenized, sources_tokenized = [_tokenize_fn(strings, tokenizer, max_length) for strings in (examples, sources)]
input_ids = examples_tokenized["input_ids"]
labels = copy.deepcopy(input_ids)
for label, source_len in zip(labels, sources_tokenized["input_ids_lens"]):
Expand All @@ -119,7 +120,7 @@ def preprocess(
class SupervisedDataset(Dataset):
"""Dataset for supervised fine-tuning."""

def __init__(self, data_path: str, tokenizer: transformers.PreTrainedTokenizer, max_datasets_size: int = None):
def __init__(self, data_path: str, tokenizer: transformers.PreTrainedTokenizer, max_datasets_size: int = None, max_length: int = 512):
super(SupervisedDataset, self).__init__()
logger.info("Loading data...")
list_data_dict = jload(data_path)
Expand All @@ -138,7 +139,7 @@ def __init__(self, data_path: str, tokenizer: transformers.PreTrainedTokenizer,
targets = [f"{example['output']}{tokenizer.eos_token}" for example in list_data_dict]

logger.info("Tokenizing inputs... This may take some time...")
data_dict = preprocess(sources, targets, tokenizer)
data_dict = preprocess(sources, targets, tokenizer, max_length)

self.input_ids = data_dict["input_ids"]
self.labels = data_dict["labels"]
Expand Down
3 changes: 3 additions & 0 deletions applications/Chat/coati/models/bloom/bloom_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ def __init__(self,
if checkpoint:
model.gradient_checkpointing_enable()
super().__init__(model, lora_rank, lora_train_bias)

def forward(self, input_ids, attention_mask=None, labels=None, **kwargs):
return self.model(input_ids, attention_mask=attention_mask, labels=labels, **kwargs)
3 changes: 3 additions & 0 deletions applications/Chat/coati/models/gpt/gpt_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ def __init__(self,
if checkpoint:
model.gradient_checkpointing_enable()
super().__init__(model, lora_rank, lora_train_bias)

def forward(self, input_ids, attention_mask=None, labels=None, **kwargs):
return self.model(input_ids, attention_mask=attention_mask, labels=labels, **kwargs)
9 changes: 4 additions & 5 deletions applications/Chat/coati/models/llama/llama_critic.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import Optional

import torch
import torch.nn as nn
from transformers import AutoModelForCausalLM, LlamaConfig, LlamaForCausalLM
from transformers import LlamaConfig, LlamaModel

from ..base import Critic

Expand All @@ -28,11 +27,11 @@ def __init__(self,
**kwargs) -> None:

if pretrained is not None:
model = LlamaForCausalLM.from_pretrained(pretrained)
model = LlamaModel.from_pretrained(pretrained)
elif config is not None:
model = LlamaForCausalLM(config)
model = LlamaModel(config)
else:
model = LlamaForCausalLM(LlamaConfig())
model = LlamaModel(LlamaConfig())

if checkpoint:
model.gradient_checkpointing_enable()
Expand Down
3 changes: 3 additions & 0 deletions applications/Chat/coati/models/opt/opt_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ def __init__(self,
if checkpoint:
model.gradient_checkpointing_enable()
super().__init__(model, lora_rank, lora_train_bias)

def forward(self, input_ids, attention_mask=None, labels=None, **kwargs):
return self.model(input_ids, attention_mask=attention_mask, labels=labels, **kwargs)
5 changes: 5 additions & 0 deletions applications/Chat/coati/models/roberta/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .roberta_actor import RoBERTaActor
from .roberta_critic import RoBERTaCritic
from .roberta_rm import RoBERTaRM

__all__ = ['RoBERTaActor', 'RoBERTaCritic', 'RoBERTaRM']
35 changes: 35 additions & 0 deletions applications/Chat/coati/models/roberta/roberta_actor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import Optional

from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.models.roberta.modeling_roberta import RobertaForCausalLM

from ..base import Actor

class RoBERTaActor(Actor):
"""
RoBERTa Actor model.

Args:
pretrained (str): Pretrained model name or path.
config (RoBERTaConfig): Model config.
checkpoint (bool): Enable gradient checkpointing.
lora_rank (int): Rank of the low-rank approximation.
lora_train_bias (str): LoRA bias training mode.
"""


def __init__(self,
pretrained: Optional[str] = None,
config: Optional[RobertaConfig] = None,
checkpoint: bool = False,
lora_rank: int = 0,
lora_train_bias: str = 'none') -> None:
if pretrained is not None:
model = RobertaForCausalLM.from_pretrained(pretrained)
elif config is not None:
model = RobertaForCausalLM(config)
else:
model = RobertaForCausalLM(RobertaConfig())
if checkpoint:
model.gradient_checkpointing_enable()
super().__init__(model, lora_rank, lora_train_bias)
38 changes: 38 additions & 0 deletions applications/Chat/coati/models/roberta/roberta_critic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from typing import Optional

import torch.nn as nn
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.models.roberta.modeling_roberta import RobertaModel

from ..base import Critic


class RoBERTaCritic(Critic):
"""
RoBERTa Critic model.

Args:
pretrained (str): Pretrained model name or path.
config (RoBERTa Config): Model config.
checkpoint (bool): Enable gradient checkpointing.
lora_rank (int): Rank of the low-rank approximation.
lora_train_bias (str): LoRA bias training mode.
"""

def __init__(self,
pretrained: Optional[str] = None,
config: Optional[RobertaConfig] = None,
checkpoint: bool = False,
lora_rank: int = 0,
lora_train_bias: str = 'none',
**kwargs) -> None:
if pretrained is not None:
model = RobertaModel.from_pretrained(pretrained, add_pooling_layer=False)
elif config is not None:
model = RobertaModel(config)
else:
model = RobertaModel(RobertaConfig())
if checkpoint:
model.gradient_checkpointing_enable()
value_head = nn.Linear(model.config.hidden_size, 1)
super().__init__(model, value_head, lora_rank, lora_train_bias, **kwargs)
39 changes: 39 additions & 0 deletions applications/Chat/coati/models/roberta/roberta_rm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import Optional

import torch.nn as nn
from transformers import RobertaConfig, RobertaModel


from ..base import RewardModel


class RoBERTaRM(RewardModel):
"""
RoBERTa Reward model.

Args:
pretrained (str): Pretrained model name or path.
config (RoBERTaConfig): Model config.
checkpoint (bool): Enable gradient checkpointing.
lora_rank (int): Rank of the low-rank approximation.
lora_train_bias (str): LoRA bias training mode.
"""

def __init__(self,
pretrained: Optional[str] = None,
config: Optional[RobertaConfig] = None,
checkpoint: bool = False,
lora_rank: int = 0,
lora_train_bias: str = 'none') -> None:
if pretrained is not None:
model = RobertaModel.from_pretrained(pretrained, add_pooling_layer=False)
elif config is not None:
model = RobertaModel(config)
else:
model = RobertaModel(RobertaConfig())
if checkpoint:
model.gradient_checkpointing_enable()

value_head = nn.Linear(model.config.hidden_size, 1)
value_head.weight.data.normal_(mean=0.0, std=1/(model.config.hidden_size + 1))
super().__init__(model, value_head, lora_rank, lora_train_bias)
17 changes: 10 additions & 7 deletions applications/Chat/coati/trainer/ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ def training_step(self, experience: Experience) -> Dict[str, float]:

# ptx loss
if self.ptx_coef != 0:
ptx = next(iter(self.pretrain_dataloader))['input_ids'].to(torch.cuda.current_device())
label = next(iter(self.pretrain_dataloader))['labels'].to(torch.cuda.current_device())[:, 1:]
attention_mask = next(iter(self.pretrain_dataloader))['attention_mask'].to(torch.cuda.current_device())
batch = next(iter(self.pretrain_dataloader))
ptx = batch['input_ids'].to(torch.cuda.current_device())
label = batch['labels'].to(torch.cuda.current_device())[:, 1:]
attention_mask = batch['attention_mask'].to(torch.cuda.current_device())
ptx_log_probs = self.actor.get_base_model()(ptx, attention_mask=attention_mask)['logits'][..., :-1, :]
ptx_loss = self.ptx_loss_fn(ptx_log_probs.view(-1, ptx_log_probs.size(-1)), label.view(-1))
actor_loss = ptx_loss * self.ptx_coef + actor_loss * (1 - self.ptx_coef)
Expand All @@ -116,6 +117,12 @@ def training_step(self, experience: Experience) -> Dict[str, float]:
self.critic_optim.zero_grad()

return {'reward': experience.reward.mean().item()}

def save_model(self, path: str, only_rank0: bool = False, tokenizer: Optional[PreTrainedTokenizerBase] = None) -> None:
self.strategy.save_model(model=self.actor, path=path, only_rank0=only_rank0, tokenizer=tokenizer)

def save_model(self, path: str, only_rank0: bool = False, tokenizer: Optional[PreTrainedTokenizerBase] = None) -> None:
self.strategy.save_model(model=self.actor, path=path, only_rank0=only_rank0, tokenizer=tokenizer)


def _set_default_generate_kwargs(strategy: Strategy, generate_kwargs: dict, actor: Actor) -> None:
Expand All @@ -129,7 +136,3 @@ def _set_default_generate_kwargs(strategy: Strategy, generate_kwargs: dict, acto
new_kwargs['update_model_kwargs_fn'] = update_model_kwargs_fn

return new_kwargs


def save_model(self, path: str, only_rank0: bool = False, tokenizer: Optional[PreTrainedTokenizerBase] = None) -> None:
self.strategy.save_model(model=self.actor, path=path, only_rank0=only_rank0, tokenizer=tokenizer)
9 changes: 4 additions & 5 deletions applications/Chat/coati/trainer/strategies/colossalai.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@
import colossalai
from colossalai.logging import get_dist_logger
from colossalai.nn.optimizer import CPUAdam, HybridAdam
from colossalai.nn.parallel import ZeroDDP, zero_model_wrapper, zero_optim_wrapper
from colossalai.nn.parallel.utils import get_static_torch_model
from colossalai.tensor import ProcessGroup, ShardSpec
from colossalai.utils import get_current_device
from colossalai.utils.model.colo_init_context import ColoInitContext

logger = get_dist_logger(__name__)
from colossalai.zero import ColoInitContext, ZeroDDP, zero_model_wrapper, zero_optim_wrapper
from colossalai.zero.gemini.utils import get_static_torch_model

from .base import Strategy
from .ddp import DDPStrategy

logger = get_dist_logger(__name__)


class ColossalAIStrategy(DDPStrategy):
"""
Expand Down
34 changes: 26 additions & 8 deletions applications/Chat/coati/trainer/strategies/ddp.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from typing import Optional

import os
import random

import numpy as np
import torch
import torch.distributed as dist
import torch.nn as nn
from coati.models.base import Actor
from coati.models.base import LM, Actor, RewardModel
from coati.models.lora import LoraLinear
from coati.replay_buffer import ReplayBuffer
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.optim import Optimizer
from torch.utils.data import DataLoader
from transformers.tokenization_utils_base import PreTrainedTokenizerBase

from .base import Strategy
from .naive import NaiveStrategy
Expand Down Expand Up @@ -72,17 +75,32 @@ def _unwrap_actor(actor: Actor) -> nn.Module:
model: DDP = Strategy._unwrap_actor(actor)
return model.module

def save_model(self, model: nn.Module, path: str, only_rank0: bool = False) -> None:
def save_model(self, model: nn.Module, path: str, only_rank0: bool = False, tokenizer: Optional[PreTrainedTokenizerBase] = None) -> None:
if only_rank0 and dist.get_rank() != 0:
return None

for module in model.modules():
if isinstance(module, LoraLinear):
module.merge_weights = True
module.eval()

if only_rank0 and dist.get_rank() != 0:
return
model = model.model.module
state_dict = model.state_dict()
torch.save(state_dict, path)

if isinstance(model, RewardModel):
state_dict = model.state_dict()
if only_rank0 and dist.get_rank() != 0:
return
torch.save(state_dict, path)
else:
try:
if isinstance(model, LM):
model = model.model
model.save_pretrained(path)
if tokenizer is not None:
tokenizer.save_pretrained(path)
except AttributeError:
state_dict = model.state_dict()
if only_rank0 and dist.get_rank() != 0:
return
torch.save(state_dict, path)

def save_optimizer(self, optimizer: Optimizer, path: str, only_rank0: bool = False) -> None:
if only_rank0 and dist.get_rank() != 0:
Expand Down
Loading