|
| 1 | +################## |
| 2 | +# Model settings # |
| 3 | +################## |
| 4 | + |
| 5 | +local pretrained_model = "t5-base"; |
| 6 | +local load_with_low_cpu_mem_usage = false; |
| 7 | + |
| 8 | +local modules_to_wrap = ["[a-zA-Z_.]+\\.[0-9]+"]; # TODO: works for t5 and gpt2. confirm with other models too. |
| 9 | + |
| 10 | +#################### |
| 11 | +# Trainer settings # |
| 12 | +#################### |
| 13 | + |
| 14 | +# Trainer settings, adjust to your use-case. |
| 15 | +local training_steps = 20; # total number of optimization steps to train for |
| 16 | +local validate_every = 5; # how often to validate and save checkpoints |
| 17 | + |
| 18 | +local devices = 1; # number of devices to train on (will use GPUs if enough are available, otherwise CPU) |
| 19 | +local grad_accum = 1; # number of gradient accumulation steps (changes the effective batch size) |
| 20 | +# This is the batch size per GPU, ignoring gradient accumulation: |
| 21 | +local batch_size = 2; |
| 22 | +# So the effective batch size is `batch_size * grad_accum * devices` |
| 23 | + |
| 24 | +local activation_checkpointing = false; # use activation/gradient checkpointing (probably need this GPT-J 6B, but not gpt2) |
| 25 | +local amp = false; # use PyTorch's native automatic mixed precision |
| 26 | +local fsdp = false; # Use FairScale's FullyShardedDataParallel (probably need this GPT-J 6B, but not gpt2) |
| 27 | +local cpu_offloading = false; # Can only be used with 'fsdp' - saves a lot of GPU memory by offloading params+gradients to CPU, but is very slow. |
| 28 | + |
| 29 | +###################### |
| 30 | +# Optimizer settings # |
| 31 | +###################### |
| 32 | + |
| 33 | +local warmup_steps = 20; |
| 34 | +local learning_rate = 0.00005; # you can probably use a higher LR for a small model like "gpt2" |
| 35 | + |
| 36 | + |
| 37 | +assert fsdp == true || cpu_offloading == false : "cpu_offloading only available with fsdp"; |
| 38 | + |
| 39 | +# FullyShardedDataParallel config: |
| 40 | +local fsdp_config = if fsdp then { |
| 41 | + reshard_after_forward: true, |
| 42 | + move_params_to_cpu: cpu_offloading, |
| 43 | + move_grads_to_cpu: cpu_offloading, |
| 44 | + mixed_precision: amp, |
| 45 | +} else null; |
| 46 | + |
| 47 | +local training_engine = { |
| 48 | + type: if fsdp then "fairscale" else "torch", |
| 49 | + optimizer: { |
| 50 | + type: "torch::AdamW", |
| 51 | + lr: learning_rate, |
| 52 | + betas: [0.9, 0.95], |
| 53 | + eps: 1e-6, |
| 54 | + }, |
| 55 | + lr_scheduler: { |
| 56 | + type: "transformers::linear", |
| 57 | + num_warmup_steps: warmup_steps, |
| 58 | + num_training_steps: training_steps, |
| 59 | + }, |
| 60 | + amp: amp, |
| 61 | + [if fsdp then "fsdp_config" else null]: fsdp_config, |
| 62 | +}; |
| 63 | + |
| 64 | +local distributed_dataloader = { |
| 65 | + batch_size: batch_size, |
| 66 | + sampler: { |
| 67 | + type: "torch::DistributedSampler", |
| 68 | + shuffle: true, |
| 69 | + drop_last: true, |
| 70 | + }, |
| 71 | +}; |
| 72 | + |
| 73 | +local single_device_dataloader = { |
| 74 | + shuffle: true, |
| 75 | + batch_size: batch_size, |
| 76 | +}; |
| 77 | + |
| 78 | +local dataloader = if devices > 1 then distributed_dataloader else single_device_dataloader; |
| 79 | + |
| 80 | +{ |
| 81 | + steps: { |
| 82 | + raw_data: { |
| 83 | + type: "datasets::load", |
| 84 | + path: "snli", |
| 85 | + }, |
| 86 | + /*"subset_data": { |
| 87 | + type: "subset-data", |
| 88 | + data: { type: "ref", ref: "raw_data" }, |
| 89 | + max_samples: 10, |
| 90 | + },*/ |
| 91 | + processed_data: { |
| 92 | + type: "snli-text2text", |
| 93 | + data: { type: "ref", ref: "raw_data" }, |
| 94 | + }, |
| 95 | + trained_model: { |
| 96 | + type: "transformers::finetune", |
| 97 | + model: { |
| 98 | + type: "fairscale::with_wrapped_modules", |
| 99 | + model: { |
| 100 | + type: "transformers::finetune::from_pretrained", |
| 101 | + pretrained_model_name_or_path: pretrained_model, |
| 102 | + low_cpu_mem_usage: load_with_low_cpu_mem_usage, |
| 103 | + }, |
| 104 | + modules_to_wrap: modules_to_wrap, # tell FairScale to wrap the transformer's blocks individually |
| 105 | + fsdp_config: fsdp_config, |
| 106 | + activation_checkpointing: activation_checkpointing, |
| 107 | + }, |
| 108 | + tokenizer: { |
| 109 | + pretrained_model_name_or_path: pretrained_model |
| 110 | + }, |
| 111 | + dataset_dict: { type: "ref", ref: "processed_data" }, |
| 112 | + train_dataloader: dataloader, |
| 113 | + validation_split: "validation", |
| 114 | + grad_accum: grad_accum, |
| 115 | + train_steps: training_steps, |
| 116 | + validate_every: validate_every, |
| 117 | + checkpoint_every: validate_every, |
| 118 | + log_every: 1, |
| 119 | + device_count: devices, |
| 120 | + training_engine: training_engine, |
| 121 | + }, |
| 122 | + generations: { |
| 123 | + type: "transformers::run_generation_dataset", |
| 124 | + max_length: 5, |
| 125 | + input: {"type": "ref", "ref": "processed_data"}, |
| 126 | + batch_size: batch_size, |
| 127 | + model: {"type": "ref", "ref": "trained_model"}, |
| 128 | + prompt_field: "source", |
| 129 | + output_field: "generation", |
| 130 | + splits: ["validation"] |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments