import%20marimo%0A%0A__generated_with%20%3D%20%220.19.9%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Monarch%3A%20History%20%26%20Vision%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Training%20at%20the%20Frontier%20Hurts%0A%0A%20%20%20%20Before%20we%20look%20at%20any%20code%2C%20let's%20talk%20about%20**why%20Monarch%20exists**.%0A%0A%20%20%20%20During%20Llama%203%20pre-training%2C%20Meta%20ran%2016%2C384%20GPUs%20for%2054%20days%20and%20hit%0A%20%20%20%20**419%20unexpected%20interruptions**%20%E2%80%94%20roughly%20one%20failure%20every%203%20hours.%0A%20%20%20%20The%20breakdown%20tells%20you%20a%20lot%20about%20what%20goes%20wrong%20at%20scale%3A%0A%0A%20%20%20%20%7C%20Cause%20%7C%20%25%20of%20interruptions%20%7C%20Count%20%7C%0A%20%20%20%20%7C-------%7C-------------------%7C-------%7C%0A%20%20%20%20%7C%20Faulty%20GPUs%20%7C%2030.1%25%20%7C%20148%20%7C%0A%20%20%20%20%7C%20GPU%20HBM3%20errors%20%7C%2017.2%25%20%7C%2072%20%7C%0A%20%20%20%20%7C%20Software%20bugs%20%7C%2012.9%25%20%7C%2054%20%7C%0A%20%20%20%20%7C%20Network%20%2F%20cables%20%7C%208.4%25%20%7C%2035%20%7C%0A%20%20%20%20...%0A%0A%20%20%20%20This%20is%20%22just%22%20for%2016K%20GPUs.%20If%20you%20further%20scale%20workloads%20to%20tens%20of%20thousands%20of%20GPUs%2C%20you%20should%20expect%20failures%0A%20%20%20%20every%20hour%20or%20more%20frequently.%20The%20distributed%20system%20must%20handle%20this%20gracefully%20%E2%80%94%20detect%2C%20checkpoint%2C%20recover%2C%20keep%20going%0A%20%20%20%20automatically%2C%20without%20requiring%20someone%20to%20SSH%20in%20to%20restart%20things%20manually.%0A%0A%20%20%20%20**Monarch%20was%20built%20for%20this%20reality.**%20It's%20a%20PyTorch-native%20distributed%0A%20%20%20%20systems%20framework%20designed%20from%20the%20ground%20up%20for%20fault%20tolerance%2C%20flexible%0A%20%20%20%20communication%20patterns%2C%20and%20scale.%20Let's%20see%20how%20it%20got%20here.%0A%0A%20%20%20%20*(Failure%20data%20from%20the%20%5BLlama%203%20paper%5D(https%3A%2F%2Farxiv.org%2Fabs%2F2407.21783)%3B%0A%20%20%20%20see%20also%20%5BIntroducing%20PyTorch%20Monarch%5D(https%3A%2F%2Fpytorch.org%2Fblog%2Fintroducing-pytorch-monarch%2F))*%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**What%20you'll%20learn%3A**%0A%0A%20%20%20%201.%20Why%20Monarch%20exists%20(the%20pain%20that%20drove%20its%20creation)%0A%20%20%20%202.%20The%20tensor%20engine%20origin%20story%0A%20%20%20%203.%20The%20actor%20model%20and%20why%20it%20matters%20for%20distributed%20ML%0A%20%20%20%204.%20Your%20first%20Monarch%20program%3A%20ping-pong%20actors%0A%20%20%20%205.%20The%20Monarch%20ontology%3A%20World%2C%20Proc%2C%20Actor%2C%20Port%0A%20%20%20%206.%20Scalable%20messaging%20via%20meshes%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20The%20Tensor%20Engine%20Origin%20Story%0A%0A%20%20%20%20The%20first%20step%20toward%20solving%20this%20was%20rethinking%20how%20we%20orchestrate%0A%20%20%20%20distributed%20computation.%0A%0A%20%20%20%20Monarch%20began%20as%20a%20**tensor%20engine**%20for%20distributed%20PyTorch.%0A%20%20%20%20It%20was%20built%20as%20a%20%22single%20controller%22%20that%20executed%20DTensor%20operations.%0A%0A%20%20%20%20In%20other%20words%2C%20in%20Monarch%20we%20have%20a%20**single%20controller**%20that%20orchestrates%20many%20GPUs%2C%0A%20%20%20%20instead%20of%20SPMD%20(Single%20Program%2C%20Multiple%20Data)%20where%20every%20rank%20runs%20the%20same%20script.%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20%E2%94%8C%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%90%0A%20%20%20%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20CONTROLLER%20(Python)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%0A%20%20%20%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%0A%20%20%20%20%E2%94%82%20%20%20%23%20Create%20a%20mesh%3A%204%20hosts%20x%208%20GPUs%20%3D%2032%20GPUs%20total%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%0A%20%20%20%20%E2%94%82%20%20%20mesh%20%3D%20DeviceMesh(hosts%3D4%2C%20gpus_per_host%3D8%2C%20dims%3D(%22dp%22%2C%20%22tp%22))%20%E2%94%82%0A%20%20%20%20%E2%94%82%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%0A%20%20%20%20%E2%94%82%20%20%20with%20mesh.activate()%3A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%0A%20%20%20%20%E2%94%82%20%20%20%20%20%20%20loss%20%3D%20model(X)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%0A%20%20%20%20%E2%94%82%20%20%20%20%20%20%20loss.backward()%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%0A%20%20%20%20%E2%94%82%20%20%20%20%20%20%20p.grad.reduce_(%22dp%22%2C%20reduction%3D%22avg%22)%20%20%23%20all-reduce%20%20%20%20%20%20%20%20%E2%94%82%0A%20%20%20%20%E2%94%82%20%20%20%20%20%20%20optimizer.step()%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%0A%20%20%20%20%E2%94%94%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%AC%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%98%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%82%20tensor%20commands%0A%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%8C%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%BC%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%90%0A%20%20%20%20%20%20%20%20%20%20%20%20%E2%96%BC%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%96%BC%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%96%BC%0A%20%20%20%20%20%20%20%E2%94%8C%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%90%20%20%20%20%20%20%20%20%20%E2%94%8C%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%90%20%20%20%20%20%20%20%20%20%E2%94%8C%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%90%0A%20%20%20%20%20%20%20%E2%94%82%20Host%200%20%20%E2%94%82%20%20%20%20%20%20%20%20%20%E2%94%82%20Host%201%20%20%E2%94%82%20%20%20%20%20%20%20%20%20%E2%94%82%20Host%202%20%20%E2%94%82%20%20...%0A%20%20%20%20%20%20%20%E2%94%82%208%20GPUs%20%20%E2%94%82%20%20%20%20%20%20%20%20%20%E2%94%82%208%20GPUs%20%20%E2%94%82%20%20%20%20%20%20%20%20%20%E2%94%82%208%20GPUs%20%20%E2%94%82%0A%20%20%20%20%20%20%20%E2%94%82%20dp%3D0%20%20%20%20%E2%94%82%20%20%20%20%20%20%20%20%20%E2%94%82%20dp%3D1%20%20%20%20%E2%94%82%20%20%20%20%20%20%20%20%20%E2%94%82%20dp%3D2%20%20%20%20%E2%94%82%0A%20%20%20%20%20%20%20%E2%94%94%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%98%20%20%20%20%20%20%20%20%20%E2%94%94%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%98%20%20%20%20%20%20%20%20%20%E2%94%94%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%98%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20This%20allows%20one%20Python%20script%20to%20orchestrate%20thousands%20of%20GPUs%20-%20bypassing%20SPMD-imposed%20complexities%20like%20per-rank%20checks%2C%20scattered%20logging%2C%20etc.%20Complex%20control%20flow%20becomes%20more%20natural%20to%20express%20in%20code.%0A%0A%20%20%20%20The%20tensor%20engine%20still%20exists%20today%2C%20but%20Monarch%20has%20evolved%20beyond%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Evolution%20to%20Actors%0A%0A%20%20%20%20While%20building%20the%20tensor%20engine%2C%20the%20Monarch%20team%20realized%20that%20the%20system%0A%20%20%20%20underneath%20%E2%80%94%20the%20Rust%20runtime%20managing%20processes%2C%20message%20routing%2C%20and%0A%20%20%20%20scheduling%20%E2%80%94%20was%20far%20more%20general%20than%20just%20tensor%20orchestration.%20The%0A%20%20%20%20**actor%20model**%20underpinning%20everything%20was%20powerful%20on%20its%20own.%0A%0A%20%20%20%20So%20the%20APIs%20shifted%20to%20bring%20those%20primitives%20directly%20to%20Python.%20Instead%20of%0A%20%20%20%20only%20exposing%20tensor%20operations%2C%20Monarch%20now%20lets%20you%20define%20arbitrary%0A%20%20%20%20**actors**%20that%20communicate%20via%20**messages**.%0A%0A%20%20%20%20**What%20is%20an%20actor%3F**%20In%20the%20formal%20sense%2C%20an%20actor%20is%20a%20concurrent%20unit%20of%0A%20%20%20%20computation%20that%3A%0A%0A%20%20%20%201.%20Has%20**private%20state**%20%E2%80%94%20no%20shared%20memory%20with%20other%20actors%0A%20%20%20%202.%20Communicates%20exclusively%20by%20**sending%20and%20receiving%20messages**%0A%20%20%20%203.%20Can%20**create%20new%20actors**%2C%20send%20messages%2C%20and%20decide%20how%20to%20handle%20the%20next%0A%20%20%20%20%20%20%20message%20it%20receives%0A%0A%20%20%20%20A%20useful%20analogy%3A%20think%20of%20actors%20as%20workers%20in%20separate%20offices.%20They%20can't%0A%20%20%20%20walk%20over%20and%20read%20each%20other's%20notebooks%20%E2%80%94%20they%20can%20only%20communicate%20by%0A%20%20%20%20passing%20notes%20through%20mail%20slots.%0A%0A%20%20%20%20This%20model%20composes%20naturally%20with%20PyTorch's%20existing%20ecosystem.%20You%20can%20wrap%20any%20SPMD%20code%20with%20Monarch's%20actors%2C%0A%20%20%20%20and%20command%20a%20group%20or%20%22gang%22%20of%20actors%20as%20a%20single%20addressable%20unit%2C%20and%20wire%20them%20together%20however%20your%20workload%20requires.%0A%0A%20%20%20%20Take%20RL%20for%20example%20(don't%20worry%20about%20the%20details%20of%20this%20snippet%20%E2%80%94%0A%20%20%20%20we'll%20cover%20these%20APIs%20hands-on%20throughout%20the%20series)%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20%23%20Spawn%20different%20actor%20types%20across%20processes%0A%20%20%20%20host%20%3D%20this_host()%0A%20%20%20%20trainer_procs%20%3D%20host.spawn_procs(per_host%3D%7B%22gpus%22%3A%201%7D)%0A%20%20%20%20generator_procs%20%3D%20host.spawn_procs(per_host%3D%7B%22gpus%22%3A%204%7D)%0A%0A%20%20%20%20trainer%20%3D%20trainer_procs.spawn(%22trainer%22%2C%20TrainerActor)%0A%20%20%20%20generators%20%3D%20generator_procs.spawn(%22generators%22%2C%20GeneratorActor)%0A%0A%20%20%20%20%23%20Wire%20them%20together%0A%20%20%20%20for%20batch%20in%20training_loop%3A%0A%20%20%20%20%20%20%20%20%23%20Call%20all%20generators%20(returns%20a%20ValueMesh)%0A%20%20%20%20%20%20%20%20sample_mesh%20%3D%20generators.generate.call(prompts).get()%0A%20%20%20%20%20%20%20%20%23%20Extract%20values%20from%20the%20ValueMesh%20before%20passing%20along%0A%20%20%20%20%20%20%20%20samples%20%3D%20list(sample_mesh.values())%0A%20%20%20%20%20%20%20%20trainer.train_step.call_one(samples).get()%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Let's%20make%20this%20concrete%20with%20a%20real%20program%20you%20can%20run.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Your%20First%20Monarch%20Program%0A%0A%20%20%20%20The%20simplest%20possible%20Monarch%20program%20%E2%80%94%20two%20actors%20playing%20ping%20pong.%0A%0A%20%20%20%20Reference%3A%20%5Bping_pong%20example%5D(https%3A%2F%2Fmeta-pytorch.org%2Fmonarch%2Fgenerated%2Fexamples%2Fping_pong.html)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20monarch.actor%20import%20Actor%2C%20endpoint%2C%20current_rank%2C%20this_host%0A%0A%20%20%20%20class%20PingPong(Actor)%3A%0A%20%20%20%20%20%20%20%20def%20__init__(self)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20rank%20%3D%20current_rank().rank%0A%20%20%20%20%20%20%20%20%20%20%20%20self.name%20%3D%20%22Ping%22%20if%20rank%20%3D%3D%200%20else%20%22Pong%22%0A%20%20%20%20%20%20%20%20%20%20%20%20self.count%20%3D%200%0A%0A%20%20%20%20%20%20%20%20%40endpoint%0A%20%20%20%20%20%20%20%20def%20ping(self%2C%20message%3A%20str)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.count%20%2B%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22%7Bself.name%7D%20received%3A%20%7Bmessage%7D%20(count%3A%20%7Bself.count%7D)%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20f%22pong%20from%20%7Bself.name%7D%22%0A%0A%20%20%20%20%23%20Spawn%20two%20actors%20on%20separate%20processes%0A%20%20%20%20host%20%3D%20this_host()%0A%20%20%20%20procs%20%3D%20host.spawn_procs(per_host%3D%7B%22gpus%22%3A%202%7D)%0A%20%20%20%20actors%20%3D%20procs.spawn(%22players%22%2C%20PingPong)%0A%0A%20%20%20%20%23%20Get%20individual%20actors%20via%20slicing%0A%20%20%20%20ping_actor%20%3D%20actors.slice(gpus%3D0)%0A%20%20%20%20pong_actor%20%3D%20actors.slice(gpus%3D1)%0A%0A%20%20%20%20%23%20Play%20ping%20pong%20%E2%80%94%20call_one%20targets%20a%20single%20actor%0A%20%20%20%20for%20i%20in%20range(3)%3A%0A%20%20%20%20%20%20%20%20response%20%3D%20ping_actor.ping.call_one(f%22round%20%7Bi%7D%22).get()%0A%20%20%20%20%20%20%20%20print(f%22Got%3A%20%7Bresponse%7D%22)%0A%20%20%20%20%20%20%20%20response%20%3D%20pong_actor.ping.call_one(f%22round%20%7Bi%7D%22).get()%0A%20%20%20%20%20%20%20%20print(f%22Got%3A%20%7Bresponse%7D%22)%0A%0A%20%20%20%20%23%20.call()%20broadcasts%20to%20ALL%20actors%20and%20returns%20a%20ValueMesh%20%E2%80%94%20a%20dict-like%0A%20%20%20%20%23%20container%20mapping%20each%20actor's%20position%20to%20its%20return%20value.%0A%20%20%20%20results%20%3D%20actors.ping.call(%22hello%20everyone%22).get()%0A%20%20%20%20for%20point%2C%20response%20in%20results.items()%3A%0A%20%20%20%20%20%20%20%20print(f%22Actor%20at%20%7Bpoint%7D%3A%20%7Bresponse%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Scalable%20Messaging%0A%0A%20%20%20%20Meshes%20of%20actors%20enable%20**scalable**%20messaging%20through%20tree-based%20routing.%0A%0A%20%20%20%20Instead%20of%20sending%20messages%20individually%20to%20every%20actor%2C%20Monarch%20routes%20through%20a%0A%20%20%20%20**tree%20of%20intermediate%20hosts**%2C%20giving%20**O(log%20N)%20broadcast**.%20The%20same%20tree%0A%20%20%20%20aggregates%20responses%20on%20the%20way%20back%20up%2C%20giving%20**O(log%20N)%20reduce**%20as%20well.%0A%0A%20%20%20%20This%20means%20primitives%20like%20barriers%20and%20all-reduces%20are%20as%20simple%20as%0A%20%20%20%20waiting%20for%20an%20aggregate%20response%20%E2%80%94%20and%20they%20scale%20to%20thousands%20of%20actors%0A%20%20%20%20without%20bottlenecking%20the%20client.%0A%0A%20%20%20%20*(See%20the%20animated%20diagrams%20in%20the%0A%20%20%20%20%5BMonarch%20presentation%20notebook%5D(https%3A%2F%2Fgithub.com%2Fmeta-pytorch%2Fmonarch%2Fblob%2Fmain%2Fexamples%2Fpresentation%2Fpresentation.ipynb))*%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Channels%20and%20Low-level%20Messaging%0A%0A%20%20%20%20It%20is%20sometimes%20useful%20to%20establish%20direct%20channels%20between%20two%20points%2C%20or%20forward%0A%20%20%20%20the%20handling%20of%20some%20messages%20from%20one%20actor%20to%20another.%20To%20enable%20this%2C%20all%20messaging%0A%20%20%20%20in%20Monarch%20is%20built%20out%20of%20%60Port%60%20objects.%0A%0A%20%20%20%20An%20actor%20can%20create%20a%20new%20%60Channel%60%2C%20which%20provides%20a%20%60Port%60%20for%20sending%20and%20a%0A%20%20%20%20%60PortReceiver%60%20for%20receiving%20messages.%20The%20%60Port%60%20object%20can%20then%20be%20sent%20to%20any%20endpoint.%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20from%20monarch.actor%20import%20Channel%2C%20Port%0A%0A%20%20%20%20port%2C%20recv%20%3D%20Channel.open()%0A%0A%20%20%20%20port.send(3)%0A%20%20%20%20print(recv.recv().get())%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Ports%20can%20be%20passed%20as%20arguments%20to%20actors%20and%20sent%20a%20response%20remotely.%20We%20can%20also%0A%20%20%20%20directly%20ask%20an%20endpoint%20to%20send%20its%20response%20to%20a%20port%20using%20the%20%60send%60%20messaging%20primitive.%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20from%20monarch.actor%20import%20send%0A%0A%20%20%20%20with%20trainer_procs.activate()%3A%0A%20%20%20%20%20%20%20%20send(check_memory%2C%20args%3D()%2C%20kwargs%3D%7B%7D%2C%20port%3Dport)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20The%20port%20will%20receive%20a%20response%20from%20each%20actor%20sent%20the%20message%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20for%20_%20in%20range(4)%3A%0A%20%20%20%20%20%20%20%20print(recv.recv().get())%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20The%20other%20adverbs%20like%20%60call%60%2C%20%60stream%60%2C%20and%20%60broadcast%60%20are%20just%20implemented%20in%20terms%0A%20%20%20%20of%20ports%20and%20%60send%60.%0A%0A%20%20%20%20%23%23%23%20Message%20Ordering%0A%0A%20%20%20%20Messages%20from%20an%20actor%20are%20delivered%20to%20the%20destination%20actor%20in%20the%20order%20in%20which%20they%0A%20%20%20%20are%20sent.%20If%20actor%20A%20sends%20message%20M0%20to%20actor%20B%2C%20then%20later%20sends%20M1%20to%20B%2C%20actor%20B%20will%0A%20%20%20%20receive%20M0%20before%20M1.%20Messages%20sent%20to%20a%20mesh%20of%20actors%20behave%20as%20if%20sent%20individually%0A%20%20%20%20to%20each%20destination.%0A%0A%20%20%20%20Each%20actor%20handles%20its%20messages%20**sequentially**%20%E2%80%94%20it%20must%20finish%20handling%20a%20message%20before%0A%20%20%20%20the%20next%20one%20is%20delivered.%20Different%20actors%20in%20the%20same%20process%20handle%20messages%20**concurrently**.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Summary%0A%0A%20%20%20%20Monarch%20uniquely%20provides%3A%0A%0A%20%20%20%201.%20Scalable%20messaging%20using%20multidimensional%20meshes%20of%20actors%0A%20%20%20%202.%20Fault%20tolerance%20through%20supervision%20trees%20and%20%60__supervise__%60%0A%20%20%20%203.%20Point-to-point%20low-level%20RDMA%0A%20%20%20%204.%20Built-in%20distributed%20tensors%0A%0A%20%20%20%20This%20foundation%20enables%20building%20sophisticated%20multi-machine%20training%20programs%0A%20%20%20%20with%20clear%20semantics%20for%20distribution%2C%20fault%20tolerance%2C%20and%20communication%20patterns.%0A%0A%20%20%20%20The%20remaining%20sections%20fill%20in%20more%20details%20about%20how%20to%20accomplish%20common%0A%20%20%20%20patterns%20with%20the%20above%20features.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20**Next%3A**%20%5BNB02%20%E2%80%94%20Interactive%20DevX%5D(.%2F02_interactive_devx.html)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
bf2329ccf6e300fa8b0930089d0fc92d