-
Notifications
You must be signed in to change notification settings - Fork 22.9k
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
Allow creation of pseudo devices for testing purposes #61654
Comments
It sounds like the
|
That's neat. Wasn't aware of this. Thanks for your help! @bdhirsh could you explain what the purpose of the |
the meta API lets you run dtype and shape inference. For example:
The above involved type promotion (to promote the output type to float64), and broadcasting (to broadcast the output shape to (1,2), but didn't allocate any storage and didn't actually "add" anything. Tensors on the meta device don't have any storage, and calling operators on meta tensors doesn't involve any compute - it only involves input error checking + running the "meta" computation. The original RFC is here if you're interested: https://github.com/pytorch/rfcs/blob/rfc-0005/RFC-0005-structured-kernel-definitions.md#how-to-get-involved-with-structured-kernels. "Structured Kernels" are a new way of authoring kernels internally, that require you separate your "meta" computation from your core kernel logic. Which is what allows us to create a meta API. |
Thanks @bdhirsh, this looks indeed promising but being able to test the |
I think there are some slight semantics differences that would make meta more useful for your use case here, so definitely OK to keep this open. |
For people waiting for this feature and using MacOS, you can use "mps" device (metal performance shaders). It turns out to be available even on macs without M1+ hardware, e.g. I have intel macbook with AMD gpu. |
When I tried it, I found mps doesn't support float64, though. |
This would be very helpful, since tests running in CI (e.g. Github Actions) usually don't have a GPU and this allows bugs related to mismatched tensors to slip through into production. The |
It might be relatively simple to do this with privateuse1. The general idea would be to look at the sample code for how to add a new custom device type that tests privateuse1 right now, but then actually just implement a single fallback kernel for everything that just redispatches to cpu. |
cc @albanD |
Ho yes we can definitely do that.
I would agree with Ed that we can make this a fully fledged extension that has fallback to CPU implementation if we need a version you can run actual compute tests against. |
Is it possible to share an example of how to use I tried the following from the pytorch test shared by @albanD, but it gives errors: import torch
from typing import Union
class DummyModule:
@staticmethod
def device_count() -> int:
return 1
@staticmethod
def get_rng_state(device: Union[int, str, torch.device] = "foo") -> torch.Tensor:
# create a tensor using our custom device object.
return torch.empty(4, 4, device="foo")
@staticmethod
def set_rng_state(
new_state: torch.Tensor, device: Union[int, str, torch.device] = "foo"
) -> None:
pass
@staticmethod
def is_available():
return True
@staticmethod
def current_device():
return 0
torch.utils.rename_privateuse1_backend("foo")
torch._register_device_module("foo", DummyModule)
x = torch.empty(4, 4, device="foo") This returns the error:
I've tried registering the actual |
🚀 Feature
Add a pseudo device cpu_testing for testing purposes.
Motivation
For machines with only a CPU there is currently no straight-forward way to test if
torch.device
is set properly for all tensors sincecpu
is the default. Having a dummy device, e.g., namedcpu_testing
which internally falls back to the default device but throws if two tensors with different devices are combined would allow the creation of unit tests.Pitch
It happens that some machines purely used for CI don't have any fancy devices other than CPU. Since CPU is the default device errors frequently occur in production where the model missed to set tensors to, e.g., GPU and this case cannot be covered in a unit test. Creating a pseudo device which also works for machines with only CPUs could help to change this defect.
cc @ezyang @bhosmer @smessmer @ljk53 @bdhirsh
The text was updated successfully, but these errors were encountered: