Smash your model with a CPU only

Open In Colab

This tutorial demonstrates how to use the pruna package to optimize any model on CPU. We will use the vit_b_16 computer visionmodel as an example.

1. Loading the CV Model

First, load your ViT model.

[ ]:
import torchvision

model = torchvision.models.vit_b_16(weights="ViT_B_16_Weights.DEFAULT")

2. Initializing the Smash Config

Next, initialize the smash_config.

[ ]:
from pruna import SmashConfig

# Initialize the SmashConfig
smash_config = SmashConfig()
smash_config["compilers"] = "torch_compile"
smash_config["comp_torch_compile_backend"] = "openvino"

3. Smashing the Model

Now, smash the model. This will only take a few seconds. Don’t forget to replace the token by the one provided by PrunaAI.

[ ]:
from pruna import smash

# Smash the model
smashed_model = smash(
    model=model,
    token='<your_token>',  # replace <your-token> with your actual token or set to None if you do not have one yet
    smash_config=smash_config,
)

4. Preparing the Input

[ ]:
import numpy as np
from torchvision import transforms

# Generating a random image
image = np.random.randint(0, 256, size=(224, 224, 3)).astype(dtype=np.float32)
input_tensor = transforms.ToTensor()(image).unsqueeze(0)

5. Running the Model

Finally, run the model to process the image.

[ ]:
# Display the result
smashed_model(input_tensor)

Wrap Up

Congratulations! You have successfully smashed a CV model on CPU. You can now use the pruna package to optimize any model on a CPU. The only parts that you should modify are step 1, 4 and 5 to fit your use case