The explosive growth of artificial intelligence has placed unprecedented demands on our electrical infrastructure, presenting a unique challenge for AI engineers.

As we push the boundaries of what's possible in AI, we must also address its environmental impact. This article introduces an innovative approach to do sustainable AI development, combining two powerful language models, Yi-Large and CodeBERT, with a specialized tool called “Refactor Earth”.

We will demonstrate how we can optimize “Code” at both the algorithmic and instruction levels, significantly reducing energy consumption and carbon emissions without compromising performance.

As we progress through this article, we'll provide in-depth explanations of how we use LLM, along with practical examples you can apply to your own projects. We will be using Yi-Large, a SOTA model from 01.ai hosted on Fireworks AI blazing-fast inference stack and CodeBERT is a pre-trained language model specifically designed for programming language tasks. You can signup to Fireworks AI platform to access Yi-Large.

Before we begin

Let's dive into a practical example to see how we can optimize code for better energy efficiency. We'll start with a simple bubble sort implementation and see how Yi-Large can help us improve it

Example: Optimizing a Sorting Algorithm

Initial Code: Bubble Sort

import random

def generate_large_dataset(size):
    return [random.randint(1, 10000) for _ in range(size)]

def bubble_sort(dataset):
    n = len(dataset)
    for i in range(n):
        for j in range(0, n-i-1):
            if dataset[j] > dataset[j+1]:
                dataset[j], dataset[j+1] = dataset[j+1], dataset[j]
    return dataset

def main():
    size = 10000
    dataset = generate_large_dataset(size)
    sorted_dataset = bubble_sort(dataset)
    print(sorted_dataset[:10])

if __name__ == "__main__":
    main()

Optimized Code: Quicksort Using Yi-Large

import random

def generate_large_dataset(size):
    return [random.randint(1, 10000) for _ in range(size)]

def quicksort(dataset):
    if len(dataset) <= 1:
        return dataset
    pivot = dataset[len(dataset) // 2]
    left = [x for x in dataset if x < pivot]
    middle = [x for x in dataset if x == pivot]
    right = [x for x in dataset if x > pivot]
    return quicksort(left) + middle + quicksort(right)

def main():
    size = 10000
    dataset = generate_large_dataset(size)
    sorted_dataset = quicksort(dataset)
    print(sorted_dataset[:10])

if __name__ == "__main__":
    main()

Results

Refactor Earth utilizes CodeCarbon, an open-source tool, to measure the energy consumption and carbon footprint of your Python code. By integrating CodeCarbon, Refactor Earth gathers precise data on energy usage, which it then processes to calculate and optimize sustainability metrics. This ensures that the reported improvements in energy efficiency and carbon reduction are both accurate and actionable.

Now, let's take a look at how our optimization efforts paid off with the Refactor Earth tool. Here's a breakdown of the improvements we achieved:

Metric Bubble Sort Quicksort (Yi-Large) Improvement (%)
Energy Consumption (J) 152,300 139,200 8.6
Carbon Footprint (g CO2) 38.1 34.8 8.7
Execution Time (s) 12.8 11.1 13.3
Memory Usage (MiB) 24.8 24.5 1.2
Sustainability Score 20.0 22.7 13.5