Data Intensive Text Processing Data-Intensive Text Processing with MapReduce
Jimmy LinJimmy LinThe iSchoolUniversity of Maryland
JHU Summer School on Human Language TechnologyWednesday, June 17, 2009
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United StatesSee http://creativecommons.org/licenses/by-nc-sa/3.0/us/ for details
No data like more data!/k l d /d t /s/knowledge/data/g;
(Banko and Brill, ACL 2001)(Brants et al., EMNLP 2007)
How do we get here if we’re not Google?
cheap commodity clusters (or utility computing)+ simple, distributed programming models
cheap commodity clusters
= data-intensive computing for the masses!
(or utility computing)
p g
Divide and Conquer
“Work” PartitionPartition
w1 w2 w3
“worker” “worker” “worker”
r1 r2 r3
“Result” Combine
It’s a bit more complex…
Message Passing Shared Memory
Different programming modelsFundamental issues
scheduling, data distribution, synchronization, inter-process communication, robustness, fault t l
Mem
ory
tolerance, …
P1 P2 P3 P4 P5 P1 P2 P3 P4 P5
Different programming constructs
Architectural issuesFlynn’s taxonomy (SIMD, MIMD, etc.),network typology, bisection bandwidth Different programming constructs
mutexes, conditional variables, barriers, …masters/slaves, producers/consumers, work queues, …
Common problemslivelock, deadlock, data starvation, priority inversion…di i hil h l i b b i tt k
UMA vs. NUMA, cache coherence
dining philosophers, sleeping barbers, cigarette smokers, …
The reality: programmer shoulders the burden y p gof managing concurrency…
Typical ProblemIterate over a large number of records
Extract something of interest from eacht act so et g o te est o eac
Shuffle and sort intermediate results
Aggregate intermediate resultsAggregate intermediate results
Generate final output
Key idea: provide a functional abstraction for these two operations
(Dean and Ghemawat, OSDI 2004)
MapReduceProgrammers specify two functions:map (k, v) → <k’, v’>*
d (k’ ’) k’ ’ *reduce (k’, v’) → <k’, v’>*All values with the same key are reduced together
Usually, programmers also specify:y p g p ypartition (k’, number of partitions) → partition for k’
Often a simple hash of the key, e.g. hash(k’) mod nAllows reduce operations for different keys in parallelAllows reduce operations for different keys in parallel
combine (k’, v’) → <k’, v’>*Mini-reducers that run in memory after the map phaseUsed as an optimization to reducer network trafficUsed as an optimization to reducer network traffic
Implementations:Google has a proprietary implementation in C++g yHadoop is an open source implementation in Java
k1 k2 k3 k4 k5 k6v1 v2 v3 v4 v5 v6
mapmap map map
Shuffle and Sort: aggregate values by keys
ba 1 2 c c3 6 a c5 2 b c7 9
a 1 5 b 2 7 c 2 3 6 9
reduce reduce reduce
r1 s1 r2 s2 r3 s31 1 2 2 3 3
MapReduce RuntimeHandles scheduling
Assigns workers to map and reduce tasks
Handles “data distribution”Moves the process to the data
Handles synchronizationHandles synchronizationGathers, sorts, and shuffles intermediate data
Handles faultsa d es au sDetects worker failures and restarts
Everything happens on top of a distributed FS (later)
“Hello World”: Word Count
Map(String input_key, String input_value):// input key: document name// input_key: document name// input_value: document contentsfor each word w in input_values:
EmitIntermediate(w, "1");( )
Reduce(String key, Iterator intermediate_values):// key: a word, same for input and output// intermediate values: a list of counts// intermediate_values: a list of countsint result = 0;for each v in intermediate_values:
result += ParseInt(v);Emit(AsString(result));
UserProgram
(1) fork (1) fork (1) fork
Master
(1) fork ( ) (1) fork
(2) assign map(2) assign reduce
split 0split 1split 2
worker
worker outputfile 0
(2) assign reduce
(3) read(4) local write
(5) remote read(6) write
split 2split 3split 4
worker
worker
worker outputfile 1
(4) local write
worker
Inputfiles
Mapphase
Intermediate files(on local disk)
Reducephase
Outputfiles
Redrawn from (Dean and Ghemawat, OSDI 2004)
How do we get data to the workers?
NAS
SAN
Compute Nodes
What’s the problem here?What s the problem here?
Distributed File SystemDon’t move data to workers… Move workers to the data!
Store data on the local disks for nodes in the clusterStart up the workers on the node that has the data local
Why?Not enough RAM to hold all the data in memoryDisk access is slow, disk throughput is good
A distributed file system is the answerA distributed file system is the answerGFS (Google File System)HDFS for Hadoop (= GFS clone)
GFS: AssumptionsCommodity hardware over “exotic” hardware
High component failure ratesg co po e t a u e atesInexpensive commodity components fail all the time
“Modest” number of HUGE files
Files are write-once, mostly appended toPerhaps concurrently
Large streaming reads over random access
High sustained throughput over low latencyg g p y
GFS slides adapted from material by (Ghemawat et al., SOSP 2003)
GFS: Design DecisionsFiles stored as chunks
Fixed size (64MB)
Reliability through replicationEach chunk replicated across 3+ chunkservers
Single master to coordinate access, keep metadataSimple centralized management
No data cachingLittle benefit due to large data sets, streaming reads
Si lif th APISimplify the APIPush some of the issues onto the client
Application
GSF Client
GFS masterFile namespace
/foo/barchunk 2ef0
(file name, chunk index)
(chunk handle, chunk location)
Instructions to chunkserver
GFS chunkserver
Linux file system
GFS chunkserver
Linux file system
Chunkserver state(chunk handle, byte range)
chunk data
… …
Redrawn from (Ghemawat et al., SOSP 2003)
Master’s ResponsibilitiesMetadata storage
Namespace management/lockinga espace a age e t/ oc g
Periodic communication with chunkservers
Chunk creation re replication rebalancingChunk creation, re-replication, rebalancing
Garbage Collection
Graph Algorithms: TopicsIntroduction to graph algorithms and graph representations
Single Source Shortest Path (SSSP) problem
PageRank
What’s a graph?G = (V,E), where
V represents the set of vertices (nodes)E represents the set of edges (links)Both vertices and edges may contain additional information
Different types of graphs:Different types of graphs:Directed vs. undirected edgesPresence or absence of cycles...
Some Graph ProblemsFinding shortest paths
Routing Internet traffic and UPS trucks
Finding minimum spanning treesTelco laying down fiber
Finding Max FlowAirline scheduling
Identify “special” nodes and communitiesBreaking up terrorist cells, spread of avian flu
Bi tit t hiBipartite matchingMonster.com, Match.com
And of course PageRankAnd of course... PageRank
Representing GraphsG = (V, E)
Two common representationso co o ep ese tat o sAdjacency matrixAdjacency list
Adjacency MatricesRepresent a graph as an n x n square matrix M
n = |V|Mij = 1 means a link from node i to j
21 2 3 4
1 0 1 0 1 1
2
2 1 0 1 13 1 0 0 0
3
3 1 0 0 04 1 0 1 0 4
Adjacency ListsTake adjacency matrices… and throw away all the zeros
1 2 3 41 0 1 0 12 1 0 1 1
1: 2, 42: 1, 3, 42 1 0 1 1
3 1 0 0 04 1 0 1 0
3: 14: 1, 3
4 1 0 1 0
Single Source Shortest PathProblem: find shortest path from a source node to one or more target nodes
Single processor machine: Dijkstra’s Algorithm
MapReduce: parallel Breadth-First Search (BFS)
Finding the Shortest PathFirst, consider equal edge weights
Solution to the problem can be defined inductivelySo ut o to t e p ob e ca be de ed duct e y
Here’s the intuition:DistanceTo(startNode) = 0DistanceTo(startNode) 0For all nodes n directly reachable from startNode, DistanceTo(n) = 1For all nodes n reachable from some other set of nodes SFor all nodes n reachable from some other set of nodes S, DistanceTo(n) = 1 + min(DistanceTo(m), m ∈ S)
From Intuition to AlgorithmA map task receives
Key: node nValue: D (distance from start), points-to (list of nodes reachable from n)
∀p ∈ points-to: emit (p D+1)∀p ∈ points to: emit (p, D+1)
The reduce task gathers possible distances to a given pand selects the minimum one
Multiple Iterations NeededThis MapReduce task advances the “known frontier” by one hop
Subsequent iterations include more reachable nodes as frontier advancesMultiple iterations are needed to explore entire graphFeed output back into the same MapReduce task
Preserving graph structure:Problem: Where did the points-to list go?Solution: Mapper emits (n, points-to) as well
Weighted EdgesNow add positive weights to the edges
Simple change: points-to list in map task includes a weight S p e c a ge po ts to st ap tas c udes a e g tw for each pointed-to node
emit (p, D+wp) instead of (p, D+1) for each node p
Comparison to DijkstraDijkstra’s algorithm is more efficient
At any step it only pursues edges from the minimum-cost path inside the frontier
MapReduce explores all paths in parallel
Random Walks Over the WebModel:
User starts at a random Web pageUser randomly clicks on links, surfing from page to page
PageRank = the amount of time that will be spent on any given pagegiven page
PageRank: DefinedGiven page x with in-bound links t1…tn, where
C(t) is the out-degree of tα is probability of random jumpN is the total number of nodes in the graph
∑=
−+⎟⎠⎞
⎜⎝⎛=
n
i i
i
tCtPR
NxPR
1 )()()1(1)( αα
X
t1
t2
t…
tn
Computing PageRankProperties of PageRank
Can be computed iterativelyEffects at each iteration is local
Sketch of algorithm:Start with seed PRi valuesEach page distributes PRi “credit” to all pages it links toEach target page adds up “credit” from multiple in-bound links to compute PRi+1
Iterate until values converge
PageRank in MapReduce
Map: distribute PageRank “credit” to link targets
Reduce: gather up PageRank “credit” from multiple sources to compute new PageRank valueto compute new PageRank value
Iterate untilconvergence
...
PageRank: IssuesIs PageRank guaranteed to converge? How quickly?
What is the “correct” value of α, and how sensitive is the What is the correct value of α, and how sensitive is the algorithm to it?
What about dangling links?
How do you know when to stop?
Graph Algorithms in MapReduceGeneral approach:
Store graphs as adjacency listsEach map task receives a node and its outlinks (adjacency list)Map task compute some function of the link structure, emits value with target as the keyReduce task collects keys (target nodes) and aggregates
Iterate multiple MapReduce cycles until some termination diticondition
Remember to “pass” graph structure from one iteration to next
Managing DependenciesRemember: Mappers run in isolation
You have no idea in what order the mappers runYou have no idea on what node the mappers runYou have no idea when each mapper finishes
Tools for synchronization:Tools for synchronization:Ability to hold state in reducer across multiple key-value pairsSorting function for keysPartitionerCleverly-constructed data structures
Motivating ExampleTerm co-occurrence matrix for a text collection
M = N x N matrix (N = vocabulary size)Mij: number of times i and j co-occur in some context (for concreteness, let’s say context = sentence)
Why?Why?Distributional profiles as a way of measuring semantic distanceSemantic distance useful for many language processing tasks
MapReduce: Large Counting ProblemsTerm co-occurrence matrix for a text collection= specific instance of a large counting problem
A large event space (number of terms)A large number of observations (the collection itself)Goal: keep track of interesting statistics about the eventsGoal: keep track of interesting statistics about the events
Basic approachMappers generate partial countsReducers aggregate partial counts
How do we aggregate partial counts efficiently?
First Try: “Pairs”Each mapper takes a sentence:
Generate all co-occurring term pairsFor all pairs, emit (a, b) → count
Reducers sums up counts associated with these pairs
Use combiners!
“Pairs” AnalysisAdvantages
Easy to implement, easy to understand
DisadvantagesLots of pairs to sort and shuffle around (upper bound?)
Another Try: “Stripes”Idea: group together pairs into an associative array
(a, b) → 1 ( ) 2(a, c) → 2 (a, d) → 5 (a, e) → 3 (a, f) → 2
a → { b: 1, c: 2, d: 5, e: 3, f: 2 }
Each mapper takes a sentence:Generate all co-occurring term pairs
(a, )
For each term, emit a → { b: countb, c: countc, d: countd … }
Reducers perform element-wise sum of associative arrays
a → { b: 1, d: 5, e: 3 }a → { b: 1, c: 2, d: 2, f: 2 }a → { b: 2, c: 2, d: 7, e: 3, f: 2 }
+
“Stripes” AnalysisAdvantages
Far less sorting and shuffling of key-value pairsCan make better use of combiners
DisadvantagesMore difficult to implementUnderlying object is more heavyweightFundamental limitation in terms of size of event space
Cluster size: 38 coresData Source: Associated Press Worldstream (APW) of the English Gigaword Corpus (v3), which contains 2.27 million documents (1.8 GB compressed, 5.7 GB uncompressed)
Conditional ProbabilitiesHow do we estimate conditional probabilities from counts?
∑==
')',(count
),(count)(count
),(count)|(
BBA
BAABAABP
Why do we want to do this?
How do we do this with MapReduce?
P(B|A): “Stripes”
a → {b1:3, b2 :12, b3 :7, b4 :1, … }
Easy!One pass to compute (a, *)One pass to compute (a, )Another pass to directly compute P(B|A)
P(B|A): “Pairs”
(a b1) → 3
(a, *) → 32
(a b1) → 3 / 32
Reducer holds this value in memory
(a, b1) → 3 (a, b2) → 12 (a, b3) → 7(a, b4) → 1
(a, b1) → 3 / 32 (a, b2) → 12 / 32(a, b3) → 7 / 32(a, b4) → 1 / 32( , 4)
…( , 4)…
For this to work:Must emit extra (a, *) for every bn in mapperMust make sure all a’s get sent to same reducer (use partitioner)Must make sure all a s get sent to same reducer (use partitioner)Must make sure (a, *) comes first (define sort order)Must hold state in reducer across different key-value pairs
Synchronization in HadoopApproach 1: turn synchronization into an ordering problem
Sort keys into correct order of computationPartition key space so that each reducer gets the appropriate set of partial resultsHold state in reducer across multiple key-value pairs to perform computationIllustrated by the “pairs” approach
Approach 2: construct data structures that “bring theApproach 2: construct data structures that bring the pieces together”
Each reducer receives all the data it needs to complete the computationIllustrated by the “stripes” approach
Issues and TradeoffsNumber of key-value pairs
Object creation overheadTime for sorting and shuffling pairs across the network
Size of each key-value pairDe/serialization overhead
Combiners make a big difference!RAM vs disk and networkRAM vs. disk and networkArrange data to maximize opportunities to aggregate partial results
Statistical Machine TranslationConceptually simple:(translation from foreign f into English e)
)()|(maxargˆ ePefPee
=
Difficult in practice!
Phrase-Based Machine Translation (PBMT) :Phrase Based Machine Translation (PBMT) :Break up source sentence into little pieces (phrases)Translate each phrase individually
Dyer et al. (Third ACL Workshop on MT, 2008)
Translation as a “Tiling” Problem
Maria no dio una bofetada a la bruja verde
M t i l t th it hMary not
did not
no
give a slap to the witch green
slap
a slap
to the
green witchby
did not give to
the
the witchslap the witchslap
Example from Koehn (2006)
MT Architecture
Word Alignment Phrase ExtractionTraining Data
i saw the small tablevi la mesa pequeña
(vi, i saw)(la mesa pequeña, the small table)…Parallel Sentences
he sat at the tablethe service was good
Target-Language Text
Translation Model
LanguageModel
Target-Language Text
Decoder
Foreign Input Sentence English Output Sentencemaria no daba una bofetada a la bruja verde mary did not slap the green witch
MT Architecture
Word Alignment Phrase ExtractionTraining Data
There are MapReduce Implementations of these two components!
i saw the small tablevi la mesa pequeña
(vi, i saw)(la mesa pequeña, the small table)…Parallel Sentences
he sat at the tablethe service was good
Target-Language Text
Translation Model
LanguageModel
Target-Language Text
Decoder
Foreign Input Sentence English Output Sentencemaria no daba una bofetada a la bruja verde mary did not slap the green witch
What’s the point?The optimally-parallelized version doesn’t exist!
It’s all about the right level of abstractiont s a about t e g t e e o abst act oGoldilocks argument
What’s next?Web-scale text processing: luxury → necessity
Fortunately, the technology is becoming more accessible
MapReduce is a nice hammer:Whack it on everything in sight!
MapReduce is only the beginning… Alternative programming modelsFundamental breakthroughs in algorithm designFundamental breakthroughs in algorithm design
Applications(NLP, IR, ML, etc.)
Programming Models(MapReduce…)
Systems (architecture, network, etc.)