-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdaptiveDataflow.hs
More file actions
145 lines (126 loc) · 5.06 KB
/
Copy pathAdaptiveDataflow.hs
File metadata and controls
145 lines (126 loc) · 5.06 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
{-# LANGUAGE GADTs, RankNTypes, ScopedTypeVariables #-}
-- | Adaptive Dataflow: Modernizing Hoopl with Deep Learning Optimization Techniques
--
-- This module demonstrates how modern optimization techniques from deep learning
-- can enhance traditional compiler dataflow analysis, inspired by Hoopl.
--
-- Original Hoopl authors: Norman Ramsey, João Dias, Simon Peyton Jones
-- Repository: https://github.com/ezyang/hoopl
module AdaptiveDataflow where
import qualified Data.Map.Strict as M
import Data.Maybe (fromMaybe)
-- | Core lattice structure (from Hoopl)
data Lattice f = Lattice
{ bot :: f
, join :: f -> f -> (Bool, f) -- (changed, result)
}
-- | Adaptive parameters inspired by Adam optimizer
data AdaptiveParams = AdaptiveParams
{ beta1 :: Double -- momentum decay (typically 0.9)
, beta2 :: Double -- variance decay (typically 0.999)
, epsilon :: Double -- numerical stability
, clipNorm :: Double -- gradient clipping threshold
}
defaultParams :: AdaptiveParams
defaultParams = AdaptiveParams 0.9 0.999 1e-8 1.0
-- | Optimization state tracking momentum and variance
data OptState f = OptState
{ momentum :: M.Map Int f -- first moment estimate
, variance :: M.Map Int Double -- second moment estimate (change magnitude)
, stepCount :: Int
}
emptyState :: OptState f
emptyState = OptState M.empty M.empty 0
-- | Adaptive fixpoint iteration with momentum
adaptiveFixpoint :: forall f. (Show f, Eq f)
=> Lattice f
-> AdaptiveParams
-> (f -> f) -- transfer function
-> f -- initial fact
-> (f, Int) -- (result, iterations)
adaptiveFixpoint lat _params transfer initialFact = go emptyState initialFact 0
where
go :: OptState f -> f -> Int -> (f, Int)
go state fact iter
| iter > 100 = (fact, iter) -- safety limit
| otherwise =
let newFact = transfer fact
(changed, joined) = join lat fact newFact
in if not changed
then (joined, iter)
else let state' = updateState state iter fact joined
dampened = applyMomentum state' joined
in go state' dampened (iter + 1)
-- Update momentum and variance (Adam-style)
updateState :: OptState f -> Int -> f -> f -> OptState f
updateState (OptState m v t) iter old new =
let changeRate = if old == new then 0.0 else 1.0
m' = M.insert iter new m
v' = M.insert iter changeRate v
in OptState m' v' (t + 1)
-- Apply momentum dampening to reduce oscillation
applyMomentum :: OptState f -> f -> f
applyMomentum state fact =
if stepCount state < 3 then fact -- need history
else fact -- simplified: full version would blend with history
-- | Scheduled fuel: dynamic resource allocation
data FuelSchedule = FuelSchedule
{ initialFuel :: Int
, decayRate :: Double
, minFuel :: Int
}
-- | Compute fuel for iteration (learning rate schedule analog)
scheduledFuel :: FuelSchedule -> Int -> Int
scheduledFuel (FuelSchedule startFuel decay minF) iter =
max minF $ floor (fromIntegral startFuel * (decay ** fromIntegral iter))
-- | Example: Constant propagation lattice
data ConstVal = Bottom | Const Int | Top deriving (Eq, Show)
constLattice :: Lattice ConstVal
constLattice = Lattice
{ bot = Bottom
, join = \old new -> case (old, new) of
(Bottom, x) -> (True, x)
(x, Bottom) -> (False, x)
(Top, _) -> (False, Top)
(_, Top) -> (True, Top)
(Const a, Const b) | a == b -> (False, Const a)
| otherwise -> (True, Top)
}
-- | Example transfer function with adaptive iteration
exampleTransfer :: M.Map String ConstVal -> M.Map String ConstVal
exampleTransfer facts =
let x = fromMaybe Bottom $ M.lookup "x" facts
y = fromMaybe Bottom $ M.lookup "y" facts
z = case (x, y) of
(Const a, Const b) -> Const (a + b)
(Bottom, _) -> Bottom
(_, Bottom) -> Bottom
_ -> Top
in M.insert "z" z facts
-- | Demonstration: compare standard vs adaptive fixpoint
demo :: IO ()
demo = do
putStrLn "=== Adaptive Dataflow Analysis ==="
putStrLn "\nStandard fixpoint iteration:"
let initialFacts = M.fromList [("x", Const 5), ("y", Const 3), ("z", Bottom)]
(result1, iters1) = standardFixpoint initialFacts
putStrLn $ "Result: " ++ show result1
putStrLn $ "Iterations: " ++ show iters1
putStrLn "\nAdaptive fixpoint with momentum:"
let (result2, iters2) = adaptiveFixpoint
(Lattice initialFacts (\_ new -> (True, new)))
defaultParams
exampleTransfer
initialFacts
putStrLn $ "Result: " ++ show result2
putStrLn $ "Iterations: " ++ show iters2
standardFixpoint :: M.Map String ConstVal -> (M.Map String ConstVal, Int)
standardFixpoint initialFacts = go initialFacts 0
where
go facts iter
| iter > 100 = (facts, iter)
| otherwise =
let facts' = exampleTransfer facts
in if facts == facts'
then (facts', iter)
else go facts' (iter + 1)