Skip to content

Bug: ChebyshevTT rank caps overflow for high-dimensional grids #24

@0xC000005

Description

@0xC000005

Related C# issue: 0xC000005/ChebyshevSharp#23

Summary

ChebyshevTT has a high-dimensional integer overflow bug in the TT-Cross rank-cap path. The cache-key design is already safe because _tt_cross uses tuple multi-indices, but rank-cap products still use np.prod, which overflows as numpy.int64 for large grids before being converted to Python int.

This is the Python-side analogue of the ChebyshevSharp high-dimensional TT issue, though the C# issue also includes a flattened long cache-key problem.

Evidence

Relevant code:

  • src/pychebyshev/tensor_train.py, _tt_cross: left_size = int(np.prod(n[:k])), right_size = int(np.prod(n[k:]))
  • Similar full-grid products appear in diagnostics such as compression_ratio, get_num_evaluation_points, and __str__

Minimal overflow check:

python -c "import numpy as np; x=np.prod([7]*35); print(x, type(x), getattr(x,'dtype',None)); print(int(x))"

Observed:

-6870017497503494569 <class 'numpy.int64'> int64
-6870017497503494569

Minimal ChebyshevTT repro:

import sys
sys.path.insert(0, "/home/max/Documents/PyChebyshev/src")
from pychebyshev import ChebyshevTT

N = 35
n = 7

def f(x, data=None):
    return float(sum(x))

tt = ChebyshevTT(
    f,
    N,
    [[-1.0, 1.0] for _ in range(N)],
    [n] * N,
    max_rank=12,
    tolerance=1e-5,
    max_sweeps=1,
)
tt.build(verbose=False, seed=42)

Observed failure:

ValueError: negative dimensions are not allowed

MoCaX comparison

MoCaX Extend handles the same high-dimensional shape differently. A local probe with:

MocaxExtend(35, [7] * 35, [[-1.0, 1.0]] * 35)

reported:

get_tensor_size() == 378818692265664781682717625943

as a Python int. subgrid_by_number(500), set_subgrid_values, gen_train_val_data, a minimal run_rank_adaptive_algo(max_rank=2, max_iters=2, max_rounds=1), and cheb_tensor_evals completed without overflow.

Proposed fix

  • Replace np.prod rank-cap products in TT-Cross with Python math.prod or, better, a capped product helper that stops once the product reaches max_rank.
  • Keep tuple multi-index cache keys; that part is already the right design.
  • Use guarded/full-precision products for diagnostics, and make full-grid materialization APIs fail clearly when the tensor is too large.
  • Add a regression test for ChebyshevTT with N=35, n=7, small max_rank, and max_sweeps=1 to verify the build path no longer overflows.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingenhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions