Skip to content

Generate robust Python solutions using Qwen3-Coder and Ampere Optimized llama.cpp on Ampere Altra & AmpereOne CPUs. Includes a Dockerized UI.

License

Notifications You must be signed in to change notification settings

AmpereComputingAI/ampere-ai-codegen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ampere AI Code Generation

Generate robust Python solutions using AI models on Ampere CPUs

License: MIT Platform Docker


Overview

This repository demonstrates the Code Generation capabilities of AI models (specifically Qwen3-Coder) running on the Ampere Altra and AmpereOne platforms. It provides a suite of example prompts and utilities to generate, test, and validate Python code solutions for algorithmic problems.

The Challenge

Automatic Code Generation is a complex task that goes beyond simple text completion. Models must understand logic, handle edge cases, and adhere to strict syntax rules. Common challenges include:

  • Algorithmic Logic: Converting a problem statement (e.g., "Find the missing number") into a working algorithm.
  • Edge Case Handling: Ensuring code doesn't crash on empty inputs, negative numbers, or boundary conditions.
  • Syntactic Correctness: Generating valid Python code that interprets and runs without errors.
  • Testability: Creating self-contained solutions that include assertions to verify their own correctness.

The Solution

This project leverages the efficient inference capabilities of Ampere Altra and AmpereOne processors to run high-performance code generation models using Ampere optimized llama.cpp.

  • Efficient Inference: Run state-of-the-art coding models like Qwen3-Coder efficiently on Cloud Native Processors.
  • Standardized Examples: Includes a curated list of 20 algorithms (Simple and Medium complexity) to explore model performance.
  • Self-Verifying Code: The prompts are designed to request not just the solution, but also the test functions to verify correctness automatically.

Software Stack

  • Ampere Optimized llama.cpp: A backend inference engine highly tuned for Ampere CPUs.
  • Open-WebUI: An extensible, self-hosted UI for interacting with the LLMs.
  • Qwen3-Coder: A state-of-the-art code generation model.

Architecture

The solution uses Docker to orchestrate the interaction between the User (via Open-WebUI) and the AI Model (via Ampere Optimized llama.cpp).

graph LR
    %% Styles for nodes
    classDef nodeStyle fill:#fff,stroke:#000,stroke-width:1px,rx:5,ry:5;

    %% Left Column: Input
    subgraph UI [Open-WebUI]
        direction TB
        A[User Enters<br>Algorithm Prompt]
    end

    %% Middle Column: Inference
    subgraph Inference [Ampere Inference]
        direction TB
        C[Ampere Optimized<br>llama.cpp Server]
    end

    %% Right Column: Output
    subgraph Output [Code Execution]
        direction TB
        D[Generated Python<br>Code returned]
        E[Test Function<br>Executed]
        F[Result Verified]
    end

    %% Connections
    A --> C
    C --> D
    D --> E
    E --> F

    %% Apply Styles
    class A,B,C,D,E,F nodeStyle

    %% Color the Subgraphs
    style UI fill:#ffdce0,stroke:#333,stroke-width:2px
    style Inference fill:#dcf5dc,stroke:#333,stroke-width:2px
    style Output fill:#dcebf5,stroke:#333,stroke-width:2px
Loading

Prerequisites

Ensure your environment meets the following requirements:

  • Ampere Altra / AmpereOne System (Recommended)
  • Docker Engine & Docker Compose

Configuration

The application is configured directly via the compose.yaml file. You can modify the environment variables under the service definitions to change model parameters or file paths.

Example compose.yaml configuration:

services:
  llama-cpp-server:
    image: amperecomputingai/llama.cpp:3.4.0
    environment:
      # Model Configuration
      - LLAMA_ARG_HF_REPO=unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF:Qwen3-Coder-30B-A3B-Instruct-Q8_0.gguf

Quick Start

Launch the Application

Use the provided helper script to build and start the services.

./start-app.sh

Stop the Application

To stop the services and clean up resources:

./stop-app.sh

Coding Prompts

This repository includes a suite of 20 prompts covering simple and medium-complexity algorithms, designed for testing code generation models (like Qwen3-Coder). Each prompt explicitly requests a Python solution and a corresponding test function.

Simple Algorithms

  1. FizzBuzz

    Write a Python function named fizz_buzz(n: int) -> List[str] that returns a list of strings representing the numbers from 1 to n. For multiples of 3, use 'Fizz'; for multiples of 5, use 'Buzz'; and for multiples of both, use 'FizzBuzz'. Include a test_fizz_buzz() function that uses assertions to verify the output for n=15.

  2. Palindrome Checker

    Create a Python function is_palindrome(s: str) -> bool that checks if a given string is a palindrome, ignoring case, spaces, and non-alphanumeric characters. Add a test function containing assertions for cases like 'A man, a plan, a canal: Panama' (True) and 'hello' (False).

  3. Factorial Calculation

    Write a recursive Python function factorial(n: int) -> int to calculate the factorial of a non-negative integer. Include error handling for negative inputs. Provide a test function that asserts the results for n=0, n=1, and n=5, and checks that a ValueError is raised for negative input.

  4. Fibonacci Generator

    Implement a Python generator function fibonacci_generator(n: int) that yields the first n numbers of the Fibonacci sequence. Write a test function that consumes the generator and asserts that the output list for n=10 matches [0, 1, 1, 2, 3, 5, 8, 13, 21, 34].

  5. Check Prime Number

    Write a Python function is_prime(num: int) -> bool to determine if a number is prime, optimized by checking divisibility up to the square root. Include a test_is_prime() function that asserts True for 2, 3, 17 and False for 1, 4, 15.

  6. Find Missing Number

    Given a list containing n distinct numbers taken from the range 0 to n, write a function find_missing_number(nums: List[int]) -> int. Include a test function that verifies inputs like [3, 0, 1] returns 2 and [0, 1] returns 2.

  7. Remove Duplicates from Sorted Array

    Write a Python function remove_duplicates(nums: List[int]) -> int that removes duplicates from a sorted array in-place and returns the new length. Include a test function that passes [1, 1, 2], asserts the return value is 2, and verifies nums is modified to [1, 2].

  8. Valid Anagram

    Write a Python function is_anagram(s: str, t: str) -> bool to check if t is an anagram of s. Provide a test function using assertions to check pairs like ('anagram', 'nagaram') and ('rat', 'car').

  9. Matrix Transpose

    Create a Python function transpose_matrix(matrix: List[List[int]]) -> List[List[int]] that returns the transpose of a 2D matrix. Include a test function that verifies the transpose of [[1,2,3], [4,5,6]] is [[1,4], [2,5], [3,6]].

  10. Sum of Digits (Digital Root)

    Write a Python function sum_of_digits(n: int) -> int that repeatedly sums the digits of a number until the result has only one digit. Include a test function checking n=38 (result 2) and n=0 (result 0).

Medium Complexity Algorithms

  1. Two Sum

    Write a Python function two_sum(nums: List[int], target: int) -> List[int] that finds the indices of two numbers in nums that add up to target. Include a test function verifying nums=[2, 7, 11, 15], target=9 returns [0, 1].

  2. Binary Search

    Implement binary_search(nums: List[int], target: int) -> int for a sorted array. Return the index if found, else -1. Provide a test function verifying cases where the target exists, does not exist, and when the list is empty.

  3. Valid Parentheses

    Write is_valid_parentheses(s: str) -> bool to validate strings containing '()', '[]', '{}'. Include a test function asserting True for '()[]{}' and False for '(]' and '([)]'.

  4. Merge Sort Implementation

    Implement merge_sort(arr: List[int]) -> List[int]. Include a helper function for the merge step. Write a test function that generates a list of random integers, sorts them using Python's built-in sort, and asserts that your merge sort produces the exact same result.

  5. Maximum Subarray (Kadane’s)

    Write max_subarray(nums: List[int]) -> int to find the largest sum of a contiguous subarray. Include a test function checking [-2,1,-3,4,-1,2,1,-5,4] returns 6 (from [4,-1,2,1]).

  6. Longest Substring Without Repeating Characters

    Write length_of_longest_substring(s: str) -> int. Include a test function asserting that 'abcabcbb' returns 3, 'bbbbb' returns 1, and 'pwwkew' returns 3.

  7. Reverse Linked List

    Define a ListNode class and a function reverse_list(head: Optional[ListNode]) -> Optional[ListNode]. Include a helper function to convert a list to a linked list and vice versa. Finally, write a test function asserting that [1,2,3,4,5] becomes [5,4,3,2,1].

  8. Binary Tree Level Order Traversal

    Given a TreeNode class, write level_order(root: Optional[TreeNode]) -> List[List[int]]. Include a test function that manually constructs the tree [3,9,20,null,null,15,7] and asserts the output is [[3],[9,20],[15,7]].

  9. Climbing Stairs (DP)

    Write a function climb_stairs(n: int) -> int calculating distinct ways to climb n steps taking 1 or 2 at a time. Include a test function asserting results for n=2 (2 ways) and n=3 (3 ways).

  10. Coin Change Problem

    Write coin_change(coins: List[int], amount: int) -> int to find the minimum coins needed for an amount. Include a test function verifying coins=[1, 2, 5], amount=11 returns 3, and amount=3 with coins=[2] returns -1.


Best Practices for Code Generation

To get the best results from the models on the Ampere platform:

  1. Clear Instructions: Always specify the input and output types (e.g., Type Hinting).
  2. Request Tests: Asking the model to generate its own test assertions helps verify the logic immediately.
  3. Iterative Refinement: If the code fails, feed the error message back to the model to request a fix.

License

This project is licensed under the MIT License. See the LICENSE file for details.

References

About

Generate robust Python solutions using Qwen3-Coder and Ampere Optimized llama.cpp on Ampere Altra & AmpereOne CPUs. Includes a Dockerized UI.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages