FLOP Cost Query API
Pre-execution cost estimation functions. These are pure functions that compute FLOP costs from shapes without executing anything or consuming budget.
Quick example
import mechestim as me
# Einsum cost
cost = me.flops.einsum_cost('ij,jk->ik', shapes=[(256, 256), (256, 256)])
print(f"Matmul: {cost:,} FLOPs") # 33,554,432
# SVD cost
cost = me.flops.svd_cost(m=256, n=256, k=10)
print(f"SVD: {cost:,} FLOPs") # 655,360
# Pointwise (unary/binary) cost
cost = me.flops.pointwise_cost(shape=(256, 256))
print(f"Pointwise: {cost:,} FLOPs") # 65,536
# Reduction cost
cost = me.flops.reduction_cost(input_shape=(1000, 100))
print(f"Reduction: {cost:,} FLOPs") # 100,000
API Reference
mechestim._flops
FLOP cost calculators for mechestim operations.
einsum_cost(subscripts, shapes, operand_symmetries=None)
FLOP cost of an einsum operation.
Delegates to contract_path from opt_einsum, which uses flop_count
with op_factor (multiply-add = 2 FLOPs for inner products).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subscripts
|
str
|
Einsum subscript string. |
required |
shapes
|
list of tuple of int
|
Shapes of the input operands. |
required |
operand_symmetries
|
list of SymmetryInfo or None
|
Symmetry information for each input operand. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count. |
Source code in src/mechestim/_flops.py
parse_einsum_subscripts(subscripts)
Parse an einsum subscript string into input and output index lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subscripts
|
str
|
Einsum subscript string (e.g., |
required |
Returns:
| Name | Type | Description |
|---|---|---|
inputs |
list of list of str
|
Index labels for each input operand. |
output |
list of str
|
Index labels for the output. |
Source code in src/mechestim/_flops.py
pointwise_cost(shape, symmetry_info=None)
FLOP cost of a pointwise (element-wise) operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple of int
|
Shape of the array. |
required |
symmetry_info
|
SymmetryInfo or None
|
If provided, only unique elements are counted. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count (one per element, or one per unique element). |
Source code in src/mechestim/_flops.py
reduction_cost(input_shape, axis=None, symmetry_info=None)
FLOP cost of a reduction operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_shape
|
tuple of int
|
Shape of the input array. |
required |
axis
|
int or None
|
Axis along which to reduce. If None, reduce over all elements. |
None
|
symmetry_info
|
SymmetryInfo or None
|
If provided, only unique elements are counted. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count (one per element). |
Notes
The axis parameter is accepted for API consistency but does not
affect the result: a reduction always touches every element regardless
of which axis is reduced, so the cost is always prod(input_shape).
Source code in src/mechestim/_flops.py
search_cost(queries, sorted_size)
FLOP cost of binary search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queries
|
int
|
Number of search queries. |
required |
sorted_size
|
int
|
Size of the sorted array being searched. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: queries * ceil(log2(sorted_size)). |
Source code in src/mechestim/_flops.py
sort_cost(n)
FLOP cost of comparison-based sort.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of elements to sort. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: n * ceil(log2(n)). |
Source code in src/mechestim/_flops.py
svd_cost(m, n, k=None)
FLOP cost of a (truncated) SVD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
int
|
Number of rows. |
required |
n
|
int
|
Number of columns. |
required |
k
|
int or None
|
Number of singular values/vectors to compute. Defaults to min(m, n). |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated FLOP count: m * n * k. |
Notes
Based on Golub-Reinsch bidiagonalization.