File size: 2,817 Bytes
7746322
 
 
 
 
 
 
82872ac
7746322
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82872ac
7746322
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
"""Directional width of an unregularized loss level set in the original weight coordinates."""

import math
import time

import torch

from .flatness_stats import width_statistics
from .models import trainable_digest
from .penalties import random_signs
from .windows import generator


def crossing(loss_at, threshold):
    """Bracket the first observed crossing on the registered outward grid and refine it."""
    lower = 0.
    for exponent in range(-3, 8):
        upper = 2.**exponent
        value = float(loss_at(upper))
        if not math.isfinite(value):
            raise FloatingPointError("Nonfinite width-probe loss")
        if value >= threshold:
            for _ in range(12):
                middle = (lower + upper) / 2
                value = float(loss_at(middle))
                if not math.isfinite(value):
                    raise FloatingPointError("Nonfinite width-probe loss")
                if value >= threshold:
                    upper = middle
                else:
                    lower = middle
            return {"lower":lower, "upper":upper}
        lower = upper
    return {"lower":lower, "upper":None}


@torch.no_grad()
def measure_width(model, loss, directions=16):
    """Probe two rays per unit direction; preserve exact weights, gradients, mode and RNG."""
    started = time.perf_counter()
    parameters = [p for p in model.parameters() if p.requires_grad]
    original = [p.detach().clone() for p in parameters]
    scale = math.sqrt(sum(p.numel() for p in parameters))
    digest = trainable_digest(model)
    modes = [(module,module.training) for module in model.modules()]
    model.eval()
    rays = []
    try:
        base = float(loss())
        if not math.isfinite(base):
            raise FloatingPointError("Nonfinite unperturbed width-probe loss")
        for index in range(directions):
            signs = random_signs(parameters,generator(parameters[0].device,88731,index,947))
            def at(radius):
                # Reconstruct each perturbation from the saved coordinates to avoid drift.
                torch._foreach_copy_(parameters,original)
                torch._foreach_add_(parameters,signs,alpha=radius/scale)
                return float(loss())
            rays.append({"direction":index,"positive":crossing(at,base+.1),
                         "negative":crossing(lambda radius: at(-radius),base+.1)})
    finally:
        torch._foreach_copy_(parameters,original)
        for module,mode in modes:
            module.training = mode
    if trainable_digest(model)!=digest:
        raise RuntimeError("Width measurement changed the training weights")
    return {"baseline_nll":base,"delta_nll":.1,**width_statistics(rays),
            "weights_sha256":digest,"weights_restored":True,"seconds":time.perf_counter()-started}