Skip to content

AttributeError: module 'torch.xpu' has no attribute 'has_2d_block_array' #831

Open
@kdbeall

Description

@kdbeall

Describe the bug

Hi! I'm running the latest 2.7 Pytorch+XPU and 2.7 IPEX
I'm using the Intel Arc B580 card.

I end up with the error

AttributeError: module 'torch.xpu' has no attribute 'has_2d_block_array' #831
import torch
from transformers import AutoTokenizer, AutoModel
import intel_extension_for_pytorch as ipex  # Triggers error internally

import torch.nn.functional as F

def mean_pooling(last_hidden_state, attention_mask):
    mask = attention_mask.unsqueeze(-1).expand(last_hidden_state.size()).float()
    return torch.sum(last_hidden_state * mask, 1) / torch.clamp(mask.sum(1), min=1e-9)

# Setup
model_name = "nomic-ai/modernbert-embed-base"
device = torch.device("xpu:1")

# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name).to(device).half().eval()

# This line triggers the AttributeError
model = ipex.optimize(model, dtype=torch.float16, inplace=True)

# Input text
texts = ["What is TSNE?", "How do vector embeddings work?"]
inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True)
inputs = {k: v.to(device).half() for k, v in inputs.items()}

# Inference
with torch.no_grad():
    out = model(**inputs)
    pooled = mean_pooling(out.last_hidden_state, inputs["attention_mask"])
    embeddings = F.normalize(pooled, p=2, dim=1)

print("Shape:", embeddings.shape)
print("First 5 dims:", embeddings[0][:5].tolist())

Versions

collect_env.py fails with

[W523 22:33:19.194673362 OperatorEntry.cpp:154] Warning: Warning only once for all operators,  other operators may also be overridden.
  Overriding a previously registered kernel for the same operator and the same dispatch key
  operator: aten::_addmm_activation(Tensor self, Tensor mat1, Tensor mat2, *, Scalar beta=1, Scalar alpha=1, bool use_gelu=False) -> Tensor
    registered at /pytorch/build/aten/src/ATen/RegisterSchema.cpp:6
  dispatch key: AutocastCPU
  previous kernel: registered at /pytorch/aten/src/ATen/autocast_mode.cpp:327
       new kernel: registered at /opt/workspace/ipex-cpu-dev/csrc/cpu/autocast/autocast_mode.cpp:112 (function operator())
Collecting environment information...
Traceback (most recent call last):
  File "/opt/backboost/temp/collect_env.py", line 616, in <module>
    main()
  File "/opt/backboost/temp/collect_env.py", line 610, in main
    output = get_pretty_env_info()
             ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/backboost/temp/collect_env.py", line 605, in get_pretty_env_info
    return pretty_str(get_env_info())
                      ^^^^^^^^^^^^^^
  File "/opt/backboost/temp/collect_env.py", line 449, in get_env_info
    pyenv, pip_list_output = get_python_packages(run_lambda)
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/backboost/temp/collect_env.py", line 442, in get_python_packages
    pkgs_filtered = filter_python_packages(out)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/backboost/temp/collect_env.py", line 407, in filter_python_packages
    for line in data.splitlines()
                ^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'splitlines'

Activity

jingxu10

jingxu10 commented on May 29, 2025

@jingxu10
Contributor

Could you share a full log of the no attribute error?

kdbeall

kdbeall commented on May 29, 2025

@kdbeall
Author

@jingxu10 Solved it with monkey patching

Unbreak4ble

Unbreak4ble commented on Jun 1, 2025

@Unbreak4ble

@jingxu10 Solved it with monkey patching

can you give more detail?

kdbeall

kdbeall commented on Jun 8, 2025

@kdbeall
Author
if hasattr(torch, "xpu") and not hasattr(torch.xpu, "has_2d_block_array"):
    torch.xpu.has_2d_block_array = lambda: False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      AttributeError: module 'torch.xpu' has no attribute 'has_2d_block_array' · Issue #831 · intel/intel-extension-for-pytorch