|
| 1 | +# Copyright 2022 The HuggingFace Team. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +""" |
| 15 | +PyTorch utilities: Utilities related to PyTorch |
| 16 | +""" |
| 17 | +from typing import List, Optional, Tuple, Union |
| 18 | + |
| 19 | +from . import logging |
| 20 | +from .import_utils import is_torch_available |
| 21 | + |
| 22 | + |
| 23 | +if is_torch_available(): |
| 24 | + import torch |
| 25 | + |
| 26 | +logger = logging.get_logger(__name__) # pylint: disable=invalid-name |
| 27 | + |
| 28 | + |
| 29 | +def torch_randn( |
| 30 | + shape: Union[Tuple, List], |
| 31 | + generator: Optional[Union[List["torch.Generator"], "torch.Generator"]] = None, |
| 32 | + device: Optional["torch.device"] = None, |
| 33 | + dtype: Optional["torch.dtype"] = None, |
| 34 | +): |
| 35 | + """This is a helper function that allows to create random tensors on the desired `device` with the desired `dtype`. When |
| 36 | + passing a list of generators one can seed each batched size individually. If CPU generators are passed the tensor |
| 37 | + will always be created on CPU. |
| 38 | + """ |
| 39 | + # device on which tensor is created defaults to device |
| 40 | + rand_device = device |
| 41 | + batch_size = shape[0] |
| 42 | + |
| 43 | + if generator is not None: |
| 44 | + if generator.device != device and generator.device.type == "cpu": |
| 45 | + rand_device = "cpu" |
| 46 | + if device != "mps": |
| 47 | + logger.info( |
| 48 | + f"The passed generator was created on 'cpu' even though a tensor on {device} was expected." |
| 49 | + f" Tensors will be created on 'cpu' and then moved to {device}. Note that one can probably" |
| 50 | + f" slighly speed up this function by passing a generator that was created on the {device} device." |
| 51 | + ) |
| 52 | + elif generator.device.type != device.type and generator.device.type == "cuda": |
| 53 | + raise ValueError(f"Cannot generate a {device} tensor from a generator of type {generator.device.type}.") |
| 54 | + |
| 55 | + if isinstance(generator, list): |
| 56 | + shape = (1,) + shape[1:] |
| 57 | + latents = [ |
| 58 | + torch.randn(shape, generator=generator[i], device=rand_device, dtype=dtype) for i in range(batch_size) |
| 59 | + ] |
| 60 | + latents = torch.cat(latents, dim=0).to(device) |
| 61 | + else: |
| 62 | + latents = torch.randn(shape, generator=generator, device=rand_device, dtype=dtype).to(device) |
| 63 | + |
| 64 | + return latents |
0 commit comments