Decentralized AI Power-Up: Fine-tuning Custom AI Models with Crynux and Irys

Decentralized AI Power-Up: Fine-tuning Custom AI Models with Crynux and Irys

Diffusion models are a powerful class of AI models for generating images from text prompts. However, its true creative potential is unlocked through fine-tuning. Fine-tuning allows you to adapt the base model to generate images in a highly specific style, or to consistently depict a particular character or object. By training the model on a curated dataset—even a small one—you can create a specialized model that produces results aligned with your unique vision. This process makes AI art creation more accessible, enabling artists, developers, and enthusiasts to craft bespoke visual content without the need to train a massive model from the ground up.

However, the conventional fine-tuning process presents significant hurdles that can make it inaccessible for many. It often demands access to expensive, high-performance GPUs, which are frequently in short supply and costly to rent from centralized cloud providers. Beyond the hardware costs, setting up and maintaining the correct software environment—with specific drivers and library versions—can be a complex and time-consuming task. Furthermore, ensuring the reproducibility of the training process and the integrity of the final model can be challenging in a traditional setup. These challenges highlight the need for a more accessible and robust infrastructure for AI development.

Solving the Challenge with Crynux and Irys

Crynux is a decentralized AI network that empowers users to fine-tune AI models like Diffusion models by tapping into a global network of GPU providers. It operates on a permissionless and trustless architecture, where tasks are distributed among multiple nodes that execute the computation.

For developers, this means there is no need to set up and maintain complex software environments. You simply define your fine-tuning task and submit it to the network, where nodes handle all the underlying dependencies and execution. This opens up access to AI model training, allowing anyone to train their own models without needing powerful and expensive hardware, all while ensuring transparency and reliability through its decentralized design.

Irys provides the data layer for the decentralized web. For AI model fine-tuning, the integrity and perpetual availability of the dataset are paramount. Irys addresses this with its specialized datachain, a blockchain optimized for affordable and immutable data storage. By hosting the training dataset on Irys, developers ensure that their data is permanently stored, tamper-proof, and globally accessible.

When a fine-tuning task is initiated on Crynux, the Crynux nodes can seamlessly and reliably retrieve the dataset from Irys, guaranteeing a consistent and reproducible training process. This combination of decentralized computation from Crynux and decentralized data from Irys creates a robust, end-to-end solution for trustless AI model development.

Example: Creating a Naruto-Style Image Generation Model

In this blog post, we'll walk through a practical example of fine-tuning a Diffusion model to generate images in a specific artistic style. We will use a dataset of images from the popular anime series Naruto. By fine-tuning the model on this dataset, we will create a LoRA (Low-Rank Adaptation) model capable of producing new images that capture the unique visual essence of Naruto. This example will showcase how you can leverage Crynux and Irys to create your own specialized image generation models.

Naruto-style images

Step 1: Prepare the Dataset

Our first step is to prepare the training data. For this Naruto example, our dataset comes from the open Naruto dataset provided by Lambda Labs. This dataset is stored in Apache Arrow format (.arrow files), which contains a single table with two columns: a column of PIL Image objects in JPEG format and another column of string captions corresponding to the images.

The training dataset

First of all we need to upload the dataset to Irys. This action gives us a unique Irys data link, which points to our immutable dataset. We can achieve it using the Irys SDK:

Upload the dataset to Irys Network

For more details on managing your data with Irys, please refer to their official documentation:

Irys | Irys SDK

Step 2: Run the Fine-Tuning Task

With our dataset ready on Irys, we can now tell the Crynux Network to start fine-tuning. The process is surprisingly simple. As you can see from the Python code below, you don't need to handle complex blockchain transactions or wallet signatures. It's all done through straightforward API calls, much like interacting with any modern web service.

import time

import httpx

client = httpx.Client(
    base_url="https://bridge.crynux.io",
    timeout=180,
)

# For public demonstration only, strict rate limit applied.
api_key = "q3hXHA_8O0LuGJ1_tou4_KamMlQqAo-aYwyAIDttdmI="

data = {
    "model_name": "crynux-ai/stable-diffusion-v1-5",
    "model_variant": "fp16",
    "dataset_url": "https://gateway.irys.xyz/DLsfGoiehnzpGNdsQ3XPEaVL9D9HNELGjL3yxsj67SUK",
    "validation_num_images": 4,
    "learning_rate": 0.0001,
    "batch_size": 1,
    "num_train_steps": 100,
    "max_train_steps": 200,
    "lr_scheduler": "cosine",
    "lr_warmup_steps": 0,
    "rank": 4,
    "init_lora_weights": "gaussian",
    "target_modules": ["to_k", "to_q", "to_v", "to_out.0"],
    "center_crop": True,
    "random_flip": True,
    "mixed_precision": "fp16",
    "seed": 42,
    "timeout": 1800,
}

header = {
    "Authorization": f"Bearer {api_key}",
}
resp = client.post(
    "/v1/images/models",
    json=data,
    headers=header,
    timeout=180,
)
resp.raise_for_status()
res = resp.json()
data = res["data"]
task_id = data["id"]
print(f"Task ID: {task_id}")

success = False
while True:
    resp = client.get(
        f"/v1/images/models/{task_id}/status",
    )
    resp.raise_for_status()
    res = resp.json()
    data = res["data"]
    status = data["status"]
    if status == "success":
        print(f"Task success")
        success = True
        break
    elif status == "failed":
        print(f"Task failed")
        success = False
        break
    elif status == "running":
        print(f"Task is still running")
    time.sleep(60)

if success:
    with client.stream(
        "GET",
        f"/v1/images/models/{task_id}/result",
        headers=header,
        timeout=180,
    ) as resp:
        resp.raise_for_status()
        with open("result.zip", "wb") as f:
            for data in resp.iter_bytes():
                f.write(data)

    print("Task completed")
The complete code of the fine-tuning task

In the code, we simply pass the Irys link to our dataset in the dataset_url parameter. Submitting the task sends it to the Crynux Network, where it is distributed to a group of decentralized nodes. Each node then independently downloads the dataset from Irys using the provided link and begins the fine-tuning computation.

Executing the script returns a unique Task ID for the job. The script then periodically checks the status of this task until it's successfully completed.

Execution of the fine-tuning task on Crynux Network

Once the fine-tuning is complete, the script automatically downloads the resulting LoRA model, a single zip file containing the learned Naruto style.

Content of the zip file of the resulting model

Step 3: Generate Images using the LoRA Model

Now for the fun part! With the trained LoRA model file, you can start generating your own Naruto-style images. You can load this file into a local tool like the Stable Diffusion WebUI, or even use the Crynux Network again to generate images through its API.

Generating images using the model in Stable Diffusion WebUI

This completes our example of creating a fine-tuned LoRA model using Crynux and Irys.

Learn More

To dive deeper into the technical details of fine-tuning on Crynux, check out the official documentation:

How to Fine-tune a Stable Diffusion Model using Crynux Network

Beyond fine-tuning, the Crynux Network also supports running a wide range of popular Large Language Models (LLMs) through an OpenAI-compliant API, making it compatible with most mainstream libraries and frameworks.

How to Run LLM using Crynux Network

You can also use the Crynux Network API to generate images:

How to Generate Images using Crynux Network

Have a GPU sitting idle? You can also contribute to the network and earn token rewards by running a Crynux Node in a single click.

Start a Crynux Node

Join the Crynux community on Discord to get instant tech support and connect with developers and other users:

Crynux Discord

Follow Crynux on X for the latest news and updates:

https://x.com/crynuxio