ã𧚠JAXã䜿çšããCloud TPU v5eã§ã®é«éã§å®å®ããXLæšè«ã®æ¡æ£ãå éããã
ã𧚠JAXã掻çšããCloud TPU v5eã§ã®é«éãã€å®å®ãªXLæšè«æ¡å€§ã®ä¿é²ã
çæAIã¢ãã«ã§ããStable Diffusion XLïŒSDXLïŒãªã©ã¯ãå¹ åºãå¿çšã«ãããŠé«å質ã§ãªã¢ã«ãªã³ã³ãã³ãã®äœæãå¯èœã«ããŸãããããããã®ãããªã¢ãã«ã®åãå©çšããã«ã¯ã倧ããªèª²é¡ãèšç®ã³ã¹ãã䌎ããŸããSDXLã¯ããã®UNetã³ã³ããŒãã³ããã¢ãã«ã®ä»¥åã®ããŒãžã§ã³ã®ãã®ãããçŽ3å倧ãã倧ããªç»åçæã¢ãã«ã§ãããã®ãããªã¢ãã«ãå®çšŒåç°å¢ã«å±éããããšã¯ãå¢å ããã¡ã¢ãªèŠä»¶ãæšè«æéã®å¢å ãªã©ã®çç±ããé£ããã§ããä»æ¥ãç§ãã¡ã¯Hugging Face DiffusersãJAX on Cloud TPUsã䜿çšããŠSDXLããµããŒãããããšãçºè¡šã§ããããšã倧ãã«åãã§ããŸããããã«ãããé«æ§èœã§ã³ã¹ãå¹çã®è¯ãæšè«ãå¯èœã«ãªããŸãã
Google Cloud TPUsã¯ã倧èŠæš¡ãªAIã¢ãã«ã®ãã¬ãŒãã³ã°ãæšè«ãå«ããæå 端ã®LLMsãçæAIã¢ãã«ãªã©ã®ããã«æé©åãããã«ã¹ã¿ã ãã¶ã€ã³ã®AIã¢ã¯ã»ã©ã¬ãŒã¿ã§ããæ°ããCloud TPU v5eã¯ã倧èŠæš¡AIãã¬ãŒãã³ã°ãæšè«ã«å¿ èŠãªã³ã¹ãå¹çãšããã©ãŒãã³ã¹ãæäŸããããç¹å¥ã«èšèšãããŠããŸããTPU v4ã®åå以äžã®ã³ã¹ãã§ãããå€ãã®çµç¹ãAIã¢ãã«ã®ãã¬ãŒãã³ã°ãšå±éãå¯èœã«ãªããŸãã
𧚠Diffusers JAXé£æºã¯ãXLAãä»ããŠTPUäžã§SDXLãå®è¡ãã䟿å©ãªæ¹æ³ãæäŸããŸããããã«å¯Ÿå¿ãããã¢ãäœæããŸããããã®ãã¢ã¯ãæéã®ãããæžåŒå€æãéä¿¡æéãããã³ããšã³ãåŠçãå«ããŠçŽ4ç§ã§4ã€ã®å€§ããª1024Ã1024ã®ç»åãæäŸããããã«è€æ°ã®TPU v5e-4ã€ã³ã¹ã¿ã³ã¹ïŒåã€ã³ã¹ã¿ã³ã¹ã«4ã€ã®TPUãããããããŸãïŒã§å®è¡ãããŸããå®éã®çææéã¯2.3ç§ã§ãã以äžã§è©³ããèŠãŠãããŸãããïŒ
ãã®ããã°èšäºã§ã¯ã
- ã人工ç¥èœã®æé»é¢ã
- ãAIæ代ã«ãããçµç¹ã®äŸ¡å€ãåŒãåºãã
- åçã«ç»åã®ãµã€ãºã調æŽãã
- ãªãJAX + TPU + Diffusersã¯SDXLãå®è¡ããããã®åŒ·åãªãã¬ãŒã ã¯ãŒã¯ãªã®ãã説æããŸãã
- DiffusersãšJAXã䜿çšããŠã·ã³ãã«ãªç»åçæãã€ãã©ã€ã³ãæžãæ¹æ³ã説æããŸãã
- ããŸããŸãªTPUèšå®ãæ¯èŒãããã³ãããŒã¯ã瀺ããŸãã
SDXLã«ã¯ãªãJAX + TPU v5eã䜿ãã®ãïŒ
JAX on Cloud TPU v5eã䜿çšããŠé«æ§èœãã€ã³ã¹ãå¹çã®è¯ãSDXLãæäŸããããšã¯ãç¹å¥ã«èšèšãããTPUããŒããŠã§ã¢ãšããã©ãŒãã³ã¹ã«æé©åããããœãããŠã§ã¢ã¹ã¿ãã¯ã®çµã¿åããã«ãã£ãŠå¯èœã«ãªããŸãã以äžã«2ã€ã®äž»èŠãªèŠçŽ ã瀺ããŸãïŒJAXã®ãžã£ã¹ãã€ã³ã¿ã€ã ïŒjitïŒã³ã³ãã€ã«ãšJAX pmapã«ããXLAã³ã³ãã€ã©é§åã®äžŠåæ§ã
ãžã£ã¹ãã€ã³ã¿ã€ã ïŒjitïŒã³ã³ãã€ã«
JAXã®æ³šç®ãã¹ãç¹åŸŽã®1ã€ã¯ããžã£ã¹ãã€ã³ã¿ã€ã ïŒjitïŒã³ã³ãã€ã«ã§ããJITã³ã³ãã€ã©ã¯ãæåã®å®è¡æã«ã³ãŒãããã¬ãŒã¹ããé«åºŠã«æé©åãããTPUãã€ããªãçæããåŸç¶ã®åŒã³åºãã§åå©çšããŸãããã®ããã»ã¹ã®æ³šæç¹ã¯ãå ¥åãäžéãåºåã®åœ¢ç¶ãéçã§ããå¿ èŠããããŸããã€ãŸããäºãç¥ãããŠããå¿ èŠããããšããããšã§ãã圢ç¶ãå€æŽããããã³ã«ãæ°ããè²»çšã®ãããã³ã³ãã€ã«ããã»ã¹ãåããªã¬ãããŸããJITã³ã³ãã€ã«ã¯ãéç圢ç¶ãåºã«èšèšããããµãŒãã¹ã«æé©ã§ããã³ã³ãã€ã«ã¯äžåºŠå®è¡ããããã®åŸã¯è¶ é«éã®æšè«æéã掻çšããããšãã§ããŸãã
ç»åçæã¯JITã³ã³ãã€ã«ã«é©ããŠããŸããåžžã«åãæ°ã®ç»åãçæããããããåããµã€ãºã§ããã°ãåºå圢ç¶ã¯äžå®ã§ãäºåã«ããã£ãŠããŸããããã¹ãå ¥åãäžå®ã§ããStable DiffusionãšSDXLã¯ããŠãŒã¶ãŒãå ¥åããããã³ãããè¡šãåºå®åœ¢ç¶ã®åã蟌ã¿ãã¯ãã«ïŒããã£ã³ã°ããïŒã䜿çšããŸãããã®ãããåºå®åœ¢ç¶ã«äŸåããJAXã³ãŒããæžãããšãã§ããå€§å¹ ã«æé©åããããšãã§ããŸãïŒ
é«ããããµã€ãºã«å¯Ÿããé«æ§èœã¹ã«ãŒããã
JAXã®pmapã䜿çšããããšã§ãã¯ãŒã¯ããŒããè€æ°ã®ããã€ã¹ã«ã¹ã±ãŒãªã³ã°ããããšãã§ããŸããpmapã¯ãåäžããã°ã©ã è€æ°ããŒã¿ïŒSPMDïŒããã°ã©ã ãè¡šçŸããŸããé¢æ°ã«pmapãé©çšãããšãXLAã§é¢æ°ãã³ã³ãã€ã«ãããããŸããŸãªXLAããã€ã¹äžã§äžŠåã«å®è¡ãããŸããããã¹ãããç»åçæã¯ãŒã¯ããŒãã§ã¯ãåæã«ã¬ã³ããªã³ã°ãããç»åã®æ°ãå¢ããããšã容æã§ãããããã©ãŒãã³ã¹ãæãªãããšã¯ãããŸãããäŸãã°ãTPUäžã§SDXLãå®è¡ããå Žåã8ã€ã®ãããã䜿çšãããšã1ã€ã®ããããåäžã®ç»åãäœæããã®ãšåãæéã§ã8ã€ã®ç»åãçæããããšãã§ããŸãã
TPU v5eã€ã³ã¹ã¿ã³ã¹ã¯ã1ã4ã8ããã圢ç¶ãªã©è€æ°ã®åœ¢ç¶ã§æäŸãããŠãããæ倧256ãããïŒå®å šãªTPU v5eãããïŒãŸã§æ¡åŒµå¯èœã§ããããã«ããããéã§ã®è¶ é«éãªICIãªã³ã¯ãåããŠããŸããããã«ãããJAXãšTPUãæäŸãã䞊ååŠçãå¹æçã«æŽ»çšããªãããæé©ãªTPU圢ç¶ãéžæããããšãå¯èœã§ãã
JAXã§ç»åçæã®ãã€ãã©ã€ã³ãäœæããæ¹æ³
JAXã䜿çšããŠæšè«ãè¶ é«éã§å®è¡ããããã«å¿ èŠãªã³ãŒããã¹ããããã€ã¹ãããã§èª¬æããŸãïŒæåã«ãå¿ èŠãªäŸåé¢ä¿ãã€ã³ããŒãããŸãããã
# SDXL JAXã®ãã¹ããã©ã¯ãã£ã¹ã衚瀺ããimport jaximport jax.numpy as jnpimport numpy as npfrom flax.jax_utils import replicatefrom diffusers import FlaxStableDiffusionXLPipelineimport time
次ã«ãåºæ¬ã®SDXLã¢ãã«ãšæšè«ã«å¿ èŠãªä»ã®ã³ã³ããŒãã³ããããŒãããŸããdiffusersãã€ãã©ã€ã³ã¯ããã¹ãŠãããŠã³ããŒãããã£ãã·ã¥ããåŠçãè«ãè² ã£ãŠããŸããJAXã®é¢æ°åã¢ãããŒãã«åŸããããã¢ãã«ã®ãã©ã¡ãŒã¿ã¯å¥ã ã«è¿ãããæšè«æã«ãã€ãã©ã€ã³ã«æž¡ãå¿ èŠããããŸãïŒ
pipelineãparams = FlaxStableDiffusionXLPipeline.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", split_head_dim=True)
ã¢ãã«ãã©ã¡ãŒã¿ã¯ããã©ã«ãã§32ããã粟床ã§ããŠã³ããŒããããŸããã¡ã¢ãªãç¯çŽãèšç®ãé«éåããããã«ãããããå¹ççãª16ãããè¡šçŸã§ããbfloat16
ã«å€æããŸãããã ãã泚æç¹ããããŸããæè¯ã®çµæãåŸãããã«ãã¹ã±ãžã¥ãŒã©ã®ç¶æ
ãfloat32
ã®ãŸãŸã«ããŠãããªããã°ãªããŸãããããããªããšã粟床ã®ãšã©ãŒãèç©ãããäœå質ãŸãã¯é»ãç»åã«ãªãå¯èœæ§ããããŸãã
scheduler_state = params.pop("scheduler")params = jax.tree_util.tree_map(lambda x: x.astype(jnp.bfloat16), params)params["scheduler"] = scheduler_state
ããã§ãããã³ãããšãã€ãã©ã€ã³ã®ä»ã®å ¥åãèšå®ããæºåãæŽããŸããã
default_prompt = "é«å質ã®åçïŒããŒã«ã§éã¶èµ€ã¡ããã€ã«ã«ã«ããŒãã£ãŒãããã被ãã"default_neg_prompt = "ã€ã©ã¹ããäœå質"default_seed = 33default_guidance_scale = 5.0default_num_steps = 25
ããã³ããã¯ãã³ãœã«ãšããŠãã€ãã©ã€ã³ã«äŸçµŠããå¿
èŠããããåŒã³åºãããšã«åã次å
ãæã£ãŠããªããã°ãªããŸãããããã«ãããæšè«åŒã³åºããã³ã³ãã€ã«ããããšãã§ããŸãããã€ãã©ã€ã³ã®prepare_inputs
ã¡ãœããã¯ããããã®æé ããã¹ãŠå®è¡ããŠããããããããã³ãããšãã¬ãã£ãããã³ããã®äž¡æ¹ããã³ãœã«ãšããŠæºåããããã®ãã«ããŒé¢æ°ãäœæããŸããåŸã§generate
é¢æ°ãã䜿çšããŸãïŒ
def tokenize_prompt(prompt, neg_prompt): prompt_ids = pipeline.prepare_inputs(prompt) neg_prompt_ids = pipeline.prepare_inputs(neg_prompt) return prompt_ids, neg_prompt_ids
䞊ååã掻çšããããã«ãå ¥åãããã€ã¹å šäœã§è€è£œããŸããCloud TPU v5e-4ã¯4ã€ã®ããããæã€ãããå ¥åãè€è£œããããšã§ãããããã®ãããã«ç°ãªãç»åã䞊åã«çæããããšãã§ããŸããåãããã«ç°ãªãã©ã³ãã ã·ãŒããæäŸããå¿ èŠãããããã泚æãå¿ èŠã§ãã
NUM_DEVICES = jax.device_count()# ã¢ãã«ãã©ã¡ãŒã¿ã¯æšè«äžã«å€ãããªããããäžåºŠã ãè€è£œããã°ååã§ããp_params = replicate(params)def replicate_all(prompt_ids, neg_prompt_ids, seed): p_prompt_ids = replicate(prompt_ids) p_neg_prompt_ids = replicate(neg_prompt_ids) rng = jax.random.PRNGKey(seed) rng = jax.random.split(rng, NUM_DEVICES) return p_prompt_ids, p_neg_prompt_ids, rng
ããã§ããã¹ãŠãå«ãã generateé¢æ°ãçµã¿ç«ãŠãæºåãæŽããŸããïŒ
def generate( prompt, negative_prompt, seed=default_seed, guidance_scale=default_guidance_scale, num_inference_steps=default_num_steps,): prompt_ids, neg_prompt_ids = tokenize_prompt(prompt, negative_prompt) prompt_ids, neg_prompt_ids, rng = replicate_all(prompt_ids, neg_prompt_ids, seed) images = pipeline( prompt_ids, p_params, rng, num_inference_steps=num_inference_steps, neg_prompt_ids=neg_prompt_ids, guidance_scale=guidance_scale, jit=True, ).images # ç»åãPIL圢åŒã«å€æãã images = images.reshape((images.shape[0] * images.shape[1], ) + images.shape[-3:]) return pipeline.numpy_to_pil(np.array(images))
jit=True
ã¯ããã€ãã©ã€ã³åŒã³åºããã³ã³ãã€ã«ããããšã瀺ããŸããããã«ã¯ãæåã« generate
ãåŒã³åºããšãã«è¡ãããŸãããéåžžã«é
ããªããŸããJAXã¯æäœããã¬ãŒã¹ããæé©åããäœã¬ãã«ã®ããªããã£ãã«å€æããå¿
èŠããããŸããããã»ã¹ãå®äºããå®è¡ãéå§ããããã«æåã®çæãå®è¡ããŸãã
start = time.time()print(f"ã³ã³ãã€ã«äž...")generate(default_prompt, default_neg_prompt)print(f"ã³ã³ãã€ã«ã«ããã£ãæéïŒ{time.time() - start}")
æåã«å®è¡ãããšãã«çŽ3åããããŸããããã ããã³ãŒããã³ã³ãã€ã«ãããããæšè«ã¯éåžžã«é«éã«ãªããŸããããäžåºŠè©ŠããŠã¿ãŸãããïŒ
start = time.time()prompt = "llama in ancient Greece, oil on canvas"neg_prompt = "cartoon, illustration, animation"images = generate(prompt, neg_prompt)print(f"æšè«ã«ããã£ãæéïŒ{time.time() - start}")
4ã€ã®ç»åãçæããã®ã«çŽ2ç§ããããŸããïŒ
ãã³ãããŒã¯
以äžã®æ°å€ã¯ãããã©ã«ãã® Euler Discrete ã¹ã±ãžã¥ãŒã©ã䜿çšããŠãSDXL 1.0 baseã20ã¹ãããå®è¡ããçµæã§ããåãããããµã€ãºã§ Cloud TPU v5e ãš TPUv4 ãæ¯èŒããŠããŸãããªãã䞊ååŠçã®ããããã¢ã§äœ¿çšããŠãã TPU v5e-4 ããã€ã¹ã¯ãããããµã€ãºã 1 ã®å Žåã«ã¯4ã€ã®ç»åãçæããŸã (ããããµã€ãºã 2 ã®å Žåã«ã¯8ã€ã®ç»å)ãåæ§ã«ãTPU v5e-8 ã¯ããããµã€ãºã 1 ã®å Žåã«ã¯ 8 ã€ã®ç»åãçæããŸãã
Cloud TPU ã®ãã¹ã㯠Python 3.10 ããã³ jax ããŒãžã§ã³ 0.4.16 ã䜿çšããŠå®è¡ãããŸãããããã¯ç§ãã¡ã® ã㢠Space ã§äœ¿çšãããä»æ§ãšåãã§ãã
TPU v5e ã¯ãTPU v4 ã«æ¯ã¹ãŠ SDXL äžã§æ倧㧠2.4 åã®ããã©ãŒãã³ã¹/ãã«ãå®çŸããææ°ã® TPU äžä»£ã®ã³ã¹ãå¹çã®è¯ãã瀺ããŠããŸãã
æšè«ããã©ãŒãã³ã¹ã枬ãããã«ãã¹ã«ãŒããããšããæ¥çæšæºã®ã¡ããªã¯ã¹ã䜿çšããŸãããŸããã³ã³ãã€ã«ããã³èªã¿èŸŒã¿æã®ç»åããšã®ã¬ã€ãã³ã·ã枬å®ããŸãã次ã«ãããããµã€ãºããããããšã®ã¬ã€ãã³ã·ã§é€ç®ããŠã¹ã«ãŒããããèšç®ããŸãããã®çµæãã¹ã«ãŒãããã¯ã䜿çšããããããã®æ°ã«é¢ä¿ãªããã¢ãã«ã補åç°å¢ã§ã®å®è¡æ§èœãèšæž¬ãããã®ã§ãããããŠãã¹ã«ãŒãããããªã¹ãäŸ¡æ Œã§é€ç®ããŠãã«ãããã®ããã©ãŒãã³ã¹ãèšç®ããŸãã
ãã¢ã¯ã©ã®ããã«åäœããŸããïŒ
åè¿°ã® ã㢠ã¯ããã®ããã°èšäºã«èšèŒãããŠããã³ãŒãã«åºã¥ããŠæ§ç¯ããããã®ã§ããããã€ãã® Cloud TPU v5e ããã€ã¹ (åããã€ã¹ã«ã¯4ã€ã®ãããããããŸã) äžã§å®è¡ããããŠãŒã¶ãŒãªã¯ãšã¹ããããã¯ãšã³ããµãŒããŒã«ã©ã³ãã ã«ã«ãŒãã£ã³ã°ããã·ã³ãã«ãªè² è·åæ£ãµãŒããŒããããŸãããã¢ã§ããã³ãããå ¥åãããšããªã¯ãšã¹ãã¯ããã¯ãšã³ããµãŒããŒã®1ã€ã«å²ãåœãŠãããçæããã4ã€ã®ç»åãåãåããŸãã
ããã¯ãããã€ãã®äºåå²ãåœãŠããã TPU ã€ã³ã¹ã¿ã³ã¹ãåºã«ããã·ã³ãã«ãªãœãªã¥ãŒã·ã§ã³ã§ããå°æ¥ã®æçš¿ã§ã¯ãGKE ã䜿çšããŠè² è·ã«é©å¿ããåçãªãœãªã¥ãŒã·ã§ã³ã®äœææ¹æ³ã«ã€ããŠèª¬æããŸãã
ãã¢ã®ã³ãŒãã¯ãã¹ãŠãªãŒãã³ãœãŒã¹ã§ãããHugging Face Diffusers ã§æäŸãããŠããŸããDiffusers + JAX + Cloud TPUs ã䜿çšããŠäœãæ§ç¯ãããã楜ãã¿ã«ããŠããŸãïŒ
We will continue to update VoAGI; if you have any questions or suggestions, please contact us!
Was this article helpful?
93 out of 132 found this helpful
Related articles
- ãã¢ãã®ã€ã³ã¿ãŒããããããããã¹ãŠã®ã€ã³ã¿ãŒãããããžïŒAIãš6Gã®èåã«ããã€ãªããç¥æ§
- ããããžã£ãŒããŒV5ïŒããããžã£ãŒããŒã®ææ°ããŒãžã§ã³
- ãCG ã®ãŒã¯ã VFX ã楜ã ã«äœããŸã ä»é±ã® ‘NVIDIA Studio’ã
- ãåå©ãžã®Vå ãµã€ããŒãã³ã¯2077 ãã¡ã³ãã ãªããã£ããGeForce NOWã«ç»å Žã
- Diginiã®ã¹ããŒãã»ã³ã¹ã®ç€Ÿé·ãã¬ã€ã»ã€ãšãã¢ãã«ããã€ã³ã¿ãã¥ãŒã·ãªãŒãº
- ãNon-engineers guide LLaMA 2ãã£ãããããã®ãã¬ãŒãã³ã°ããšãªããŸã
- ãã£ãããã³ãã¬ãŒãïŒéããªããã©ãŒãã³ã¹ãã©ãŒãžã®çµæ¢ç¬Š