mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-24 03:03:37 +00:00
[Examples] Add lazy init to OPT and GPT examples (#5924)
Co-authored-by: Edenzzzz <wtan45@wisc.edu>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import argparse
|
||||
from contextlib import nullcontext
|
||||
from typing import Callable, List, Union
|
||||
|
||||
import evaluate
|
||||
@@ -17,6 +18,7 @@ from colossalai.accelerator import get_accelerator
|
||||
from colossalai.booster import Booster
|
||||
from colossalai.booster.plugin import GeminiPlugin, HybridParallelPlugin, LowLevelZeroPlugin, TorchDDPPlugin
|
||||
from colossalai.cluster import DistCoordinator
|
||||
from colossalai.lazy import LazyInitContext
|
||||
from colossalai.nn.optimizer import HybridAdam
|
||||
|
||||
# ==============================
|
||||
@@ -186,7 +188,6 @@ def main():
|
||||
help="only gpt2 now",
|
||||
)
|
||||
parser.add_argument("--target_f1", type=float, default=None, help="target f1 score. Raise exception if not reached")
|
||||
parser.add_argument("--use_lazy_init", type=bool, default=False, help="for initiating lazy init context")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.model_type == "gpt2":
|
||||
@@ -250,10 +251,16 @@ def main():
|
||||
pad_token_id=data_builder.tokenizer.pad_token_id,
|
||||
)
|
||||
|
||||
if model_name == "gpt2":
|
||||
model = GPT2ForSequenceClassification.from_pretrained(model_name, config=cfg).cuda()
|
||||
else:
|
||||
raise RuntimeError
|
||||
init_ctx = (
|
||||
LazyInitContext(default_device=get_accelerator().get_current_device())
|
||||
if isinstance(plugin, (GeminiPlugin))
|
||||
else nullcontext()
|
||||
)
|
||||
with init_ctx:
|
||||
if model_name == "gpt2":
|
||||
model = GPT2ForSequenceClassification.from_pretrained(model_name, config=cfg).cuda()
|
||||
else:
|
||||
raise RuntimeError
|
||||
|
||||
# optimizer
|
||||
no_decay = ["bias", "LayerNorm.weight"]
|
||||
|
Reference in New Issue
Block a user