Repository navigation
Lex Groebner bases by sparse and classical FGLM (option -x) - #401
Conversation
|
There seems to be a bug with ideal [1] that makes the CI fail. I will check this on another computer. |
| /* create the lex Gb [1] */ | ||
| FILE *of = (files->out_file != NULL) ? | ||
| fopen(files->out_file, "ab") : OUTSTREAM; | ||
| fglm_gb_t *one= malloc(sizeof(fglm_gb_t)); |
There was a problem hiding this comment.
one is never freed, maybe add free_fglm_gb(one).
| } | ||
| fprintf(ERRSTREAM, "Computing Groebner basis\n"); | ||
| print_gb=2; | ||
| print_gb -= 2; |
There was a problem hiding this comment.
At the moment running msolve -e k over QQ prints a basis, here we set a print_gb=0 to -2.
Maybe better do something like
print_gb = (print_gb == 3) ? 1 : 2;
like it is done in the finite field branch of the code?
| const int lifted = use_ml && ML.lifted; | ||
| const int K = lifted ? Kmax : nthr; | ||
| for(int i = 0; i < K; i++){ | ||
| prime = lexgb_next_prime(prime, bs, p0); | ||
| bprimes[i] = prime; | ||
| lprimes[i] = prime; | ||
| } | ||
| if(lifted){ | ||
| lexgb_matlift_residues(bmat, mstride, pbad, &ML, lprimes, K, nthr); | ||
| } else { | ||
| nf4 += K; | ||
| } | ||
| if(info_level && first_batch){ | ||
| fprintf(VERBSTREAM, "New prime = %u\n", bprimes[0]); | ||
| } | ||
| #pragma omp parallel for num_threads(nthr) schedule(dynamic) | ||
| for(int i = 0; i < K; i++){ | ||
| CF_t *ri = bres + (uint64_t)i * (tr.lex.ncoeffs + 1); | ||
| if(lifted){ | ||
| /* lifted matrices: neither F4 nor core_nf */ | ||
| int bad = 0; | ||
| if(pbad[i]){ | ||
| bad = 3; /* p divides a denominator */ | ||
| } else { | ||
| sp_multimatfglm_t *Mq = | ||
| lexgb_matrices_view(&tr, bmat + (uint64_t)i * mstride, | ||
| bprimes[i]); | ||
| if(fglm_classic_apply(ri, Mq, &tr)) bad = 1; | ||
| Mq->dense_mat = NULL; | ||
| free_sp_multimat_fglm(Mq); | ||
| } | ||
| bbad[i] = bad; | ||
| continue; | ||
| } |
There was a problem hiding this comment.
I'm not sure if I understand the code completely, but this should be with -L1 option. Do here later primes get their matrices from the already lifted ones? Shouldn't it get them directly from f4?
There was a problem hiding this comment.
Just to be clear, if the matrix has been lifted with -L 1, then the code takes this matrix for the new prime, reduces it and then does its FGLM stuff. Otherwise, it performs the code below (i.e. runs F4, builds the matrices), which, in the next commit will be in an else (with the continue removed) and properly commented.
| /* progress display: {n} = good primes used, p0 included, as for the | ||
| * parametrizations (msolve_trace_qq) */ | ||
| int32_t lstart = dl->lstart; | ||
| while(ok && !done && consec_bad < LEXGB_MAX_BAD){ |
There was a problem hiding this comment.
What if an accepted prime gives correct leading monomials but wrong coeffs? Don't we need a cap on the number of primes or something in order to prevent an infinite loop here?
There was a problem hiding this comment.
But how to cap this? Maybe the coefficient is very large, isn't it?
I don't think we have such limit in other multi-modular code. But I will check again.
If not, we might need discuss about this.
There was a problem hiding this comment.
This non-resolved comment made me curious: does this mean msolve can infinitely-loop in multimodular approaches (in other places maybe, at least in the code here apparently)? I thought it was a Monte Carlo algorithm?
There was a problem hiding this comment.
Here is an explicit example with an infinite loop, obtained with the help of Claude Opus 5.5. My usual viewpoint is that we do not merge something when termination is known to fail on some input, but if you confirm that this already happens in other places in msolve and is declared ok, then I will adapt.
There are two systems, the base one, and the modified one (just one constant coefficient changed, if you open the file you cannot miss this coefficient).
Called with ./msolve -f lexgb-loop-qq.ms -x 2 -L 1 -t 1 --random-seed 7 -v 2 -o out1.res , and
./msolve -f lexgb-loop-base-qq.ms -x 2 -L 1 -t 1 --random-seed 7 -v 2 -o out2.res,
we can see with diff that out1.res and out2.res give the same shape lex bases, which should not be.
Called with -t 2, lexgb-loop-qq.ms does not terminate (the base system takes a few seconds). Building a system with -t 1 should be feasible too, although more difficult.
There was a problem hiding this comment.
The last commits fix the infinite loop with -t 2.
With -t 1, or on the base system, the verbose output is
multi-modular steps
------------------------------------------------------------------------------------------------------
{2}{4}{8}{16}{32}{64}<33.33%>{128}(->2){256}[L](->4){512}(->8){1024}(->16){2048}(->32){4096}<100.00%>
------------------------------------------------------------------------------------------------------
(with -t 1, the modified system still returns the lex basis of the base system: with a fixed seed, all the primes used can divide the modified constant, and this cannot be detected).
With -t 2, the modified system now gives
multi-modular steps
------------------------------------------------------------------------------------------------------
{2}{4}{8}{16}{32}{64}<33.33%>{128}(->2){256}[L]<-L given up>(->4){512}(->8){1024}(->16){2048}(->32){4096}(->64){8192}(->128){16384}(->256){32768}^C
The prime in flight, which ran F4 on the actual input, disagrees with the lifted matrices, so -L is given up before any image is computed from them. From then on, the computation is exactly the one without -L. It terminates, but it is very long on this input: with a 15,000-bit constant, the lex basis has huge coefficients.
On a smaller example built the same way (3 variables, 302-bit constant), -x 2 -L 1 -t 2 used to loop and now terminates in 0.02 s, with the same output as -x 2 -t 2.
The same mixing exists on master in the lifting of the parametrization with -P 2 -L 1 (for -t 2 and more). It is fixed in a separate PR.
|
One question about the shape position case: |
| static inline CF_t fglmc_negmod(CF_t a, CF_t p){ return a ? p - a : 0; } | ||
| static inline CF_t fglmc_mulmod(CF_t a, CF_t b, CF_t p){ | ||
| return (CF_t)(((uint64_t)a * b) % p); | ||
| } |
There was a problem hiding this comment.
This is a slow way to do modular multiplication. But anyway it seems this is not used anywhere, is it? in that case it could be removed
|
I put some comments on the basic linear algebra tools, after a quick reading of Another interesting addition would be a performance comparison with the existing code for the shape case, and as mentioned before in this thread, a branching mechanism so that this goes the shape case route when applicable. |
1d33b1d to
3683e05
Compare
-x)
abb4324 to
f9cbff9
Compare
|
The PR has been rebased and now uses the same machinery as #409: the output time is reported separately and integer coefficients are converted through |
The dense rows of T_n are stored transposed once, so that the products by x_n are D dot products of length nd (libfglm AVX2/AVX-512 kernels), instead of a combination of rows. Used when all the rows of T_n are known and p <= 1515531528; otherwise the previous product is kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…/64 points At most (2L + 32)/64 points more than the 2L + 32 needed, instead of (2L + 32)/8; Berlekamp-Massey stays negligible (30 ms vs 12 ms on a D = 4096 example, sequence 2.79 s -> 2.36 s). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For -x 1 and -x 2 over Q, sparse-FGLM is tried on the first prime. If the minimal polynomial of x_n has degree D (radical or not), the lex Gb is in shape position: every prime then uses sparse-FGLM instead of fglm_classic_apply (after traced F4 or with the matrices lifted by -L), and the lifting is unchanged. Otherwise classical FGLM is used as before. The variables outside the staircase (leading monomials of linear DRL polynomials) are passed as linvars; their coordinates are computed from NF(x_i), the row of T_i at 1, since there is no DRL Gb with -L. No radical check (squvars is zero). Same outputs; dq7 (D = 128): 99.6 s -> 85 s (-t 1). Co-Authored-By: Claude <noreply@anthropic.com>
The lex Groebner bases computed by FGLM (mod p and over Q), the unit ideal and the positive-dimensional output of -x were written outside the output timer, so their writing time was counted as computation time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
As for the DRL Groebner bases, the coefficients of lex Groebner bases over Q, stored as mpz_t by the lifting, are written with mpz_out_str_10 (FLINT's fmpz_get_str) instead of being copied to an fmpz_t and written with fmpz_fprint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Solve issue algebraic-solving#393 by calling classical FGLM by default on diff_bug-68.
The matrix-vector products of sparse-FGLM (linalg-fglm.c) accumulate 8 products and need p <= DOT2_ACC8_MAX_MODULUS = 1515531528, while FGLM_SPARSE_MAX_PRIME was 1518500213: primes in between could give wrong results. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Over prime fields of characteristic less than 2^15, the default call still computes the lex Groebner basis (as -x 2), but prints it in the default output format: [-1] if there is no solution, [1, nvars, -1, []] in positive dimension, and in dimension 0 the header of a rational parametrization w.r.t. the last variable (characteristic, number of variables, degree of the ideal, variables, linear form [0,...,0,1]) followed by the list of the polynomials of the lex basis. print_lex_basis is split into print_lex_basis_header and print_lex_basis_polys, the latter being used by print_lex_basis_default. The internal value PRINT_LEX_DEFAULT of print_gb selects this output. New test one-8 (unit ideal modulo 251); expected outputs of bug-68 and mq-2-1 updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
For For shape over QQ: yes what I had in mind was recovering the lex basis from the parametrization (when we are sufficient conditions to do this). It can be kept as a future idea. If you feel like doing it, I think there should be a at least a note somewhere (tutorial?) warning the user so that they are aware that For #393, I'm not sure how to interpret your answer, i.e. if it is "this issue is solved by this PR" or not. Calling msolve with |
|
(minor cleaning, maybe: |
I am committing an update on the |
|
When the ideal is radical and in shape position over QQ, do you compute the lex basis from the rational parametrization or do you compute the lex basis through a multi-modular process directly? I think it could be worse to compute the lex basis (for this radical and shape case) from the rational parametrization. These are reduced to extended euclidean division-like algorithms which you could delegate to FLINT. I wouldn't be surprised that this is more efficient than computing the lex basis from a complete multi-modular run |
If I got it correctly: through multi-modular, and experiments show that it could be faster from the parametrization (using rational reconstruction / XGCD as you say), but this is kept for another improvement. |
The rows of the multiplication matrices are frozen one by one, and the primes of the batch in which their lifting completes have already run F4: their lex residues entered the CRT without any check. If the lifted rows are wrong (e.g. all their primes divide some coefficient of the input, which can be forced with a fixed seed), these residues are mixed with the images computed from the wrong matrices and the lifting never terminates (with -t 2 and more). Now every prime that ran F4 is checked against the rows already frozen, including the primes of that batch; on a mismatch, -L is given up (all residues obtained so far come from F4). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Lex Groebner bases by sparse and classical FGLM (option
-x)This PR adds the computation of reduced lexicographical Groebner bases of
zero-dimensional ideals by the FGLM algorithm, over prime fields and over the
rationals.
It first tries the sparse-FGLM algorithm and, if the ideal is in shape position,
it returns the elimination polynomial and the parametrizations as polynomials.
Otherwise, it switches to the classical FGLM algorithm to compute the whole basis.
Sparse-FGLM computes the scalar sequence s_i = <1, M_n^i r>, for r random, and runs
Berlekamp-Massey on it. Berlekamp-Massey is checked regularly for early termination:
as soon as the linear complexity L of the sequence is less than the degree D of the
ideal and the minimal generator predicts the last 32 terms, the ideal is very likely
not in shape position. The computation then stops after about 2L + 32 matrix-vector
products and switches to classical FGLM. This keeps the overhead of trying
sparse-FGLM first small for ideals not in shape position.
New option
-x,--lex-groebner-basis:-x 0default output (default)-x 1leading ideal of the lex Groebner basis-x 2full reduced lex Groebner basis-xcannot be combined with-g,-S,-Cor-n. With-e, msolve falls back to-g 1/-g 2(as for parametrizations).If the ideal has positive dimension,
-x 1and-x 2output[1, nvars, -1, []]:, as thedefault call. If it is the unit ideal, they output the lex Groebner basis
[1].Over prime fields of characteristic less than 2^15, the default call now computes the
reduced lex Groebner basis (as
-x 2), unless one of the options-P,-g,-e,-S,-C,-n,--booleanor--boolean-zerosis given, or the field is GF(16) or GF(256).It is printed in the default output format:
[-1]:if there is no solution,[1, nvars, -1, []]:in positive dimension, and in dimension 0 the header of a rationalparametrization w.r.t. the last variable (characteristic, number of variables, degree of
the ideal, variables, linear form), followed by the polynomials of the lex basis instead
of the parametrization. For instance, for cyclic5 modulo 7919:
As for
-P 0,-x 0gives the default output. The description of the default outputin the help is updated accordingly:
The tests
bug-68(characteristic 257) andmq-2-1(characteristic 251) call msolvewithout option: their expected outputs are now lex Groebner bases in this format. The new
test
one-8checks the output[-1]:for the unit ideal modulo 251. Since the lex basisdoes not depend on the random projection of sparse-FGLM (classical FGLM is used after an
unlucky one),
diff_bug-68.shno longer fails for some seeds, which was the problem ofissue #393; with
-P 2, an unlucky projection can of course still happen.Over QQ,
-x 1behaves as-g 1: the leading ideal is computed modulo one prime;is used for all the other primes and the polynomials of the lex Groebner basis are
lifted; otherwise, a tracer records the needed rows of the multiplication matrices
and the leading monomials of the lex Groebner basis, and classical FGLM is used for
all the other primes;
-L 1to lift the multiplication matrices is available, at the cost of ahigher memory usage;
matrices, FGLM) are multi-threaded, then several primes are handled in parallel, each
on one thread; the rational reconstruction is sequential.
Over a prime field whose characteristic is a 31-bit prime (timing for FGLM, memory overall)
-P 2(s)-P 2(MB)Over QQ (timing and memory overall)
The output follows that of
-g 1and-g 2, with-x 1:-x 2:Similar outputs are provided over QQ, with the same convention as for
-g 1and-g 2:a coefficient
1of a non-constant monomial and an exponent1are not printed.Verbose mode prints information as follows:
where
multiplication matrixis the computation of the last multiplication matrix, as in the-P 2case;scalar sequenceis the computation of the sequence for sparse-FGLM;nfrow is the computation of the needed rows of the multiplication matrices;elimination polynomialis the computation of the univariate polynomial in the last variable, as in the-P 2case(first from the scalar sequence and, if the ideal is not in shape position, by classical FGLM);
variable kis the computation of all the polynomials involving variablekand no larger variable;#dense rowsis the number of dense rows in all the multiplication matrices together, without duplicates,i.e. the size of the staircase and its border;
#normal formsis the number of normal forms computed for the matrices;deg. elim. pol.is the degree of the elimination polynomial, as in the-P 2case;size of basisand#terms in basisare the number of polynomials and the sum of the sizes of their supports.As a consequence, the verbose output of the parametrization and normal form computations has been slightly
modified.
Parametrization:
Normal forms with option
-n:Although the option is
-x 1or-x 2, internally it usesprint_gbwith values3and4respectively.Therefore, if you prefer these computations to be called with
-g 3and-g 4, thisis an easy change.
Use of generative AI
AI-assisted code writing, debugging and testing.