Problemcore
Constrained random stimulus generation
Also called constrained random verification, constrained random test generation.
Generate random input vectors that satisfy declared constraints, with a controlled and ideally uniform distribution, to drive simulation of a design.
Constrained random stimulus generation is the problem of producing random input values for the simulation of a design such that every value satisfies a set of declared constraints, and the values follow a chosen distribution over the legal ones, most often the uniform distribution. The constraints say what a legal input is: a valid opcode with its operand fields, a packet whose length matches its header, a request that only comes when the queue is not full. The verification languages make this the main way to write stimulus; SystemVerilog defines random variables, constraint blocks and the randomize method in Clause 18 of IEEE Std 1800-2017. Finding one legal value is SAT, so the problem is NP-hard; drawing each legal value with the same probability is tied to model counting. Methods are on BDD-based constrained random generation and the pages listed under Known algorithms.
In plain words
A simulation needs inputs, and most random inputs are nonsense for the design: a bus transfer with a length that does not match its address, an instruction with an illegal field. So the engineer writes rules that every input must obey, and a solver picks random values that obey all of them.
Obeying the rules is not enough. If the solver always picks the same easy answer, or favors some answers by accident, many legal situations are never tried. The usual goal is that every legal input is equally likely, or that the likelihoods follow weights the engineer chose on purpose. Finding even one legal input can be as hard as any puzzle of this kind, and picking among all of them fairly is harder still.
Definition
Input. Variables over finite domains, which after bit-blasting are bits; a constraint , given as a Boolean formula, a circuit or a set of word-level relations; and a target distribution. The set of solutions (models) is , and .
Output. A random , or the answer "no solution" when . Each call is independent of the others, and a stream of calls gives a stream of stimuli.
Uniform generation. The output law is for every .
Near-uniform generation. For a tolerance , for every , possibly with a stated probability that the call fails and returns nothing.
Weighted generation. A weight is given, and for . A common form is a product of independent biases, , which is the bias law conditioned on .
With state. In a simulation the constraint often depends on the state of the design or of the test bench, , and a new instance with the current is solved in each cycle or transaction.
Variants
- Uniform, near-uniform, weighted. As defined above. Most practical generators guarantee none of the three and only aim at a spread of values; the definitions say what a claim of "uniform" must mean.
- SystemVerilog constraints. Variables declared
randare solved together;randcvariables cycle through all values of their domain in a random order before any value repeats. The standard asks that, without distributions and ordering, all legal combinations be equally likely. Adistconstraint gives relative weights to the values of one expression; when other constraints also restrict that expression, the result depends on the tool.solve a before bkeeps the set of solutions and changes the law: is drawn as if alone, then given . Soft constraints are dropped when they conflict with hard ones. - Sequences. Constraints over several transactions, such as "no two writes to the same address within four cycles", are written with state in the test bench, which turns them into the stateful form above.
- Word-level or bit-level. Constraints over integers with arithmetic are solved either on words by interval and modular reasoning, or after bit-blasting on bits, where a constraint like becomes an adder circuit.
- Projection. The constraint may use auxiliary variables that are not stimulus, . Then the target is uniform over , which is not the same as uniform over pairs when the number of varies with .
Examples
An ordered pair. Two 2-bit variables with the constraint . The solutions are , so of the 16 assignments, and the uniform law gives each one .
| solution | uniform | before | before |
|---|---|---|---|
| (0,1) | 1/6 | 1/9 | 1/3 |
| (0,2) | 1/6 | 1/9 | 1/6 |
| (0,3) | 1/6 | 1/9 | 1/9 |
| (1,2) | 1/6 | 1/6 | 1/6 |
| (1,3) | 1/6 | 1/6 | 1/9 |
| (2,3) | 1/6 | 1/3 | 1/9 |
The second column draws uniformly from its legal values , then uniformly from the values above : gets and gets . The third column draws from first. Both orders keep the six solutions, and both put three times as much weight on one solution as on another. Drawing all 16 assignments uniformly and keeping the first that satisfies gives the uniform column, after draws on average.
Projection. Let and an auxiliary with . The pairs with are 4 and the pairs with are 1, so uniform pairs give with probability , while uniform stimulus over gives .
A header and a length. A packet has a 2-bit kind and a 6-bit len, with kind == 0 -> len == 0 and kind != 0 -> len inside [1:63]. There are solutions. The uniform law gives kind == 0 the probability ; a generator that first picks kind uniformly gives it . Which one the engineer wants depends on the coverage goal, which is why the law must be stated.
Known algorithms
- Rejection sampling from the uniform law on all assignments. Exact; expected tries .
- BDD-based constrained random generation (Yuan, Shultz, Pixley, Miller and Aziz 1999). Exact uniform or exact weighted generation from path counts on a binary decision diagram of , in time linear in the number of variables per sample once the BDD is built.
- Counting by self-reducibility. Fix the variables one at a time, with probabilities given by the model counts of the two restricted constraints. Exact with an exact counter.
- Hashing. UniWit (CAV 2013) and UniGen (DAC 2014) of Chakraborty, Meel and Vardi add random XOR constraints from a -wise independent family to cut into small cells, enumerate one cell with a SAT solver, and return a random member: near-uniform with a stated tolerance.
- SAT or constraint solvers with random choices. Random decisions and random restarts of a CDCL solver give a spread of solutions with no bound on the bias.
- Steering the law. Coverage-directed test generation changes the weights between runs to reach coverage holes.
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| BDD-based constrained random generation | O(n^2 / w) query | O(B n / w) extra | randomizedexactofflineiterative | 1 language |
Bounds
- NP-hardness. A generator that runs in polynomial time and returns a solution whenever one exists decides SAT: run it on and check the output. If it is randomized and returns a solution with probability at least whenever one exists, the same check puts SAT in RP. So no such generator exists unless , respectively . This holds for any target law, uniform or not.
- Rejection. Drawing uniformly from until returns each solution with probability exactly , and the number of draws has a geometric distribution with success probability , so its mean is . The proof is on BDD-based constrained random generation. The method is exact and fast when solutions are dense, and useless when they are rare, as they are for most real constraints.
- Exact uniform generation and counting. With exact model counts of restricted constraints, self-reducibility gives exact uniform generation with counting calls. Counting is #P-complete, so this route is not polynomial in general. Jerrum, Valiant and Vazirani (1986) showed that exactly uniform generation of witnesses of an NP relation reduces to approximate counting, which places it in the third level of the polynomial hierarchy.
- An NP oracle suffices. Bellare, Goldreich and Petrank (2000) generate each witness with exactly the same probability in probabilistic polynomial time with an NP oracle, failing with a small probability.
- Almost uniform equals approximate counting. For self-reducible relations, almost uniform generation and randomized approximate counting are inter-reducible in polynomial time (Jerrum-Valiant-Vazirani theorem).
- Compiled forms. If is given as a reduced ordered BDD, uniform and weighted generation take time per sample after preprocessing, where is the number of nodes. The BDD itself can be exponential in under every variable order, for example for a constraint on the middle output bit of a multiplier.
Reductions
- SAT to generation. Shown in Bounds: a generator decides SAT by one call and one check.
- Generation to counting. By self-reducibility, as in Known algorithms.
- Circuits to CNF. The Tseitin transformation of a circuit constraint adds one variable per gate whose value is fixed by the inputs, so the models of the CNF are in bijection with the solutions of the circuit. Uniform generation on the CNF, projected onto the input variables, is uniform generation for the circuit.
- Word-level to bit-level. Bit-blasting maps word constraints to Boolean ones with the same solutions on the stimulus bits, plus auxiliary bits fixed by them.
- Weighted to uniform. Product weights with rational biases reduce to uniform generation by giving variable fresh bits and constraining it to be 1 exactly when the number those bits encode is below , for . A solution then has extensions, a number proportional to , so uniform generation on the new constraint, projected onto , is the weighted law.
Applications
- Simulation-based verification. Test benches in SystemVerilog and similar languages declare transactions with constraints and call the solver for each one; the functional coverage model then measures which legal situations the runs reached.
- Processor verification. Instruction stream generators draw instructions, operands and addresses under constraints that keep the program legal and steer it toward interesting cases, such as cache conflicts.
- Closure loops. Coverage-directed generation tunes the weights of the generator from coverage data.
- Beyond hardware. The same problem appears in software test input generation and in sampling configurations of product lines, where hashing-based samplers are used.
References
- IEEE Standard for SystemVerilog, Unified Hardware Design, Specification, and Verification Language, IEEE Std 1800-2017, Clause 18 (Constrained random value generation, with random variables, constraint blocks, distributions, implication, variable ordering and the randomize method)
- Jun Yuan, Carl Pixley and Adnan Aziz, Constraint-Based Verification, Springer, 2006, the chapters on constrained random simulation, the constraint languages, and the BDD-based and other constraint solvers (general locator)
- Mihir Bellare, Oded Goldreich and Erez Petrank, Uniform generation of NP-witnesses using an NP-oracle, Information and Computation 163(2), 510 to 526, 2000, the probabilistic polynomial-time generator, with an NP oracle, that outputs each witness with exactly the same probability
- Mark R. Jerrum, Leslie G. Valiant and Vijay V. Vazirani, Random generation of combinatorial structures from a uniform distribution, Theoretical Computer Science 43, 169 to 188, 1986, the reduction of exactly uniform generation to approximate counting, and the equivalence of almost uniform generation and randomized approximate counting for self-reducible relations
- Supratik Chakraborty, Kuldeep S. Meel and Moshe Y. Vardi, A scalable and nearly uniform generator of SAT witnesses, Computer Aided Verification (CAV 2013), LNCS 8044, 608 to 623, UniWit, near-uniform generation of the models of a CNF formula by random XOR constraints
- Supratik Chakraborty, Kuldeep S. Meel and Moshe Y. Vardi, Balancing scalability and uniformity in SAT witness generator, Proceedings of the 51st Design Automation Conference (DAC 2014), 1 to 6, UniGen, its tolerance parameter and its guarantee that each model is returned with probability within a bounded factor of the uniform probability
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag H74QKF Edit this page History Report an error Markdown