Skip to content

Commit

Permalink
BERT pre-training mp fork to spawn (#6442)
Browse files Browse the repository at this point in the history
* change bert fork to spawn

Signed-off-by: Abhinav Khattar <[email protected]>

* num_workers=0 fix

Signed-off-by: Abhinav Khattar <[email protected]>

---------

Signed-off-by: Abhinav Khattar <[email protected]>
  • Loading branch information
aklife97 authored and web-flow committed Apr 19, 2023
1 parent be711c9 commit 789ede7
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
3 changes: 3 additions & 0 deletions examples/nlp/language_modeling/megatron_bert_pretraining.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import torch.multiprocessing as mp
from omegaconf.omegaconf import OmegaConf, open_dict
from pytorch_lightning import Trainer
from pytorch_lightning.plugins.environments import TorchElasticEnvironment
Expand All @@ -28,6 +29,8 @@
from nemo.utils import logging
from nemo.utils.exp_manager import exp_manager

mp.set_start_method("spawn", force=True)


@hydra_runner(config_path="conf", config_name="megatron_bert_config")
def main(cfg) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,11 @@ def build_pretraining_data_loader(self, dataset, consumed_samples):

# Torch dataloader.
return torch.utils.data.DataLoader(
dataset, batch_sampler=batch_sampler, num_workers=self.cfg.data.num_workers, pin_memory=True,
dataset,
batch_sampler=batch_sampler,
num_workers=self.cfg.data.num_workers,
pin_memory=True,
persistent_workers=True if self.cfg.data.num_workers > 0 else False,
)

def setup_training_data(self, cfg):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ def build_pretraining_data_loader(
batch_sampler=batch_sampler,
num_workers=self.cfg.data.num_workers,
pin_memory=True,
persistent_workers=True,
persistent_workers=True if self.cfg.data.num_workers > 0 else False,
)

def setup(self, stage=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,9 @@ def build_virtual_prompt_dataset(
drop_last=drop_last,
num_workers=num_workers,
pin_memory=pin_memory,
persistent_workers=True, # (@adithyare and @eharper) We need this to make spawn=True to work.
persistent_workers=True
if num_workers > 0
else False, # (@adithyare and @eharper) We need this to make spawn=True to work.
)

return dataset, dataloader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,9 @@ def build_virtual_prompt_dataset(
drop_last=drop_last,
num_workers=num_workers,
pin_memory=pin_memory,
persistent_workers=True, # (@adithyare and @eharper) We need to set this to True to get around issues with spawn=True
persistent_workers=True
if num_workers > 0
else False, # (@adithyare and @eharper) We need to set this to True to get around issues with spawn=True
)
print('build success', len(dataloader), dataset_paths)
return dataset, dataloader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def _setup_eval_dataloader_from_config(self, cfg: DictConfig, dataset):
pin_memory=cfg.get("pin_memory", False),
drop_last=cfg.get("drop_last", False),
shuffle=False,
persistent_workers=True,
persistent_workers=True if cfg.get("num_workers", 0) > 0 else False,
)
)

Expand Down Expand Up @@ -592,7 +592,7 @@ def _setup_megatron_dataloader_from_config(self, cfg, dataset, consumed_samples)
collate_fn=collate_fn,
num_workers=cfg.num_workers,
pin_memory=cfg.pin_memory,
persistent_workers=True,
persistent_workers=True if cfg.num_workers > 0 else False,
)

def process_global_batch_for_text_translation_datasets(self, batch):
Expand Down

0 comments on commit 789ede7

Please sign in to comment.