# PixArt

Generate images quickly with PixArt-Alpha and PixArt-Sigma.

{% hint style="success" %}
All examples can be run on GPU servers rented through [CLORE.AI Marketplace](https://clore.ai/marketplace).
{% endhint %}

## Renting on CLORE.AI

1. Visit [CLORE.AI Marketplace](https://clore.ai/marketplace)
2. Filter by GPU type, VRAM, and price
3. Choose **On-Demand** (fixed rate) or **Spot** (bid price)
4. Configure your order:
   * Select Docker image
   * Set ports (TCP for SSH, HTTP for web UIs)
   * Add environment variables if needed
   * Enter startup command
5. Select payment: **CLORE**, **BTC**, or **USDT/USDC**
6. Create order and wait for deployment

### Access Your Server

* Find connection details in **My Orders**
* Web interfaces: Use the HTTP port URL
* SSH: `ssh -p <port> root@<proxy-address>`

## What is PixArt?

PixArt models offer:

* 10x faster than SDXL
* High-quality 1024px images
* Strong text rendering
* Efficient training methods

## Model Variants

| Model        | Quality | Speed  | VRAM |
| ------------ | ------- | ------ | ---- |
| PixArt-Alpha | Great   | Fast   | 8GB  |
| PixArt-Sigma | Best    | Medium | 12GB |

## Quick Deploy

**Docker Image:**

```
pytorch/pytorch:2.5.1-cuda12.4-cudnn9-devel
```

**Ports:**

```
22/tcp
7860/http
```

**Command:**

```bash
pip install diffusers transformers accelerate gradio && \
python -c "
import gradio as gr
from diffusers import PixArtAlphaPipeline
import torch

pipe = PixArtAlphaPipeline.from_pretrained('PixArt-alpha/PixArt-XL-2-1024-MS', torch_dtype=torch.float16)
pipe.to('cuda')

def generate(prompt, steps):
    image = pipe(prompt, num_inference_steps=steps).images[0]
    return image

demo = gr.Interface(fn=generate, inputs=[gr.Textbox(), gr.Slider(10, 50, 20)], outputs=gr.Image(), title='PixArt')
demo.launch(server_name='0.0.0.0', server_port=7860)
"
```

## Accessing Your Service

After deployment, find your `http_pub` URL in **My Orders**:

1. Go to **My Orders** page
2. Click on your order
3. Find the `http_pub` URL (e.g., `abc123.clorecloud.net`)

Use `https://YOUR_HTTP_PUB_URL` instead of `localhost` in examples below.

## Installation

```bash
pip install diffusers transformers accelerate
```

## PixArt-Alpha

### Basic Generation

```python
from diffusers import PixArtAlphaPipeline
import torch

pipe = PixArtAlphaPipeline.from_pretrained(
    "PixArt-alpha/PixArt-XL-2-1024-MS",
    torch_dtype=torch.float16
)
pipe.to("cuda")

prompt = "A cat astronaut floating in space, Earth in background, photorealistic"

image = pipe(
    prompt=prompt,
    num_inference_steps=20,
    guidance_scale=4.5
).images[0]

image.save("output.png")
```

### Generation Parameters

```python
image = pipe(
    prompt="a beautiful sunset over mountains",
    negative_prompt="blurry, low quality",
    num_inference_steps=20,      # Quality (10-50)
    guidance_scale=4.5,          # Prompt adherence (3-7)
    height=1024,
    width=1024,
    generator=torch.Generator("cuda").manual_seed(42)
).images[0]
```

## PixArt-Sigma

Higher quality version:

```python
from diffusers import PixArtSigmaPipeline
import torch

pipe = PixArtSigmaPipeline.from_pretrained(
    "PixArt-alpha/PixArt-Sigma-XL-2-1024-MS",
    torch_dtype=torch.float16
)
pipe.to("cuda")
pipe.enable_model_cpu_offload()

image = pipe(
    prompt="a professional photograph of a red sports car",
    num_inference_steps=30,
    guidance_scale=4.5
).images[0]

image.save("sigma_output.png")
```

## Memory Optimization

### For 8GB VRAM

```python
pipe = PixArtAlphaPipeline.from_pretrained(
    "PixArt-alpha/PixArt-XL-2-1024-MS",
    torch_dtype=torch.float16
)

# CPU offload
pipe.enable_model_cpu_offload()

# Sequential CPU offload (more aggressive)

# pipe.enable_sequential_cpu_offload()
```

### Enable VAE Slicing

```python
pipe.enable_vae_slicing()
pipe.enable_vae_tiling()
```

## Batch Generation

```python
from diffusers import PixArtAlphaPipeline
import torch

pipe = PixArtAlphaPipeline.from_pretrained(
    "PixArt-alpha/PixArt-XL-2-1024-MS",
    torch_dtype=torch.float16
).to("cuda")

prompts = [
    "a cyberpunk city at night",
    "a peaceful japanese garden",
    "a fantasy castle on a cliff",
    "an underwater coral reef"
]

for i, prompt in enumerate(prompts):
    image = pipe(prompt, num_inference_steps=20).images[0]
    image.save(f"output_{i:03d}.png")
    print(f"Generated: {prompt[:50]}...")
```

## Different Resolutions

```python

# Supported resolutions
resolutions = [
    (512, 512),
    (768, 768),
    (1024, 1024),
    (1024, 512),   # Landscape
    (512, 1024),   # Portrait
    (768, 1024),
    (1024, 768),
]

for w, h in resolutions:
    image = pipe(
        prompt="a beautiful landscape",
        width=w,
        height=h,
        num_inference_steps=20
    ).images[0]

    image.save(f"output_{w}x{h}.png")
```

## Text Rendering

PixArt excels at text in images:

```python
prompt = """
A vintage movie poster with the title "COSMIC ADVENTURE" in bold letters,
featuring a spaceship and planets, retro 1950s style
"""

image = pipe(
    prompt=prompt,
    num_inference_steps=30,
    guidance_scale=5.0
).images[0]
```

## Gradio Interface

```python
import gradio as gr
from diffusers import PixArtAlphaPipeline
import torch

pipe = PixArtAlphaPipeline.from_pretrained(
    "PixArt-alpha/PixArt-XL-2-1024-MS",
    torch_dtype=torch.float16
).to("cuda")

def generate(prompt, negative_prompt, steps, guidance, width, height, seed):
    generator = torch.Generator("cuda").manual_seed(seed) if seed > 0 else None

    image = pipe(
        prompt=prompt,
        negative_prompt=negative_prompt,
        num_inference_steps=steps,
        guidance_scale=guidance,
        width=width,
        height=height,
        generator=generator
    ).images[0]

    return image

demo = gr.Interface(
    fn=generate,
    inputs=[
        gr.Textbox(label="Prompt", lines=3),
        gr.Textbox(label="Negative Prompt"),
        gr.Slider(10, 50, value=20, step=1, label="Steps"),
        gr.Slider(1, 10, value=4.5, step=0.5, label="Guidance"),
        gr.Slider(512, 1024, value=1024, step=64, label="Width"),
        gr.Slider(512, 1024, value=1024, step=64, label="Height"),
        gr.Number(value=-1, label="Seed (-1 for random)")
    ],
    outputs=gr.Image(label="Generated Image"),
    title="PixArt Image Generator"
)

demo.launch(server_name="0.0.0.0", server_port=7860)
```

## API Server

```python
from fastapi import FastAPI
from fastapi.responses import Response
from diffusers import PixArtAlphaPipeline
import torch
import io

app = FastAPI()

pipe = PixArtAlphaPipeline.from_pretrained(
    "PixArt-alpha/PixArt-XL-2-1024-MS",
    torch_dtype=torch.float16
).to("cuda")

@app.post("/generate")
async def generate(
    prompt: str,
    negative_prompt: str = "",
    steps: int = 20,
    guidance: float = 4.5,
    width: int = 1024,
    height: int = 1024
):
    image = pipe(
        prompt=prompt,
        negative_prompt=negative_prompt,
        num_inference_steps=steps,
        guidance_scale=guidance,
        width=width,
        height=height
    ).images[0]

    buffer = io.BytesIO()
    image.save(buffer, format="PNG")
    return Response(content=buffer.getvalue(), media_type="image/png")

# Run: uvicorn server:app --host 0.0.0.0 --port 8000
```

## Performance Comparison

| Model        | GPU      | 1024x1024 Time |
| ------------ | -------- | -------------- |
| PixArt-Alpha | RTX 3090 | \~3s           |
| PixArt-Sigma | RTX 3090 | \~5s           |
| SDXL         | RTX 3090 | \~15s          |
| PixArt-Alpha | RTX 4090 | \~2s           |
| PixArt-Sigma | RTX 4090 | \~3s           |

## Quality Settings

| Use Case     | Steps | Guidance |
| ------------ | ----- | -------- |
| Preview      | 10-15 | 4.0      |
| Standard     | 20    | 4.5      |
| High Quality | 30-40 | 5.0      |

## Troubleshooting

### Out of Memory

```python

# Enable offloading
pipe.enable_model_cpu_offload()

# Or use smaller resolution
width, height = 768, 768
```

### Poor Quality

* Increase steps (25-40)
* Adjust guidance scale
* More detailed prompts

### Slow Generation

* Use PixArt-Alpha (faster)
* Reduce steps
* Lower resolution

## Cost Estimate

Typical CLORE.AI marketplace rates (as of 2024):

| GPU       | Hourly Rate | Daily Rate | 4-Hour Session |
| --------- | ----------- | ---------- | -------------- |
| RTX 3060  | \~$0.03     | \~$0.70    | \~$0.12        |
| RTX 3090  | \~$0.06     | \~$1.50    | \~$0.25        |
| RTX 4090  | \~$0.10     | \~$2.30    | \~$0.40        |
| A100 40GB | \~$0.17     | \~$4.00    | \~$0.70        |
| A100 80GB | \~$0.25     | \~$6.00    | \~$1.00        |

*Prices vary by provider and demand. Check* [*CLORE.AI Marketplace*](https://clore.ai/marketplace) *for current rates.*

**Save money:**

* Use **Spot** market for flexible workloads (often 30-50% cheaper)
* Pay with **CLORE** tokens
* Compare prices across different providers

## Next Steps

* FLUX Generation - Best quality
* Stable Diffusion WebUI - More features
* [ControlNet Guide](https://docs.clore.ai/guides/image-processing/controlnet-advanced) - Add control
