flopscope.
Guides

Migrate from NumPy

Use this page when converting existing NumPy code to flopscope.

You will learn:

  • How to convert NumPy imports and operations to flopscope equivalents
  • Which NumPy behaviors stay the same and which change
  • How to avoid common pitfalls when migrating

Prerequisites

The basics

Change your import and wrap computation in a BudgetContext:

Before (NumPy):

import numpy as np

W = np.random.randn(256, 256)
x = np.random.randn(256)
h = np.dot(W, x)
h = np.maximum(h, 0)

After (flopscope) — simplest form:

import flopscope as flops
import flopscope.numpy as fnp

# No setup needed — global default budget tracks FLOPs automatically
W = fnp.random.randn(256, 256)
x = fnp.random.randn(256)
h = fnp.dot(W, x)
h = fnp.maximum(h, 0)

# No setup needed — global default budget tracks FLOPs automatically
W = fnp.random.randn(256, 256)
x = fnp.random.randn(256)
h = fnp.dot(W, x)
h = fnp.maximum(h, 0)

flops.budget_summary()  # see what you spent

After (flopscope) — with explicit budget control:

import flopscope as flops
import flopscope.numpy as fnp

with flops.BudgetContext(flop_budget=20_000_000) as budget:
    W = fnp.random.randn(256, 256)
    x = fnp.random.randn(256)
    h = fnp.dot(W, x)
    h = fnp.maximum(h, 0)

What stays the same

  • Function signatures match NumPy for supported operations
  • Broadcasting rules are identical
  • Array indexing, slicing, and assignment work normally

What changes

NumPyflopscopeNotes
import numpy as npimport flopscope.numpy as fnpUse import flopscope as flops alongside it for budgets, symmetry, and accounting helpers
Call ops anywhereWorks anywhere tooA global default budget auto-activates; use explicit BudgetContext for limits and namespacing
np.linalg.svd(A)fnp.linalg.svd(A, k=10)Truncated SVD with explicit k
Plain ndarray onlySymmetricTensor availableWrap with flops.as_symmetric() for cost savings
All NumPy ops availableMost available, 32 blacklistedI/O and config ops raise AttributeError
No cost trackingAutomatic FLOP countingEvery counted op deducts from budget

Common pitfalls

Symptom: AttributeError when calling an I/O or config function (e.g., fnp.save, fnp.seterr)

Fix: 32 operations are blacklisted because they are I/O, configuration, or datetime functions with no FLOP cost. See Operation Categories for the full list. Use numpy directly for these.

Symptom: Using np.linalg.svd instead of fnp.linalg.svd

Fix: If you import NumPy alongside flopscope, make sure to use fnp. for counted operations. Operations called through np. bypass FLOP counting entirely.

On this page