Synthesis with the Sketch System
description
Transcript of Synthesis with the Sketch System
![Page 1: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/1.jpg)
Synthesis with the Sketch System
DAY 1
Armando Solar-Lezama
![Page 2: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/2.jpg)
The ChallengeComputer should help make programming easier
Problem Programming requires insight and experience
Computers are not that smart
Interaction between programmers and tools is key
![Page 3: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/3.jpg)
The sketching approach
Let the programmer control the implementation strategy
Focus the synthesizer on the low-level details
Key design principle: Exploit familiar programming concepts
![Page 4: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/4.jpg)
Example•You want to partition N elements over P procs
How many elements should a processor get?
•Obvious answer is N/P
•Obvious answer is wrong!
N = 18P = 5
![Page 5: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/5.jpg)
void partition(int p, int P, int N, ref int ibeg, ref int iend){ if(p< {$ p, P, N, N/P, N%P : *, + $} ){ iend = {$ p, P, N, N/P, N%P : *, + $}; ibeg = {$ p, P, N, N/P, N%P : *, + $}; }else{ iend = {$ p, P, N, N/P, N%P : *, + $}; ibeg = {$ p, P, N, N/P, N%P : *, + $}; }}
Synthesizing a partition function•What do we know?
The interface to the function we want Not all processors will get the same # of elements The kind of expressions we expect
p
PNN/P
N%P* +
![Page 6: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/6.jpg)
harness void testPartition(int p, int N, int P){ if(p>=P || P < 1){ return; } int ibeg, iend; partition(p, P, N, ibeg, iend); assert iend - ibeg < (N/P) + 2; if(p+1 < P){ int ibeg2, iend2; partition(p+1, P, N, ibeg2, iend2); assert iend == ibeg2; } if(p==0){ assert ibeg == 0; } if(p==P-1){ assert iend == N; } }
Synthesizing a partition function•How does the system know what a partition is?
Partitions should be balanced
Adjacent partitions should match
First and last partition should go all the way to the
ends
![Page 7: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/7.jpg)
DEMO
![Page 8: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/8.jpg)
Solution
void partition(int p, int P, int N, ref int ibeg, ref int iend){ if(p < (N % P)){ iend = ((N / P) + 1) * (1 + p); ibeg = p + ((N / P) * p); }else{ iend = ((N / P) * p) + ((N / P) + (N % P)); ibeg = (N % P) + ((N / P) * p); }}
![Page 9: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/9.jpg)
THE SKETCH LANGUAGE
![Page 10: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/10.jpg)
Sketch language basics •Sketches are programs with holes
write what you know use holes for the rest
![Page 11: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/11.jpg)
Specifications•Idea: Use unit tests as specification
Programmers know how to write those
•Two mechanisms assertions
function equivalenceblockedMatMul(Mat a, Mat b) implements matMul
assert x > y;
![Page 12: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/12.jpg)
Holes•Holes are placeholders for the synthesizer
synthesizer replaces hole with concrete code fragment fragment must come from a set defined by the user
Defining sets of code fragments is the key to Sketching effectively
![Page 13: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/13.jpg)
Language Design StrategyExtend base language with one construct
Constant hole: ??
Synthesizer replaces ?? with a constantHigh-level constructs defined in terms of ??
int bar (int x){ int t = x * ??; assert t == x + x; return t;}
int bar (int x){ int t = x * 2; assert t == x + x; return t;}
![Page 14: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/14.jpg)
Integer Holes Sets of Expressions
•Expressions with ?? == sets of expressions
linear expressions x*?? + y*?? polynomials x*x*?? + x*?? + ??
sets of variables ?? ? x : y
![Page 15: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/15.jpg)
Integer Holes Sets of Expressions•Example: Least Significant Zero Bit
0010 0101 0000 0010
•Trick: Adding 1 to a string of ones turns the next zero to a 1 i.e. 000111 + 1 = 001000
int W = 32;bit[W] isolate0 (bit[W] x) { // W: word size
bit[W] ret = 0;for (int i = 0; i < W; i++)
if (!x[i]) { ret[i] = 1; return ret; } }
!(x + ??) & (x + ??) !(x + 1) & (x + 0)
!(x + 0) & (x + 1)
!(x + 1) & (x + 0xFFFF)
!(x + 0xFFFF) & (x + 1)
![Page 16: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/16.jpg)
Integer Holes Sets of Expressions•Example: Least Significant Zero Bit
0010 0101 0000 0010int W = 32;bit[W] isolate0 (bit[W] x) { // W: word size
bit[W] ret = 0;for (int i = 0; i < W; i++)
if (!x[i]) { ret[i] = 1; return ret; } }
bit[W] isolateSk (bit[W] x) implements isolate0 {
return !(x + ??) & (x + ??) ;}
![Page 17: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/17.jpg)
Integer Holes Sets of Expressions
•Expressions with ?? == sets of expressions linear expressions x*?? + y*?? polynomials x*x*?? + x*?? + ?? sets of variables ?? ? x : y
•Semantically powerful but syntactically clunky Regular Expressions are a more convenient
![Page 18: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/18.jpg)
Regular Expression Generators•{| RegExp |}
•RegExp supports choice ‘|’ and optional ‘?’ can be used arbitrarily within an expression- to select operands {| (x | y | z) + 1 |}- to select operators {| x (+ | -) y |}- to select fields {| n(.prev | .next)? |}- to select arguments {| foo( x | y, z) |}
•Set must respect the type system all expressions in the set must type-check all must be of the same type
![Page 19: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/19.jpg)
Sets of statements•Statements with holes = sets of statements
•Higher level constructs for Statements too repeatbit[W] tmp=0;
repeat(3){ {| x | tmp |} = {| (!)?((x | tmp) (& | +) (x | tmp | ??)) |};}return tmp;
![Page 20: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/20.jpg)
repeat
•Avoid copying and pasting repeat(n){ s} s;s;…s;
each of the n copies may resolve to a distinct stmt
n can be a hole too.
n
bit[W] tmp=0;repeat(??){ {| x | tmp |} = {| (!)?((x | tmp) (& | +) (x | tmp | ??)) |};}return tmp;
![Page 21: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/21.jpg)
GENERATORS
![Page 22: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/22.jpg)
User defined generators•Mechanism to define sets of code fragments
•They look like functions But with a few caveats
![Page 23: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/23.jpg)
Key features of generators•Different dynamic invocations • different code
•Recursive generators = grammar of expressionsgenerator bit[W] gen(bit[W] x, int bnd){ assert bnd > 0; if(??) return x; if(??) return ??; if(??) return ~gen(x, bnd-1); if(??){ return {| gen(x, bnd-1) (+ | & | ^) gen(x, bnd-1) |}; }}
bit[W] isolate0sk (bit[W] x) implements isolate0 { return gen(x, 3);}
![Page 24: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/24.jpg)
CONSTRAINT BASED SYNTHESIS PRIMER
![Page 25: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/25.jpg)
Synthesis problem at the high level•Game theoretic view of synthesis
For every move of the environment
Synthesized program makes a counter move
![Page 26: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/26.jpg)
The challenge of synthesis•For functions, the environment controls the inputs
i.e. whatever we synthesize must work for all inputs
•Modeled with a doubly quantified constraint
What does it mean to quantify over programs?
![Page 27: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/27.jpg)
Quantifying over programs•Synthesis as curve fitting
we want a function that satisfies some properties
•It’s hard to do curve fitting with arbitrary curves Instead, people use parameterized families of curves Quantify over parameters instead of over functions
•A sketch is just a way of describing these families
![Page 28: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/28.jpg)
31
InsightSketches are not arbitrary constraint systems
They express the high level structure of a program
A small number of inputs can be enough focus on corner cases
This is an inductive synthesis problem !
∃𝑐 ∀ 𝑖𝑛∈𝐸𝑄(𝑖𝑛 ,𝑐 )where E = {x1, x2, …, xk}
![Page 29: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/29.jpg)
Insert your favorite checker here
CEGIS
{𝒊𝒏𝒊}
∃𝒄 𝒔 .𝒕 .𝑪𝒐𝒓𝒓𝒆𝒄𝒕 (𝑷 𝒄 , 𝒊𝒏𝒊) ∃ 𝒊𝒏𝒔 .𝒕 .¬𝑪𝒐𝒓𝒓𝒆𝒄𝒕 (𝑷𝒄 , 𝒊𝒏𝒊)
Synthesize Check𝒄
𝒊𝒏
![Page 30: Synthesis with the Sketch System](https://reader036.fdocuments.net/reader036/viewer/2022062812/56816414550346895dd5c708/html5/thumbnails/30.jpg)
•Constraints for each follow from semantics
Loops handled through simple unrolling
Insert your favorite checker here
CEGIS
{𝒊𝒏𝒊}
∃𝒄 𝒔 .𝒕 .𝑪𝒐𝒓𝒓𝒆𝒄𝒕 (𝑷 𝒄 , 𝒊𝒏𝒊)
Synthesize Check𝒄
𝒊𝒏