File size: 4,446 Bytes
32d1b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Persistent exact frontier for K simultaneous quality chains and shared logits."""
import jax
import jax.numpy as jnp
from jax import lax
from jax.experimental import pallas as pl
from jax.experimental.pallas import triton as plt
from d3_fbqs_scalar_backend import gather, physical


def make_decoder(width=10, families=3, interpret=False, derivatives=False, num_warps=8):
    size = 1 << (width + families)
    def decode(theta, dets, probabilities, masks, measured, quality_times, transition_gaps):
        nops, batch = probabilities.shape
        directions = 3 if derivatives else 1
        def kernel(th, ds, ps, ms, ts, qs, gs, out, jac):
            shot, direction = pl.program_id(0), pl.program_id(1)
            theta = tuple(pl.load(th, (j,)) for j in range(3))
            unit = tuple((direction == j).astype(jnp.float32) for j in range(3))
            ix = jnp.arange(size, dtype=jnp.int32)
            quality = tuple((ix & (1 << (width + j))) != 0 for j in range(families))
            def initial(t0, t1, t2):
                pi, _, _ = physical(t0, t1, t2)
                f = ((ix & ((1 << width) - 1)) == 0).astype(jnp.float32)
                for j in range(families):
                    f = f * jnp.where(quality[j], pi, 1 - pi)
                return f
            if derivatives:
                f, df = jax.jvp(initial, theta, unit)
            else:
                f = initial(*theta); df = jnp.zeros_like(f)
            def iteration(op, carry):
                f, df = carry
                p, mask, detector = pl.load(ps, (op, shot)), pl.load(ms, (op,)), pl.load(ts, (op,))
                times = tuple(pl.load(qs, (j, op)) for j in range(families))
                gaps = tuple(pl.load(gs, (j, op)) for j in range(families))
                observed = pl.load(ds, (shot, jnp.maximum(detector, 0))).astype(jnp.int32)
                def step(t0, t1, t2, f):
                    pi, rho, alpha = physical(t0, t1, t2)
                    for j in range(families):
                        def transition(f):
                            persistence = rho ** gaps[j]
                            a, b = pi * (1 - persistence), pi + (1 - pi) * persistence
                            flipped = gather(f, ix ^ (1 << (width + j)))
                            return jnp.where(quality[j], b * f + a * flipped, (1 - a) * f + (1 - b) * flipped)
                        f = lax.cond(gaps[j] > 0, transition, lambda f: f, f)
                    def error(f):
                        deviation = jnp.zeros_like(f)
                        count = jnp.int32(0)
                        for j in range(families):
                            active = times[j] >= 0
                            deviation = deviation + jnp.where(active, quality[j].astype(jnp.float32) - pi, 0.)
                            count = count + active.astype(jnp.int32)
                        deviation = deviation / jnp.maximum(count, 1).astype(jnp.float32)
                        rate = p + alpha * jnp.minimum(p / pi, (1 - p) / (1 - pi)) * deviation
                        return (1 - rate) * f + rate * gather(f, ix ^ mask)
                    def project(f):
                        g = gather(f, (ix & ~mask) | (observed * mask)) * ((ix & mask) == 0)
                        return g / jnp.maximum(jnp.sum(g), 1e-30)
                    return lax.cond(detector < 0, error, project, f)
                if derivatives:
                    return jax.jvp(step, (*theta, f), (*unit, df))
                return step(*theta, f), df
            f, df = lax.fori_loop(0, nops, iteration, (f, df))
            def observable(f):
                return jnp.sum(jnp.where((ix & 1) != 0, f, 0)) / jnp.maximum(jnp.sum(f), 1e-30)
            if derivatives:
                q, dq = jax.jvp(observable, (f,), (df,))
            else:
                q, dq = observable(f), jnp.float32(0)
            pl.store(out, (direction, shot), q)
            pl.store(jac, (direction, shot), dq)
        return pl.pallas_call(kernel, out_shape=(jax.ShapeDtypeStruct((directions, batch), jnp.float32), jax.ShapeDtypeStruct((directions, batch), jnp.float32)),
                              grid=(batch, directions), interpret=interpret, backend='triton',
                              compiler_params=plt.CompilerParams(num_warps=num_warps, num_stages=1))(
            theta, dets, probabilities, masks, measured, quality_times, transition_gaps)
    return jax.jit(decode)