flopscope.accounting.svd_cost
flopscope.accounting.svd_cost(m: 'int', n: 'int', k: 'int | None' = None, *, with_vectors: 'bool' = False, full_matrices: 'bool' = False) -> 'int'[flopscope source]
Weighted FLOP cost of an SVD (FMA=2, leading order).
- Full decomposition (
k is None), with a = max(m, n), b = min(m, n): values only (with_vectors=False): 2*a*b^2 + 2*b^3 thin U, V (full_matrices=False): 6*a*b^2 + 20*b^3 full U (full_matrices=True and m != n): 4*a^2*b + 22*b^3
<string>:5: (WARNING/2) Definition list ends without a blank line; unexpected unindent.Constants follow the LAPACK dgesdd path; see docs/reference/cost-model.md.
- Top-k truncated SVD (
1 <= k < min(m, n)): with vectors: min(4mnk + 6(m+n)k^2 + 20k^3, thin economy) values only: min(4mnk + 2(m+n)k^2 + 2k^3, values-only economy)
<string>:10: (WARNING/2) Definition list ends without a blank line; unexpected unindent.The rank-dependent model includes two matrix passes plus quadratic
basis/factor work and a cubic small-decomposition term. This is an
analytical pricing convention, not a literal count of the backend,
which computes an exact economy SVD and slices its outputs.
k >= min(m, n) bills the corresponding economy cost. The full-U
premium applies only to a full decomposition (k is None).
See docs/reference/cost-model.md.
Parameters
- m:int
Number of rows in the input matrix.
- n:int
Number of columns in the input matrix.
- k:int | None, optional
Target rank or number of singular components to estimate. Defaults to
None.- with_vectors:bool, optional
Argument forwarded to the analytical linalg.svd cost formula. Defaults to
False.- full_matrices:bool, optional
Argument forwarded to the analytical linalg.svd cost formula. Defaults to
False.
Returns
- :int
Weighted public cost estimate, floored to match runtime accounting.
Notes
This helper multiplies the analytical FLOP count by the active weight from flopscope._weights and then applies int(...) so public estimates match budget deductions.
Examples
>>> import flopscope as flops
>>> cost = flops.accounting.svd_cost(128, 64)
>>> cost_topk = flops.accounting.svd_cost(128, 64, k=8) # values-only rank-dependent cost, capped at full values-only price
>>> isinstance(cost, int)
True