NVIDIA / NVIDIA/apex

Inaccurate reduction in OPT_LEVEL 'O1'

Open
#700 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
9k
Forks
1.5k
Avg merge
2d 4h
Merged PRs (30d)
3

Description

I want to disable all-reduce during gradient accumulation. If my gradient accumulation is 2, I want to enable all reduce every other step. This will speed up my training.

I am using the following code with latest apex but the reduced gradients are coming inaccurate. Can I know what's wrong here?

from apex.parallel import DistributedDataParallel

    self.model = DistributedDataParallel(self.model, delay_allreduce=True)
    def _enable_or_disable_reduction(self, step):
            # https://github.com/NVIDIA/apex/blob/master/apex/parallel/distributed.py
            if (step + 1) % self.args.gradient_accumulation_steps == 0:
                self.logger.debug('Enabling all reduce')
                self.model.enable_allreduce()
            else:
                self.logger.debug('Disabling all reduce')
                self.model.disable_allreduce()
            pass
    def _train_step(self, batch, step):
        self.logger.debug(f'***** Training step {step} *****')
        """ Performs forward and backwards pass"""
        batch = [t.to(self.args.device) for t in batch] # move data to device
        batch, batch_len = Tasks.get_model_inputs(self.args, batch)
        self.sequences += batch_len
        self.logger.debug(self.get_param_info())
        output = self._forward(batch)
        loss = output[0]
        loss = loss / self.args.gradient_accumulation_steps

        self._enable_or_disable_reduction(step)
        self.logger.debug(f'step = {step}, _disable_allreduce = {self.model._disable_allreduce}')

        self._backward(loss)
        self.logger.debug(self.get_param_info())
        return loss

With this code reduction is not happening between 2 processes
image

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with apex/parallel/distributed.py, the linked implementation, and inspect enable_allreduce(), disable_allreduce(), and their call order around backward(). Reproduce the provided two-process gradient-accumulation example and verify when reduction occurs and whether the resulting gradients are accurate; the issue does not name a test or a specific expected code change.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
distributed-systems, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.