# GET was unable to find an engine to execute this computation

**URL:** <https://discuss.pytorch.org/t/get-was-unable-to-find-an-engine-to-execute-this-computation/200761>\
**Category:** PyTorch Live\
**Created:** [April 12, 2024, 3:14pm UTC](https://discuss.pytorch.org/t/get-was-unable-to-find-an-engine-to-execute-this-computation/200761 "2024-04-12T15:14:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikas\_Amaraneni](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vikas_amaraneni/32/69195_2.png) [@Vikas\_Amaraneni](https://discuss.pytorch.org/u/Vikas_Amaraneni)\
**Post date:** [April 12, 2024, 3:14pm UTC](https://discuss.pytorch.org/t/get-was-unable-to-find-an-engine-to-execute-this-computation/200761/1 "2024-04-12T15:14:30Z")

</div>

RuntimeError Traceback (most recent call last)  
Cell In[16], line 47  
45 num\_epochs = 10  
46 for epoch in range(num\_epochs):  
—\> 47 train\_loss, train\_time = train(model, train\_loader, criterion, optimizer)  
48 val\_loss, val\_accuracy, val\_time = validate(model, val\_loader, criterion)  
49 print(f’Epoch {epoch+1}/{num\_epochs}, Train Loss: {train\_loss:.4f}, Train Time: {train\_time:.2f}s, ’  
50 f’Val Loss: {val\_loss:.4f}, Val Accuracy: {val\_accuracy:.4f}, Val Time: {val\_time:.2f}s’)

Cell In[16], line 13, in train(model, train\_loader, criterion, optimizer)  
11 outputs = model(inputs)  
12 loss = criterion(outputs, labels) # Calculate loss between model outputs and ground truth  
—\> 13 loss.backward()  
14 optimizer.step()  
15 running\_loss += loss.item() \* inputs.size(0) # Update running loss

File ~/.conda/envs/torchTest1/lib/python3.12/site-packages/torch/\_tensor.py:522, in Tensor.backward(self, gradient, retain\_graph, create\_graph, inputs)  
512 if has\_torch\_function\_unary(self):  
513 return handle\_torch\_function(  
514 Tensor.backward,  
515 (self,),  
(…)  
520 inputs=inputs,  
521 )  
 → 522 torch.autograd.backward(  
523 self, gradient, retain\_graph, create\_graph, inputs=inputs  
524 )

File ~/.conda/envs/torchTest1/lib/python3.12/site-packages/torch/autograd/ **init**.py:266, in backward(tensors, grad\_tensors, retain\_graph, create\_graph, grad\_variables, inputs)  
261 retain\_graph = create\_graph  
263 # The reason we repeat the same comment below is that  
264 # some Python versions print out the first line of a multi-line function  
265 # calls in the traceback and some print out the last line  
 → 266 Variable._execution\_engine.run\_backward( # Calls into the C++ engine to run the backward pass  
267 tensors,  
268 grad\_tensors_,  
269 retain\_graph,  
270 create\_graph,  
271 inputs,  
272 allow\_unreachable=True,  
273 accumulate\_grad=True,  
274 )

RuntimeError: GET was unable to find an engine to execute this computation.

Hi, Can anyone help me with this error.

 ![Screenshot 2024-04-12 at 11.13.53 AM](https://discuss.pytorch.org/uploads/default/original/3X/e/c/ecac3196d664cb93df6ec34385036fc05db119cd.png)

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 12, 2024, 9:04pm UTC](https://discuss.pytorch.org/t/get-was-unable-to-find-an-engine-to-execute-this-computation/200761/2 "2024-04-12T21:04:45Z")

</div>

Could you post a minimal and executable code snippet reproducing the issue, please?

---

<div class="post-metadata">

**Author:** ![Vikas\_Amaraneni](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vikas_amaraneni/32/69195_2.png) [@Vikas\_Amaraneni](https://discuss.pytorch.org/u/Vikas_Amaraneni)\
**Post date:** [April 13, 2024, 1:12pm UTC](https://discuss.pytorch.org/t/get-was-unable-to-find-an-engine-to-execute-this-computation/200761/3 "2024-04-13T13:12:23Z")

</div>

# Split the image paths into training and validation sets

train\_images = final\_gray\_images[:92677]  
train\_labels = labels[1:92678]  
val\_images = final\_gray\_images[92678:115845]  
val\_labels = labels[92679:115846]

# Print the lengths of training and validation sets

print(“shape of training images:”, train\_images.shape)  
print(“shape of training labels:”, train\_labels.shape)  
print(“shape of validation images:”, val\_images.shape)  
print(“shape of validation labels:”, val\_labels.shape)

import torch  
import torch.nn as nn  
import torch.optim as optim  
from torch.utils.data import DataLoader, TensorDataset  
from sklearn.preprocessing import LabelEncoder

# Checking if CUDA (GPU) is available

device = “cuda” if torch.cuda.is\_available() else “cpu”  
print(f"Using device: {device}")

# Create a label encoder

label\_encoder = LabelEncoder()

# Fit the label encoder on the entire labels array

label\_encoder.fit(labels)

# Transform the labels to numerical labels

train\_labels\_encoded = label\_encoder.transform(train\_labels)  
val\_labels\_encoded = label\_encoder.transform(val\_labels)

# Convert NumPy arrays to PyTorch tensors and move to the GPU

train\_tensor = torch.tensor(train\_images).float().to(device)  
train\_labels\_tensor = torch.from\_numpy(train\_labels\_encoded).long().to(device)  
val\_tensor = torch.tensor(val\_images).float().to(device)  
val\_labels\_tensor = torch.from\_numpy(val\_labels\_encoded).long().to(device)  
torch.cuda.empty\_cache()

# Create TensorDatasets

train\_dataset = TensorDataset(train\_tensor, train\_labels\_tensor)  
val\_dataset = TensorDataset(val\_tensor, val\_labels\_tensor)

# Create DataLoaders

train\_loader = DataLoader(train\_dataset, batch\_size=1000, shuffle=False)  
val\_loader = DataLoader(val\_dataset, batch\_size=1000, shuffle=False)

class CNNModel(nn.Module):  
def **init** (self):  
super(CNNModel, self). **init** ()  
self.conv1 = nn.Conv2d(1, 64, kernel\_size=3, padding=1)  
self.relu1 = nn.ReLU(inplace=True)  
self.maxpool1 = nn.MaxPool2d(kernel\_size=2, stride=2)  
self.conv2 = nn.Conv2d(64, 128, kernel\_size=3, padding=1)  
self.relu2 = nn.ReLU(inplace=True)  
self.maxpool2 = nn.MaxPool2d(kernel\_size=2, stride=2)  
self.conv3 = nn.Conv2d(128, 256, kernel\_size=3, padding=1)  
self.relu3 = nn.ReLU(inplace=True)  
self.conv4 = nn.Conv2d(256, 256, kernel\_size=3, padding=1)  
self.relu4 = nn.ReLU(inplace=True)  
self.maxpool3 = nn.MaxPool2d(kernel\_size=2, stride=2)  
self.conv5 = nn.Conv2d(256, 512, kernel\_size=3, padding=1)  
self.relu5 = nn.ReLU(inplace=True)  
self.conv6 = nn.Conv2d(512, 512, kernel\_size=3, padding=1)  
self.relu6 = nn.ReLU(inplace=True)  
self.maxpool4 = nn.MaxPool2d(kernel\_size=2, stride=2)  
self.avgpool = nn.AdaptiveAvgPool2d((7, 7))  
self.fc = nn.Linear(512 \* 7 \* 7, 1000)

```
def forward(self, x):
    x = self.conv1(x)
    x = self.relu1(x)
    x = self.maxpool1(x)
    x = self.conv2(x)
    x = self.relu2(x)
    x = self.maxpool2(x)
    x = self.conv3(x)
    x = self.relu3(x)
    x = self.conv4(x)
    x = self.relu4(x)
    x = self.maxpool3(x)
    x = self.conv5(x)
    x = self.relu5(x)
    x = self.conv6(x)
    x = self.relu6(x)
    x = self.maxpool4(x)
    x = self.avgpool(x)
    x = torch.flatten(x, 1)
    x = self.fc(x)
    
    return x

```

# Move the model to the GPU if available

model = CNNModel().to(device)  
criterion = nn.CrossEntropyLoss()  
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Train the model

def train(model, train\_loader, criterion, optimizer):  
model.train()  
running\_loss = 0.0  
start\_time = time.time()  
for inputs, labels in train\_loader:  
inputs = inputs.unsqueeze(1) # Convert input to Float  
labels = labels.view(-1)  
inputs, labels = inputs.to(device), labels.to(device)  
optimizer.zero\_grad()  
outputs = model(inputs)  
loss = criterion(outputs, labels) # Calculate loss between model outputs and ground truth  
loss.backward()  
optimizer.step()  
running\_loss += loss.item() \* inputs.size(0) # Update running loss  
end\_time = time.time()  
return running\_loss / len(train\_loader.dataset), end\_time - start\_time

# Validation function

def validate(model, val\_loader, criterion):  
model.eval()  
start\_time = time.time()  
with torch.no\_grad():  
running\_loss = 0.0  
correct\_preds = 0  
total\_preds = 0  
for inputs, labels in tqdm(val\_loader):  
inputs = inputs.to(device)  
labels = labels.to(device)

```
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        running_loss += loss.item() * inputs.size(0)
        
        # Calculate accuracy
        _, preds = torch.max(outputs, 1)
        correct_preds += torch.sum(preds == labels).item()
        total_preds += inputs.size(0)
end_time = time.time()
val_loss = running_loss / len(val_loader.dataset)
val_accuracy = correct_preds / total_preds
return val_loss, val_accuracy, end_time - start_time

```

# Training loop

num\_epochs = 10  
for epoch in range(num\_epochs):  
train\_loss, train\_time = train(model, train\_loader, criterion, optimizer)  
val\_loss, val\_accuracy, val\_time = validate(model, val\_loader, criterion)  
print(f’Epoch {epoch+1}/{num\_epochs}, Train Loss: {train\_loss:.4f}, Train Time: {train\_time:.2f}s, ’  
f’Val Loss: {val\_loss:.4f}, Val Accuracy: {val\_accuracy:.4f}, Val Time: {val\_time:.2f}s’)

This is the code I’m running. I’m using ImageNet-LT dataset which has 1000 classes. While training the model I’m facing the issue. Can you please help me with this.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 13, 2024, 1:39pm UTC](https://discuss.pytorch.org/t/get-was-unable-to-find-an-engine-to-execute-this-computation/200761/4 "2024-04-13T13:39:38Z")

</div>

Your code is neither properly formatted nor is it executable. Remove the data dependency, check if random inputs would reproduce the issue (they should) and use these instead, and format the code by wrapping it into three backticks ```.

---

<div class="post-metadata">

**Author:** ![Vikas\_Amaraneni](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vikas_amaraneni/32/69195_2.png) [@Vikas\_Amaraneni](https://discuss.pytorch.org/u/Vikas_Amaraneni)\
**Post date:** [April 14, 2024, 5:29pm UTC](https://discuss.pytorch.org/t/get-was-unable-to-find-an-engine-to-execute-this-computation/200761/5 "2024-04-14T17:29:41Z")

</div>

I’m sorry. It went wrong while copy and pasting the code

---

<div class="post-metadata">

**Author:** ![Vikas\_Amaraneni](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vikas_amaraneni/32/69195_2.png) [@Vikas\_Amaraneni](https://discuss.pytorch.org/u/Vikas_Amaraneni)\
**Post date:** [April 14, 2024, 5:37pm UTC](https://discuss.pytorch.org/t/get-was-unable-to-find-an-engine-to-execute-this-computation/200761/6 "2024-04-14T17:37:45Z")

</div>

![Screenshot 2024-04-14 at 1.34.03 PM](https://discuss.pytorch.org/uploads/default/original/3X/9/b/9b601f29dd3a72d0f3b2093b212ad2ac7d8060d1.png)  
I’m getting error while executing the code and the error is highlighting at ‘loss = criterion(outputs, labels)’. I’m using ImageNet-LT dataset and it has 1000 classes. torch version is 2.2.1, python version is 3.12.2, and CUDA version is 12.1.
