Repository navigation
fix: validation loss accumulation rescaling - #1758
Conversation
[Problem] When gradient accumulation is enabled, the validation loss is incorrect. This is a consequence of the loss_fn being shared between train and validation mode. The loss function is wrapped with rescale_accumulated_loss(), which rescales the loss by the number of gradient accumulation steps. This works for train, as the loss from N accumulation steps is summed at the end of the accumulation, before being logged. In the case of validation, the loop assumes that each step is a normal step, and the reported loss is scaled by 1/N. There are two separate pathways by which this occurs. In the normal case, the wrapped loss function is used "as-is." In the case of PP, the loss function is not directly used, but is still applied indirectly through the pipeline scheduler. In both cases, the result is that the reported validation loss is divided by the number of accumulation steps. [Solution] The number of gradient accumulation steps is not communicated to the validation-factory, thus it does not have access to this parameter, which is computed in the Trainer's constructor. One possible solution would be to pass this value, as an argument, to the validator, which could then rescale the loss by this factor, before logging it. This was the suggested solution to the problem. My solution is a little different. The function "rescale_accumulated_loss()" now returns a loss function with a "no_rescale()" context manager, which we apply before calling the validator. This transparently disables rescaling while within this context. This approach avoids the need to communicate this detail to the Validator and avoids having to modify the signature of the Validator factory -- which means modifying quite a few other files. Note that in the case of PP, we double-wrap the loss function. The context manager only applies to the outer layer, thus has no impact on the PP scaling, which is what we want. [Testing] First, run the trainer with both a normal, single GPU, run and a 2-stage pipeline. In both cases, set the number of accumulation steps to be sufficiently hight that the problem is obvious. This covers both pathways to the wrapped loss function. Repeat the tests with the fix. The magnitude of the validation loss should roughly correlate with the train loss.
These are the configuration I used for testing the validation loss issue. Specifically, "control.yaml" and "pp.yaml" Committing for potential replication of findings. See: pytorch/torchtitan#1758
tianyu-l
left a comment
There was a problem hiding this comment.
oh this sounds correct, but also sounds a bit overkill
|
This shows the train and validation loss before and after the change. There is one run with PP and one with a single GPU. Both use 10 gradient accumulation steps. The test configurations can be found here: |
|
It seems likely that there may be more than one call to the Validator class in the codebase. First, let me know if this approach works for you. If it does, I'll be sure to track the others down and add the context manager. |
As part of fixing validation loss scaling, when using gradient accumulation, I have added a new context manger to the wrapped loss function for disabling scaling. This added the context manager to the remaining instances of validation, the one instance in forge.
tianyu-l
left a comment
There was a problem hiding this comment.
I think this approach is fine. Here correctness is more important than simplicity.
Please update https://github.com/pytorch/torchtitan/blob/main/torchtitan/train.py#L359 to also use self.loss_fn.
As we can now correctly handle the case of using the train loss function with validation, we now use the same function for both. [Testing] Ran the "control.yaml" configuration, which uses the passed in loss function. The results track exactly with the previouos run.
|
I have verified that this works. It was OK before, but it's cleaner to use only one instance of the loss function. This makes it more difficult to create a divergence between PP and non-PP and not notice. Partial logs, with 1 GPU, debug model, and 10 accumulation steps |
[Problem] When gradient accumulation is enabled, the validation loss is incorrect. This is a consequence of the loss_fn being shared between train and validation mode. The loss function is wrapped with rescale_accumulated_loss(), which rescales the loss by the number of gradient accumulation steps. This works for train, as the loss from N accumulation steps is summed at the end of the accumulation, before being logged. In the case of validation, the loop assumes that each step is a normal step, and the reported loss is scaled by 1/N. There are two separate pathways by which this occurs. In the normal case, the wrapped loss function is used "as-is." In the case of PP, the loss function is not directly used, but is still applied indirectly through the pipeline scheduler. In both cases, the result is that the reported validation loss is divided by the number of accumulation steps. [Solution] The number of gradient accumulation steps is not communicated to the validation-factory, thus it does not have access to this parameter, which is computed in the Trainer's constructor. One possible solution would be to pass this value, as an argument, to the validator, which could then rescale the loss by this factor, before logging it. This was the suggested solution to the problem. My solution is a little different. The function "rescale_accumulated_loss()" now returns a loss function with a "no_rescale()" context manager, which we apply before calling the validator. This transparently disables rescaling while within this context. This approach avoids the need to communicate this detail to the Validator and avoids having to modify the signature of the Validator factory -- which means modifying quite a few other files. Note that in the case of PP, we double-wrap the loss function. The context manager only applies to the outer layer, thus has no impact on the PP scaling, which is what we want. [Testing] First, run the trainer with both a normal, single GPU, run and a 2-stage pipeline. In both cases, set the number of accumulation steps to be sufficiently hight that the problem is obvious. This covers both pathways to the wrapped loss function. Repeat the tests with the fix. The magnitude of the validation loss should roughly correlate with the train loss.
[Problem] When gradient accumulation is enabled, the validation loss is incorrect. This is a consequence of the loss_fn being shared between train and validation mode. The loss function is wrapped with rescale_accumulated_loss(), which rescales the loss by the number of gradient accumulation steps. This works for train, as the loss from N accumulation steps is summed at the end of the accumulation, before being logged. In the case of validation, the loop assumes that each step is a normal step, and the reported loss is scaled by 1/N. There are two separate pathways by which this occurs. In the normal case, the wrapped loss function is used "as-is." In the case of PP, the loss function is not directly used, but is still applied indirectly through the pipeline scheduler. In both cases, the result is that the reported validation loss is divided by the number of accumulation steps. [Solution] The number of gradient accumulation steps is not communicated to the validation-factory, thus it does not have access to this parameter, which is computed in the Trainer's constructor. One possible solution would be to pass this value, as an argument, to the validator, which could then rescale the loss by this factor, before logging it. This was the suggested solution to the problem. My solution is a little different. The function "rescale_accumulated_loss()" now returns a loss function with a "no_rescale()" context manager, which we apply before calling the validator. This transparently disables rescaling while within this context. This approach avoids the need to communicate this detail to the Validator and avoids having to modify the signature of the Validator factory -- which means modifying quite a few other files. Note that in the case of PP, we double-wrap the loss function. The context manager only applies to the outer layer, thus has no impact on the PP scaling, which is what we want. [Testing] First, run the trainer with both a normal, single GPU, run and a 2-stage pipeline. In both cases, set the number of accumulation steps to be sufficiently hight that the problem is obvious. This covers both pathways to the wrapped loss function. Repeat the tests with the fix. The magnitude of the validation loss should roughly correlate with the train loss.

[Problem]
When gradient accumulation is enabled, the validation loss is incorrect. This is a consequence of the loss_fn being shared between train and validation mode. The loss function is wrapped with rescale_accumulated_loss(), which rescales the loss by the number of gradient accumulation steps.
This works for train, as the loss from N accumulation steps is summed at the end of the accumulation, before being logged. In the case of validation, the loop assumes that each step is a normal step, and the reported loss is scaled by 1/N.
There are two separate pathways by which this occurs. In the normal case, the wrapped loss function is used "as-is." In the case of PP, the loss function is not directly used, but is still applied indirectly through the pipeline scheduler.
In both cases, the result is that the reported validation loss is divided by the number of accumulation steps.
[Solution]
The number of gradient accumulation steps is not communicated to the validation-factory, thus it does not have access to this parameter, which is computed in the Trainer's constructor.
One possible solution would be to pass this value, as an argument, to the validator, which could then rescale the loss by this factor, before logging it. This was the suggested solution to the problem.
My solution is a little different. The function
"rescale_accumulated_loss()" now returns a loss function with a "no_rescale()" context manager, which we apply before calling the validator. This transparently disables rescaling while within this context.
This approach avoids the need to communicate this detail to the Validator and avoids having to modify the signature of the Validator factory -- which means modifying quite a few other files.
Note that in the case of PP, we double-wrap the loss function. The context manager only applies to the outer layer, thus has no impact on the PP scaling, which is what we want.
[Testing]
First, run the trainer with both a normal, single GPU, run and a 2-stage pipeline. In both cases, set the number of accumulation steps to be sufficiently hight that the problem is obvious. This covers both pathways to the wrapped loss function.
Repeat the tests with the fix. The magnitude of the validation loss should roughly correlate with the train loss.