Naïve Bayes with huge feature sets
-i.e. ones that don’t fit in memory
Pros and cons of possible approaches
-Traditional “DB” (actually, key-value store)
-Memory-based distributed DB
-Stream-and-sort counting
Other tasks for stream-and-sort
…MapReduce?

注脚

展开查看详情

1.Basics of MapReduce Shannon Quinn

2.Today Naïve Bayes with huge feature sets i.e. ones that don’t fit in memory Pros and cons of possible approaches Traditional “DB” (actually, key-value store) Memory-based distributed DB Stream-and-sort counting Other tasks for stream-and-sort … MapReduce ?

3.Complexity of Naïve Bayes You have a train dataset and a test dataset Initialize an “event counter” ( hashtable ) C For each example id, y, x 1 ,…., x d in train: C(“ Y =ANY”) ++; C(“ Y=y”) ++ For j in 1..d : C(“ Y=y ^ X= x j ”) ++ For each example id, y, x 1 ,…., x d in test: For each y’ in dom (Y): Compute log Pr (y’,x 1 ,…., x d ) = Return the best y’ where: q j = 1/|V| q y = 1/| dom (Y)| m=1 Complexity: O( n), n= size of train Complexity: O(| dom (Y)|*n’), n’= size of test Assume hashtable holding all counts fits in memory Sequential reads Sequential reads

4.What’s next How to implement Naïve Bayes Assuming the event counters do not fit in memory Why? Micro: 0.6G memory Standard : S: 1.7Gb L: 7.5Gb XL: 15Mb Hi Memory : XXL: 34.2 XXXXL: 68.4

5.What’s next How to implement Naïve Bayes Assuming the event counters do not fit in memory Why? Zipf’s law: many words that you see, you don’t see often.

6.[Via Bruce Croft]

7.What’s next How to implement Naïve Bayes Assuming the event counters do not fit in memory Why? Heaps’ Law: If V is the size of the vocabulary and the n is the length of the corpus in words: Typical constants: K  1/10 1/ 100   0.4  0.6 (approx. square-root) Why? Proper names, missspellings , neologisms, … Summary: For text classification for a corpus with O(n) words, expect to use O( sqrt (n)) storage for vocabulary. Scaling might be worse for other cases (e.g., hypertext, phrases, …)

8.What’s next How to implement Naïve Bayes Assuming the event counters do not fit in memory Possible approaches: Use a database? (or at least a key-value store)

9.Numbers (Jeff Dean says) Everyone Should Know ~= 10x ~= 15x ~= 100,000x 40x

10.Using a database for Big ML We often want to do random access on big data E.g., different versions of examples for q/a E.g., spot-checking parameter weights to see if they are sensible Simplest approach: Sort the data and use binary search  O(log 2 n) seeks to find query row

11.Using a database for Big ML We often want to do random access on big data E.g., different versions of examples for q/a E.g., spot-checking parameter weights to see if they are sensible Almost-as-simple idea based on fact that disk seek time ~= reading 1Mb Let K=rows/Mb (e.g., K=1000) Scan through data once and record the seek position of every K- th row in an index file (or memory) To find row r: Find the r’, last item in the index smaller than r Seek to r’ and read the next megabyte Cheap since index is size n/1000 Cost is ~= 2 seeks

12.Using a database for Big ML Summary: we’ve gone from ~= 1 seek (best possible) to ~= 2 seeks---plus finding r’ in the index. If index is O(1Mb) then finding r’ is also like 1 seek So we’re paying about 3 seeks per random access in a Gb What if the index is still large? Build (the same sort of index) for the index! Now we’re paying 4 seeks for each random access into a Tb ….and repeat recursively if you need This is called a B-tree It only gets complicated when we want to delete and insert.

13.

14.

15.Numbers (Jeff Dean says) Everyone Should Know ~= 10x ~= 15x ~= 100,000x 40x Best case (data is in same sector/block)

16.A single large file can be spread out among many non-adjacent blocks/sectors… and then you need to seek around to scan the contents of the file… Question: What could you do to reduce this cost?

17.What’s next How to implement Naïve Bayes Assuming the event counters do not fit in memory Possible approaches: Use a database? Counts are stored on disk, not in memory …So, accessing a count might involve some seeks Caveat: many DBs are good at caching frequently-used values, so seeks might be infrequent ….. O(n*scan)  O(n*scan*4*seek)

18.What’s next How to implement Naïve Bayes Assuming the event counters do not fit in memory Possible approaches: Use a memory-based distributed database? Counts are stored on disk, not in memory …So, accessing a count might involve some seeks Caveat: many DBs are good at caching frequently-used values, so seeks might be infrequent ….. O(n*scan)  O(n*scan*???)

19.Counting example 1 example 2 example 3 …. Counting logic Hash table, database, etc “increment C[ x] by D”

20.Counting example 1 example 2 example 3 …. Counting logic Hash table, database, etc “increment C[ x] by D” Hashtable issue: memory is too small Database issue: seeks are slow

21.Distributed Counting example 1 example 2 example 3 …. Counting logic Hash table1 “increment C[ x] by D” Hash table2 Hash table2 Machine 1 Machine 2 Machine K . . . Machine 0 Now we have enough memory….

22.Distributed Counting example 1 example 2 example 3 …. Counting logic Hash table1 “increment C[ x] by D” Hash table2 Hash table2 Machine 1 Machine 2 Machine K . . . Machine 0 New issues: Machines and memory cost $$! Routing increment requests to right machine Sending increment requests across the network Communication complexity

23.Distributed Counting example 1 example 2 example 3 …. Counting logic Hash table1 “increment C[ x] by D” Hash table2 Hash table2 Machine 1 Machine 2 Machine K . . . Machine 0 New issues: Machines and memory cost $$! Routing increment requests to right machine Sending increment requests across the network Communication complexity

24.What’s next How to implement Naïve Bayes Assuming the event counters do not fit in memory Possible approaches: Use a memory-based distributed database? Extra cost: Communication costs: O(n) … but that’s “ok” Extra complexity: routing requests correctly Note: If the increment requests were ordered seeks would not be needed! O(n*scan)  O(n* scan+n *send) 1) Distributing data in memory across machines is not as cheap as accessing memory locally because of communication costs. 2) The problem we’re dealing with is not size. It’s the interaction between size and locality: we have a large structure that’s being accessed in a non-local way.

25.What’s next How to implement Naïve Bayes Assuming the event counters do not fit in memory Possible approaches: Use a memory-based distributed database? Extra cost: Communication costs: O(n) … but that’s “ok” Extra complexity: routing requests correctly Compress the counter hash table? Use integers as keys instead of strings? Use approximate counts? Discard infrequent/unhelpful words? Trade off time for space somehow? Observation: if the counter updates were better-ordered we could avoid using disk Great ideas which we’ll discuss more later O(n*scan)  O(n* scan+n *send)

26.Large-vocabulary Naïve Bayes One way trade off time for space: Assume you need K times as much memory as you actually have Method: Construct a hash function h(event) For i =0,…,K-1 : Scan thru the train dataset Increment counters for event only if h(event) mod K == i Save this counter set to disk at the end of the scan After K scans you have a complete counter set Comment: this works for any counting task, not just naïve Bayes What we’re really doing here is organizing our “messages” to get more locality…. Counting

27.Large-vocabulary Naïve Bayes One way trade off time for space: Assume you need K times as much memory as you actually have Method: Construct a hash function h(event) For i =0,…,K-1 : Scan thru the train dataset Increment counters for event only if h(event) mod K == i Save this counter set to disk at the end of the scan After K scans you have a complete counter set Comment: this works for any counting task, not just naïve Bayes What we’re really doing here is organizing our “messages” to get more locality…. Counting

28.Large vocabulary counting Another approach: Start with Q: “what can we do for large sets quickly”? A: sorting It’s O(n log n), not much worse than linear You can do it for very large datasets using a merge sort sort k subsets that fit in memory, merge results, which can be done in linear time

29.Large-vocabulary Naïve Bayes Create a hashtable C For each example id, y, x 1 ,…., x d in train: C(“ Y =ANY”) ++; C(“ Y=y”) ++ For j in 1..d : C(“ Y=y ^ X= x j ”) ++

30.Large-vocabulary Naïve Bayes Create a hashtable C For each example id, y, x 1 ,…., x d in train: C(“ Y =ANY”) ++; C(“ Y=y”) ++ Print “Y=ANY += 1” Print “Y= y += 1” For j in 1..d : C(“ Y=y ^ X= x j ”) ++ Print “ Y=y ^ X= x j += 1” Sort the event-counter update “messages” Scan the sorted messages and compute and output the final counter values Think of these as “messages” to another component to increment the counters java MyTrainer train | sort | java MyCountAdder > model

31.Large-vocabulary Naïve Bayes Create a hashtable C For each example id, y, x 1 ,…., x d in train: C(“ Y =ANY”) ++; C(“ Y=y”) ++ Print “Y=ANY += 1” Print “Y= y += 1” For j in 1..d : C(“ Y=y ^ X= x j ”) ++ Print “ Y=y ^ X= x j += 1” Sort the event-counter update “messages” We’re collecting together messages about the same counter Scan and add the sorted messages and output the final counter values Y=business += 1 Y=business += 1 … Y=business ^ X = aaa += 1 … Y=business ^ X= zynga += 1 Y=sports ^ X=hat += 1 Y=sports ^ X=hockey += 1 Y=sports ^ X=hockey += 1 Y=sports ^ X=hockey += 1 … Y=sports ^ X=hoe += 1 … Y=sports += 1 …

32.Large-vocabulary Naïve Bayes Y=business += 1 Y=business += 1 … Y=business ^ X = aaa += 1 … Y=business ^ X= zynga += 1 Y=sports ^ X=hat += 1 Y=sports ^ X=hockey += 1 Y=sports ^ X=hockey += 1 Y=sports ^ X=hockey += 1 … Y=sports ^ X=hoe += 1 … Y=sports += 1 … previousKey = Null sumForPreviousKey = 0 For each ( event,delta ) in input: If event == previousKey sumForPreviousKey += delta Else OutputPreviousKey () previousKey = event sumForPreviousKey = delta OutputPreviousKey () define OutputPreviousKey (): If PreviousKey !=Null print PreviousKey,sumForPreviousKey Accumulating the event counts requires constant storage … as long as the input is sorted. streaming Scan-and-add:

33.Distributed Counting  Stream and Sort Counting example 1 example 2 example 3 …. Counting logic Hash table1 “C[ x] +=D” Hash table2 Hash table2 Machine 1 Machine 2 Machine K . . . Machine 0 Message-routing logic

34.Distributed Counting  Stream and Sort Counting example 1 example 2 example 3 …. Counting logic “C[ x] +=D” Machine A Sort C[x1] += D1 C[x1] += D2 …. Logic to combine counter updates Machine C Machine B

35.Stream and Sort Counting  Distributed Counting example 1 example 2 example 3 …. Counting logic “C[ x] +=D” Machines A1,… Sort C[x1] += D1 C[x1] += D2 …. Logic to combine counter updates Machines C1,.., Machines B1,…, Trivial to parallelize! Easy to parallelize! Standardized message routing logic

36.Large-vocabulary Naïve Bayes For each example id, y, x 1 ,…., x d in train: Print Y=ANY += 1 Print Y= y += 1 For j in 1..d : Print Y=y ^ X= x j += 1 Sort the event-counter update “messages” Scan and add the sorted messages and output the final counter values Complexity: O( n), n= size of train Complexity: O( nlogn ) Complexity: O( n) O(|V|| dom (Y)|) (Assuming a constant number of labels apply to each document) java MyTrainer train | sort | java MyCountAdder > model Model size: max O(n), O(|V|| dom (Y)|)

37.Other stream-and-sort tasks “Meaningful” phrase-finding

38.ACL Workshop 2003

39.ACL Workshop 2003

40.Why phrase-finding? There are lots of phrases There’s not supervised data It’s hard to articulate What makes a phrase a phrase, vs just an n-gram? a phrase is independently meaningful (“test drive”, “red meat”) or not (“are interesting”, “are lots”) What makes a phrase interesting?

41.The breakdown: what makes a good phrase Two properties: Phraseness : “the degree to which a given word sequence is considered to be a phrase” Statistics: how often words co-occur together vs separately Informativeness : “how well a phrase captures or illustrates the key ideas in a set of documents” – something novel and important relative to a domain Background corpus and foreground corpus; how often phrases occur in each

42.“Phraseness” 1 – based on BLRT Binomial Ratio Likelihood Test (BLRT): Draw samples: n 1 draws, k 1 successes n 2 draws, k 2 successes Are they from one binominal (i.e., k 1 /n 1 and k 2 /n 2 were different due to chance) or from two distinct binomials? Define p 1 =k 1 / n 1 , p 2 =k 2 / n 2, p=(k 1 +k 2 )/(n 1 +n 2 ), L( p,k,n ) = p k (1-p) n-k

43.“Phraseness” 1 – based on BLRT Binomial Ratio Likelihood Test (BLRT): Draw samples: n 1 draws, k 1 successes n 2 draws, k 2 successes Are they from one binominal (i.e., k 1 /n 1 and k 2 /n 2 were different due to chance) or from two distinct binomials? Define p i = k i / n i , p=(k 1 +k 2 )/(n 1 +n 2 ), L( p,k,n ) = p k (1-p) n-k

44.“Phraseness” 1 – based on BLRT Define p i = k i / n i , p=(k 1 +k 2 )/(n 1 +n 2 ), L( p,k,n ) = p k (1-p) n-k comment k 1 C(W 1 =x ^ W 2 =y ) how often bigram x y occurs in corpus C n 1 C(W 1 =x) how often word x occurs in corpus C k 2 C(W 1 ≠x^W 2 =y) how often y occurs in C after a non -x n 2 C(W 1 ≠x) how often a non- x occurs in C Phrase x y : W 1 = x ^ W 2 = y Does y occur at the same frequency after x as in other positions?

45.“Informativeness” 1 – based on BLRT Define p i = k i / n i , p=(k 1 +k 2 )/(n 1 +n 2 ), L( p,k,n ) = p k (1-p) n-k Phrase x y : W 1 = x ^ W 2 = y and two corpora, C and B comment k 1 C(W 1 =x ^ W 2 =y ) how often bigram x y occurs in corpus C n 1 C(W 1 =* ^ W 2 =* ) how many bigrams in corpus C k 2 B(W 1 =x ^W 2 =y) how often x y occurs in background corpus n 2 B(W 1 =* ^ W 2 =*) how many bigrams in background corpus Does x y occur at the same frequency in both corpora?

46.“Informativeness” 1 – based on BLRT Define p i = k i / n i , p=(k 1 +k 2 )/(n 1 +n 2 ), L( p,k,n ) = p k (1-p) n-k Phrase x y : W 1 = x ^ W 2 = y and two corpora, C and B comment k 1 C(W 1 =x ^ W 2 =y ) how often bigram x y occurs in corpus C n 1 C(W 1 =* ^ W 2 =* ) how many bigrams in corpus C k 2 B(W 1 =x ^W 2 =y) how often x y occurs in background corpus n 2 B(W 1 =* ^ W 2 =*) how many bigrams in background corpus Does x y occur at the same frequency in both corpora?

47.The breakdown: what makes a good phrase Two properties: Phraseness : “the degree to which a given word sequence is considered to be a phrase” Statistics: how often words co-occur together vs separately Informativeness : “how well a phrase captures or illustrates the key ideas in a set of documents” – something novel and important relative to a domain Background corpus and foreground corpus; how often phrases occur in each Another intuition: our goal is to compare distributions and see how different they are: Phraseness : estimate x y with bigram model or unigram model Informativeness : estimate with foreground vs background corpus

48.The breakdown: what makes a good phrase Another intuition: our goal is to compare distributions and see how different they are: Phraseness : estimate x y with bigram model or unigram model Informativeness : estimate with foreground vs background corpus To compare distributions, use KL-divergence “ Pointwise KL divergence”

49.The breakdown: what makes a good phrase To compare distributions, use KL-divergence “ Pointwise KL divergence” Phraseness : difference between bigram and unigram language model in foreground Bigram model: P(x y)=P(x)P( y|x ) Unigram model: P(x y)=P(x)P(y)

50.The breakdown: what makes a good phrase To compare distributions, use KL-divergence “ Pointwise KL divergence” Informativeness : difference between foreground and background models Bigram model: P(x y)=P(x)P( y|x ) Unigram model: P(x y)=P(x)P(y)

51.The breakdown: what makes a good phrase To compare distributions, use KL-divergence “ Pointwise KL divergence” Combined: difference between foreground bigram model and background unigram model Bigram model: P(x y)=P(x)P( y|x ) Unigram model: P(x y)=P(x)P(y)

52.The breakdown: what makes a good phrase To compare distributions, use KL-divergence Combined: difference between foreground bigram model and background unigram model Subtle advantages: BLRT scores “more frequent in foreground” and “more frequent in background” symmetrically, pointwise KL does not. Phrasiness and informativeness scores are more comparable – straightforward combination w/o a classifier is reasonable. Language modeling is well-studied: extensions to n-grams, smoothing methods, … we can build on this work in a modular way

53.Pointwise KL, combined

54.Why phrase-finding? Phrases are where the standard supervised “bag of words” representation starts to break. There’s not supervised data, so it’s hard to see what’s “right” and why It’s a nice example of using unsupervised signals to solve a task that could be formulated as supervised learning It’s a nice level of complexity, if you want to do it in a scalable way.

55.Implementation Request-and-answer pattern Main data structure: tables of key-value pairs key is a phrase x y value is a mapping from a attribute names (like phraseness , freq -in-B, … ) to numeric values. Keys and values are just strings We’ll operate mostly by sending messages to this data structure and getting results back, or else streaming thru the whole table For really big data: we’d also need tables where key is a word and val is set of attributes of the word ( freq -in-B, freq -in-C, …)

56.Generating and scoring phrases: 1 Stream through foreground corpus and count events “W 1 = x ^ W 2 = y ” the same way we do in training naive Bayes: stream-and sort and accumulate deltas (a “sum-reduce”) Don’t bother generating boring phrases (e.g., crossing a sentence, contain a stopword , …) T hen stream through the output and convert to phrase, attributes-of-phrase records with one attribute: freq -in-C=n Stream through foreground corpus and count events “W 1 = x ” in a (memory-based) hashtable …. This is enough* to compute phrasiness : ψ p ( x y ) = f ( freq -in-C(x), freq -in-C(y), freq -in-C(x y)) … so you can do that with a scan through the phrase table that adds an extra attribute (holding word frequencies in memory). * actually you also need total # words and total #phrases….

57.Generating and scoring phrases: 2 Stream through background corpus and count events “W 1 = x ^ W 2 = y ” and convert to phrase, attributes-of-phrase records with one attribute: freq -in- B =n Sort the two phrase-tables: freq -in-B and freq -in-C and run the output through another “reducer” that appends together all the attributes associated with the same key, so we now have elements like

58.Generating and scoring phrases: 3 Scan the through the phrase table one more time and add the informativeness attribute and the overall quality attribute Summary , assuming word vocabulary n W is small: Scan foreground corpus C for phrases: O( n C ) producing m C phrase records – of course m C << n C Compute phrasiness : O( m C ) Scan background corpus B for phrases: O( n B ) producing m B Sort together and combine records: O(m log m), m= m B + m C Compute informativeness and combined quality: O(m) Assumes word counts fit in memory

59.Ramping it up – keeping word counts out of memory Goal: records for xy with attributes freq -in-B, freq -in-C, freq -of-x-in-C, freq -of -y- in- C, … Assume I have built built phrase tables and word tables….how do I incorporate the word attributes into the phrase records? For each phrase xy , request necessary word frequencies: Print “ x ~ request= freq -in- C,from = xy ” Print “ y ~ request= freq -in- C,from = xy ” Sort all the word requests in with the word tables Scan through the result and generate the answers: for each word w, a 1 =n 1 ,a 2 =n 2 ,…. Print “ xy ~request= freq -in- C,from = w ” Sort the answers in with the xy records Scan through and augment the xy records appropriately

60.Generating and scoring phrases: 3 Summary Scan foreground corpus C for phrases, words: O( n C ) producing m C phrase records, v C word records Scan phrase records producing word- freq requests: O( m C ) producing 2m C requests Sort requests with word records: O((2m C + v C )log(2m C + v C )) = O( m C log m C ) since v C < m C Scan through and answer requests: O( m C ) Sort answers with phrase records: O ( m C log m C ) Repeat 1-5 for background corpus: O( n B + m B logm B ) Combine the two phrase tables: O(m log m), m = m B + m C Compute all the statistics: O(m )

61.Outline Even more on stream-and-sort and naïve Bayes Request-answer pattern Another problem: “meaningful” phrase finding Statistics for identifying phrases (or more generally correlations and differences) Also using foreground and background corpora Implementing “phrase finding” efficiently Using request-answer Some other phrase-related problems Semantic orientation Complex named entity recognition

62.Basically… Stream-and-sort == ?

63.J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 63

64.MapReduce ! Sequentially read a lot of data Map: Extract something you care about Group by key: Sort and Shuffle Reduce: Aggregate, summarize, filter or transform Write the result Outline stays the same, Map and R educe change to fit the problem J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 64

65.MapReduce : The Map Step v k k v k v map v k v k … k v map Input key-value pairs Intermediate key-value pairs … k v J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 65

66.MapReduce : The Reduce Step k v … k v k v k v Intermediate key-value pairs Group by key reduce reduce k v k v k v … k v … k v k v v v v Key-value groups Output key-value pairs J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 66

67.More Specifically Input: a set of key-value pairs Programmer specifies two methods: Map(k, v)  <k’, v’>* Takes a key-value pair and outputs a set of key-value pairs E.g., key is the filename, value is a single line in the file There is one Map call for every ( k,v ) pair Reduce(k’, <v’>*)  <k’, v’’>* All values v’ with same key k’ are reduced together and processed in v’ order There is one Reduce function call per unique key k’ J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 67

68.Large-scale Computing Large-scale computing for data mining problems on commodity hardware Challenges: How do you distribute computation? How can we make it easy to write distributed programs? Machines fail: One server may stay up 3 years (1,000 days) If you have 1,000 servers, expect to loose 1/day People estimated Google had ~1M machines in 2011 1,000 machines fail every day! J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 68

69.Idea and Solution Issue: Copying data over a network takes time Idea: Bring computation close to the data Store files multiple times for reliability Map-reduce addresses these problems Google’s computational/data manipulation model Elegant way to work with big data Storage Infrastructure – File system Google: GFS. Hadoop : HDFS Programming model Map-Reduce J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 69

70.Storage Infrastructure Problem: If nodes fail , how to store data persistently? Answer: Distributed File System: Provides global file namespace Google GFS; Hadoop HDFS ; Typical usage pattern Huge files (100s of GB to TB) Data is rarely updated in place Reads and appends are common J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 70

71.Distributed File System Chunk servers File is split into contiguous chunks Typically each chunk is 16-64MB Each chunk replicated (usually 2x or 3x) Try to keep replicas in different racks Master node a.k.a. Name Node in Hadoop’s HDFS Stores metadata about where files are stored Might be replicated Client library for file access Talks to master to find chunk servers Connects directly to chunk servers to access data J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 71

72.Distributed File System Reliable distributed file system Data kept in “chunks” spread across machines Each chunk replicated on different machines Seamless recovery from disk or machine failure C 0 C 1 C 2 C 5 Chunk server 1 D 1 C 5 Chunk server 3 C 1 C 3 C 5 Chunk server 2 … C 2 D 0 D 0 Bring computation directly to the data! C 0 C 5 Chunk server N C 2 D 0 J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 72 Chunk servers also serve as compute servers

73.Programming Model: MapReduce Warm-up task: We have a huge text document Count the number of times each distinct word appears in the file Sample application: Analyze web server logs to find popular URLs J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 73

74.Task: Word Count Case 1: File too large for memory, but all <word, count> pairs fit in memory Case 2: Count occurrences of words: words(doc.txt) | sort | uniq -c where words takes a file and outputs the words in it, one per a line Case 2 captures the essence of MapReduce Great thing is that it is naturally parallelizable J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 74

75.Data Flow Input and final output are stored on a distributed file system (FS): Scheduler tries to schedule map tasks “close” to physical storage location of input data Intermediate results are stored on local FS of Map and Reduce workers Output is often input to another MapReduce task J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 75

76.Coordination: Master Master node takes care of coordination: Task status: (idle, in-progress, completed) Idle tasks get scheduled as workers become available When a map task completes, it sends the master the location and sizes of its R intermediate files, one for each reducer Master pushes this info to reducers Master pings workers periodically to detect failures J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 76

77.Dealing with Failures Map worker failure Map tasks completed or in-progress at worker are reset to idle Reduce workers are notified when task is rescheduled on another worker Reduce worker failure Only in-progress tasks are reset to idle Reduce task is restarted Master failure MapReduce task is aborted and client is notified J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 77

78.How many Map and Reduce jobs? M map tasks, R reduce tasks Rule of a thumb : Make M much larger than the number of nodes in the cluster One DFS chunk per map is common Improves dynamic load balancing and speeds up recovery from worker failures Usually R is smaller than M B ecause output is spread across R files J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 78

79.Task Granularity & Pipelining Fine granularity tasks: map tasks >> machines Minimizes time for fault recovery Can do pipeline shuffling with map execution Better dynamic load balancing J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 79

80.Refinement: Combiners Often a Map task will produce many pairs of the form (k,v 1 ), (k,v 2 ), … for the same key k E.g., popular words in the word count example Can save network time by pre-aggregating values in the mapper: combine(k, list(v 1 ))  v 2 Combiner is usually same as the reduce function Works only if reduce function is commutative and associative J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 80

81.Refinement: Combiners Back to our word counting example: Combiner combines the values of all keys of a single mapper (single machine): Much less data needs to be copied and shuffled! J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 81

82.Refinement: Partition Function Want to control how keys get partitioned Inputs to map tasks are created by contiguous splits of input file Reduce needs to ensure that records with the same intermediate key end up at the same worker System uses a default partition function: hash(key ) mod R Sometimes useful to override the hash function: E.g., hash(hostname(URL)) mod R ensures URLs from a host end up in the same output file J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 82

83.Cost Measures for Algorithms In MapReduce we quantify the cost of an algorithm using Communication cost = total I/O of all processes Elapsed communication cost = max of I/O along any path ( Elapsed ) computation cost analogous, but count only running time of processes Note that here the big-O notation is not the most useful ( adding more machines is always an option) J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 83

84.Example: Cost Measures For a map-reduce algorithm: Communication cost = input file size + 2  (sum of the sizes of all files passed from Map processes to Reduce processes) + the sum of the output sizes of the Reduce processes. Elapsed communication cost is the sum of the largest input + output for any map process, plus the same for any reduce process J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 84

85.What Cost Measures Mean Either the I/O (communication) or processing (computation) cost dominates Ignore one or the other Total cost tells what you pay in rent from your friendly neighborhood cloud Elapsed cost is wall-clock time using parallelism J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 85

86.Cost of Map-Reduce Join Total communication cost = O(|R|+|S|+|R ⋈ S|) Elapsed communication cost = O(s) We’re going to pick k and the number of Map processes so that the I/O limit s is respected We put a limit s on the amount of input or output that any one process can have. s could be: What fits in main memory What fits on local disk With proper indexes, computation cost is linear in the input + output size So computation cost is like comm. cost J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 86

87.Performance IMPORTANT You may not have room for all reduce values in memory In fact you should PLAN not to have memory for all values Remember, small machines are much cheaper you have a limited budget

88.Implementations Google Not available outside Google Hadoop An open-source implementation in Java Uses HDFS for stable storage Download : http:/ / hadoop.apache.org / Spark An open-source implementation in Scala Uses several distributed filesystems Download: http:/ / spark.apache.org / Others J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 88

89.Reading Jeffrey Dean and Sanjay Ghemawat : MapReduce : Simplified Data Processing on Large Clusters http://labs.google.com/papers/mapreduce.html Sanjay Ghemawat , Howard Gobioff , and Shun- Tak Leung: The Google File System http://labs.google.com/papers/gfs.html J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 89

90.Further Reading Programming model inspired by functional language primitives Partitioning/shuffling similar to many large-scale sorting systems NOW-Sort [97] Re-execution for fault tolerance BAD-FS [04] and TACC [97] Locality optimization has parallels with Active Disks/Diamond work Active Disks [01], Diamond [04] Backup tasks similar to Eager Scheduling in Charlotte system Charlotte [96] Dynamic load balancing solves similar problem as Rivers distributed queues River [99] J. Leskovec, A. Rajaraman, J. Ullman: Mining of Massive Datasets, http://www.mmds.org 90