{"id":17121,"date":"2026-07-27T00:13:13","date_gmt":"2026-07-27T00:13:13","guid":{"rendered":"https:\/\/techtrendfeed.com\/?p=17121"},"modified":"2026-07-27T00:13:13","modified_gmt":"2026-07-27T00:13:13","slug":"run-ray-on-tpu-half-2-ray-ai-libraries","status":"publish","type":"post","link":"https:\/\/techtrendfeed.com\/?p=17121","title":{"rendered":"Run Ray on TPU, Half 2: Ray AI libraries"},"content":{"rendered":"<p> <br \/>\n<\/p>\n<div>\n<p><img decoding=\"async\" class=\"banner-image\" src=\"https:\/\/storage.googleapis.com\/gweb-developer-goog-blog-assets\/images\/header.original_nhYbgwk.png\" alt=\"header\"\/>  <\/p>\n<div class=\"inner-block-content rich-content\">\n<p data-block-key=\"boywc\"><b><i>TL;DR<\/i><\/b><i>: Half 2 of two.<\/i> <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/developers.googleblog.com\/run-ray-on-tpu-part-1-the-foundations\/\"><i>Half 1<\/i><\/a> <i>lined the one {hardware} thought you want and the 2 layers beneath (GKE and Ray Core). This half exhibits the libraries you really construct with, Ray Serve, Ray Knowledge, and Ray Prepare.<\/i><\/p>\n<h2 data-block-key=\"vxxo5\" id=\"recap\"><b>Recap<\/b><\/h2>\n<p data-block-key=\"c77ks\"><b>Fast recap if you happen to&#8217;re touchdown right here first.<\/b> Operating Ray on TPU comes down to at least one caveat: TPU chips are wired into fastened teams known as <b>slices<\/b> (host VMs sharing a high-speed hyperlink known as the ICI), and a multi-host mannequin has to land on <b>one complete slice<\/b> or its staff cannot attain one another and the job hangs.<\/p>\n<p data-block-key=\"5he6s\">Google Kubernetes Engine (GKE) with the Ray Operator add-on provisions slices and labels their hosts, and a Ray Core primitive, <code>slice_placement_group()<\/code>, reserves an entire slice directly. You declare a <b>topology<\/b> (the slice form, like 4&#215;4 for 16 chips, for instance) and the libraries beneath deal with the location for you.<\/p>\n<p data-block-key=\"6uerf\">With Core dealing with placement beneath, the libraries all observe the identical sample: declare a topology, let Core reserve the slice. What adjustments per library is barely what you declare it on. We&#8217;ll go within the order most groups undertake them, serving first.<\/p>\n<h2 data-block-key=\"t0ohi\" id=\"ray-serve-on-tpu\"><b>Ray Serve on TPU<\/b><\/h2>\n<p data-block-key=\"6be6v\">Serving is the place most groups begin from. A mannequin that wants a number of GPUs to suit can run on a single TPU host, and TPUs are sometimes a extra accessible and cost-effective choice for inference. Ray Serve offers you the standard autoscaling, load-balancing, and multi-model composition, and on TPU it serves LLMs by <b>vLLM<\/b>, a high-throughput engine.<\/p>\n<\/div>\n<div class=\"inner-block-content video-block\">\n<p>        <video autoplay=\"\" loop=\"\" muted=\"\" playsinline=\"\" poster=\"https:\/\/storage.googleapis.com\/gweb-developer-goog-blog-assets\/original_videos\/wagtailvideo-dmkb1ph6_thumb.jpg\"><source src=\"https:\/\/storage.googleapis.com\/gweb-developer-goog-blog-assets\/original_videos\/servevllm.mp4\" type=\"video\/mp4\"><p>Sorry, your browser does not help playback for this video<\/p>\n<p><\/source><\/video><\/p>\n<\/div>\n<div class=\"inner-block-content rich-content\">\n<p data-block-key=\"boywc\">The exhausting case is when a mannequin is just too massive for one host (say one sharded tensor-parallel throughout 16 chips). That is the place Serve clears it with a single further discipline, topology.<\/p>\n<\/div>\n<div class=\"inner-block-content code-block line-numbers\">\n<pre><code class=\"language-plaintext\">accelerator_type: TPU-V6E&#13;\naccelerator_config:&#13;\n  type: tpu&#13;\n  topology: \"4x4\"<\/code><\/pre>\n<p>\n        Plain textual content\n    <\/p>\n<\/div>\n<div class=\"inner-block-content rich-content\">\n<p data-block-key=\"boywc\">That one discipline is price understanding, as a result of getting it improper is the basic multi-host TPU failure. With topology set, Serve&#8217;s TPU backend skips its typical upfront placement group and defers to the duplicate, which creates a slice placement group at startup. That deferral is what retains a tensor-parallel mannequin&#8217;s staff on one shared ICI mesh. Go away it off and Serve falls again to per-chip bundles; on a multi-host mannequin these bundles can scatter throughout two slices, and since there is no ICI between slices, the employees by no means end their first collective. You aren&#8217;t getting a crash, you get a deployment that sits in DEPLOYING endlessly when you burn TPU-hours attempting to find a bug that is actually one lacking line of YAML. So bear in mind, topology discipline makes the distinction.<\/p>\n<p data-block-key=\"6heb1\">In observe you deploy a RayService (advisable over a uncooked RayCluster for manufacturing) on a printed vLLM TPU picture, look ahead to it to achieve Operating, and curl the endpoint. The official GKE tutorials cowl Llama 3 8B and Mistral 7B on v5e, Llama 3.1 70B on v6e, and Steady Diffusion. The <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/github.com\/GoogleCloudPlatform\/kubernetes-engine-samples\/tree\/main\/ai-ml\/gke-ray\/tpu\/get-started\/serve\">serve step<\/a> of the get-started instance walks the total deployment finish to finish.<\/p>\n<h2 data-block-key=\"7kwxm\" id=\"ray-data-on-tpu:-feeding-the-accelerators-with-iter_jax_batches\"><b>Ray Knowledge on TPU: feeding the accelerators with iter_jax_batches<\/b><\/h2>\n<p data-block-key=\"8ppln\">A quick accelerator is barely as helpful as the information you may preserve flowing into it, and TPUs are quick sufficient {that a} naive loader turns into the bottleneck. That is the issue <code>iter_jax_batches()<\/code> solves. It arms you batches which can be already JAX arrays and already device-sharded, so a coaching enter pipeline or a big batch-inference job pulls straight from a Ray Knowledge pipeline with no host-side NumPy-to-JAX copy stalling the step.<\/p>\n<\/div>\n<div class=\"inner-block-content code-block line-numbers\">\n<pre><code class=\"language-python\">ds = ray.knowledge.read_parquet(\"gs:\/\/my-bucket\/prepare\/\")&#13;\nfor batch in ds.iter_jax_batches(batch_size=1024):&#13;\n    # batch arrives as device-sharded JAX arrays, prepared for the coaching step&#13;\n    loss = train_step(batch)<\/code><\/pre>\n<p>\n        Python\n    <\/p>\n<\/div>\n<div class=\"inner-block-content rich-content\">\n<p data-block-key=\"boywc\"><code>iter_jax_batches<\/code> API does the machine sharding for you, and it handles the ragged closing batch (the one which is not a clear a number of of your batch dimension) with an express alternative of drop, pad, or increase, as a substitute of a form error three hours right into a run.<\/p>\n<p data-block-key=\"9nqc6\">You need to use it because the enter facet of a <code>JaxTrainer<\/code> job, and it is simply as helpful by itself for offline batch inference over an enormous dataset on a TPU slice. It landed lately in Ray, and the information step of the get-started instance makes use of it for dataset prep and batch inference.<\/p>\n<p data-block-key=\"bfr3t\">Ray Prepare on TPU: distributed coaching with JaxTrainer<\/p>\n<p data-block-key=\"a9sq7\">Coaching was the complicated a part of Ray on TPU, due to topology and having to account for the slice form in your code. <code>JaxTrainer<\/code> addresses that. It brings Ray Prepare&#8217;s coaching loop (checkpointing, fault tolerance, multi-slice scale-out) to JAX, Google&#8217;s array and autodiff library and the native framework for TPU. You hand it a coaching operate and a slice form and Ray launches one employee per host, wires them right into a single mesh, and runs your operate on every.<\/p>\n<\/div>\n<div class=\"inner-block-content code-block line-numbers\">\n<pre><code class=\"language-python\">from ray.prepare import ScalingConfig&#13;\nfrom ray.prepare.v2.jax import JaxTrainer&#13;\n&#13;\ndef train_loop_per_worker(config):&#13;\n    import jax            # import jax INSIDE the employee fn (TPU requirement)&#13;\n    # ... your JAX\/Flax coaching step runs right here, as soon as per host ...&#13;\n&#13;\ncoach = JaxTrainer(&#13;\n    train_loop_per_worker=train_loop_per_worker,&#13;\n    scaling_config=ScalingConfig(&#13;\n        use_tpu=True,&#13;\n        topology=\"4x4\",            # the slice form, NOT a chip rely&#13;\n        accelerator_type=\"TPU-V6E\",&#13;\n    ),&#13;\n)&#13;\ncoach.match()<\/code><\/pre>\n<p>\n        Python\n    <\/p>\n<\/div>\n<div class=\"inner-block-content rich-content\">\n<p data-block-key=\"boywc\">Two issues on this snippet you need to bear in mind to save lots of debugging time. The import jax lives <i>inside<\/i> <code>train_loop_per_worker<\/code>, not on the prime of the file, as a result of every employee initializes JAX in its personal TPU context; import it at module scope and you will combat cryptic device-init errors earlier than step one. And <code>topology=\"4x4\"<\/code> is your complete placement declaration, the road that was a block of hand-written coordination code. Set subsequent to a GPU JaxTrainer or TorchTrainer, the one actual distinction is use_tpu=True and a topology as a substitute of a GPU rely.<\/p>\n<p data-block-key=\"163cp\">The remainder it simply runs. It is because Ray Prepare owns the loop, you get checkpointing and fault-tolerant restarts, which is what makes lengthy TPU runs on preemptible capability really end, and topology scales to multi-slice (Ray wires the cross-slice coordination) when one slice is not sufficient. The <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/github.com\/GoogleCloudPlatform\/kubernetes-engine-samples\/tree\/main\/ai-ml\/gke-ray\/tpu\/get-started\/train\">prepare step<\/a> of the get-started instance is a whole JaxTrainer DPO run.<\/p>\n<h2 data-block-key=\"jq3z4\" id=\"two-final-extras:-tpu-docker-images-and-dashboard-metrics\"><b>Two closing extras: TPU Docker photos and dashboard metrics<\/b><\/h2>\n<p data-block-key=\"8uehk\">As a part of the first-class accelerator help, Ray now publishes official <code>rayproject\/ray:*-tpu<\/code> photos with the JAX\/TPU stack (<code>jax[tpu]<\/code>, flax, optax, orbax-checkpoint) and profiling tooling already put in, so you do not have to assemble a working TPU setting by hand. You possibly can simply base your picture on the tagged <code>-tpu<\/code> one.<\/p>\n<p data-block-key=\"4p33p\">And for monitoring, the Ray Dashboard, Ray&#8217;s built-in internet UI for cluster and job state, now exhibits TPU utilization and reminiscence subsequent to CPU and GPU on the Cluster tab, with <code>ray.util.tpu.init_jax_profiler()<\/code> exposing a per-worker JAX profiler the dashboard can connect to.<\/p>\n<\/div>\n<div class=\"inner-block-content\">\n<div class=\"image-wrapper\">\n<p>                <img decoding=\"async\" class=\"regular-image\" src=\"https:\/\/storage.googleapis.com\/gweb-developer-goog-blog-assets\/images\/fig4alt.original.png\" alt=\"fig4alt\"\/><\/p><\/div><\/div>\n<div class=\"inner-block-content rich-content\">\n<h2 data-block-key=\"n6ucs\" id=\"in-summary\"><b>In Abstract<\/b><\/h2>\n<p data-block-key=\"frpef\">On this developer information on Ray on TPU, we lined the entire journey from how Ray runs on TPU to working AI workloads.<\/p>\n<p data-block-key=\"1lf51\"><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/developers.googleblog.com\/run-ray-on-tpu-part-1-the-foundations\/\">Half 1<\/a> confirmed that working Ray on TPU comes down to at least one caveat, preserving a multi-host mannequin on a single intact slice, and that GKE (by the Ray Operator add-on) and Ray Core (by <code>slice_placement_group())<\/code> deal with that for you.<br \/>This half put the AI libraries on prime: Ray Serve gang-schedules a multi-host mannequin onto one slice with a single <code>accelerator_config.topology<\/code> discipline, Ray Knowledge feeds the slice JAX-native batches by <code>iter_jax_batches()<\/code>, and JaxTrainer runs a distributed coaching loop from one ScalingConfig. The identical Ray you already use on GPUs, now on TPU.<\/p>\n<h2 data-block-key=\"b6ipk\" id=\"what's-next\"><b>What&#8217;s subsequent<\/b><\/h2>\n<p data-block-key=\"sjfj\">And extra is coming. The Ray staff on Google Cloud is widening TPU help from <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/discuss.google.dev\/t\/google-cloud-tpus-are-now-a-first-class-accelerator-in-ray\/345281#p-914268-whats-next-in-progress-and-future-work-3\">right here<\/a>: deeper Ray Knowledge and Ray LLM TPU integration, SkyRL on multi-host TPU for reinforcement studying and post-training, and dynamic tremendous\/sub-slice help are all on the roadmap.<br \/>To your personal subsequent step, my advice is: clone the <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/github.com\/GoogleCloudPlatform\/kubernetes-engine-samples\/tree\/main\/ai-ml\/gke-ray\/tpu\/get-started\">get-started instance<\/a>, arise the cluster, then run serve, knowledge, or prepare. Or simply allow <code>--enable-ray-operator<\/code> on a cluster and run one Ray activity on a small slice to see it work. You do not have to develop into a TPU professional to make use of one, simply give it a strive.<br \/>For now, thanks for studying! And when you have any further questions or suggestions, be at liberty to achieve out on socials (<a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/www.linkedin.com\/in\/ivan-nardini\">LinkedIn<\/a>, <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/x.com\/ivnardini\">X<\/a>).<\/p>\n<p data-block-key=\"9lv5m\">Joyful constructing!<\/p>\n<h2 data-block-key=\"9oj0p\" id=\"additional-resources\"><b>Further assets<\/b><\/h2>\n<p data-block-key=\"q8sh\"><i>New right here?<\/i> <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/developers.googleblog.com\/run-ray-on-tpu-part-1-the-foundations\/\"><i>Half 1<\/i><\/a> <i>explains slices, GKE, and Ray Core, the inspiration every part above builds on.<\/i><\/p>\n<\/div><\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>TL;DR: Half 2 of two. Half 1 lined the one {hardware} thought you want and the 2 layers beneath (GKE and Ray Core). This half exhibits the libraries you really construct with, Ray Serve, Ray Knowledge, and Ray Prepare. Recap Fast recap if you happen to&#8217;re touchdown right here first. Operating Ray on TPU comes [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":17123,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[2492,668,736,733,9679],"class_list":["post-17121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-libraries","tag-part","tag-ray","tag-run","tag-tpu"],"_links":{"self":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts\/17121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17121"}],"version-history":[{"count":1,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts\/17121\/revisions"}],"predecessor-version":[{"id":17122,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts\/17121\/revisions\/17122"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/media\/17123"}],"wp:attachment":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<!-- This website is optimized by Airlift. Learn more: https://airlift.net. Template:. Learn more: https://airlift.net. Template: 69d9690a190636c2e0989534. Config Timestamp: 2026-04-10 21:18:02 UTC, Cached Timestamp: 2026-07-27 02:49:40 UTC -->