Skip to content

Commit a92db5d

Browse files
eunwooshkprokofi
andauthored
XPU code update + PyTorch upgrade (#4048)
* draft implementation * remove unnecessary file * remove unnecessary directory * update cuda oriented code * fix typo * separate device util files * add missing file * fix bug * use bf16 when mixed precision * add warn * remove code to disable xpu * enable all models in the perf test * fix linter * remove warning * added missing head * add installation to toml for xpu * added fixes for few tasks * fix linter, add device to tests * remove redundant test * remove outdated piece of test * fix linter * rever MaskDino back * fix unit test * installation for XPU is verified * fix unit tests * update guide --------- Co-authored-by: kprokofi <[email protected]>
1 parent fc221e8 commit a92db5d

File tree

35 files changed

+85
-520
lines changed

35 files changed

+85
-520
lines changed

docs/source/guide/get_started/installation.rst

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -79,71 +79,26 @@ Install OpenVINO™ Training Extensions for users (XPU devices)
7979
*************************************************************
8080

8181
1. Install OpenVINO™ Training Extensions
82-
package:
83-
84-
* A local source in development mode
82+
from source to use XPU functionality.
8583

8684
.. tab-set::
8785

88-
.. tab-item:: PyPI
89-
90-
.. code-block:: shell
91-
92-
pip install otx
93-
94-
.. tab-item:: Source
86+
.. tab-item:: Minimum requirements
9587

9688
.. code-block:: shell
9789
9890
# Clone the training_extensions repository with the following command:
9991
git clone https://github.com/openvinotoolkit/training_extensions.git
10092
cd training_extensions
10193
102-
# Set up a virtual environment.
103-
python -m venv .otx
104-
source .otx/bin/activate
105-
106-
pip install -e .
107-
108-
2. Install Intel Extensions for Pytorch & Requirements
109-
for training according to your system environment.
110-
111-
.. tab-set::
112-
113-
.. tab-item:: Minimum requirements
114-
115-
.. code-block:: shell
116-
117-
pip install '.[xpu]' --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
118-
119-
.. note::
120-
121-
Models from mmlab are not available for this environment. If you want to use mmlab models, you must install them with Full Requirements.
122-
Also, some tasks may not be supported by minimum requirements.
94+
pip install -e '.[base]' --extra-index-url https://download.pytorch.org/whl/test/xpu
12395
124-
.. tab-item:: Full Requirements
125-
126-
.. code-block:: shell
127-
python -m pip install torch==2.1.0.post2 torchvision==0.16.0.post2 torchaudio==2.1.0.post2 intel-extension-for-pytorch==2.1.30+xpu oneccl_bind_pt==2.1.300+xpu --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
128-
git clone https://github.com/open-mmlab/mmcv
129-
cd mmcv
130-
git checkout v2.1.0
131-
MMCV_WITH_OPS=1 pip install -e .
132-
cd ..
133-
otx install -v --do-not-install-torch
134-
135-
[Optional] Refer to the `Intel® Extension for PyTorch documentation guide <https://intel.github.io/intel-extension-for-pytorch/index.html#installation?platform=gpu&version=v2.1.10%2bxpu&os=linux%2fwsl2&package=pip>`_
136-
137-
3. Activate OneAPI environment
138-
and export required IPEX system variables
139-
140-
.. code-block:: shell
96+
.. note::
14197

142-
source /path/to/intel/oneapi/setvars.sh
143-
export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.30
144-
export IPEX_FP32_MATH_MODE=TF32
98+
Please, refer to the `PyTorch official documentation guide <https://pytorch.org/docs/stable/notes/get_start_xpu.html>`_
99+
to install prerequisites and resolve possible issues.
145100

146-
3. Once the package is installed in the virtual environment, you can use full
101+
2. Once the package is installed in the virtual environment, you can use full
147102
OpenVINO™ Training Extensions command line functionality.
148103

149104
.. code-block:: shell

pyproject.toml

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,8 @@ docs = [
7272
"nbsphinx",
7373
]
7474

75-
xpu = [
76-
"torch==2.1.0.post2",
77-
"torchvision==0.16.0.post2",
78-
"intel-extension-for-pytorch==2.1.30+xpu",
79-
"oneccl_bind_pt==2.1.300+xpu",
80-
"lightning==2.2",
81-
"pytorchcv==0.0.67",
82-
"timm==1.0.3",
83-
"openvino==2024.4",
84-
"openvino-dev==2024.4",
85-
"openvino-model-api==0.2.5",
86-
"onnx==1.17.0",
87-
"onnxconverter-common==1.14.0",
88-
"nncf==2.13.0",
89-
"anomalib[core]==1.1.0",
90-
]
91-
9275
base = [
93-
"torch==2.4.0",
76+
"torch==2.5.1",
9477
"lightning==2.4.0",
9578
"pytorchcv==0.0.67",
9679
"timm==1.0.3",

src/otx/algo/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55

66
from . import (
77
accelerators,
8-
plugins,
98
strategies,
109
)
1110

1211
__all__ = [
1312
"strategies",
1413
"accelerators",
15-
"plugins",
1614
]

src/otx/algo/accelerators/xpu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from lightning.pytorch.accelerators import AcceleratorRegistry
1111
from lightning.pytorch.accelerators.accelerator import Accelerator
1212

13-
from otx.utils.utils import is_xpu_available
13+
from otx.utils.device import is_xpu_available
1414

1515

1616
class XPUAccelerator(Accelerator):

src/otx/algo/common/losses/cross_focal_loss.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import torch
99
import torch.nn.functional
10+
from otx.utils.device import get_available_device
1011
from torch import Tensor, nn
1112
from torch.amp import custom_fwd
1213

@@ -79,7 +80,7 @@ def __init__(
7980

8081
self.cls_criterion = cross_sigmoid_focal_loss
8182

82-
@custom_fwd(cast_inputs=torch.float32, device_type="cuda")
83+
@custom_fwd(device_type=get_available_device(), cast_inputs=torch.float32)
8384
def forward(
8485
self,
8586
pred: Tensor,

src/otx/algo/common/utils/samplers/base_sampler.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import numpy as np
1515
import torch
1616
from otx.algo.common.utils.structures import AssignResult, SamplingResult
17+
from otx.utils.device import is_xpu_available
1718

1819
if TYPE_CHECKING:
1920
from otx.algo.utils.mmengine_utils import InstanceData
@@ -218,7 +219,13 @@ def random_choice(self, gallery: torch.Tensor | np.ndarray | list, num: int) ->
218219
raise ValueError(msg)
219220

220221
is_tensor = isinstance(gallery, torch.Tensor)
221-
device = torch.cuda.current_device() if torch.cuda.is_available() else "cpu"
222+
if is_xpu_available():
223+
device = torch.xpu.current_device()
224+
elif torch.cuda.is_available():
225+
device = torch.cuda.current_device()
226+
else:
227+
device = "cpu"
228+
222229
_gallery: torch.Tensor = torch.tensor(gallery, dtype=torch.long, device=device) if not is_tensor else gallery
223230
perm = torch.randperm(_gallery.numel())[:num].to(device=_gallery.device)
224231
rand_inds = _gallery[perm]

src/otx/algo/detection/atss.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
if TYPE_CHECKING:
2828
from lightning.pytorch.cli import LRSchedulerCallable, OptimizerCallable
2929
from torch import nn
30-
from typing_extensions import Self
3130

3231
from otx.core.metrics import MetricCallable
3332
from otx.core.schedulers import LRSchedulerListCallable
@@ -195,11 +194,3 @@ def _exporter(self) -> OTXModelExporter:
195194
def load_from_otx_v1_ckpt(self, state_dict: dict, add_prefix: str = "model.") -> dict:
196195
"""Load the previous OTX ckpt according to OTX2.0."""
197196
return OTXv1Helper.load_det_ckpt(state_dict, add_prefix)
198-
199-
def to(self, *args, **kwargs) -> Self:
200-
"""Return a model with specified device."""
201-
ret = super().to(*args, **kwargs)
202-
if self.model_name == "atss_resnext101" and self.device.type == "xpu":
203-
msg = f"{type(self).__name__} doesn't support XPU."
204-
raise RuntimeError(msg)
205-
return ret

src/otx/algo/detection/utils/assigners/sim_ota_assigner.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ def assign(
9090
priors = pred_instances.priors # type: ignore[attr-defined]
9191
num_bboxes = decoded_bboxes.size(0)
9292

93+
device = gt_bboxes.device.type
94+
9395
# assign 0 by default
9496
assigned_gt_inds = decoded_bboxes.new_full((num_bboxes,), 0, dtype=torch.long)
9597
if num_gt == 0 or num_bboxes == 0:
@@ -117,7 +119,7 @@ def assign(
117119

118120
valid_pred_scores = valid_pred_scores.unsqueeze(1).repeat(1, num_gt, 1)
119121
# disable AMP autocast and calculate BCE with FP32 to avoid overflow
120-
with torch.cuda.amp.autocast(enabled=False):
122+
with torch.amp.autocast(device_type=device, enabled=False):
121123
cls_cost = (
122124
F.binary_cross_entropy(
123125
valid_pred_scores.to(dtype=torch.float32),

src/otx/algo/keypoint_detection/heads/rtmcc_head.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def loss(self, x: tuple[Tensor], entity: KeypointDetBatchDataEntity) -> dict:
192192
mask=mask,
193193
)
194194

195-
loss_pose = -1 * torch.tensor(avg_acc, device=device)
195+
loss_pose = -1 * torch.tensor(avg_acc, device=device, dtype=torch.float32)
196196
losses.update(loss_pose=loss_pose)
197197

198198
return losses

src/otx/algo/plugins/__init__.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)