Circuit timing analysis, linear maps, and semantic morphisms Conal - - PowerPoint PPT Presentation

circuit timing analysis linear maps and semantic morphisms
SMART_READER_LITE
LIVE PREVIEW

Circuit timing analysis, linear maps, and semantic morphisms Conal - - PowerPoint PPT Presentation

Circuit timing analysis, linear maps, and semantic morphisms Conal Elliott Tabula IFIP WG 2.8, Nov. 2012 Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 1 / 29 Outline Timing


slide-1
SLIDE 1

Circuit timing analysis, linear maps, and semantic morphisms

Conal Elliott

Tabula

IFIP WG 2.8, Nov. 2012

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 1 / 29

slide-2
SLIDE 2

Outline

Timing analysis Linear transformations Semantics and implementation

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 2 / 29

slide-3
SLIDE 3

Timing analysis

Timing analysis

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 3 / 29

slide-4
SLIDE 4

Timing analysis

Simple timing analysis

Computation with a time delay:

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 4 / 29

slide-5
SLIDE 5

Timing analysis

Trivial timings

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 5 / 29

slide-6
SLIDE 6

Timing analysis

Sequential composition

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 6 / 29

slide-7
SLIDE 7

Timing analysis

Parallel composition

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 7 / 29

slide-8
SLIDE 8

Timing analysis

But ...

Oops: Same circuit (f × g), different timings.

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 8 / 29

slide-9
SLIDE 9

Timing analysis

Multi-path analysis

◮ Max delay for each input/output pair ◮ How do delays compose?

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 9 / 29

slide-10
SLIDE 10

Timing analysis

How do delays compose?

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 10 / 29

slide-11
SLIDE 11

Timing analysis

How do delays compose?

V ⊙ U = W , where Wi,k = Max

j

(Ui,j + Vj,k)

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 11 / 29

slide-12
SLIDE 12

Timing analysis

How do delays compose?

V ⊙ U = W , where Wi,k = Max

j

(Ui,j + Vj,k) Look familiar?

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 11 / 29

slide-13
SLIDE 13

Timing analysis

How do delays compose?

V ⊙ U = W , where Wi,k = Max

j

(Ui,j + Vj,k) Look familiar? Matrix multiplication?

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 11 / 29

slide-14
SLIDE 14

Timing analysis

MaxPlus algebra

type Delay = MaxPlus Double data MaxPlus a = MP a instance Ord a ⇒ AdditiveGroup (MaxPlus a) where MP a ˆ + MP b = MP (a ‘max‘ b) instance (Ord a, Num a) ⇒ VectorSpace (MaxPlus a) where type Scalar (MaxPlus a) = a a · MP b = MP (a + b) Oops – We also need a zero. VectorSpace is overkill. Module over a semi-ring suffices.

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 12 / 29

slide-15
SLIDE 15

Timing analysis

MaxPlus algebra

type Delay = MaxPlus Double data MaxPlus a = −∞ | Fi a instance Ord a ⇒ AdditiveGroup (MaxPlus a) where 0 = −∞ MP a ˆ + MP b = MP (a ‘max‘ b) − ∞ ˆ + = −∞ ˆ + −∞ = −∞ instance (Ord a, Num a) ⇒ VectorSpace (MaxPlus a) where type Scalar (MaxPlus a) = a a · MP b = MP (a + b) · −∞ = −∞

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 13 / 29

slide-16
SLIDE 16

Linear transformations

Linear transformations

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 14 / 29

slide-17
SLIDE 17

Linear transformations

Representation?

How might we represent linear maps/transformations a ⊸ b?

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 15 / 29

slide-18
SLIDE 18

Linear transformations

Representation?

How might we represent linear maps/transformations a ⊸ b?

◮ Matrices ◮ Functions ◮ What else?

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 15 / 29

slide-19
SLIDE 19

Linear transformations

Matrices

   a11 · · · a1m . . . ... . . . an1 · · · anm    Static typing?

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 16 / 29

slide-20
SLIDE 20

Linear transformations

Statically sized matrices

type Mat m n a = Vec m (Vec n a) (◦) :: (IsNat m, IsNat o) ⇒ Mat n o D → Mat m n D → Mat o m D no ◦ mn = crossF dot (transpose no) mn crossF :: (IsNat m, IsNat o) ⇒ (a → b → c) → Vec o a → Vec m b → Mat o m c crossF f as bs = (λa → f a < $ > bs) < $ > as dot :: (Ord a, Num a) ⇒ Vec n a → Vec n a → a u ‘dot‘ v = sum (zipWithV (∗) u v)

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 17 / 29

slide-21
SLIDE 21

Linear transformations

Generalizing

type Mat m n a = m (n a) (◦) :: (Functor m, Applicative n, Traversable n, Applicative o) ⇒ Mat n o D → Mat m n D → Mat o m D no ◦ mn = crossF dot (sequenceA no) mn crossF :: (Functor m, Functor o) ⇒ (a → b → c) → o a → m b → Mat o m c crossF f as bs = (λa → f a < $ > bs) < $ > as dot :: (Foldable n, Applicative n, Ord a, Num a) ⇒ n a → n a → a u ‘dot‘ v = sum (liftA2 (∗) u v)

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 18 / 29

slide-22
SLIDE 22

Linear transformations

Represent via type family (old)

class VectorSpace v ⇒ HasBasis v where type Basis v :: ∗ coord :: v → (Basis v → Scalar v) Linear map as memoized function from basis: newtype a ⊸ b = L (Basis a →M b) See Beautiful differentiation (ICFP 2009).

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 19 / 29

slide-23
SLIDE 23

Linear transformations

Represent as GADT

data a ⊸ b where Dot :: InnerSpace b ⇒ b → (b ⊸ Scalar b) (:△) :: VS3 a c d ⇒

  • - vector spaces with same scalar field

(a ⊸ c) → (a ⊸ d) → (a ⊸ c × d)

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 20 / 29

slide-24
SLIDE 24

Semantics and implementation

Semantics and implementation

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 21 / 29

slide-25
SLIDE 25

Semantics and implementation

Semantics

[ [·] ] :: (a ⊸ b) → (a → b) [ [Dot b] ] = dot b [ [f :△ g] ] = [ [f ] ] △[ [g] ] where, on functions, (f △ g) a = (f a, g a) Recall: data a ⊸ b where Dot :: InnerSpace b ⇒ b → (b ⊸ Scalar b) (:△) :: VS3 a c d ⇒ (a ⊸ c) → (a ⊸ d) → (a ⊸ c × d)

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 22 / 29

slide-26
SLIDE 26

Semantics and implementation

Semantic type class morphisms

Category instance specification: [ [id] ] ≡ id [ [g ◦ f ] ] ≡ [ [g] ] ◦ [ [f ] ] Arrow instance specification: [ [f △ g] ] ≡ [ [f ] ] △[ [g] ] [ [f × g] ] ≡ [ [f ] ] ×[ [g] ] where (△) :: Arrow (❀) ⇒ (a ❀ c) → (a ❀ d) → (a ❀ c × d) (×) :: Arrow (❀) ⇒ (a ❀ c) → (b ❀ d) → (a × b ❀ c × d) The Category and Arrow laws then follow.

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 23 / 29

slide-27
SLIDE 27

Semantics and implementation

Deriving a Category instance

One case: [ [(f :△ g) ◦ h] ] ≡ ([ [f ] ] △[ [g] ]) ◦ [ [h] ] ≡ [ [f ] ] ◦ [ [h] ] △[ [g] ] ◦ [ [h] ] ≡ [ [f ◦ h △ g ◦ h] ] (where f ◦ h △ g ◦ h ≡ (f ◦ h) △(g ◦ h)). Uses: (f △ g) ◦ h ≡ f ◦ h △ g ◦ h Implementation: (f :△ g) ◦ h = f ◦ h :△ g ◦ h

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 24 / 29

slide-28
SLIDE 28

Semantics and implementation

Deriving a Category instance

[ [Dot s ◦ Dot b] ] ≡ dot s ◦ dot b ≡ dot (s · b) ≡ [ [Dot (s · b)] ] [ [Dot (a, b) ◦ (f :△ g)] ] ≡ dot (a, b) ◦ ([ [f ] ] △[ [g] ]) ≡ add ◦ (dot a ◦ [ [f ] ] △ dot b ◦ [ [g] ]) ≡ dot a ◦ [ [f ] ] ˆ + dot b ◦ [ [g] ] ≡ [ [Dot a ◦ f ˆ + Dot b ◦ g] ] Uses: dot (a, b) ≡ add ◦ (dot a × dot b) (k × h) ◦ (f △ g) ≡ k ◦ f △ h ◦ g [ [f ˆ + g] ] ≡ [ [f ] ] ˆ +[ [g] ]

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 25 / 29

slide-29
SLIDE 29

Semantics and implementation

Deriving an Arrow instance

[ [f △ g] ] ≡ [ [f ] ] △[ [g] ] ≡ [ [f :△ g] ] [ [f × g] ] ≡ [ [f ] ] ×[ [g] ] ≡ [ [f ] ] ◦ fst △[ [g] ] ◦ snd ≡ [ [compFst f ] ] △[ [compSnd g] ] ≡ [ [compFst f :△ compSnd g] ] assuming [ [compFst f ] ] ≡ [ [f ] ] ◦ fst [ [compSnd g] ] ≡ [ [g] ] ◦ snd

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 26 / 29

slide-30
SLIDE 30

Semantics and implementation

Composing with fst and snd

compFst :: VS3 a b c ⇒ a ⊸ c → a × b ⊸ c compSnd :: VS3 a b c ⇒ b ⊸ c → a × b ⊸ c Derivation: dot a

  • fst ≡ dot (a, 0)

(f △ g) ◦ fst ≡ f ◦ fst △ g ◦ fst Implementation: compFst (Dot a) = Dot (a, 0) compFst (f :△ g) = compFst f △ compFst g compSnd (Dot b) = Dot (0, b) compSnd (f :△ g) = compSnd f △ compSnd g

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 27 / 29

slide-31
SLIDE 31

Semantics and implementation

Adding linear maps

[ [Dot b ˆ + Dot c] ] ≡ dot b ˆ + dot c ≡ dot (b ˆ + c) ≡ [ [Dot (b ˆ + c)] ] [ [(f :△ g) ˆ +(h :△ k)] ] ≡ ([ [f ] ] △[ [g] ]) ˆ +([ [h] ] △[ [k] ]) ≡ ([ [f ] ] ˆ +[ [h] ]) △([ [g] ] ˆ +[ [k] ]) ≡ [ [(f ˆ + h) △(g ˆ + k)] ] Other cases don’t type-check. Uses (on functions): (f △ g) ˆ +(h △ k) ≡ (f ˆ + h) △(g ˆ + k)

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 28 / 29

slide-32
SLIDE 32

Semantics and implementation

What next?

◮ Fancier timing analysis ◮ What else is linear? ◮ More examples of semantic type class morphisms

Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 29 / 29