Tessero 0.4.0
Current stable · released 2026-11-18
Changed
-
Single maintainer, closed contribution. Tessero stays open source (BSD-3-Clause) but no longer accepts pull requests or patches, and the public issue tracker is closed. Bugs go to [email protected], vulnerabilities to [email protected].
CONTRIBUTING.md,GOVERNANCE.md,MAINTAINERS.md(Douglas Johnson, sole maintainer) and the roadmap are updated; the issue templates and local agent notes are removed from the repository. -
minimizewith methods 'TNC' and 'Newton-CG' runs SciPy's algorithms (approved by DJ, 15 Nov; their results change to SciPy's). 'TNC' is a port of the C code SciPy wraps (Nash's truncated Newton, scipy/optimize/tnc/tnc.c) with SciPy's defaults, return codes and messages, including the scaling to the bounds, the preconditioned CG direction, Gill and Murray's line search,maxfunand SciPy's forward-difference gradient when none is given;minimizetakes variables fixed by equal bounds out of the problem as SciPy's does. It was Tessero's own truncated Newton (on a 10,000 x 50 logistic regression it ran 800 iterations without converging, 5.4 s; now 12 iterations, as SciPy, 0.14 to 0.16 s against SciPy's 0.06 s). 'Newton-CG' follows SciPy's code with its line search (MINPACK's dcsrch, then scalar_search_wolfe2) and no longer evaluates the objective at every Hessian-vector product. On objectives whose arithmetic is the same in PHP and Python both return SciPy's iterates and results bit for bit (16 test problems). New TNC options:eps,scale,offset,maxCGit,maxfun,eta,stepmx,accuracy,minfev,ftol,xtol,rescale; Newton-CG takeseps,c1andc2.nfevandnjevcount the evaluations Tessero makes, which can be fewer than SciPy's. -
SciPy's own algorithms for
quadand the special-function zeros (their values now follow SciPy's, from the same steps):integrate.quadis QUADPACK's DQAGSE, ported step for step from SciPy's C translation: the 10-21 Gauss-Kronrod rule, bisection of the largest error and the epsilon-algorithm extrapolation past endpoint singularities. It was a 7-15 rule without extrapolation. The value, the error estimate and the number of integrand calls are SciPy's: 21 calls instead of 45 forsin(x) exp(-x)on [0, 10], 2 to 5 times fewer on endpoint singularities (sqrt,log,1/sqrtat 0), 21 instead of 15 where one 15-point panel was enough. Equal limits return[0.0, 0.0]without a call and reversed limits are integrated as SciPy does (over[b, a], negated).dblquad,tplquadandnquaduse it.- The zeros of the Kelvin functions (
kelvin_zeros,ber_zeros...keip_zeros), ofJ_n,J_n',Y_n,Y_n'(jn_zeros,jnp_zeros,yn_zeros,ynp_zeros,jnyn_zeros), ofY0,Y1,Y1'(y0_zeros,y1_zeros,y1p_zeros) and of the Airy functions (ai_zeros,bi_zeros) come from SciPy's specfun routines (KLVNZO, JYZO, CYZO, AIRYZO) in the vendored xsf: asymptotic starts and Newton steps, where Tessero scanned in steps of 0.02 or 0.05 and refined each sign change (4 to 180 times slower than SciPy). Unlike xsf, a zero that does not converge ends with an error instead of iterating forever (ber_zeros(5000)reaches past the functions' range). The Bessel and Airy limits rise from 1,000 and 2,000 zeros to a bound on the work. pro_cv_seqandobl_cv_seqtake every mode's characteristic value from one SEGV call, as SciPy does, instead of onepro_cv/obl_cvcall per mode.optimize.curve_fit(LeastSquares::curveFit) runs MINPACK'slmdif, the Levenberg-Marquardt method with a QR-factored step that SciPy'scurve_fituses without bounds (SciPy 1.17.1's C translation, in the kernel aslinalg.lmdif), with leastsq's defaults (maxfev200 (n + 1),ftol=xtol= 1.49012e-8,gtol0). It ran Tessero's own Levenberg-Marquardt on the normal equations. On models whose arithmetic is the same in PHP and NumPy the parameters, residuals, number of evaluations and status are SciPy's bit for bit (11 test problems, both backends), and the covariance (leastsq'scov_x, by a fixed-order triangular inverse) is within 1 ulp of SciPy's, which its BLAS forms. A failed fit raisesConvergenceErrorwith leastsq's message; a covariance that cannot be estimated is allINF, as in SciPy. New:fullOutput: truereturns[popt, pcov, infodict (nfev, fvec), mesg, ier], and xdata given as an NDArray (either backend) calls the model on the whole array, as SciPy does (f(NDArray $x, float ...$p)), the data staying in the kernel; such a model gets a Jacobian's n evaluations in one call (x as a row (1, m), the parameters as columns (n, 1)) when that gives the same bits as n calls, checked once per fit. The 1,000-point exponential fit: 5.0 ms before, 0.44 ms with an array model through the extension (SciPy 0.55 ms), 1.1 ms through FFI, 1.8 to 2.6 ms with a per-point model.optimize.leastsq(LeastSquares::solve) andoptimize.least_squares(LeastSquares::leastSquares) run the same MINPACK code (approved by DJ, 14 Nov; the results' last bits change).solverunslmdif, orlmderwhen givenjac, asleastsqdoes with and withoutDfun. It now takes leastsq's defaults (ftol=xtol= 1.49012e-8,gtol0,maxfev200 (n + 1), or 100 (n + 1) withjac; it used 1e-8, 1e-8, 1e-8 and 100 (n + 1)).statusis leastsq'sier,messageitsmesg, and the result gainsnjevandcov_x.leastSquaresrunslmderwith SciPy's 2-point Jacobian, asleast_squares(method='lm')does in SciPy 1.17, and gainsjac,ftol,xtol,gtolandmaxNfev. It reports SciPy'sstatus,messageandnfev, and itsjacis the Jacobian at the solution. Both previously ran Tessero's own Levenberg-Marquardt on the normal equations. The kernel'slinalg.lmdiftakesjac(0 lmdif's differences, 1 the caller's Jacobian, 2 2-point differences), andlinalg.lmdif_resultalso returnsnjev. On residuals whose arithmetic is the same in PHP and NumPy, the parameters, residuals, evaluation counts, status and least_squares' Jacobian match SciPy's bit for bit, andcov_xis within 1 ulp. The kernel'slinalg.fd_normal(Linalg::fdNormal, an internal helper for the old normal equations) is removed. Nothing in Tessero, Talario or Kinetix calls it.
-
signal.fftconvolveandoaconvolveuse the FFT on large inputs, as SciPy does (both inputs padded to a length of 2, 3 and 5 factors, real transforms along the last axis): where the direct sum's multiply-adds exceed the FFT's work about 20 times over. Those results move by rounding, as SciPy's do; small inputs keep the direct sum and its exact values. -
Faster, with results unchanged byte for byte:
ndimage.gaussian_filter,uniform_filter(and the other separable filters) read the input in place and write the last pass straight into the result, dropping three full copies of the image;signal.lfilterruns its main loop without per-term index tests and, whena[0] == 1, without the division by it on the recurrence's critical path;stats.page_trend_testkeeps the exact null distribution's table per column count between calls, as SciPy's module-level state does; thespatial.KDTreeconstructor andqueryconcatenate rows that are lists in one call. -
Np::finfoandNp::iinfoof a dtype name are computed once (FFI package): their numbers depend only on the dtype, so the kernel is asked on the first call and the result is kept (returned as a copy); about 0.2 us a call instead of about 4 (NumPy 0.5).finfois among the 15 most-called NumPy functions in the usage corpus. -
Lower per-call cost for the most-called NumPy helpers through FFI, with results unchanged byte for byte:
- Sequence arguments (
concatenate,vstack,hstack,column_stack,stack, ...) reuse their argument-array type and write NDArray items directly;concatenateof two small arrays drops from about 16 to about 7 us. zeros_like/empty_likeof an NDArray with a dimension, andatleast_1dof one such array, are answered in PHP with the same new array the kernel returns, without the routine call (about 2 us saved each).
- Sequence arguments (
-
NDArray::matmul/dotof two small matrices about 2x faster (FFI package, results unchanged byte for byte): two C-contiguous float matrices of one dtype go straight to the same BLAS gemm call, and gemm takes its operands' addresses directly instead of through libtessero pointers and casts.dotof 3 x 3 matrices about 14 -> 7 us;numpy.dotis the fourth most-called NumPy function in the usage corpus. -
tools/parity/usage_speed.php: per-call speed weighted by real-world usage. Each timed function's per-call time is weighted by how often the corpus of the usage-weighted coverage calls it, giving the time a million and a billion calls of that mix take on each side. -
special.ellip_harm_2andspecial.ellip_normalabout 1,000x faster (18 ms to 12 and 21 us a call; SciPy takes 44 and 145 us): the 256-point Gauss-Legendre rule of their quadratures is computed once per process instead of on every call. The Kelvin-function zeros (kelvin_zeros,ber_zeros...keip_zeros) are about 1.4x faster: their bisection no longer evaluates the function at the bracket's lower end twice. Results unchanged, byte for byte. -
A default native memory budget in the CLI: half of the machine's physical memory, on both backends, where it was unlimited. The CLI also runs queue workers and artisan commands, and a single oversized call there (a
numpy.meshgridof nine 9-value vectors asks for 26 GiB) is now refused up front withMemoryError/Tessero\Ext\MemoryExceptioninstead of exhausting a shared host.TESSERO_MEMORY_BUDGETnow also applies in the CLI (FFI package);TESSERO_MEMORY_BUDGET=0ortessero.memory_budget = -1opts out; an explicit budget is kept. Web SAPIs keep max(256 MiB,memory_limit). The kernel exportstsr_physical_bytes()for it (a compatible ABI addition). -
Lower fixed cost per call on the FFI backend: the registry binding prepares each routine once (its label, result type, sequence and in-place arguments), fills the kernel's array descriptors in place instead of building and copying a
tsr_arrayper argument, keeps short string arguments and the C types it needs, and reuses the operand buffers of scalar element-wise calls.NDArray::meta()and the C-order strides of new arrays are cheaper too. A scalarSpecial::ndtr(0.3)2.7 -> 1.3 us; small routines (Np::diff,unique,interp,trapezoidon 8 values) 1.4-1.6x faster. Results are unchanged, byte for byte. -
Element-wise registry functions on small arrays (
scipy.special, distribution methods such asStats::norm()->cdf($x)): PHP scalar arguments next to arrays are passed as one value with zero strides instead of as a new 0-d array each, broadcasting computes the strides instead of building views, the pointer, shape and stride arrays are reused, the output goes straight to the constructor and keeps thevoid*it was allocated with, and each function's metadata is looked up once. In the kernel, an element-wise loop over several rows runs without an OpenMP parallel region when it uses one thread (also the NumPy ufunc engine behindMath::hypot,arctan2and the other loop ufuncs on broadcast or strided operands).Special::ndtron 8 values 9.8 -> 5.0 us,jv(1.0, $x)21 -> 9.2 us, a 2x1 by 4 broadcastjv24 -> 12.6 us,Stats::norm()->cdfon 8 values 24 -> 6.0 us (SciPy 41 us). Results are unchanged, byte for byte (NaN sign and payload bits included). -
Array arithmetic, copies and axis functions on small arrays:
NDArraybinary and unary operations,copy(),astype()and the loop ufuncs ofMathfill a reused set of shape and stride arrays instead of four new ones per call, pass the buffers' cachedvoid*, compute broadcast strides once (without building views), build the result through the constructor, and pass a PHP scalar operand's bytes directly instead of a new 0-d array (when no cast is needed). The generalised ufuncs (partition,searchsorted,percentile,median, ...) parse their C types once, write the kernel's array descriptors in place, and pass a scalar input after the first (kth, a value to search for) through a slot of its own. On 8 values:$x->mul(2.0)11.8 -> 4.8 us,$x->add($x)8.9 -> 5.2 us,copy()5.0 -> 3.1 us,Np::partition18.7 -> 11.1 us,Np::searchsorted17.3 -> 11.8 us. Results are unchanged, byte for byte. -
Routine calls, list arguments, small sorts and
numpy.ffton small inputs: the FFI binding parses each C type it casts to once (a cast by name parsed it on every call, inLibrary::address()andBuffer::ptr()among others), anNDArrayargument writes its own descriptor into the kernel's slot (NDArray::writeMeta()), number results are read without a call, a routine's array result keeps the block'svoid*for freeing and is wrapped without the constructor repeating the checks the binding has just made, and variadic routines prepare their signature once.NDArray::array(),fromFlat(),empty(),zeros(),copy()andastype()check a shape once. In the kernel, a stable argsort of 32 keys or fewer (argsort,lexsort,uniquewith indices, and others) runs an insertion sort instead of the radix passes, whose 96 KB of count tables were zeroed and scanned on every call; the stable order is unique, so the permutation is the same.argsort()along the last axis of a C-contiguous array reads it in place, andsort()andnumpy.fftalong the last axis skip the axis round trip. On 8 values:argsort()18.8 -> 3.7 us,Np::lexsort(2 x 5) 17.6 -> 4.6 us,sort()9.8 -> 4.1 us,Fft::fft10.5 -> 4.7 us,Fft::irfft15.4 -> 8.2 us,Np::uniquewith all outputs 21.0 -> 12.6 us,NDArray::arrayof 4 floats 4.0 -> 2.9 us,Np::ndim2.2 -> 1.6 us,Np::flipud5.3 -> 4.4 us. Results are unchanged, byte for byte (NaN sign and payload bits included). -
PHP lists as arguments, and generalised ufuncs with a scalar result: a list of up to 4,096 values passed to a registry routine, an element-wise function or a generalised ufunc is packed into a block of its own, with the values, shape and dtype
NDArray::array()gives it (NDArray::listParts()andflatBytes(), the codearray()itself now runs), instead of becoming an array with a buffer of its own that is freed after the call. A generalised ufunc's 0-d result (median,percentilewith one q,nansum,searchsortedof one value, ...) is written to a slot of its own and read back as the numberitem()returns, instead of through an array, and its other outputs are wrapped without the constructor repeating the checks made on the kernel's shape. On 8 values:Np::medianof a list 14.1 -> 7.8 us (of an array 8.2 -> 5.8 us),Np::percentileof a list 16.9 -> 9.8 us,Np::nansumof 4 integers 15.7 -> 8.6 us,Np::searchsortedof one value 10.5 -> 7.2 us,Np::broadcastShapesof three lists 20.2 -> 14.0 us,Special::ndtrof a list 7.8 -> 6.6 us. Results are unchanged, byte for byte (NaN sign and payload bits included), and so are the exceptions for ragged, non-numeric, empty and too deep lists. -
Generalised ufuncs, routine arguments and new blocks on the FFI backend: a generalised ufunc keeps the C blocks of its last call (axis masks, array descriptors, parameters, output shapes) and reuses them when no call is using them, instead of allocating six per call; a routine writes
nulland integer arguments (axis,dtype=None, ...) into their slots without a call each; andLibrary::address()reads a pointer back as an integer through a union instead of two casts, which every new array block and every adopted routine result goes through. On 8 values:Np::median5.6 -> 4.6 us (of a list 7.6 -> 6.3 us),Np::percentile6.7 -> 6.0 us,Np::partition9.8 -> 8.5 us,Np::nansumof 4 integers 5.7 -> 4.9 us,Np::emptyLike4.4 -> 3.7 us,Np::swapaxes5.8 -> 4.4 us,NDArray::empty2.0 -> 1.8 us. Results are unchanged, byte for byte (NaN sign and payload bits included). -
np.diff,outer,inner,kron,ediff1d,trapezoid,cumulativeTrapezoid,geomspace,pad(odd reflection) and thepoly*routines: their shared element-wise step read every operand through a one-element conversion call, also when no conversion was needed; operands already in the loop dtype are now read in place, and a float64 difference runs row by row.np.diffon 1M values 127 -> 1.4 ms (NumPy 1.2),np.outer1000 x 1000 128 -> 10 ms (NumPy 1.3). Results are unchanged, byte for byte (NaN sign and payload bits included). -
Large-block reuse: blocks of 1 MiB or more freed with
tsr_freeare kept per process and handed out again bytsr_allocfor the next array of about their size, instead of going back to the operating system and page-faulting in again on the next call (a 1024 x 1024fft2in a fresh process 64.5 -> 29.7 ms, NumPy 32.2;rfft240.0 -> 15.7 ms). The blocks stay libtessero's and are released only by it. Limit per process: 64 MiB in the CLI, 16 MiB in a web SAPI; set withtsr_set_alloc_cache_limit/tsr_alloc_cache_limit/tsr_alloc_cache_bytes,Tessero::setAllocCacheLimit()/allocCacheLimit()/allocCacheBytes()and the same onTessero\Ext\Engine, orTESSERO_ALLOC_CACHE_BYTES(0turns it off). Kept blocks are outside the memory budget. Results are unchanged. Off in the ownership-check and AddressSanitizer builds. -
sort()(Ctsr_sort) is now the AVX2 quicksort ofsortSimd()where the CPU has AVX2, the radix sort elsewhere. The result is unchanged (both sorts return the same sorted array); the time is 2-25x lower below a few thousand values and about 2x lower on 1M random values (one thread: 17 ms against NumPy's 14 ms, was 31 ms).sortSimd()stays as an explicit name. The radix sort stays tested on AVX2 machines through a test-only switch.
Added
tools/parity/run-tests.php --hash=OUT.txt: one line per fixture call with a hash of its result (arrays by dtype, shape and bytes, floats by their bits, exceptions by class and message), for regression checks: run it before and after a change on the same machine anddiffthe files.bench/tier1_bench.php/bench/tier1_bench.py: the element-wise tier of the speed sweep (everyscipy.specialfunction on the fixture rows tiled to 100,000 values), paired bybench/sweep_report.py.SparseLinalgsolvers take sparse matrices (spsolve,eigsh,cg,bicgstab,bicg,cgs,minres,gmres,lgmres,gcrotmk,qmr,tfqmr; both backends): pass the CSR array thatSparse::diags/Sparse::csrArrayreturn (or aCsrMatrixon FFI). The bindings hand it to the kernel as the group data, indices, indptr, shape, checked before use. Iterative solvers use sparse products (the same results as on the dense matrix);spsolvea banded LU sized by the matrix's bandwidth (dense fallback when the band is wide and the matrix fits, else an error);eigshthick-restart Lanczos with a deflation search so repeated eigenvalues are found (SMdense up to n = 4096;sigmaunsupported). Before, these solvers took dense matrices only: a 100k tridiagonalspsolve(80 GB dense) now takes 15 ms (SciPy 51 ms); a 100 x 100 grid Poisson system:spsolve25 ms (SciPy 31),cg21 ms (SciPy 21).Generator::multivariateNormal(..., method:)takes'cholesky'and'eigh'besides'svd', on both backends, each factorisation computed as NumPy computes it (C:tsr_pcg64_multivariate_normal_method). A cov that is not positive definite fails'cholesky'with an error, as NumPy raises.- Typed C entry points
tsr_cholesky,tsr_solve,tsr_slogdet(requested by a native consumer): row-major float64, running the registry routines of the same name, so the results equal the registry's byte for byte, without packingtsr_arg/tsr_result. tsr_unary_find/tsr_binary_findandtsr_unary_name/tsr_binary_name: the core element-wise op codes by NumPy ufunc name (exp,tanh,add, ...), which were documented only in an internal header. New pagedocs/reference/c-function-lookup.md: the four ways to reach a function from C, and why registry ids may be kept for a process but not across releases.- Two more sorts with the same result as
sort():NDArray::sortSimd()(Ctsr_sort_simd, an AVX2 quicksort: faster on random data, many duplicates and sorted input; the radix sort where AVX2 is missing) andNDArray::sortRuns()(Ctsr_sort_runs, a natural merge sort: fastest on mostly sorted data, slowest on random data), on both backends. All three sort the same order-preserving keys by comparison only, so they return the same sorted array as one another, at any thread count;sort()(radix) stays the default. When to use which, with measurements:docs/project/benchmarks.md.
Performance
-
Small calls cost less: the extension's dispatch and the kernel's argument checks. On small inputs a registry call was mostly fixed cost: about 0.75 us before any work. Measured with cycle counters, it went to:
- Zeroing about 28 KB of stack per routine call. The build's
-ftrivial-auto-var-init=zerohardening zeroes every local, and the routine dispatch keeps its argument and result arrays on the stack (each argument slot holds a wholetsr_array). Those buffers are now exempt (TSR_NOINIT,__attribute__((uninitialized))), and only the slots a call passes are cleared. Every other variable keeps the hardening, and the kernel clears its result slots before anything else. - 64-bit integer divisions in the argument gate's overflow and alignment checks and in
tsr_shape_size. They now use__builtin_mul_overflowand power-of-two masks, which flag exactly the same cases (the INT64_MIN operand and power-of-two conditions are kept), with the same error messages. - Looking up the method by building and lower-casing its
class::methodname on every call. Methods are now indexed by their function once at start-up, with the name lookup kept as the fallback. get_arrzeroing its 550-byte holder before copying an array argument over it.
Results and errors are unchanged: the byte hash of all 47,869 fixture results is identical on both backends. Per call through the extension (one host, single thread; DJ's final pass times the record),
np.ndim1.26 -> 0.38 us,promote_types0.88 -> 0.36,shape1.56 -> 0.62,ascontiguousarray1.46 -> 0.77,special.wofz1.5 -> 0.83,hankel12.4 -> 1.55. On the tier-3 per-call fixtures the functions slower than NumPy/SciPy fell from 12 to 4 (np.json), 47 to 33 (np_shape), 34 to 21 (np_numeric) and 38 to 20 (special). Most of the special ones left are at 1.00 to 1.08: they run SciPy's own specfun routines. - Zeroing about 28 KB of stack per routine call. The build's
-
Element-wise integer and float routines read their inputs in place.
bitwise_and/or/xor,left_shift,right_shift,bitwise_count,ldexp,frexp,modfanddivmodmade an owned copy of every input, even one already C-contiguous in the right dtype. They now read such an input where it is, throughown_in, and copy only to convert. Each routine was checked to only read its inputs and never emit them. Per call through the extension:bitwise_and0.96 -> 0.70 us,ldexp0.98 -> 0.74,divmod1.27 -> 1.03,frexp0.93 -> 0.81. Byte hash identical on both backends. -
lu_factorandcho_factorreturn their factor in column (Fortran) order, as SciPy does;lu_solveandcho_solveread it without a copy. The factor is LAPACK's own column-major array, handed to PHP as an ordinary NDArray of the same shape and values.toBytes()andtoList()give exactly what they gave before; only the memory order differs (strides()is[8, 8 * rows], andisContiguous()is false). That is also what NumPy reports for SciPy's factors (F-contiguous). The solves see that order and pass the factor straight to LAPACK. A factor in C order (a copy, or one built by hand) or of another dtype is still copied into column order as before. LAPACK sees identical matrices, so results are byte-identical:- 641 cases on FFI and 612 on the extension, old library against new, including solves given C-ordered and float32 factors.
- tests/Unit/LinalgFactorLayoutTest.php checks the order and that the solves give the same bytes from every layout.
In the C ABI,
tsr_result.flagsbit 0 (documented in tessero.h) marks a column-major 2-D array result. Only these two routines set it, and both PHP bindings hand such a result out as a transposed view. Same host, single thread, best of five alternating rounds (load 16 to 36, so unchanged routines swing by up to 30%; not the record, DJ's final pass times it), FFI / extension:lu_factor600x600: 8.1 / 7.3 -> 5.7 / 5.5 ms (SciPy 5.9)lu_solve600, one right-hand side: 1.71 / 1.49 -> 0.41 / 0.23 ms (SciPy 0.17); 100 of them: 4.2 / 6.0 -> 3.8 / 2.6 ms (SciPy 2.9)cho_factor1000x1000: 20.6 / 22.2 -> 16.7 / 14.1 ms (SciPy 15.8)cho_solve, one right-hand side: 3.8 / 3.6 -> 1.7 / 1.1 ms (SciPy 1.9); 100 of them: 11.5 / 12.7 -> 7.8 / 6.7 ms (SciPy 8.4)
-
inv,cho_factor,cho_solveandlu_solvewithout LAPACKE's row-major copies; Cholesky copies only the triangle it reads. The same change asslogdetand the Cholesky routines below:inv(NumPy and SciPy) factors and inverts in its result array, holding the column-major copy LAPACKE builds. The hand-over from dgetrf to dgetri was an exact copy out and back, so it is dropped, and one in-place transpose makes the result row-major.Tessero\Linalg\Linalg::inv(the PHP class over LAPACKE) does the same.cho_factorfactors in its result, andcho_solveandlu_solvesolve in column-major copies, reading the factor and right-hand side in place.numpy.linalg.cholesky,scipy.linalg.cholesky,cho_factorandcho_solvecopy only the triangle dpotrf or dpotrs reads, as LAPACKE does. A full copy madecho_solveslower than before.
LAPACK sees identical matrices and LAPACKE's NaN checks are unchanged, so the bytes are the same: 558 cases on FFI and 529 on the extension, old library against new, plus the Cholesky set above. The cases cover inv, qr, lu, lu_factor, lu_solve, cho_factor and cho_solve: square sizes 1 to 1000 across the tile edges, rectangular shapes, batched and empty input, float32, int and strided input, junk in the unused triangle, NaN, Inf, singular and not positive definite, transposed solves and zero right-hand sides. Same host, single thread, best of five alternating rounds at load 18 to 24 (not the record, DJ's final pass times it), FFI / extension:
inv600x600: 19.1 / 19.7 -> 18.0 / 17.6 ms (NumPy 21.9)scipy.linalg.inv: 19.6 / 20.7 -> 17.2 / 17.4 ms (SciPy 19.4)numpy.linalg.cholesky1000x1000: 15.8 / 14.7 -> 13.9 / 13.8 ms (NumPy 20.5)scipy.linalg.cholesky: 17.4 / 16.6 -> 14.6 / 14.9 ms (SciPy 14.1)cho_factor1000x1000: 18.2 / 18.1 -> 16.2 / 15.5 ms (SciPy 14.4)cho_solve, one right-hand side: 3.8 / 3.9 -> 2.7 / 2.3 ms (SciPy 1.6); 100 of them: 9.4 / 9.5 -> 8.5 / 8.0 ms (SciPy 7.4)lu_solve600, one right-hand side: 1.29 / 1.22 -> 1.01 / 0.65 ms (SciPy 0.17); 100 of them: 4.4 / 4.0 -> 3.9 / 3.5 ms (SciPy 3.7)
On these routines SciPy keeps the factor in Fortran order, so its solves copy nothing. Tessero's factors are row-major, so every solve transposes the factor once: that is the remaining gap on
lu_solveandcho_solve. The same change toqr,luandlu_factorwas tried, measured and left out. It saved nothing there: on this host a transpose costs about three plain copies, whichever tiling is used, so dropping LAPACKE's copies only pays where it removes several of them around a fast factorization. -
slogdet/detandscipy.linalg.choleskywithout LAPACKE's row-major copies;choleskyfactors in its result. The same changesolveandnumpy.linalg.choleskygot earlier: the kernel hands LAPACK the column-major copy that LAPACKE's row-major wrapper would build, made once with a tiled transpose, and skips the wrapper's own copy in and back. The input is read in place when it is contiguous float64. Both Cholesky routines now factor in the result array and turn the factor row-major in place, which saves one full-size buffer and a pass.Tessero\Linalg\Linalg::slogdet(the PHP class over LAPACKE) does the same. LAPACK sees the identical matrix and LAPACKE's NaN checks run as before, so results and errors are byte-identical (279 cases on FFI and 185 on the extension, old library against new: sizes 1 to 1000 across the 32-wide tile edges, batched, empty, float32, int and strided input, junk in the unused triangle, NaN and Inf, singular, not positive definite). Same host, single thread, best of three alternating rounds (not the record, DJ's final pass times it):numpy.linalg.cholesky1000x1000 17.0 -> 14.1 ms (NumPy 18.7), 600x600 4.7 -> 3.5 ms (NumPy 4.1);slogdet600x600 6.1 -> 5.2 ms (NumPy 5.6);scipy.linalg.cholesky1000x1000 about the same, 15.6 -> 15.0 to 15.8 ms (SciPy 13.5 to 14.8). Most of that gap is OpenBLAS's upper-triangle kernel: dpotrf 'U' runs about 1 ms slower than 'L', here and in SciPy's own OpenBLAS. The 'L' kernel computes in a different order and would not keep SciPy's bits, so it is not used. -
sparse.linalg.eigshon CSR checks for missed repeated eigenvalues faster. After the Lanczos run converges, a second Lanczos search of the complement of the k Ritz vectors makes sure no repeated eigenvalue was missed. The first such search now starts from the main run's next Ritz vector plus the usual start vector. The first part makes it quick when nothing was missed; the second keeps every missed direction in reach, so the guarantee stands. On the 2-D Poisson case of the benchmark the check takes 390 matrix-vector products instead of 700: about 600 ms where it took 700 (SciPy's ARPACK 580 ms, same host and load; not the record, DJ's final pass times it). Results:- Wherever the main run misses nothing (every fixture, the benchmark, 72 of 72 compared cases), the results are byte-identical.
- Where it does miss a copy, which is forced in tests with a start vector blind to one block, the replacement eigenvalue is found by another path. It lands at the same accuracy: on 48 such cases the largest error against the closed form is 2.1e-14 before and after.
- Tried and dropped: a second reorthogonalization pass only when needed (ARPACK's test). It saved little and made the values slightly less accurate.
-
Spatial\KDTreeandSpatial\ConvexHull(2-D) take an NDArray, of either backend, read in place, as SciPy takes an ndarray. Points given as PHP rows are converted in C in one call through the extension, not flattened in PHP (the same doubles; the PHP loop remains for values the extension refuses, such as numeric strings). The hull's perimeter and area come from its own vertices, fetched alone. In the kd-tree search, each node now uses the box bound its parent computed to order the children, instead of computing it again. Every result is unchanged, bit for bit: the old against the new library on 24 point sets (random, integer grids with ties, duplicates; up to 8-D; k from 1 to n + 2), the old classes against the new on 100 inputs, and rows against both backends' arrays in tests/Unit/SpatialArrayInputTest.php. Profiled on one host, the extension against SciPy at the same load (not the record; DJ's final pass times it): KDTree build of 100k 3-D points 39 ms from rows and 33 ms from an array (SciPy 36; 54 ms before); 100 queries with k = 5 0.17 ms (SciPy 0.19; 0.21 before); ConvexHull of 10k points 0.73 ms from rows and 0.57 ms from an array (SciPy 1.6; 2.5 ms before). -
Spatial\Delaunay(2-D) in the kernel: the sweep-hull algorithm (distance.delaunay_2d, O(n log n), the hull exact by construction) replaces a Bowyer-Watson that scanned every triangle in PHP: 10k points 57 s -> 29 ms (SciPy 46 ms). The same simplex set (parity against SciPy; PHPUnit against the old algorithm);griddata,LinearNDInterpolatorandCloughTocher2DInterpolatorbuild on it. The PHP Bowyer-Watson remains for input the sweep cannot triangulate. -
Spatial\ConvexHull(2-D): the monotone chain in the kernel (distance.convex_hull_2d), the same vertices: 10k points 43 -> 4.1 ms (SciPy 1.5). -
hierarchy.linkage: each cluster caches its nearest later neighbour, so a merge rescans only the rows it affects (about n^2 instead of n^3); the same Z byte for byte, ties included (C test against the old loop, all 7 methods). Ward on 2000 points 1.6 s -> 70 ms (SciPy 92). -
linalg.solve_triangular: the column-majordtrtrson the row-major matrix (triangle and transpose flipped) instead of LAPACKE's row-major copies, a contiguous matrix read in place: 1000 x 1000 4.2 -> 0.88 ms (SciPy 1.0). -
median,percentile,quantile: a two-pointer (Hoare) introselect, and the second order statistic of an interpolated quantile taken as the smallest of the rest: percentile 1M 18 -> 5.9 ms (NumPy 12), median 13 -> 12 ms (NumPy 11). The same values (the k-th value of the total order has one bit pattern). -
Spatial\KDTreebuilds its tree once, in the constructor, and every query searches it (new registry routinedistance.kdtree_build;distance.kdtree_querytakes its idx/nodes/box). 100 queries in 100k points: 0.57 ms, was 37 ms (SciPy 0.20 ms); the query reads the data and the tree in place. The constructor now pays the build (88 ms with the conversion of the PHP point arrays; SciPy 34 ms from an ndarray), where the old timing built nothing. A malformed tree is refused before use.kdtree_buildhas no SciPy counterpart, so it has no fixture; the C tests and PHPUnit pin it (queries on its tree byte-identical to a fresh build and to an exhaustive search). -
Routine arguments that are already contiguous float64 are copied with one
memcpyinstead of element by element (fn_arg_doubles): the same bytes, less time per call in every routine that reads arrays. -
Spatial\KDTreeis a k-d tree: the queries ran an exhaustive search in PHP (10,000 queries in 100,000 points: hours); they now run in the kernel (distance.kdtree_query, both backends: median splits, branch-and-bound, a k-best heap), 55 ms for that case with the tree build included (SciPy: 49 ms). Same neighbours, distances and order (nearest first, ties by index); non-finite points are refused, as SciPy refuses them. -
Faster
median,percentile/quantile,unique,einsum,cov,trapezoid,maximum_filter/minimum_filter(1M values / 1000^2 images, single-threaded): median 144 -> 13 ms, percentile 119 -> 18 ms (introselect), unique 97 -> 15 ms, the set routines likewise (their libcqsortreplaced bysortSimd/ the radixtsr_argsort), einsum ij,jk->ik 300^2 267 -> 15 ms (incremental offsets, strided inner loop, same summation order), cov 50 x 100k 235 -> 45 ms (one BLAS dgemm, as NumPy), trapezoid 353 -> 1.6 ms, maximum_filter 5x5 822 -> 25 ms (separable, as SciPy). Values unchanged. -
sortSimd()sorts in place at the data's own width: 8 float32/int32 keys per AVX2 vector instead of 4 widened 64-bit keys, integers without any conversion, floats mapped to order-preserving integers in place (the map is its own inverse), and no scratch buffer. 1M random values: float32 24.8 -> 18.0 ms, int32 24.0 -> 17.0 ms, int64 25.3 -> 22.0 ms, float64 25.2 -> 23.3 ms; sorted input about twice as fast. Output unchanged, byte for byte. -
sortSimd()leaves go to in-register AVX2 sorting networks (up to 64 keys for 64-bit data, 128 for 32-bit; generated straight-line code,tools/gen-sort-net.php), and float64 is sorted as doubles (one-instruction compares and min/max; the zeros' signs are restored afterwards, and denormals-are-zero mode falls back to integer keys). 1M random values: float64 23.3 -> 15.9 ms, float32 18.0 -> 7.8 ms, int64 22.0 -> 16.7 ms (NumPy 17.1), int32 17.0 -> 7.4 ms. Now the fastest of the three sorts on almost every input. Output unchanged, byte for byte. -
sortSimd()is multithreaded from 131,072 values, using the threads set withTessero::setThreads()(OpenMP tasks for the top partitions; the pivots do not depend on the thread count, so the output is the one-thread output, byte for byte). 1M random values with 4 threads: float64 5.7 ms, float32 3.4, int64 6.5, int32 2.8 -- 2.2-2.6x faster than NumPy's single-threaded sort (14.1 / 7.5 / 17.1 / 6.8 ms).
Fixed
-
special.jn_zeros,jnp_zeros,yn_zeros,ynp_zerosandjnyn_zeroswith an order above about 46000 no longer overflow anintinside the vendored xsf code (jynbhandjynddformedk * kandn * n; undefined behaviour, found by the standalone fuzz driver in CI). The product is now computed as SciPy's x86-64 builds compute it, made explicit, so results are unchanged. CI now uploads the standalone driver's crash inputs too. -
ndimage.map_coordinates,shift,zoom,affine_transformandrotatereturn SciPy's values in every boundary mode (approved by DJ, 15 Nov). At the B-spline orders (2 to 5) they matched SciPy only in 'mirror' and 'reflect'; in 'constant' (the default), 'nearest' and 'wrap' they were off by up to 0.2 to 0.4 on data in [0, 1], andprefilter=Falsewas ignored. They now run SciPy's code (ni_interpolation.c, ni_splines.c): the prefilter only withprefiltertrue, on the input padded by 12 for 'nearest' and 'grid-constant'; the coordinate folding of each mode; the taps past an edge folded as SciPy does (mirrored for 'constant' and 'wrap'); the products in SciPy's order; and the result in the input's dtype (integers rounded and clipped as SciPy stores them; they were float64). New: the modes 'grid-constant', 'grid-wrap' and 'grid-mirror', homogeneousaffine_transformmatrices ((n, n + 1) and (n + 1, n + 1)),zoom'sgrid_mode, zoom's output shape rounded half to even as Python'sround, androtateof 3-D and higher arrays prefiltering each plane as SciPy does.spline_filterandspline_filter1dstart the 'reflect' and 'nearest' recursions as SciPy's code does (a last-bits change) and take the grid modes. Checked byte for byte against SciPy 1.17.1 on 882 calls (every mode, orders 0 to 5, with and without the prefilter, 1-D to 3-D, int32, int64, uint8, float32 and bool input): 872 identical in dtype, shape and bytes; the other 10 are bool results from acvaloutside [0, 1], where SciPy writes the truncated value into the bool byte (2 or 253) and Tessero writes 1 -- the same truth values. -
Extension: a malformed array result is freed once.
tsr_ndarray_adoptfrees the kernel's block itself when it refuses an array (more than 32 dimensions), and its caller then freed it again. The kernel never returns such a result, so this could not happen in practice. The caller no longer frees it, and adopt's comment states the ownership rule. -
invof empty matrices and the completeqrof an m x 0 matrix answer as NumPy and SciPy do.numpy.linalg.inv/scipy.linalg.invof a 0x0 matrix, or of a stack of them (2x0x0), raised "illegal argument -7" (LAPACKE's dgetri refuses n = 0), and LAPACK printed "On entry to DGETRI parameter number 6 had an illegal value" into the output. They now return the empty result.qr(a, 'complete')/scipy.linalg.qr(a, 'full')of an m x 0 matrix returned a Q that was never written (whatever the memory held; it differed from run to run). Q is now the m x m identity, R stays m x 0.- Found by the byte comparison of the linalg speed round. tests/Unit/LinalgEmptyInputTest.php covers both backends.
Tessero\Linalg\Linalg(the PHP class over LAPACKE):inv,choleskyandsolveon a stack holding no matrices (for example 0x3x3) threw a TypeError. They now return NumPy's empty shapes: 0x3x3 for inv and cholesky; for solve 0x3 (shared vector b) or 0x3x2 (b of 3x2, shared or stacked). A b with the wrong number of rows is refused before the stack is split, so the empty case is checked too.slogdet,detandeighalready handled it, and the test now covers them.
-
Special::hyp1f1(a, b, INF)no longer hangs. For x = +inf Boost's evaluation can return a scaling exponent near 2^63, and the loop that removed it 707 at a time ran about 10^16 times (SciPy hangs on the same inputs, e.g.hyp1f1(0.5, 1.5, inf)). The loop's step count is now computed and the factor applied only while it still changes the result: the same result as the original loop, bit for bit (NaN for most such inputs), at once. Every other input gives the same bytes as before (THIRD_PARTY_NOTICES.md, local modifications). -
sort()(the radix sort) crashed on small thread stacks: it kept up to 49 KB of count tables per recursion level on the stack, which overflowed musl's 128 KB default for threads and OpenMP workers (Alpine; the CI musl job failed from the sort commits on) and glibc threads whose stack is shared with large static TLS. The tables now live in the heap block of the scratch buffer; results unchanged, speed equal or up to 8% faster at 10M values. Regression test on a 128 KB thread at the full recursion depth. -
Generator::defaultRng([...])refused list elements of 2^32 and above; NumPy'sSeedSequencesplits every element into little-endian 32-bit words ([2**40 + 7, 5]seeds as[7, 256, 5]), and both backends now do the same. -
einsumhid a NaN behind a zero: once a running product reached 0 the remaining operands were skipped, soeinsum('i,i', [0], [nan])gave 0; NumPy gives nan. -
scipy.fftdct / idct / dst / idst were O(N^2) (direct sums; 1M points took hours): above 128 points they now run through one real FFT (Makhoul), 1M points in about 50 ms, agreeing with SciPy to 1e-16 of the output scale; small sizes keep the exact direct sums. New fixture cases above the switch (129, 257, 1000 points; 3x130 and 130x3). -
interpolate.CubicSpline/make_interp_spline(k=3) solved their tridiagonal system densely (n x n: 800 MB and 8-17 s at 10,000 knots): now LAPACKdgbsvon the band, as SciPy does; 76 ms, O(n) memory. -
sparse.linalg.lsmrreadmaxiterfrom theconlimargument, so a default call ran 100 million iterations (15 s on a 10 x 6 system; the results were right): now 16 us.lsmrstill ignoresdamp,atol,btolandconlimand runs 4n iterations unlessmaxiteris given. -
signal.butter,cheby1,cheby2,ellip,bessel,iirfiltersilently ignoredoutput=: they return (b, a) only and now refuseoutput='sos'/'zpk'with an error instead of answering in the wrong format. -
Signed zeros now as NumPy / SciPy:
np.modf(-0.0)gives a-0.0fraction;polymulxand the polynomial derivatives givec[0]*0(NumPy);csgraph.laplacianfollows SciPy's operation order (off-diagonal zeros-0.0);slinalg.invpascaltriangular zeros are+0.0(SciPy's default exact=True result is integer). -
sortSimd()with threads under per-thread FP modes: OpenMP worker threads keep their own MXCSR, so a worker left in denormals-are-zero mode compared subnormal float64 values as zero while the caller (not in DAZ) had chosen the double-compare path. Every thread of the sort now runs with the caller's MXCSR and restores its own. -
Registry input gate: no signed overflow building the message for an element offset of INT64_MIN (found by the libFuzzer CI job).
Testing
- Speed sweep tooling:
tools/parity/run-tests.php --time=OUT.jsontimes every passing fixture call,tools/parity/time_calls.pytimes the same cases in NumPy/SciPy andtools/parity/time_report.pypairs them;bench/tier2_bench.{php,py}(83 heavier calls) andbench/sweep_report.py. Results:docs/project/speed-sweep.md. - The parity comparator checks the sign of exact zeros in real results (complex parts and four rotation-based decompositions exempt, each with its reason).
make -C csrc coverage(gcov line coverage of the C tests) and a nightly job that runs it.- Sort risk register and hardening tests (
docs/project/sort-design.md): an independent qsort oracle over the documented order at every size 0..1100, the 0-1 principle exhaustively for the leaf networks, 15 adversarial / structured / float-edge patterns at serial and parallel sizes, forced heapsort fallback, DAZ in worker threads, concurrent sorts from application OpenMP threads and pthreads, and a new fuzz targetsort(CI and nightly). A mutation check (planted bugs) confirmed the suite catches each one.