Skip to content

Commit

Permalink
Fix a bug that engine.test doesn't work with XPU (#3293)
Browse files Browse the repository at this point in the history
* fix bug

* align with pre-commit

---------

Co-authored-by: Emily <emily.chun@intel.com>
  • Loading branch information
2 people authored and kprokofi committed Apr 12, 2024
1 parent ae03351 commit 023cc8f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/otx/algo/strategies/xpu_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ def is_distributed(self) -> bool:
def setup_optimizers(self, trainer: pl.Trainer) -> None:
"""Sets up optimizers."""
super().setup_optimizers(trainer)
if len(self.optimizers) != 1: # type: ignore[has-type]
if len(self.optimizers) > 1: # type: ignore[has-type]
msg = "XPU strategy doesn't support multiple optimizers"
raise RuntimeError(msg)
if trainer.task != "SEMANTIC_SEGMENTATION":
model, optimizer = torch.xpu.optimize(trainer.model, optimizer=self.optimizers[0]) # type: ignore[has-type]
self.optimizers = [optimizer]
self.model = model
if len(self.optimizers) == 1: # type: ignore[has-type]
model, optimizer = torch.xpu.optimize(trainer.model, optimizer=self.optimizers[0]) # type: ignore[has-type]
self.optimizers = [optimizer]
self.model = model
else: # for inference
trainer.model.eval()
self.model = torch.xpu.optimize(trainer.model)


StrategyRegistry.register(
Expand Down
2 changes: 1 addition & 1 deletion src/otx/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ def _build_trainer(self, **kwargs) -> None:
if self._device.accelerator == DeviceType.xpu:
self._cache.update(strategy="xpu_single")
# add plugin for Automatic Mixed Precision on XPU
if self._cache.args["precision"] == 16:
if self._cache.args.get("precision", 32) == 16:
self._cache.update(plugins=[MixedPrecisionXPUPlugin()])
self._cache.args["precision"] = None

Expand Down

0 comments on commit 023cc8f

Please sign in to comment.