MCPcopy Create free account
hub / github.com/zai-org/CodeGeeX / model_provider

Function model_provider

codegeex/megatron/tools/pretrain_codegeex.py:20–86  ·  view source on GitHub ↗

Build the model.

(pre_process=True, post_process=True)

Source from the content-addressed store, hash-verified

18
19
20def model_provider(pre_process=True, post_process=True):
21 """Build the model."""
22
23 print_rank_0("building GPT model ...")
24 see_memory_usage(f"Before Building Model", force=True)
25
26 args = get_args()
27 with deepspeed.zero.Init(
28 data_parallel_group=mpu.get_data_parallel_group(),
29 remote_device=None if args.remote_device == "none" else args.remote_device,
30 config_dict_or_path=args.deepspeed_config,
31 enabled=args.zero_stage == 3,
32 mpu=mpu,
33 ):
34 if args.deepspeed and not args.no_pipeline_parallel:
35 model = CodeGeeXModelPipe(num_tokentypes=0, parallel_output=True)
36 # This is a hack to give us a reference to get_batch_pipe from within training.py
37 # We need to call model.set_batch_fn after deepspeed.initialize
38 model._megatron_batch_fn = get_batch_pipe
39
40 # Predompute the attention mask and store it in args. This avoids having to
41 # pipeline it as an activation during training. The mask is constant, and thus
42 # we can reuse it.
43 attention_mask = torch.tril(
44 torch.ones(
45 (1, args.seq_length, args.seq_length),
46 device=torch.cuda.current_device(),
47 )
48 ).view(1, 1, args.seq_length, args.seq_length)
49
50 # Convert attention mask to binary:
51 attention_mask = attention_mask < 0.5
52 if args.fp16:
53 attention_mask = attention_mask.half()
54 elif args.bf16:
55 attention_mask = attention_mask.bfloat16()
56
57 # Attention mask must be bool.
58 args.attn_mask = attention_mask.to(torch.bool)
59
60 else:
61 model = CodeGeeXModel(
62 num_tokentypes=0,
63 parallel_output=True,
64 )
65
66 if args.load_state is not None:
67 timers = get_timers()
68 print_rank_0("Loading warmstarting model states ...")
69 timers("load-model-states").start()
70 mp_rank = mpu.get_tensor_model_parallel_rank()
71 if os.path.isdir(args.load_state):
72 model_path = os.path.join(
73 args.load_state, "mp_rank_{:02d}_model_states.pt".format(mp_rank)
74 )
75 else:
76 model_path = args.load_state
77 print_rank_0(f"Loading model from {model_path} ...")

Callers

nothing calls this directly

Calls 9

load_state_dictMethod · 0.95
print_rank_0Function · 0.90
get_argsFunction · 0.90
CodeGeeXModelClass · 0.90
get_timersFunction · 0.90
CodeGeeXModelPipeClass · 0.85
startMethod · 0.80
stopMethod · 0.80
logMethod · 0.80

Tested by

no test coverage detected