Prefer `is_torch_tensor` over `hasattr` for torch.compile. (#2387)
* Prefer `is_torch_tensor` over `hasattr` for `torch.compile`.
`torch.compile` breaks when using `hasattr` but succeeds when using `isinstance(torch.Tensor)`. This commit short-circuits the `hasattr` call for `torch.Tensor`s if possible.
Note: `is_npu_available` is also not torch.compila compatible due to (1) lru_cache and (2) importlib checks, so I've moved it into the try block, catching the AssertionError instead.
* Fix torch.device("npu").
This is not available in non-npu pytorch. Note that
torch.device automatically assigns an index when created as torch.device("npu"), so overwriting device with `"npu:0"` is only required if device is a string "npu".
* Remove unittest.main execution.
* Fix style broken by merge save.
* Import operations functions directly.
* fix style
* Fix imports attempt 2.
* Re-raise error if no NPU available.