CoolFace
Modelpublic

ky00040/MotokoCoderV0

sourceHugging Faceapache-2.0updated 6mo agoView on Hugging Face
0likes14downloads
README.md223 linesDownload Raw Back to root
1---2language:3- en4license: apache-2.05library_name: peft6base_model: Qwen/Qwen3-Coder-30B-A3B-Instruct7tags:8- motoko9- internet-computer10- icp11- code-generation12- blockchain13- defi14- lora15 16datasets: []17pipeline_tag: text-generation18---19 20# MotokoCoderV021 22**The first code generation model for Motoko** — the native language of the [Internet Computer](https://internetcomputer.org/) blockchain.23 24Part of the **Motoko Coder** model series by **Mercatura Forum AI Lab** and **ICP Hub Egypt**. Smaller and larger models are planned for production use, with an API available for developers to try. This V0 release uses Qwen3-Coder-30B-A3B as the base — a commercially licensable model you can run and deploy freely.25 26## Highlights27 28- **70% compilation rate** on a balanced evaluation set of 20 diverse Motoko programming tasks29- Generates production-quality `persistent actor` code with proper `mo:core` imports30- Writes compilable **AMM swap pools**, **escrow services**, **token ledgers**, **staking contracts**, **admin access control**, and more31- LoRA adapter (205MB) on top of Qwen3-Coder-30B-A3B-Instruct32- Verified against the official `moc` compiler from DFINITY SDK33 34## Motoko Coder Series35 36| Model | Base | Status | Use Case |37|-------|------|--------|----------|38| **MotokoCoderV0** | Qwen3-Coder-30B-A3B | ✅ Released | Local development, commercial use |39| MotokoCoderV1 | TBD | 🔜 Coming soon | Higher accuracy, self-repair |40| MotokoCoder-API | Hosted | 🔜 Coming soon | API access for all developers |41| MotokoCoder-Small | Qwen3-Coder-8B | 🔜 Planned | Edge deployment, IDE plugins |42| MotokoCoder-Pro | Qwen3-Coder-235B | 🔜 Planned | Production code generation |43 44## Evaluation Results45 46Tested against the `moc` compiler — every "compiled" result is verified machine-checked code.47 48| Category | Compiled | Rate |49|----------|----------|------|50| Easy (contact forms, todo lists, profiles) | 4/7 | 57% |51| Medium (voting, ledgers, config stores, event logs) | 6/8 | 75% |52| Hard (AMM pools, staking, escrow, batch transfers) | 4/5 | 80% |53| **Overall** | **14/20** | **70%** |54 55### What it compiles56 57- Persistent actors with `Map`, `Set`, `Principal`, `Time` state management58- CRUD operations with proper `Map.add`/`Map.get`/`Map.delete` and compare functions59- DeFi primitives: constant product AMM formula, fee collection, reserve tracking60- State machines with variant types (`#Created`, `#Funded`, `#Released`)61- Admin access control with `Principal.equal` checks62- Record updates with `{ record with field = newValue }` syntax63- Result types with `#ok`/`#err` error handling64- Query vs update function separation65- Token ledgers with transfer, mint, burn operations66- Escrow services with full lifecycle management67- Online stores (bookstore, restaurant menus) with inventory management68 69### Example: AMM Swap Pool (compiles ✅)70 71```motoko72import Map "mo:core/Map";73import Nat "mo:core/Nat";74import Principal "mo:core/Principal";75import Result "mo:core/Result";76 77persistent actor AMMSwapPool {78  var reserveA : Nat = 1_000_000;79  var reserveB : Nat = 1_000_000;80  var totalFees : Nat = 0;81 82  func getOutputAmount(inputAmount : Nat, inputReserve : Nat, outputReserve : Nat) : Nat {83    let numerator = inputAmount * outputReserve * 997;84    let denominator = (inputReserve * 1000) + (inputAmount * 997);85    numerator / denominator;86  };87 88  public shared(msg) func swap(inputToken : Text, inputAmount : Nat) : async Result.Result<Nat, Text> {89    if (inputAmount == 0) { return #err("Amount must be > 0") };90    let outputAmount = getOutputAmount(inputAmount, reserveA, reserveB);91    let fee = inputAmount * 3 / 1000;92    totalFees += fee;93    reserveA += inputAmount;94    reserveB -= outputAmount;95    #ok(outputAmount);96  };97 98  public query func getReserves() : async { reserveA : Nat; reserveB : Nat } {99    { reserveA; reserveB };100  };101};102```103 104### Example: Escrow Service (compiles ✅, 156 lines)105 106```motoko107persistent actor EscrowService {108  public type EscrowState = {109    #Created; #Funded; #Disputed; #Released; #Refunded;110  };111 112  public type Escrow = {113    id : Nat; buyer : Principal; seller : Principal;114    amount : Nat; state : EscrowState; createdAt : Int;115  };116 117  var escrows = Map.empty<Nat, Escrow>();118 119  public shared(msg) func createEscrow(seller : Principal, amount : Nat) : async Result.Result<Nat, Text> { ... };120  public shared(msg) func fundEscrow(id : Nat) : async Result.Result<(), Text> { ... };121  public shared(msg) func releaseFunds(id : Nat) : async Result.Result<(), Text> { ... };122  public shared(msg) func dispute(id : Nat) : async Result.Result<(), Text> { ... };123};124```125 126## Usage127 128```python129from transformers import AutoModelForCausalLM, AutoTokenizer130from peft import PeftModel131import torch132 133base_model = "Qwen/Qwen3-Coder-30B-A3B-Instruct"134adapter = "ky00040/MotokoCoderV0"135 136tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)137model = AutoModelForCausalLM.from_pretrained(base_model, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True)138model = PeftModel.from_pretrained(model, adapter)139model = model.merge_and_unload()140 141messages = [142    {"role": "system", "content": "You are a Motoko expert for the Internet Computer. Write clean, compilable Motoko code using mo:core imports. Use `persistent actor` for actors, Map.empty/add/get with compare functions."},143    {"role": "user", "content": "Write a Motoko persistent actor for a token balance ledger with transfer, mint, and balance query."}144]145 146text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)147inputs = tokenizer(text, return_tensors="pt").to(model.device)148 149with torch.no_grad():150    outputs = model.generate(**inputs, max_new_tokens=2048, temperature=0.1, do_sample=True, top_p=0.95)151 152response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)153print(response)154```155 156## System Prompt157 158For best results, use this system prompt:159 160```161You are a Motoko expert for the Internet Computer. Write clean, compilable Motoko code using mo:core imports. Use `persistent actor` for actors, Map.empty/add/get with compare functions.162```163 164## Tips for Best Results165 1661. **Ask for full actors**: "Write a Motoko persistent actor for X" works better than "Write a function that does X"1672. **Describe the types**: "Store items with name, price, and category" helps the model define proper types1683. **Mention state**: "Use Map for storage" guides the model toward correct patterns1694. **Temperature 0.1** for reliable code, **0.7** for creative variations170 171## Hardware Requirements172 173This is a **MoE (Mixture of Experts) model** — 30B total parameters but only **3B active** per forward pass, making it much lighter than a dense 30B model.174 175| Setup | VRAM | Precision | Works? |176|-------|------|-----------|--------|177| 1x RTX 5090 / A100 40GB | 32-40GB | INT8 | ✅ Recommended |178| 2x RTX 5090 / 1x A100 80GB | 64-80GB | bf16 | ✅ Full precision |179| 1x RTX 5080 / 4090 / 4080 | 16-24GB | AWQ 4-bit | ✅ Quantized |180| Apple M4 Pro/Max | 36-128GB unified | MLX / llama.cpp | ✅ |181 182**Supported frameworks:**183- `transformers` + `peft` (recommended, tested)184- `vLLM` for serving185- `llama.cpp` / `Ollama` (with GGUF conversion)186 187> **Note:** This model is NOT compatible with Unsloth due to MoE architecture limitations.188 189## Known Limitations190 191- Standalone function prompts without context may reference undefined types192- Very long actors (200+ lines) may occasionally truncate193- String manipulation and regex-style operations are weak194- HTTP outcall and inter-canister call patterns are limited195- Sometimes uses OOP-style method calls (`.toArray()`) instead of module functions (`Iter.toArray()`)196 197## Model Details198 199- **Base model**: Qwen3-Coder-30B-A3B-Instruct (MoE architecture, 30B total parameters, 3B active per forward pass)200- **Adapter type**: LoRA with rsLoRA scaling201- **Adapter config**: r=64, alpha=128202- **Target modules**: q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj203- **Trainable parameters**: 53.5M (0.17% of total)204- **Compilation verification**: All evaluation results verified against `moc` (Motoko compiler) from DFINITY SDK v0.31.0205 206## About Motoko207 208[Motoko](https://internetcomputer.org/docs/motoko/main/getting-started/motoko-introduction) is a programming language designed specifically for the Internet Computer blockchain. Key features include:209- **Persistent actors** — canister smart contracts with automatic state persistence210- **Async/await** — native support for inter-canister communication211- **Strong type system** — derived from OCaml, with variants, options, and generics212- **mo:core standard library** — Map, Set, List, Array, Principal, Time, and more213 214MotokoCoderV0 uses the modern `mo:core` standard library (not the deprecated `mo:base`).215 216## About217 218**Mercatura Forum AI Lab** and **ICP Hub Egypt** are building developer tooling and AI infrastructure for the Internet Computer ecosystem.219 220## License221 222Apache 2.0 — free for commercial use.223