- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have installed IPEX and I ran the following code:
!pip3 install torch
!pip3 install intel_extension_for_pytorch
import logging
import os
import random
import re
os.environ["SYCL_PI_LEVEL_ZERO_USE_IMMEDIATE_COMMANDLISTS"] = "1"
os.environ["ENABLE_SDP_FUSION"] = "1"
import warnings
# Suppress warnings for a cleaner output
warnings.filterwarnings("ignore")
import torch
import intel_extension_for_pytorch as ipex
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers import LlamaTokenizer, LlamaForCausalLM
from transformers import BertTokenizer, BertForSequenceClassification
from ipywidgets import VBox, HBox, Button, Dropdown, IntSlider, FloatSlider, Text, Output, Label, Layout
import ipywidgets as widgets
from ipywidgets import HTML
# random seed
if torch.xpu.is_available():
seed = 88
random.seed(seed)
torch.xpu.manual_seed(seed)
torch.xpu.manual_seed_all(seed)
def select_device(preferred_device=None):
"""
Selects the best available XPU device or the preferred device if specified.
Args:
preferred_device (str, optional): Preferred device string (e.g., "cpu", "xpu", "xpu:0", "xpu:1", etc.). If None, a random available XPU device will be selected or CPU if no XPU devices are available.
Returns:
torch.device: The selected device object.
"""
try:
if preferred_device and preferred_device.startswith("cpu"):
print("Using CPU.")
return torch.device("cpu")
if preferred_device and preferred_device.startswith("xpu"):
if preferred_device == "xpu" or (
":" in preferred_device
and int(preferred_device.split(":")[1]) >= torch.xpu.device_count()
):
preferred_device = (
None # Handle as if no preferred device was specified
)
else:
device = torch.device(preferred_device)
if device.type == "xpu" and device.index < torch.xpu.device_count():
vram_used = torch.xpu.memory_allocated(device) / (
1024**2
) # In MB
print(
f"Using preferred device: {device}, VRAM used: {vram_used:.2f} MB"
)
return device
if torch.xpu.is_available():
device_id = random.choice(
range(torch.xpu.device_count())
) # Select a random available XPU device
device = torch.device(f"xpu:{device_id}")
vram_used = torch.xpu.memory_allocated(device) / (1024**2) # In MB
print(f"Selected device: {device}, VRAM used: {vram_used:.2f} MB")
return device
except Exception as e:
print(f"An error occurred while selecting the device: {e}")
print("No XPU devices available or preferred device not found. Using CPU.")
return torch.device("cpu")
Error: /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages/oneccl_bindings_for_pytorch/lib/liboneccl_bindings_for_pytorch.so: undefined symbol: _ZN3c106DeviceC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi cboiteux,
Thank you for reaching out.
Have you run any training from Training and Workshops? And which kernel did you use?
There is a discussion about this issue here that you can look at.
Regards,
Zul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for your question. If you need any additional information from Intel, please submit a new question as this thread is no longer being monitored.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page