{"id":1766,"library":"ultralytics-thop","title":"Ultralytics THOP","description":"Ultralytics THOP is a Python library for fast computation of PyTorch model Multiply-Accumulate Operations (MACs) and parameters. It is based on the original `thop` project, maintained by Ultralytics, and provides a robust tool for analyzing the computational cost of deep learning models. The current version is 2.0.18, with a maintenance-focused release cadence.","status":"active","version":"2.0.18","language":"en","source_language":"en","source_url":"https://github.com/ultralytics/thop","tags":["pytorch","model profiling","flops","macs","performance","deep learning"],"install":[{"cmd":"pip install ultralytics-thop","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for PyTorch model definition and tensor operations.","package":"torch","optional":false}],"imports":[{"symbol":"profile","correct":"from thop import profile"},{"symbol":"clever_format","correct":"from thop import clever_format"},{"symbol":"profile_origin","correct":"from thop import profile_origin"}],"quickstart":{"code":"import torch\nimport torch.nn as nn\nfrom thop import profile, clever_format\n\n# Define a simple PyTorch model\nclass SimpleModel(nn.Module):\n    def __init__(self):\n        super(SimpleModel, self).__init__()\n        self.conv1 = nn.Conv2d(3, 64, kernel_size=3, padding=1)\n        self.relu = nn.ReLU()\n        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)\n        self.conv2 = nn.Conv2d(64, 128, kernel_size=3, padding=1)\n        self.fc = nn.Linear(128 * 8 * 8, 10) # Assuming input size 3x32x32 initially\n\n    def forward(self, x):\n        x = self.pool(self.relu(self.conv1(x)))\n        x = self.pool(self.relu(self.conv2(x)))\n        x = torch.flatten(x, 1)\n        x = self.fc(x)\n        return x\n\n# Instantiate the model and create a dummy input\nmodel = SimpleModel()\ninput_tensor = torch.randn(1, 3, 32, 32)\n\n# Profile the model\nmacs, params = profile(model, inputs=(input_tensor,), verbose=False)\n\n# Format the output for readability\nmacs_formatted, params_formatted = clever_format([macs, params], \"%.3f\")\n\nprint(f\"Model MACs: {macs_formatted}\")\nprint(f\"Model Params: {params_formatted}\")","lang":"python","description":"This example defines a basic convolutional neural network, creates a dummy input tensor, and then uses `thop.profile` to compute its MACs and parameters. `clever_format` is used to make the output human-readable."},"warnings":[{"fix":"Be aware of the distinction when comparing with other profiling tools. The v2.0.16 release explicitly clarified this terminology.","message":"Ultralytics THOP reports 'MACs' (Multiply-Accumulate Operations) by default, not 'FLOPs' (Floating Point Operations). While often used interchangeably, MACs are technically different from FLOPs and can lead to different interpretations of computational cost.","severity":"gotcha","affected_versions":">=2.0.16"},{"fix":"For custom layers or specific non-counted operations, you may need to define and pass `custom_ops` to the `profile` function to ensure comprehensive counting. Consult the `thop` documentation for how to implement custom hooks.","message":"By default, `thop.profile` primarily counts operations for common layers (e.g., convolutions, linear layers). It may not accurately count all operations, especially for non-linearities (like ReLU, Sigmoid), element-wise operations, or custom layers.","severity":"gotcha","affected_versions":"all"},{"fix":"Always profile your model with an input shape representative of its actual use case to get meaningful and accurate performance estimates.","message":"The MACs and parameter counts are sensitive to the input tensor's shape. Different input sizes will yield different profiling results for many layers (e.g., convolutional layers, pooling).","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}