Joins Aggregates Optimization https://fdbresearch.github.io Dan - - PowerPoint PPT Presentation

joins aggregates optimization
SMART_READER_LITE
LIVE PREVIEW

Joins Aggregates Optimization https://fdbresearch.github.io Dan - - PowerPoint PPT Presentation

Joins Aggregates Optimization https://fdbresearch.github.io Dan Olteanu PhD Open School University of Warsaw November 24, 2018 1 / 1 Acknowledgements Some work reported in this course has been done in the context of the FDB project,


slide-1
SLIDE 1

1 / 1

Joins → Aggregates → Optimization

https://fdbresearch.github.io Dan Olteanu

PhD Open School University of Warsaw November 24, 2018

slide-2
SLIDE 2

Acknowledgements

Some work reported in this course has been done in the context of the FDB project, LogicBlox, and RelationalAI by Zavodn´ y, Schleich, Kara, Nikolic, Zhang, Ciucanu, and Olteanu (Oxford) Abo Khamis and Ngo (RelationalAI), Nguyen (U. Michigan) Some of the following slides are derived from presentations by Aref (motivation) Abo Khamis (optimization diagrams) Kara (covers, IVMǫ, and many graphics) Ngo (functional aggregate queries) Schleich (performance and quizzes) Lastly, Kara and Schleich proofread the slides. I would like to thank them for their support!

2 / 1

slide-3
SLIDE 3

Goal of This Course

Introduction to a principled approach to in-database computation This course starts where mainstream database courses finish. Part 1: Joins Part 2: Aggregates

Part 3: Optimization

◮ Learning models inside vs outside the database ◮ From learning to factorized aggregate computation ◮ Learning under functional dependencies ◮ In-database linear algebra: Decompositions of matrices defined by joins

3 / 1

slide-4
SLIDE 4

4 / 1

Outline of Part 3: Optimization

slide-5
SLIDE 5

AI/ML: The Next Big Opportunity

AI is emerging as general purpose technology

◮ Just as computing became general purpose 70 years ago

A core ability of intelligence is the ability to predict

◮ Convert information you have into information you need

The quality of the prediction is increasing as the cost per prediction is decreasing

◮ We use more of it to solve existing problems

◮ Consumer demand forecasting

◮ We use it for new problems where it was not used before

◮ From broadcast to personalized advertising ◮ From shop-then-ship to ship-then-shop

5 / 1

slide-6
SLIDE 6

Most Enterprises Rely on Relational Data for AI Models

Retail: 86% relational Insurance: 83% relational Marketing: 82% relational Financial: 77% relational

Source: The State of Data Science & Machine Learning 2017, Kaggle, October 2017 (based on 2017 Kaggle survey of 16,000 ML practitioners)

6 / 1

slide-7
SLIDE 7

Relational Model: The Jewel in the Database Crown

Last 40 years have witnessed massive adoption

  • f the Relational Model

Many human hours invested in building relational models Relational databases are rich with knowledge

  • f the underlying domains

Availability of curated data made it possible to learn from the past and to predict the future for both humans (BI) and machines (AI)

7 / 1

slide-8
SLIDE 8

Current State of Affairs in Building Predictive Models

Current ML technology THROWS AWAY the relational structure and domain knowledge that can help build BETTER MODELS Design matrix Features Samples

8 / 1

slide-9
SLIDE 9

Learning over Relational Databases: Revisit from First Principles

9 / 1

slide-10
SLIDE 10

In-database vs. Out-of-database Learning

Feature Extraction Query DB materialized output = design matrix ML tool θ Model

10 / 1

Out-of-database learning requires: [KBY17,PRWZ17]

  • 1. Materializing the query result
  • 2. DBMS data export and ML tool import
  • 3. One/multi-hot encoding of categorical variables
slide-11
SLIDE 11

In-database vs. Out-of-database Learning

Feature Extraction Query DB materialized output = design matrix ML tool θ Model

10 / 1

Out-of-database learning requires: [KBY17,PRWZ17]

  • 1. Materializing the query result
  • 2. DBMS data export and ML tool import
  • 3. One/multi-hot encoding of categorical variables

All these steps are very expensive and unnecessary!

slide-12
SLIDE 12

In-database vs. Out-of-database Learning [ANNOS18a+b]

Feature Extraction Query DB materialized output = design matrix ML Tool θ Model Model Reformulation Optimized Query+Aggregates Factorized Query Evaluation Optimization

In-database learning exploits the query structure, the database schema, and the constraints.

11 / 1

slide-13
SLIDE 13

Aggregation is the Aspiring to All Problems [SOANN19]

Model # Features # Aggregates Supervised: Regression Linear regression n O(n2) Polynomial regression degree d O(nd) O(n2d) Factorization machines degree d O(nd) O(n2d) Supervised: Classification Decision tree (k nodes) n O(k · n · p · c) (c conditions/feature, p categories/label) Unsupervised k-means (const approx) n O(k · n) PCA (rank k) n O(k · n2) Chow-Liu tree n O(n2)

12 / 1

slide-14
SLIDE 14

Does This Matter in Practice? A Retailer Use Case

Relation Cardinality Arity (Keys+Values) File Size (CSV) Inventory 84,055,817 3 + 1 2 GB Items 5,618 1 + 4 129 KB Stores 1,317 1 + 14 139 KB Demographics 1,302 1 + 15 161 KB Weather 1,159,457 2 + 6 33 MB 2.1 GB

13 / 1

slide-15
SLIDE 15

Out-of-Database Solution: PostgreSQL+TensorFlow

Train a linear regression model to predict inventory units Design matrix defined by the natural join of all relations, where the join keys are removed Join of Inventory, Items, Stores, Demographics, Weather Cardinality (# rows) 84,055,817 Arity (# columns) 44 (3 + 41) Size on disk 23GB Time to compute in PostgreSQL 217 secs Time to Export from PostgreSQL 373 secs Time to learn parameters with TensorFlow∗ > 12,000 secs

TensorFlow: 1 epoch; no shuffling; 100K tuple batch; FTRL gradient descent

14 / 1

slide-16
SLIDE 16

In-Database versus Out-of-Database Learning

PostgreSQL+TensorFlow In-Database (Sept’18) Time Size (CSV) Time Size (CSV) Input data – 2.1 GB – 2.1 GB Join 217 secs 23 GB – – Export 373 secs 23 GB – – Aggregates – – 18 secs 37 KB GD > 12K secs – 0.5 secs – Total time > 12.5K secs 18.5 secs

15 / 1

slide-17
SLIDE 17

In-Database versus Out-of-Database Learning

PostgreSQL+TensorFlow In-Database (Sept’18) Time Size (CSV) Time Size (CSV) Input data – 2.1 GB – 2.1 GB Join 217 secs 23 GB – – Export 373 secs 23 GB – – Aggregates – – 18 secs 37 KB GD > 12K secs – 0.5 secs – Total time > 12.5K secs 18.5 secs > 676× faster while 600× more accurate (RMSE on 2% test data) [SOANN19] TensorFlow trains one model. In-Database Learning takes 0.5 sec for any extra model over a subset of the given feature set.

15 / 1

slide-18
SLIDE 18

16 / 1

Outline of Part 3: Optimization

slide-19
SLIDE 19

Learning Regression Models with Least Square Loss

We consider here ridge linear regression fθ(x) = θ, x =

  • f ∈F

θf , xf Training dataset D = Q(I), where

◮ Q(XF ) is a feature extraction query, I is the input database ◮ D consists of tuples (x, y) of feature vector x and response y

Parameters θ obtained by minimizing the objective function: J(θ) = least square loss

  • 1

2|D|

  • (x,y)∈D

(θ, x − y)2 +

ℓ2−regularizer

λ 2 θ2

2

17 / 1

slide-20
SLIDE 20

Side Note: One-hot Encoding of Categorical Variables

Continuous variables are mapped to scalars

◮ xunitsSold, xsales ∈ R.

Categorical variables are mapped to indicator vectors

◮ country has categories vietnam and england ◮ country is then mapped to an indicator vector xcountry = [xvietnam, xengland]⊤ ∈ ({0, 1}2)⊤. ◮ xcountry = [0, 1]⊤ for a tuple with country = ‘‘england’’

This encoding leads to wide training datasets and many 0s

18 / 1

slide-21
SLIDE 21

From Optimization to SumProduct Queries

We can solve θ∗ := arg minθ J(θ) by repeatedly updating θ in the direction of the gradient until convergence (in more detail, Algorithm 1 in [ANNOS18a]): θ := θ − α · ∇J(θ). Model reformulation idea: Decouple data-dependent (x, y) computation from data-independent (θ) computation in the formulations of the objective J(θ) and its gradient ∇J(θ).

19 / 1

slide-22
SLIDE 22

From Optimization to SumProduct FAQs

J(θ) = 1 2|D|

  • (x,y)∈D

(θ, x − y)2 + λ 2 θ2

2

= 1 2θ⊤Σθ − θ, c + sY 2 + λ 2 θ2

2

∇J(θ) = Σθ − c + λθ,

20 / 1

slide-23
SLIDE 23

From Optimization to SumProduct FAQs

J(θ) = 1 2|D|

  • (x,y)∈D

(θ, x − y)2 + λ 2 θ2

2

= 1 2θ⊤Σθ − θ, c + sY 2 + λ 2 θ2

2

∇J(θ) = Σθ − c + λθ, where matrix Σ = (σij)i,j∈[|F|], vector c = (ci)i∈[|F|], and scalar sY are: σij = 1 |D|

  • (x,y)∈D

xix⊤

j

ci = 1 |D|

  • (x,y)∈D

y · xi sY = 1 |D|

  • (x,y)∈D

y 2

20 / 1

slide-24
SLIDE 24

Expressing Σ, c, sY using SumProduct FAQs

FAQ queries for σij =

1 |D|

  • (x,y)∈D xix⊤

j

(w/o factor

1 |D|):

xi, xj continuous ⇒ no free variable ψij =

  • f ∈F:af ∈Dom(Xf )
  • b∈B:ab∈Dom(Xb)

ai · aj ·

  • k∈[m]

1Rk (aS(Rk )) xi categorical, xj continuous ⇒ one free variable ψij[ai] =

  • f ∈F−{i}:af ∈Dom(Xf )
  • b∈B:ab∈Dom(Xb)

aj ·

  • k∈[m]

1Rk (aS(Rk )) xi, xj categorical ⇒ two free variables ψij[ai, aj] =

  • f ∈F−{i,j}:af ∈Dom(Xf )
  • b∈B:ab∈Dom(Xb)
  • k∈[m]

1Rk (aS(Rk )) {Rk}k∈[m] is the set of relations in the query Q; F and B are the sets of the indices of the free and, respectively, bound variables in Q; S(Rk) is the set of variables of Rk; aS(Rk )) is a tuple over S(Rk)); 1E is the Kronecker delta that evaluates to 1 (0) whenever the event E (not) holds.

21 / 1

slide-25
SLIDE 25

Expressing Σ, c, sY using SQL Queries

Queries for σij =

1 |D|

  • (x,y)∈D xix⊤

j

(w/o factor

1 |D|):

22 / 1

slide-26
SLIDE 26

Expressing Σ, c, sY using SQL Queries

Queries for σij =

1 |D|

  • (x,y)∈D xix⊤

j

(w/o factor

1 |D|):

xi, xj continuous ⇒ no group-by variable SELECT SUM (xi * xj) FROM D; where D is the result of the feature extraction query.

22 / 1

slide-27
SLIDE 27

Expressing Σ, c, sY using SQL Queries

Queries for σij =

1 |D|

  • (x,y)∈D xix⊤

j

(w/o factor

1 |D|):

xi, xj continuous ⇒ no group-by variable SELECT SUM (xi * xj) FROM D; xi categorical, xj continuous ⇒ one group-by variable SELECT xi, SUM(xj) FROM D GROUP BY xi; where D is the result of the feature extraction query.

22 / 1

slide-28
SLIDE 28

Expressing Σ, c, sY using SQL Queries

Queries for σij =

1 |D|

  • (x,y)∈D xix⊤

j

(w/o factor

1 |D|):

xi, xj continuous ⇒ no group-by variable SELECT SUM (xi * xj) FROM D; xi categorical, xj continuous ⇒ one group-by variable SELECT xi, SUM(xj) FROM D GROUP BY xi; xi, xj categorical ⇒ two group-by variables SELECT xi, xj, SUM(1) FROM D GROUP BY xi, xj; where D is the result of the feature extraction query.

22 / 1

slide-29
SLIDE 29

Expressing Σ, c, sY using SQL Queries

Queries for σij =

1 |D|

  • (x,y)∈D xix⊤

j

(w/o factor

1 |D|):

xi, xj continuous ⇒ no group-by variable SELECT SUM (xi * xj) FROM D; xi categorical, xj continuous ⇒ one group-by variable SELECT xi, SUM(xj) FROM D GROUP BY xi; xi, xj categorical ⇒ two group-by variables SELECT xi, xj, SUM(1) FROM D GROUP BY xi, xj; where D is the result of the feature extraction query. This query encoding is more compact than one-hot encoding can sometimes be computed with lower complexity than D

22 / 1

slide-30
SLIDE 30

Zoom In: In-database vs. Out-of-database Learning

Feature Extraction Query Q DB ✶ ✶ x y |DB|ρ∗(Q) ML Tool θ∗ MODEL

Model Reformulation

Queries σ11 . . . σij . . . c1 . . . Query Optimizer Factorized Evaluation Cost ≤

i,j∈[|F|] |DB|fhtw(σij ) ≪ |DB|ρ∗(Q)|

Σ, c θ J(θ) ∇J(θ) converged?

Gradient Descent

No Yes

23 / 1

slide-31
SLIDE 31

Complexity Analysis: The General Case

Complexity of learning models falls back to factorized computation of aggregates over joins [BKOZ13,OZ15,SOC16,ANR16] Let (V, E) = hypergraph of the feature extraction query Q fhtwij = fractional hypertree width of the query that expresses σij over Q DB = input database The tensors σij and cj can be computed in time [ANNOS18a] O  |V|2 · |E| ·

  • i,j∈[|F|]

(|DB|fhtwij + |σij|) · log |DB|   .

24 / 1

slide-32
SLIDE 32

Complexity Analysis: Continuous Features Only

Recall the complexity in the general case: O  |V|2 · |E| ·

  • i,j∈[|F|]

(|DB|fhtwij + |σij|) · log |DB|   . Complexity in case all features are continuous: [SOC16] O(|V|2 · |E| · |F|2 · |DB|fhtw(Q) · log |DB|). fhtwij becomes the fractional hypertree width fhtw of Q.

25 / 1

slide-33
SLIDE 33

26 / 1

Outline of Part 3: Optimization

slide-34
SLIDE 34

Indicator Vectors under Functional Dependencies

Consider the functional dependency city → country and country categories: vietnam, england city categories: saigon, hanoi, oxford, leeds, bristol The one-hot encoding enforces the following identities: xvietnam = xsaigon + xhanoi country is vietnam ≡ city is either saigon or hanoi xvietnam = 1 ≡ either xsaigon = 1 or xhanoi = 1 xengland = xoxford + xleeds + xbristol country is england ≡ city is either oxford, leeds, or bristol xengland = 1 ≡ either xoxford = 1 or xleeds = 1 or xbristol = 1

27 / 1

slide-35
SLIDE 35

Indicator Vector Mappings

Identities due to one-hot encoding xvietnam = xsaigon + xhanoi xengland = xoxford + xleeds + xbristol Encode xcountry as xcountry = Rxcity, where R = saigon hanoi

  • xford

leeds bristol 1 1 vietnam 1 1 1 england For instance, if city is saigon, i.e., xcity = [1, 0, 0, 0, 0]⊤, then country is vietnam, i.e., xcountry = Rxcity = [1, 0]⊤.

  • 1

1 1 1 1

      1        =

  • 1
  • 28 / 1
slide-36
SLIDE 36

Rewriting the Loss Function

Functional dependency: city → country xcountry = Rxcity Replace all occurrences of xcountry by Rxcity:

  • f ∈F−{city,country}

θf , xf + θcountry, xcountry + θcity, xcity =

  • f ∈F−{city,country}

θf , xf + θcountry, Rxcity + θcity, xcity =

  • f ∈F−{city,country}

θf , xf +

  • R⊤θcountry + θcity
  • γcity

, xcity

  • 29 / 1
slide-37
SLIDE 37

Rewriting the Loss Function

Functional dependency: city → country xcountry = Rxcity Replace all occurrences of xcountry by Rxcity:

  • f ∈F−{city,country}

θf , xf + θcountry, xcountry + θcity, xcity =

  • f ∈F−{city,country}

θf , xf + θcountry, Rxcity + θcity, xcity =

  • f ∈F−{city,country}

θf , xf +

  • R⊤θcountry + θcity
  • γcity

, xcity

  • We avoid the computation of the aggregates over xcountry.

We reparameterize and ignore parameters θcountry. What about the penalty term in the objective function?

29 / 1

slide-38
SLIDE 38

Rewriting the Regularizer (1/2)

Functional dependency: city → country xcountry = Rxcity γcity = R⊤θcountry + θcity The penalty term is: λ 2 θ2

2 = λ

2

j=city

θj2

2 +

  • γcity − R⊤θcountry
  • 2

2 + θcountry2 2

  • We can optimize out θcountry by expressing it in terms of γcity:

1 λ ∂ λ

2 θ2 2

  • ∂θcountry

= R(R⊤θcountry − γcity) + θcountry By setting this to 0 we obtain θcountry in terms of γcity (Iv is the order-Nv identity matrix): θcountry = (Icountry + RR⊤)−1Rγcity = R(Icity + R⊤R)−1γcity

30 / 1

slide-39
SLIDE 39

Rewriting the Regularizer (2/2)

We obtained (Iv is the order-Nv identity matrix): θcountry = (Icountry + RR⊤)−1Rγcity = R(Icity + R⊤R)−1γcity The penalty term becomes (after several derivation steps) λ 2 θ2

2 = λ

2

j=city

θj2

2 +

  • (Icity + R⊤R)−1γcity, γcity

31 / 1

slide-40
SLIDE 40

32 / 1

Outline of Part 3: Optimization

slide-41
SLIDE 41

Linear Algebra is a Key Building Block for ML

Setting: Input matrices defined by queries over relational databases Matrix A = Q(D) Q is a feature extraction query and D a database A has m = |Q(D)| rows = number of tuples in Q(D) A has n columns (= variables in Q) that define features and label In our setting: m ≫ n, i.e., we train in the column space We should avoid materializing A whenever possible.

33 / 1

slide-42
SLIDE 42

Why?

Examples of linear algebra computation needed for ML

DB (assuming A ∈ Rm×n):

Matrix multiplication for learning linear regression models: Σ = ATA ∈ Rn×n Matrix inversion for learning under functional dependencies: (Icity + RTR)−1 Matrix factorization

◮ QR decomposition A = Q R, where Q ∈ Rm×n is orthogonal and R ∈ Rn×n is upper triangular ◮ Rank-k approximation of A A ≈ X Y, where X ∈ Rm×k and Y ∈ Rk×n

34 / 1

slide-43
SLIDE 43

From A to Σ = ATA

The matrix Σ = ATA pops up in several ML-relevant computations, eg: Least squares problem Given A ∈ Rm×n, b ∈ Rm×1, find x ∈ Rn×1 that minimizes Ax − b2. If A has linearly independent columns, then the unique solution of the least square problem is x = (ATA)−1ATb A† = (ATA)−1AT is called the Moore-Penrose pseudoinverse. In-DB setting: The query defines the extended input matrix [A b]. Gram-Schmidt process for QR decomposition

35 / 1

slide-44
SLIDE 44

Classical QR Factorization

  • a1

. . . an

  • =
  • e1

. . . en

     e1, a1 e1, a2 . . . e1, an e2, a2 . . . e2, an . . . ... . . . . . . . . . en, an       A ∈ Rm×n. We do not discuss the categorical case here. Q = [e1, . . . , en] ∈ Rm×n is orthogonal: ∀i, j ∈ [n], i = j : ei, ej = 0 R ∈ Rn×n is upper triangular: ∀i, j ∈ [n], i > j : Ri,j = 0 This is the thin QR decomposition.

36 / 1

slide-45
SLIDE 45

Applications of QR Factorization

Solve linear equations Ax = b for nonsingular A ∈ Rn×n

  • 1. Decompose A as A = QR.

Then, QRx = b ⇒ QTQRx = QTb ⇒ Rx = QTb

  • 2. Compute y = QTb
  • 3. Solve Rx = y by back substitution

Variant: Solve k sets of linear equations with the same A Use QR decomposition of A only once for all k sets!

37 / 1

slide-46
SLIDE 46

Applications of QR Factorization

Pseudo-inverse of a matrix with linearly independent columns A† = (ATA)−1AT = ((QR)T(QR))−1(QR)T = (RTQTQR)−1RTQT = (RTR)−1RTQT (since QTQ = I) = R−1R−TRTQT (since R is nonsingular) = R−1QT Inverse of a nonsingular square matrix A−1 = (QR)−1 = R−1QT Singular Value Decomposition (SVD) of A via Golub-Kahan bidiagonalization of R

38 / 1

slide-47
SLIDE 47

Applications of QR Factorization

Least square problem Given A ∈ Rm×n, b ∈ Rm×1, find x ∈ Rn×1 that minimizes Ax − b2. If A has linearly independent columns, then the unique solution of the least square problem is ˆ x = (ATA)−1ATb = A†b = R−1QTb For m > n this is an overdetermined system of linear equations. In-DB setting: The query defines the extended input matrix [A b].

39 / 1

slide-48
SLIDE 48

QR Factorization using the Gram-Schmidt Process

Project the vector ak orthogonally onto the line spanned by vector uj: projuj ak = uj, ak uj, uj uj. Gram-Schmidt orthogonalization: ∀k ∈ [n] : uk = ak −

  • j∈[k−1]

projuj ak = ak −

  • j∈[k−1]

uj, ak uj, uj uj. The vectors in the orthogonal matrix Q are normalized: Q =

  • e1 =

u1 u1,

. . . , en =

un un

  • 40 / 1
slide-49
SLIDE 49

Example: QR Factorization using Gram-Schmidt

Given A = [v1, v2, v3]. Task: Compute Q = [e1 =

u1 u1, e2 = u2 u2, e3 = u3 u3].

Source: Wikipedia

41 / 1

slide-50
SLIDE 50

Example: QR Factorization using Gram-Schmidt

Given A = [v1, v2, v3]. Task: Compute Q = [e1 =

u1 u1, e2 = u2 u2, e3 = u3 u3].

41 / 1

slide-51
SLIDE 51

Example: QR Factorization using Gram-Schmidt

Given A = [v1, v2, v3]. Task: Compute Q = [e1 =

u1 u1, e2 = u2 u2, e3 = u3 u3].

41 / 1

slide-52
SLIDE 52

Example: QR Factorization using Gram-Schmidt

Given A = [v1, v2, v3]. Task: Compute Q = [e1 =

u1 u1, e2 = u2 u2, e3 = u3 u3].

41 / 1

slide-53
SLIDE 53

Example: QR Factorization using Gram-Schmidt

Given A = [v1, v2, v3]. Task: Compute Q = [e1 =

u1 u1, e2 = u2 u2, e3 = u3 u3].

41 / 1

slide-54
SLIDE 54

Example: QR Factorization using Gram-Schmidt

Given A = [v1, v2, v3]. Task: Compute Q = [e1 =

u1 u1, e2 = u2 u2, e3 = u3 u3].

41 / 1

slide-55
SLIDE 55

Example: QR Factorization using Gram-Schmidt

Given A = [v1, v2, v3]. Task: Compute Q = [e1 =

u1 u1, e2 = u2 u2, e3 = u3 u3].

41 / 1

slide-56
SLIDE 56

Example: QR Factorization using Gram-Schmidt

Given A = [v1, v2, v3]. Task: Compute Q = [e1 =

u1 u1, e2 = u2 u2, e3 = u3 u3].

41 / 1

slide-57
SLIDE 57

Example: QR Factorization using Gram-Schmidt

Given A = [v1, v2, v3]. Task: Compute Q = [e1 =

u1 u1, e2 = u2 u2, e3 = u3 u3].

41 / 1

slide-58
SLIDE 58

Example: QR Factorization using Gram-Schmidt

Given A = [v1, v2, v3]. Task: Compute Q = [e1 =

u1 u1, e2 = u2 u2, e3 = u3 u3].

41 / 1

slide-59
SLIDE 59

Example: QR Factorization using Gram-Schmidt

Given A = [v1, v2, v3]. Task: Compute Q = [e1 =

u1 u1, e2 = u2 u2, e3 = u3 u3].

41 / 1

slide-60
SLIDE 60

Example: QR Factorization using Gram-Schmidt

Given A = [v1, v2, v3]. Task: Compute Q = [e1 =

u1 u1, e2 = u2 u2, e3 = u3 u3].

41 / 1

slide-61
SLIDE 61

Example: QR Factorization using Gram-Schmidt

Given A = [v1, v2, v3]. Task: Compute Q = [e1 =

u1 u1, e2 = u2 u2, e3 = u3 u3].

41 / 1

slide-62
SLIDE 62

How to Lower the Complexity of Gram-Schmidt?

Challenges: How does not materializing A help? Q has the same dimension as A! The Gram-Schmidt process is inherently sequential and not parallelizable Computing uk requires the computation of u1, . . . , uk−1 in Q.

42 / 1

slide-63
SLIDE 63

How to Lower the Complexity of Gram-Schmidt?

Challenges: How does not materializing A help? Q has the same dimension as A! Trick 1: Only use R and do not require full Q in subsequent computation The Gram-Schmidt process is inherently sequential and not parallelizable Computing uk requires the computation of u1, . . . , uk−1 in Q. Trick 2: Rewrite uk to refer to columns in A instead of Q

42 / 1

slide-64
SLIDE 64

Factorizing the QR Factorization

Express each vector uj as a linear combination of vectors a1, . . . , aj in A:

  • u1

. . . un

  • = −
  • a1

. . . an

     c1,1 c1,2 . . . c1,n c2,2 . . . c2,n . . . ... ... . . . cn,n       That is, uk = −

j∈[k] cj,kaj. The coefficients cj,k are:

∀j ∈ [k − 1] : cj,k =

  • i∈[j,k−1]

ui,k di · cj,i ck,k = −1 ∀j ∈ [k − 1] : uj,k =

  • l∈[j]

cl,j · al, ak ∀i ∈ [n] : di =

  • l∈[i]
  • p∈[i]

cl,i · cp,i · al, ap

43 / 1

slide-65
SLIDE 65

Factorizing the QR Factorization

Express each vector uj as a linear combination of vectors a1, . . . , aj in A:

  • u1

. . . un

  • = −
  • a1

. . . an

     c1,1 c1,2 . . . c1,n c2,2 . . . c2,n . . . ... ... . . . cn,n       That is, uk = −

j∈[k] cj,kaj. The coefficients cj,k are:

∀j ∈ [k − 1] : cj,k =

  • i∈[j,k−1]

ui,k di · cj,i ck,k = −1 ∀j ∈ [k − 1] : uj,k =

  • l∈[j]

cl,j · al, ak ∀i ∈ [n] : di =

  • l∈[i]
  • p∈[i]

cl,i · cp,i · al, ap The coefficients are defined by FAQs over the entries in Σ = ATA

43 / 1

slide-66
SLIDE 66

Expressing Q

Q = AC, where uk =

  • uk, uk =
  • l∈[k]
  • p∈[k]

cl,k · cp,k · al, ap =

  • dk

C =

  • c1

. . . cn

  • =

       

c1,1

d1 c1,2

d1

. . .

c1,n

d1 c2,2

d2

. . .

c2,n

d2

. . . ... ... . . .

cn,n √dn

       

44 / 1

slide-67
SLIDE 67

Expressing R

Entries in the upper triangular R are ei, aj = ui ,aj √

di

= Aci, aj , ∀i ≤ j. Then, R =      

  • c1, ATa1
  • c1, ATa2
  • . . .
  • c1, ATan
  • c2, ATa2
  • . . .
  • c2, ATan
  • .

. . ... . . . . . . . . .

  • cn, ATan

     The entries in R are defined by FAQs over Σ = ATA = [ATa1, . . . , ATan]

45 / 1

slide-68
SLIDE 68

Revisiting The Least Squares Problem

Given A ∈ Rm×n, b ∈ Rm×1, find x ∈ Rn×1 that minimizes Ax − b2. In-DB setting: The query defines the extended input matrix [A b]. Solution ˆ x = R−1QTb requires: The inverse R−1 of the upper triangular matrix R; or back substitution The vector QTb computable directly over the input data QTb = (AC)Tb = CTATb = CT     a1, b . . . an, b     The dot products aj, b are FAQs computable without A!

46 / 1

slide-69
SLIDE 69

Computing Coefficient Matrix C without A

Data complexity of C is the same as of Σ Given Σ, O(n3) time to compute matrix C and vector d There are n(n − 2)/2 entries in coefficient matrix C that are not 0 and -1

◮ Each of them takes 3n arithmetic operations

There are n entries in the vector d

◮ Entry di takes i2 arithmetic operations

47 / 1

slide-70
SLIDE 70

Computing Coefficient Matrix C without A

Data complexity of C is the same as of Σ Given Σ, O(n3) time to compute matrix C and vector d There are n(n − 2)/2 entries in coefficient matrix C that are not 0 and -1

◮ Each of them takes 3n arithmetic operations

There are n entries in the vector d

◮ Entry di takes i2 arithmetic operations

Computing sparse-encoded C from sparse-encoded Σ a bit tricky Same complexity overhead as for Σ Nicely parallelizable, accounting for the dependencies between entries in C

47 / 1

slide-71
SLIDE 71

Our Journey So Far with QR Factorization

F-GS system on top of LMFAO for QR factorization of matrices defined over database joins 33 numerical + 3,702 categorical features

◮ Σ computed on one core by LMFAO in 18 sec ◮ C, d, and R (and Linear Regression on top) computed on one core by F-GS in 18 sec ◮ F-GS on 8 cores is 3× faster than on one core ◮ Any of C, d, and R cannot be computed by LAPACK

33 numerical + 55 categorical features

◮ Σ computed on one core by LMFAO in 6.4 sec ◮ C, d, and R (and Linear Regression on top) computed one one core by F-GS in 1 sec ◮ R can be computed by LAPACK on one core in 313 sec It also needs to read in the data: + ≈ 70 sec ◮ LAPACK on 8 cores is 7× faster than on one core

Retailer dataset (86M), acyclic natural join of 5 relations, 26x compression by factorization; Intel i7-4770 3.40GHz/64bit/32GB, Linux 3.13.0, g++4.8.4, libblas3 1.2 (one core), OpenBLAS

48 / 1

slide-72
SLIDE 72

49 / 1

Outline of Part 3: Optimization

slide-73
SLIDE 73

Beyond Linear Regression

This approach has been or is applied to a host of ML models: Polynomial regression (done) Factorization machines (done) Decision trees (done) Principal component analysis (done) Generalised low-rank models (on-going) Sum-product networks (on-going) K-means & k-median clustering (on-going) Gradient boosting decision trees (on-going) Random forests (on-going) Some models seem inherently hard for in-db learning Logistic regression (unclear)

50 / 1

slide-74
SLIDE 74

Beyond Polynomial Loss

There are common loss functions that are: Convex, Non-differentiable, but Admit subgradients with respect to model parameters. Examples: Hinge (used for linear SVM, ReLU) J(θ) = max(0, 1 − y · θ, x) Huber, ℓ1, scalene, fractional, ordinal, interval Their subgradients may not be as factorisable as the gradient of the square loss.

51 / 1

slide-75
SLIDE 75

References

SOC16 Learning Linear Regression Models over Factorized Joins. Schleich, Olteanu, Ciucanu. In SIGMOD 2016. http://dl.acm.org/citation.cfm?doid=2882903.2882939 A17 Research Directions for Principles of Data Management (Dagstuhl Perspectives Workshop 16151). Abiteboul et al. In SIGMOD Rec. 2017. https://arxiv.org/pdf/1701.09007.pdf KBY17 Data Management in Machine Learning: Challenges, Techniques, and Systems. Kumar, Boehm, Yang. In SIGMOD 2017, Tutorial. https://www.youtube.com/watch?v=U8J0Dd_Z5wo PRWZ17 Data Management Challenges in Production Machine Learning. Polyzotis, Roy, Whang, Zinkevich. In SIGMOD 2017, Tutorial. http://dl.acm.org/citation.cfm?doid=3035918.3054782 ANNOS18a In-Database Learning with Sparse Tensors. Abo Khamis, Ngo, Nguyen, Olteanu, Schleich. In PODS 2018. https://arxiv.org/abs/1703.04780 (extended version) ANNOS18b AC/DC: In-Database Learning Thunderstruck. Abo Khamis, Ngo, Nguyen, Olteanu, Schleich. In DEEM@SIGMOD 2018. https://arxiv.org/abs/1803.07480

52 / 1

slide-76
SLIDE 76

References

NO18 Incremental View Maintenance with Triple Lock Factorisation Benefits. Nikolic, Olteanu. In SIGMOD 2018. https://arxiv.org/abs/1703.07484 SOANN19 Under submission.

53 / 1

slide-77
SLIDE 77

54 / 1

Outline of Part 3: Optimization

slide-78
SLIDE 78

QUIZ on Optimization

Assume that the natural join of the following relations provides the features we use to predict revenue: Sales(store id, product id, quantity, revenue), Product(product id, color), Store(store id, distance city center). Variables revenue, quantity, and distance city center stand for continuous features, while product id and color for categorical features.

  • 1. Give the FAQs required to compute the gradient of the squares loss

function for learning a ridge linear regression models with the above

  • features. Give the same for a polynomial regression model of degree two.
  • 2. We know that product id functionally determines color. Give a rewriting
  • f the objective function that exploits the functional dependency.
  • 3. The FAQs require the computation of a lot of common sub-problems. Can

you think of ways to share as much computation as possible?

55 / 1