Skip to content

Fix nondeterministic tests for GPU runs #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 1, 2022
Merged

Conversation

anton-l
Copy link
Member

@anton-l anton-l commented Sep 1, 2022

This ensures that:

  • Stable Diffusion fast tests only run on CPU due to a device-dependent Generator
  • Training tests only run on CPU, to avoid setting extra determinism environment flags for CUBLAS and CUDA>10.2 which might affect other tests

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Sep 1, 2022

The documentation is not available anymore as the PR was closed or merged.

@patrickvonplaten
Copy link
Contributor

@anton-l what do you think about instead of skipping those tests when a GPU is available as is now simply forcing the device to be on CPU? This should be a bit safer no? Also I think we can be sure all GPUs have a CPU

@anton-l
Copy link
Member Author

anton-l commented Sep 1, 2022

@patrickvonplaten if removing autocast is ok with you, then yes, that was my first thought too :D

@anton-l anton-l requested review from patrickvonplaten and removed request for patrickvonplaten September 1, 2022 12:55
@patrickvonplaten
Copy link
Contributor

@patrickvonplaten if removing autocast is ok with you, then yes, that was my first thought too :D

Sure (sorry I copy-pasted that yesterday)

@patrickvonplaten
Copy link
Contributor

Thanks for making the change!

Copy link
Contributor

@patrickvonplaten patrickvonplaten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing!

@anton-l anton-l merged commit 4724250 into main Sep 1, 2022
@antoche
Copy link
Contributor

antoche commented Sep 2, 2022

Just wading into this after the fact as I've hit similar issues in the past and this is affecting me too...

Unfortunately the device arg in the Generator is ignored by pytorch (see bug 62451), so just using a pre-seeded, cpu-bound generator is not enough to ensure determinism.

The following code allows you to get deterministic tests by enforcing random number generation to actually happen on the CPU, while still allowing model evaluation to happen on the GPU:

import torch

def randn(size, generator=None, device=None, **kwargs):
    """
    Wrapper around torch.randn providing proper reproducibility.

    Generation is done on the given generator's device, then moved to the
    given ``device``.

    Args:
        size: tensor size
        generator (torch.Generator): RNG generator
        device (torch.device): Target device for the resulting tensor
    """
    # FIXME: generator RNG device is ignored and needs to be passed to torch.randn (torch issue #62451)
    rng_device = generator.device if generator is not None else device
    image = torch.randn(size, generator=generator, device=rng_device, **kwargs)
    image = image.to(device=device)
    return image


def randn_like(tensor, generator=None, **kwargs):
    return randn(tensor.shape, layout=tensor.layout, generator=generator, device=tensor.device, **kwargs)

Replacing all uses of torch.randn and torch.randn_like with this code lets you get deterministic results when using a generator such as generator = torch.Generator(device='cpu').manual_seed(0), and you can keep device to "cuda" everywhere else.

@anton-l anton-l deleted the fix-nondeterm-tests branch September 2, 2022 14:10
@patrickvonplaten
Copy link
Contributor

Just wading into this after the fact as I've hit similar issues in the past and this is affecting me too...

Unfortunately the device arg in the Generator is ignored by pytorch (see bug 62451), so just using a pre-seeded, cpu-bound generator is not enough to ensure determinism.

The following code allows you to get deterministic tests by enforcing random number generation to actually happen on the CPU, while still allowing model evaluation to happen on the GPU:

import torch

def randn(size, generator=None, device=None, **kwargs):
    """
    Wrapper around torch.randn providing proper reproducibility.

    Generation is done on the given generator's device, then moved to the
    given ``device``.

    Args:
        size: tensor size
        generator (torch.Generator): RNG generator
        device (torch.device): Target device for the resulting tensor
    """
    # FIXME: generator RNG device is ignored and needs to be passed to torch.randn (torch issue #62451)
    rng_device = generator.device if generator is not None else device
    image = torch.randn(size, generator=generator, device=rng_device, **kwargs)
    image = image.to(device=device)
    return image


def randn_like(tensor, generator=None, **kwargs):
    return randn(tensor.shape, layout=tensor.layout, generator=generator, device=tensor.device, **kwargs)

Replacing all uses of torch.randn and torch.randn_like with this code lets you get deterministic results when using a generator such as generator = torch.Generator(device='cpu').manual_seed(0), and you can keep device to "cuda" everywhere else.

Thanks - that looks very interesting! @anton-l - let's try it out no?

natolambert pushed a commit that referenced this pull request Sep 7, 2022
* Fix nondeterministic tests for GPU runs

* force SD fast tests to the CPU
PhaneeshB pushed a commit to nod-ai/diffusers that referenced this pull request Mar 1, 2023
)

This commit removes unused parameters in the v-diffusion model. It also
updated the server parameters in order to make multiple requests to be
handled sequentially.

Signed-Off-by: Gaurav Shukla <gaurav@nod-labs.com>

Signed-off-by: Gaurav Shukla <gaurav@nod-labs.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants