-
Couldn't load subscription status.
- Fork 6.5k
introduce compute arch specific expectations and fix test_sd3_img2img_inference failure #11227
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
Changes from 5 commits
3c4707b
7f4747d
46bcfd9
559901a
5ea4e7e
340eb92
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,10 +14,11 @@ | |
| import time | ||
| import unittest | ||
| import urllib.parse | ||
| from collections import UserDict | ||
| from contextlib import contextmanager | ||
| from io import BytesIO, StringIO | ||
| from pathlib import Path | ||
| from typing import Callable, Dict, List, Optional, Union | ||
| from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union | ||
|
|
||
| import numpy as np | ||
| import PIL.Image | ||
|
|
@@ -48,6 +49,17 @@ | |
| from .logging import get_logger | ||
|
|
||
|
|
||
| if is_torch_available(): | ||
| import torch | ||
|
|
||
| IS_ROCM_SYSTEM = torch.version.hip is not None | ||
| IS_CUDA_SYSTEM = torch.version.cuda is not None | ||
| IS_XPU_SYSTEM = getattr(torch.version, "xpu", None) is not None | ||
| else: | ||
| IS_ROCM_SYSTEM = False | ||
| IS_CUDA_SYSTEM = False | ||
| IS_XPU_SYSTEM = False | ||
|
|
||
| global_rng = random.Random() | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
@@ -1275,3 +1287,93 @@ def update_mapping_from_spec(device_fn_dict: Dict[str, Callable], attribute_name | |
| update_mapping_from_spec(BACKEND_RESET_PEAK_MEMORY_STATS, "RESET_PEAK_MEMORY_STATS_FN") | ||
| update_mapping_from_spec(BACKEND_RESET_MAX_MEMORY_ALLOCATED, "RESET_MAX_MEMORY_ALLOCATED_FN") | ||
| update_mapping_from_spec(BACKEND_MAX_MEMORY_ALLOCATED, "MAX_MEMORY_ALLOCATED_FN") | ||
|
|
||
|
|
||
| # below codes are copied from https://github.com/huggingface/transformers/blob/main/src/transformers/testing_utils.py#L3090 | ||
hlky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Type definition of key used in `Expectations` class. | ||
| DeviceProperties = Tuple[Union[str, None], Union[int, None]] | ||
|
|
||
|
|
||
| @functools.lru_cache | ||
| def get_device_properties() -> DeviceProperties: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this return a dict instead? We could have sensible key namings like: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's raise it internally with transformers team as it's best to keep this in sync with their code (except Python 3.8 changes) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine by me. Cc: @ivarflakstad There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey! Feel free to upstream any improvements you can think of |
||
| """ | ||
| Get environment device properties. | ||
| """ | ||
| if IS_CUDA_SYSTEM or IS_ROCM_SYSTEM: | ||
| import torch | ||
|
|
||
| major, _ = torch.cuda.get_device_capability() | ||
| if IS_ROCM_SYSTEM: | ||
| return ("rocm", major) | ||
| else: | ||
| return ("cuda", major) | ||
| elif IS_XPU_SYSTEM: | ||
| import torch | ||
|
|
||
| # To get more info of the architecture meaning and bit allocation, refer to https://github.com/intel/llvm/blob/sycl/sycl/include/sycl/ext/oneapi/experimental/device_architecture.def | ||
| arch = torch.xpu.get_device_capability()["architecture"] | ||
| gen_mask = 0x000000FF00000000 | ||
| gen = (arch & gen_mask) >> 32 | ||
| return ("xpu", gen) | ||
| else: | ||
| return (torch_device, None) | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| DevicePropertiesUserDict = UserDict[DeviceProperties, Any] | ||
| else: | ||
| DevicePropertiesUserDict = UserDict | ||
|
|
||
|
|
||
| class Expectations(DevicePropertiesUserDict): | ||
| def get_expectation(self) -> Any: | ||
| """ | ||
| Find best matching expectation based on environment device properties. | ||
| """ | ||
| return self.find_expectation(get_device_properties()) | ||
|
|
||
| @staticmethod | ||
| def is_default(key: DeviceProperties) -> bool: | ||
| return all(p is None for p in key) | ||
|
|
||
| @staticmethod | ||
| def score(key: DeviceProperties, other: DeviceProperties) -> int: | ||
| """ | ||
| Returns score indicating how similar two instances of the `Properties` tuple are. Points are calculated using | ||
| bits, but documented as int. Rules are as follows: | ||
| * Matching `type` gives 8 points. | ||
| * Semi-matching `type`, for example cuda and rocm, gives 4 points. | ||
| * Matching `major` (compute capability major version) gives 2 points. | ||
| * Default expectation (if present) gives 1 points. | ||
| """ | ||
| (device_type, major) = key | ||
| (other_device_type, other_major) = other | ||
|
|
||
| score = 0b0 | ||
| if device_type == other_device_type: | ||
| score |= 0b1000 | ||
| elif device_type in ["cuda", "rocm"] and other_device_type in ["cuda", "rocm"]: | ||
| score |= 0b100 | ||
|
|
||
| if major == other_major and other_major is not None: | ||
| score |= 0b10 | ||
|
|
||
| if Expectations.is_default(other): | ||
| score |= 0b1 | ||
|
|
||
| return int(score) | ||
|
|
||
| def find_expectation(self, key: DeviceProperties = (None, None)) -> Any: | ||
| """ | ||
| Find best matching expectation based on provided device properties. | ||
| """ | ||
| (result_key, result) = max(self.data.items(), key=lambda x: Expectations.score(key, x[0])) | ||
|
|
||
| if Expectations.score(key, result_key) == 0: | ||
| raise ValueError(f"No matching expectation found for {key}") | ||
|
|
||
| return result | ||
|
|
||
| def __repr__(self): | ||
| return f"{self.data}" | ||
Uh oh!
There was an error while loading. Please reload this page.