
Stoke – A stochastic superoptimizer and program synthesizer - lelf
http://stoke.stanford.edu/
======
P-ala-din
I had to use a very crude form of superoptimization for an assembly course
this summer. The goal was to create a fast to upper function for ASCII.

I was already familiar with the method to check for zero (with chars being
packed in a word): ( x - 0x010101010x01010101UL) & ~(x) & 0x8080808080808080UL

so my idea was to find an equivalent formula that puts 0x80 in every byte
where there is a value between 0 - 25, shift it by 2, and xor it with the
original word.

I simply tired every combination of simple operators and repeating-constants
and found (0x9999999999999999UL - x) & (~x) & 0x8080808080808080UL

------
jonahbenton
Back in the day, "stochastic superoptimization" would be a reasonable
description for an application of genetic programming. Guess I will have to
read the paper to understand what's new.

~~~
CalChris
Yes, _genetic programming_ arguably goes back to Turing which is way back in
the day, but the term _superoptimizer_ goes back to Henry Masselin's 1987
paper, _Superoptimizer - a look at the smallest program_.

[https://web.stanford.edu/class/cs343/resources/cs343-annot-s...](https://web.stanford.edu/class/cs343/resources/cs343-annot-
superopt.pdf)

~~~
jonahbenton
Thanks for the pointer. Yeah, re: GP I was more referring to the work that
came with Holland and after.

Re: "smallest program" I will always think of Tom Ray's Tierra. Went to a demo
of his in 1991, mind-blowing.

[https://www.cc.gatech.edu/~turk/bio_sim/articles/tierra_thom...](https://www.cc.gatech.edu/~turk/bio_sim/articles/tierra_thomas_ray.pdf)

------
red2awn
I haven't looked at it in detail, but from what I gathered from the README, it
just seems like a very simple form of linear genetic programming (with only
mutation and no crossover).

------
TriNetra
Can it also reduce the size of the binary? I imagine there could be lot of
opportunities there given the huge size of generated programs these days.

~~~
pierrec
Yes. " _STOKE can be used in many different scenarios, such as optimizing code
for performance or size_ ". Note that this is the 4th sentence in the linked
page. However the README hints that STOKE is unlikely to improve much over gcc
or llvm optimizations such as -Os / -Oz for binary size.

------
arauhala
This is extremely intesting domain. I feel that even more interesting topic
than optimizing code would be in generating code to replicate a process based
on input / output samples.

~~~
robertlagrant
It's as easy as knowing the next number in the sequence:

1, 2, 4, ?

~~~
IHLayman
I know that one: 1, 2, 4, 8, 16, 31...
[https://oeis.org/A001591](https://oeis.org/A001591)

~~~
jonathanstrange
Off-topic but this made me wonder. As the name implies, Solomonoff Induction
only works for inductive inference from empirical data. Is there something
similar for mathematics that would allow us to list the simplest sequences
first, then more complex sequences, and so on? Or does the complexity of the
formulation of a sequence depend on the choice of a foundation of mathematics
and/or arbitrary conventions?

~~~
qnsi
sorry this is also offtopic. Saw your link 2 years ago to
[https://sites.google.com/view/lvu18lisbon](https://sites.google.com/view/lvu18lisbon)

can you recommend some decision theory reading? Maybe more on a practical
side?

~~~
jonathanstrange
Depends on what you're interested in and your level of mathematical maturity.
I assume you're interested in real-world, actually applied decision making
rather than philosophical debates, right? Then you have to look for
multicriteria decision making (MCDM) and multi-attribute utility theory
(MAUT).

Eisenführ/Weber/Langer: _Rational Decision Making_. Springer 2010. This is a
good practical introduction in the standard methodology.

Keeney/Raiffa: _Decisions with Multiple Objectives: Preferences and Value
Tradeoffs_. Wiley & Sons 1976. This is an older work, but very good. It's
mathematically more rigorous and important if you want to understand additive
and multiplicative models.

Both of them are practical and give good examples. The first one is easier to
read. If you're not afraid of more complicated mathematics, I can also give
you some more other references. I guess you wouldn't be asking me then,
however, because there is plenty of sources. I generally recommend Peter C.
Fishburn's work, insofar as I can understand it -- some of it is too hard for
me.

~~~
qnsi
thank you so much for taking time to reply! it really means a lot and I will
follow your recommendations

------
emayljames
Could you use such a system to replicate a closed source program?.

~~~
red2awn
This is, in essence, a monkey trying to lower a cost function by randomly
manipulating an input AST (empty for synthesis). So theoretically it will be
able to produce any program, just not in a reasonable time frame.

------
jonnypotty
How do you reason about correctness when stoke changes your code? Does it rely
on invariance of test cases or does it try to 'understand' the intent of the
program?

~~~
sanxiyn
STOKE proves full equivalence.

~~~
TheOtherHobbes
Of trivial code blocks.

That's not a terrible goal if you want to make key libraries faster and more
efficient.

But I'm unconvinced that it would be easy to scale up these techniques to
auto-clone and improve a big application like Photoshop or Word - or even a
generic CRUD app.

~~~
jcranmer
This is basically doing what compiler engineers call peephole optimization:
optimizing small windows of code, which is a standard part of the optimizer
pipeline.

One advantage of peephole optimization is that pretty much all code is
amenable to it, so it doesn't rely on their being one key, hot loop to
optimize to see performance gains. The main disadvantage is that gains in
peephole optimization tend to be quite small, so you're combing for single-
digit percentage speedups.

------
yazr
Should this be modifiable for higher-level byte-code?

My thinking is to provide hand-crafted primitives to transform byte code, and
maybe use an existing optimizer to do the search.

~~~
CalChris
_Souper_ does something like that with IR optimization.

[https://github.com/google/souper](https://github.com/google/souper)

------
TheSpiceIsLife
Slightly offtop, there was a wee thread a few days ago about naming software
projects, and now we see _Stroke_.

I hope we see a trend in brain-injury related software project names.

~~~
rscho
Yes, we should absolutely guard FOSS projects against people who read too
fast.

It's a nice name for a strochastic optimizer, though...

~~~
TheSpiceIsLife
Maybe I was having a stroke when I read it early. Heh. Oops.

