Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      10 Benefits of Hiring a React.js Development Company (2025–2026 Edition)

      August 13, 2025

      From Line To Layout: How Past Experiences Shape Your Design Career

      August 13, 2025

      Hire React.js Developers in the US: How to Choose the Right Team for Your Needs

      August 13, 2025

      Google’s coding agent Jules gets critique functionality

      August 13, 2025

      The best smartphones without AI features in 2025: Expert tested and recommended

      August 13, 2025

      GPT-5 was supposed to simplify ChatGPT but now it has 4 new modes – here’s why

      August 13, 2025

      Gemini just got two of ChatGPT’s best features – and they’re free

      August 13, 2025

      I found the easiest way to send files between my Android phone and desktop – and it’s free

      August 13, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Laravel Boost is released

      August 13, 2025
      Recent

      Laravel Boost is released

      August 13, 2025

      Frontend Standards for Optimizely Configured Commerce: Clean & Scalable Web Best Practices

      August 13, 2025

      Live Agent Escalation in Copilot Studio Using D365 Omnichannel – Architecture and Use Case

      August 13, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      OpenAI’s Sam Altman: GPT-5 fails to meet AGI standards amid Microsoft’s fading partnership — “it’s still missing something”

      August 13, 2025
      Recent

      OpenAI’s Sam Altman: GPT-5 fails to meet AGI standards amid Microsoft’s fading partnership — “it’s still missing something”

      August 13, 2025

      You Think You Need a Monster PC to Run Local AI, Don’t You? — My Seven-Year-Old Mid-range Laptop Says Otherwise

      August 13, 2025

      8 Registry Tweaks that will Make File Explorer Faster and Easier to Use on Windows 11

      August 13, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Machine Learning»Cost-effective AI image generation with PixArt-Σ inference on AWS Trainium and AWS Inferentia

    Cost-effective AI image generation with PixArt-Σ inference on AWS Trainium and AWS Inferentia

    May 14, 2025

    PixArt-Sigma is a diffusion transformer model that is capable of image generation at 4k resolution. This model shows significant improvements over previous generation PixArt models like Pixart-Alpha and other diffusion models through dataset and architectural improvements. AWS Trainium and AWS Inferentia are purpose-built AI chips to accelerate machine learning (ML) workloads, making them ideal for cost-effective deployment of large generative models. By using these AI chips, you can achieve optimal performance and efficiency when running inference with diffusion transformer models like PixArt-Sigma.

    This post is the first in a series where we will run multiple diffusion transformers on Trainium and Inferentia-powered instances. In this post, we show how you can deploy PixArt-Sigma to Trainium and Inferentia-powered instances.

    Solution overview

    The steps outlined below will be used to deploy the PixArt-Sigma model on AWS Trainium and run inference on it to generate high-quality images.

    • Step 1 – Pre-requisites and setup
    • Step 2 – Download and compile the PixArt-Sigma model for AWS Trainium
    • Step 3 – Deploy the model on AWS Trainium to generate images

    Step 1 – Prerequisites and setup

    To get started, you will need to set up a development environment on a trn1, trn2, or inf2 host. Complete the following steps:

    1. Launch a trn1.32xlarge or trn2.48xlarge instance with a Neuron DLAMI. For instructions on how to get started, refer to Get Started with Neuron on Ubuntu 22 with Neuron Multi-Framework DLAMI.
    2. Launch a Jupyter Notebook sever. For instructions to set up a Jupyter server, refer to the following user guide.
    3. Clone the aws-neuron-samples GitHub repository:
      git clone https://github.com/aws-neuron/aws-neuron-samples.git
    4. Navigate to the hf_pretrained_pixart_sigma_1k_latency_optimized.ipynb notebook:
      cd aws-neuron-samples/torch-neuronx/inference

    The provided example script is designed to run on a Trn2 instance, but you can adapt it for Trn1 or Inf2 instances with minimal modifications. Specifically, within the notebook and in each of the component files under the neuron_pixart_sigma directory, you will find commented-out changes to accommodate Trn1 or Inf2 configurations.

    Step 2 – Download and compile the PixArt-Sigma model for AWS Trainium

    This section provides a step-by-step guide to compiling PixArt-Sigma for AWS Trainium.

    Download the model

    You will find a helper function in cache-hf-model.py in above mentioned GitHub repository that shows how to download the PixArt-Sigma model from Hugging Face. If you are using PixArt-Sigma in your own workload, and opt not to use the script included in this post, you can use the huggingface-cli to download the model instead.

    The Neuron PixArt-Sigma implementation contains a few scripts and classes. The various files and scrips are broken down as follows:

    ├── compile_latency_optimized.sh # Full Model Compilation script for Latency Optimized
    ├── compile_throughput_optimized.sh # Full Model Compilation script for Throughput Optimized
    ├── hf_pretrained_pixart_sigma_1k_latency_optimized.ipynb # Notebook to run Latency Optimized Pixart-Sigma
    ├── hf_pretrained_pixart_sigma_1k_throughput_optimized.ipynb # Notebook to run Throughput Optimized Pixart-Sigma
    ├── neuron_pixart_sigma
    │ ├── cache_hf_model.py # Model downloading Script
    │ ├── compile_decoder.py # Text Encoder Compilation Script and Wrapper Class
    │ ├── compile_text_encoder.py # Text Encoder Compilation Script and Wrapper Class
    │ ├── compile_transformer_latency_optimized.py # Latency Optimized Transformer Compilation Script and Wrapper Class
    │ ├── compile_transformer_throughput_optimized.py # Throughput Optimized Transformer Compilation Script and Wrapper Class
    │ ├── neuron_commons.py # Base Classes and Attention Implementation
    │ └── neuron_parallel_utils.py # Sharded Attention Implementation
    └── requirements.txt

    This notebook will help you to download the model, compile the individual component models, and invoke the generation pipeline to generate an image. Although the notebooks can be run as a standalone sample, the next few sections of this post will walk through the key implementation details within the component files and scripts to support running PixArt-Sigma on Neuron.

    Sharding PixArt linear layers

    For each component of PixArt (T5, Transformer, and VAE), the example uses Neuron specific wrapper classes. These wrapper classes serve two purposes. The first purpose is it allows us to trace the models for compilation:

    class InferenceTextEncoderWrapper(nn.Module):
        def __init__(self, dtype, t: T5EncoderModel, seqlen: int):
            super().__init__()
            self.dtype = dtype
            self.device = t.device
            self.t = t
        def forward(self, text_input_ids, attention_mask=None):
            return [self.t(text_input_ids, attention_mask)['last_hidden_state'].to(self.dtype)]
    

    Please refer to the neuron_commons.py file for all wrapper modules and classes.

    The second reason for using wrapper classes is to modify the attention implementation to run on Neuron. Because diffusion models like PixArt are typically compute-bound, you can improve performance by sharding the attention layer across multiple devices. To do this, you replace the linear layers with NeuronX Distributed’s RowParallelLinear and ColumnParallelLinear layers:

    def shard_t5_self_attention(tp_degree: int, selfAttention: T5Attention):
        orig_inner_dim = selfAttention.q.out_features
        dim_head = orig_inner_dim // selfAttention.n_heads
        original_nheads = selfAttention.n_heads
        selfAttention.n_heads = selfAttention.n_heads // tp_degree
        selfAttention.inner_dim = dim_head * selfAttention.n_heads
        orig_q = selfAttention.q
        selfAttention.q = ColumnParallelLinear(
            selfAttention.q.in_features,
            selfAttention.q.out_features,
            bias=False, 
            gather_output=False)
        selfAttention.q.weight.data = get_sharded_data(orig_q.weight.data, 0)
        del(orig_q)
        orig_k = selfAttention.k
        selfAttention.k = ColumnParallelLinear(
            selfAttention.k.in_features, 
            selfAttention.k.out_features, 
            bias=(selfAttention.k.bias is not None),
            gather_output=False)
        selfAttention.k.weight.data = get_sharded_data(orig_k.weight.data, 0)
        del(orig_k)
        orig_v = selfAttention.v
        selfAttention.v = ColumnParallelLinear(
            selfAttention.v.in_features, 
            selfAttention.v.out_features, 
            bias=(selfAttention.v.bias is not None),
            gather_output=False)
        selfAttention.v.weight.data = get_sharded_data(orig_v.weight.data, 0)
        del(orig_v)
        orig_out = selfAttention.o
        selfAttention.o = RowParallelLinear(
            selfAttention.o.in_features,
            selfAttention.o.out_features,
            bias=(selfAttention.o.bias is not None),
            input_is_parallel=True)
        selfAttention.o.weight.data = get_sharded_data(orig_out.weight.data, 1)
        del(orig_out)
        return selfAttention
    

    Please refer to the neuron_parallel_utils.py file for more details on parallel attention.

    Compile individual sub-models

    The PixArt-Sigma model is composed of three components. Each component is compiled so the entire generation pipeline can run on Neuron:

    • Text encoder – A 4-billion-parameter encoder, which translates a human-readable prompt into an embedding. In the text encoder, the attention layers are sharded, along with the feed-forward layers, with tensor parallelism.
    • Denoising transformer model – A 700-million-parameter transformer, which iteratively denoises a latent (a numerical representation of a compressed image). In the transformer, the attention layers are sharded, along with the feed-forward layers, with tensor parallelism.
    • Decoder – A VAE decoder that converts our denoiser-generated latent to an output image. For the decoder, the model is deployed with data parallelism.

    Now that the model definition is ready, you need to trace a model to run it on Trainium or Inferentia. You can see how to use the trace() function to compile the decoder component model for PixArt in the following code block:

    compiled_decoder = torch_neuronx.trace(
        decoder,
        sample_inputs,
        compiler_workdir=f"{compiler_workdir}/decoder",
        compiler_args=compiler_flags,
        inline_weights_to_neff=False
    )
    

    Please refer to the compile_decoder.py file for more on how to instantiate and compile the decoder.

    To run models with tensor parallelism, a technique used to split a tensor into chunks across multiple NeuronCores, you need to trace with a pre-specified tp_degree. This tp_degree specifies the number of NeuronCores to shard the model across. It then uses the parallel_model_trace API to compile the encoder and transformer component models for PixArt:

    compiled_text_encoder = neuronx_distributed.trace.parallel_model_trace(
        get_text_encoder_f,
        sample_inputs,
        compiler_workdir=f"{compiler_workdir}/text_encoder",
        compiler_args=compiler_flags,
        tp_degree=tp_degree,
    )
    

    Please refer to the compile_text_encoder.py file for more details on tracing the encoder with tensor parallelism.

    Lastly, you trace the transformer model with tensor parallelism:

    compiled_transformer = neuronx_distributed.trace.parallel_model_trace(
        get_transformer_model_f,
        sample_inputs,
        compiler_workdir=f"{compiler_workdir}/transformer",
        compiler_args=compiler_flags,
        tp_degree=tp_degree,
        inline_weights_to_neff=False,
    )
    

    Please refer to the compile_transformer_latency_optimized.py file for more details on tracing the transformer with tensor parallelism.

    You will use the compile_latency_optimized.sh script to compile all three models as described in this post, so these functions will be run automatically when you run through the notebook.

    Step 3 – Deploy the model on AWS Trainium to generate images

    This section will walk us through the steps to run inference on PixArt-Sigma on AWS Trainium.

    Create a diffusers pipeline object

    The Hugging Face diffusers library is a library for pre-trained diffusion models, and includes model-specific pipelines that bundle the components (independently-trained models, schedulers, and processors) needed to run a diffusion model. The PixArtSigmaPipeline is specific to the PixArtSigma model, and is instantiated as follows:

    pipe: PixArtSigmaPipeline = PixArtSigmaPipeline.from_pretrained(
        "PixArt-alpha/PixArt-Sigma-XL-2-1024-MS",
        torch_dtype=torch.bfloat16,
        local_files_only=True,
        cache_dir="pixart_sigma_hf_cache_dir_1024")
    

    Please refer to the hf_pretrained_pixart_sigma_1k_latency_optimized.ipynb notebook for details on pipeline execution.

    Load compiled component models into the generation pipeline

    After each component model has been compiled, load them into the overall generation pipeline for image generation. The VAE model is loaded with data parallelism, which allows us to parallelize image generation for batch size or multiple images per prompt. For more details, refer to the hf_pretrained_pixart_sigma_1k_latency_optimized.ipynb notebook.

    vae_decoder_wrapper.model = torch_neuronx.DataParallel( 
        torch.jit.load(decoder_model_path), [0, 1, 2, 3], False
    )
    
    text_encoder_wrapper.t = neuronx_distributed.trace.parallel_model_load(
        text_encoder_model_path
    )
    

    Finally, the loaded models are added to the generation pipeline:

    pipe.text_encoder = text_encoder_wrapper
    pipe.transformer = transformer_wrapper
    pipe.vae.decoder = vae_decoder_wrapper
    pipe.vae.post_quant_conv = vae_post_quant_conv_wrapper
    

    Compose a prompt

    Now that the model is ready, you can write a prompt to convey what kind of image you want generated. When creating a prompt, you should always be as specific as possible. You can use a positive prompt to convey what is wanted in your new image, including a subject, action, style, and location, and can use a negative prompt to indicate features that should be removed.

    For example, you can use the following positive and negative prompts to generate a photo of an astronaut riding a horse on mars without mountains:

    # Subject: astronaut
    # Action: riding a horse
    # Location: Mars
    # Style: photo
    prompt = "a photo of an astronaut riding a horse on mars"
    negative_prompt = "mountains"
    

    Feel free to edit the prompt in your notebook using prompt engineering to generate an image of your choosing.

    Generate an image

    To generate an image, you pass the prompt to the PixArt model pipeline, and then save the generated image for later reference:

    # pipe: variable holding the Pixart generation pipeline with each of 
    # the compiled component models
    images = pipe(
            prompt=prompt,
            negative_prompt=negative_prompt,
            num_images_per_prompt=1,
            height=1024, # number of pixels
            width=1024, # number of pixels
            num_inference_steps=25 # Number of passes through the denoising model
        ).images
        
        for idx, img in enumerate(images): 
            img.save(f"image_{idx}.png")
    

    Cleanup

    To avoid incurring additional costs, stop your EC2 instance using either the AWS Management Console or AWS Command Line Interface (AWS CLI).

    Conclusion

    In this post, we walked through how to deploy PixArt-Sigma, a state-of-the-art diffusion transformer, on Trainium instances. This post is the first in a series focused on running diffusion transformers for different generation tasks on Neuron. To learn more about running diffusion transformers models with Neuron, refer to Diffusion Transformers.


    About the Authors

    Achintya Pinninti is a Solutions Architect at Amazon Web Services. He supports public sector customers, enabling them to achieve their objectives using the cloud. He specializes in building data and machine learning solutions to solve complex problems.

    Miriam Lebowitz is a Solutions Architect focused on empowering early-stage startups at AWS. She leverages her experience with AI/ML to guide companies to select and implement the right technologies for their business objectives, setting them up for scalable growth and innovation in the competitive startup world.

    Sadaf Rasool is a Solutions Architect in Annapurna Labs at AWS. Sadaf collaborates with customers to design machine learning solutions that address their critical business challenges. He helps customers train and deploy machine learning models leveraging AWS Trainium or AWS Inferentia chips to accelerate their innovation journey.

    John Gray is a Solutions Architect in Annapurna Labs, AWS, based out of Seattle. In this role, John works with customers on their AI and machine learning use cases, architects solutions to cost-effectively solve their business problems, and helps them build a scalable prototype using AWS AI chips.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleMeta AI Introduces CATransformers: A Carbon-Aware Machine Learning Framework to Co-Optimize AI Models and Hardware for Sustainable Edge Deployment
    Next Article Customize DeepSeek-R1 671b model using Amazon SageMaker HyperPod recipes – Part 2

    Related Posts

    Machine Learning

    How to Evaluate Jailbreak Methods: A Case Study with the StrongREJECT Benchmark

    August 13, 2025
    Machine Learning

    Nebius AI Advances Open-Weight LLMs Through Reinforcement Learning for Capable SWE Agents

    August 13, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Rilasciato PeaZip 10.4: Miglioramenti nell’interfaccia e gestione degli errori

    Linux

    CVE-2025-4602 – eMagicOne Store Manager for WooCommerce WordPress Arbitrary File Read Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Xbox Game Pass gets Blue Prince, South of Midnight, another Blizzard Entertainment game, and more

    News & Updates

    This AI Breakthrough Could Put the Same Brain in Every Robot

    News & Updates

    Highlights

    News & Updates

    Windows 10 is 10 years old today — let’s look back at 10 controversial and defining moments in its history

    July 29, 2025

    From modern app platforms to forced feature upgrades and telemetry concerns, we take a look…

    From Accountant to Data Engineer with Alyson La [Podcast #168]

    April 12, 2025

    CVE-2025-49792 – Apache HTTP Server Cross-Site Request Forgery

    June 11, 2025

    Microsoft shares rare look at radical Windows 11 Start menu designs it explored before settling on the least interesting one of the bunch

    May 13, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.