declare-lab/tango2
92
1# Copyright 2023 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 at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# 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 and13# limitations under the License.14"""15Generic utilities16"""17 18from collections import OrderedDict19from dataclasses import fields20from typing import Any, Tuple21 22import numpy as np23 24from .import_utils import is_torch_available25 26 27def is_tensor(x):28 """29 Tests if `x` is a `torch.Tensor` or `np.ndarray`.30 """31 if is_torch_available():32 import torch33 34 if isinstance(x, torch.Tensor):35 return True36 37 return isinstance(x, np.ndarray)38 39 40class BaseOutput(OrderedDict):41 """42 Base class for all model outputs as dataclass. Has a `__getitem__` that allows indexing by integer or slice (like a43 tuple) or strings (like a dictionary) that will ignore the `None` attributes. Otherwise behaves like a regular44 python dictionary.45 46 <Tip warning={true}>47 48 You can't unpack a `BaseOutput` directly. Use the [`~utils.BaseOutput.to_tuple`] method to convert it to a tuple49 before.50 51 </Tip>52 """53 54 def __post_init__(self):55 class_fields = fields(self)56 57 # Safety and consistency checks58 if not len(class_fields):59 raise ValueError(f"{self.__class__.__name__} has no fields.")60 61 first_field = getattr(self, class_fields[0].name)62 other_fields_are_none = all(getattr(self, field.name) is None for field in class_fields[1:])63 64 if other_fields_are_none and isinstance(first_field, dict):65 for key, value in first_field.items():66 self[key] = value67 else:68 for field in class_fields:69 v = getattr(self, field.name)70 if v is not None:71 self[field.name] = v72 73 def __delitem__(self, *args, **kwargs):74 raise Exception(f"You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.")75 76 def setdefault(self, *args, **kwargs):77 raise Exception(f"You cannot use ``setdefault`` on a {self.__class__.__name__} instance.")78 79 def pop(self, *args, **kwargs):80 raise Exception(f"You cannot use ``pop`` on a {self.__class__.__name__} instance.")81 82 def update(self, *args, **kwargs):83 raise Exception(f"You cannot use ``update`` on a {self.__class__.__name__} instance.")84 85 def __getitem__(self, k):86 if isinstance(k, str):87 inner_dict = dict(self.items())88 return inner_dict[k]89 else:90 return self.to_tuple()[k]91 92 def __setattr__(self, name, value):93 if name in self.keys() and value is not None:94 # Don't call self.__setitem__ to avoid recursion errors95 super().__setitem__(name, value)96 super().__setattr__(name, value)97 98 def __setitem__(self, key, value):99 # Will raise a KeyException if needed100 super().__setitem__(key, value)101 # Don't call self.__setattr__ to avoid recursion errors102 super().__setattr__(key, value)103 104 def to_tuple(self) -> Tuple[Any]:105 """106 Convert self to a tuple containing all the attributes/keys that are not `None`.107 """108 return tuple(self[k] for k in self.keys())109 