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 spentAfter (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
| NumPy | flopscope | Notes |
|---|---|---|
import numpy as np | import flopscope.numpy as fnp | Use import flopscope as flops alongside it for budgets, symmetry, and accounting helpers |
| Call ops anywhere | Works anywhere too | A 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 only | SymmetricTensor available | Wrap with flops.as_symmetric() for cost savings |
| All NumPy ops available | Most available, 32 blacklisted | I/O and config ops raise AttributeError |
| No cost tracking | Automatic FLOP counting | Every 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.
Related pages
- Operation Categories — what's supported and what isn't
- API Reference — full list of all operations