|
Download docs/TOURNAMENT.md from Snapkitty/forge-code: direct link, hf CLI and curl.
- Browser
- Download file 13.7 kB
-
https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/docs/TOURNAMENT.md
- Command line
-
hf download hf://datasets/Snapkitty/forge-code/docs/TOURNAMENT.md
-
curl -L -o TOURNAMENT.md https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/docs/TOURNAMENT.md
13.7 kB
| # FORGE CODE Tournament System | |
| **Complete Guide to Creating, Running, and Judging Elixir Tournaments** | |
| --- | |
| ## Overview | |
| The FORGE CODE Tournament System provides a deterministic framework for comparing multiple Elixir implementations of the same computational problem. Contestants submit independent implementations, and the system evaluates them across multiple dimensions to determine the optimal solution. | |
| ### Key Principles | |
| 1. **Determinism**: Same inputs always produce same outputs | |
| 2. **Isolation**: Each contestant runs in isolated context | |
| 3. **Fairness**: All contestants face identical conditions | |
| 4. **Transparency**: Full provenance tracking of all artifacts | |
| 5. **Rigorous**: Correctness gates all other metrics | |
| --- | |
| ## Tournament Lifecycle | |
| ``` | |
| ┌─────────────────────────────────────────────────────────────────┐ | |
| │ TOURNAMENT LIFECYCLE │ | |
| ├─────────────────────────────────────────────────────────────────┤ | |
| │ │ | |
| │ 1. DEFINITION 2. REGISTRATION 3. COMPILATION │ | |
| │ ├─ Challenges ├─ Contestants ├─ Source → BEAM │ | |
| │ ├─ Inputs ├─ Submissions ├─ Metadata Capture │ | |
| │ ├─ Expected Outputs └─ Artifact Hashes └─ Provenance Chain │ | |
| │ └─ Constraints │ | |
| │ │ | |
| │ 4. EXECUTION 5. JUDGING 6. SCORING │ | |
| │ ├─ Sandboxed Run ├─ Correctness ├─ Multi-Dimensional │ | |
| │ ├─ Timeout Enforced ├─ Type-Aware ├─ Weighted Metrics │ | |
| │ ├─ Memory Limited └─ Comparison Notes └─ Normalized Scores │ | |
| │ └─ Multiple Iterations │ | |
| │ │ | |
| │ 7. RANKING 8. REPORTING 9. ARCHIVE │ | |
| │ ├─ Sort by Score ├─ JSON/Markdown ├─ Full Artifacts │ | |
| │ ├─ Tie-Breaking ├─ Detailed Metrics ├─ Provenance Sealed │ | |
| │ └─ Winner Selection └─ Judge Output └─ Tournament Manifest│ | |
| │ │ | |
| └─────────────────────────────────────────────────────────────────┘ | |
| ``` | |
| --- | |
| ## Creating a Tournament | |
| ### Tournament DSL | |
| Use `Forge.Tournament.define/2` to create tournament configurations: | |
| ```elixir | |
| # tournament.exs | |
| Forge.Tournament.define "Integer Hashing" do | |
| # Tournament-level metadata | |
| id "integer_hashing_2024" | |
| description "Benchmark SHA-256 and arithmetic implementations" | |
| author "ahmedparr93@gmail.com" | |
| # Tournament configuration | |
| compiler_version Elixir.version() | |
| otp_version System.otp_release() | |
| timeout 5000 # 5 seconds per challenge | |
| memory_limit 8 * 1024 * 1024 # 8MB | |
| iterations 100 # Multiple runs for averaging | |
| # Challenge definitions | |
| challenge "sha256" do | |
| id "sha256_forge" | |
| description "Compute SHA-256 hash of 'forge'" | |
| input "forge" | |
| expected "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824" | |
| weight 1.0 # Weight in final score | |
| timeout 1000 | |
| memory_limit 1024 * 1024 | |
| end | |
| challenge "integer_sum" do | |
| id "sum_list" | |
| description "Sum a list of integers" | |
| input [1, 2, 3, 4, 5] | |
| expected 15 | |
| weight 0.5 | |
| timeout 1000 | |
| end | |
| challenge "factorial" do | |
| id "factorial_5" | |
| description "Compute factorial of 5" | |
| input 5 | |
| expected 120 | |
| weight 0.3 | |
| timeout 1000 | |
| end | |
| # Scoring configuration | |
| scoring do | |
| correctness_weight 1.0 | |
| time_weight 0.3 | |
| memory_weight 0.2 | |
| binary_size_weight 0.1 | |
| instruction_count_weight 0.15 | |
| end | |
| end | |
| ``` | |
| ### Tournament Configuration Options | |
| | Option | Type | Default | Description | | |
| |--------|------|---------|-------------| | |
| | `id` | String | Required | Unique tournament identifier | | |
| | `description` | String | "" | Human-readable description | | |
| | `author` | String | System user | Tournament creator | | |
| | `timeout` | Integer | 5000 | Milliseconds per challenge | | |
| | `memory_limit` | Integer | 8MB | Bytes per contestant | | |
| | `iterations` | Integer | 10 | Runs for averaging | | |
| | `warmup_iterations` | Integer | 1 | Warm-up runs before timing | | |
| | `parallel` | Boolean | false | Run contestants in parallel | | |
| | `sandbox` | Boolean | true | Use sandboxed execution | | |
| --- | |
| ## Challenge Types | |
| ### Simple Challenges | |
| ```elixir | |
| challenge "reverse_string" do | |
| input "hello" | |
| expected "olleh" | |
| end | |
| ``` | |
| ### Parameterized Challenges | |
| ```elixir | |
| challenge "fibonacci" do | |
| input 10 # Can be any term | |
| expected 55 | |
| end | |
| ``` | |
| ### List Challenges | |
| ```elixir | |
| challenge "list_reverse" do | |
| input [1, 2, 3, 4, 5] | |
| expected [5, 4, 3, 2, 1] | |
| end | |
| ``` | |
| ### Map Challenges | |
| ```elixir | |
| challenge "map_transform" do | |
| input %{a: 1, b: 2} | |
| expected %{a: 2, b: 4} | |
| end | |
| ``` | |
| ### Custom Judging | |
| ```elixir | |
| challenge "custom_judge" do | |
| input fn -> 42 end | |
| expected fn output -> output == 42 end | |
| end | |
| ``` | |
| --- | |
| ## Contestant Structure | |
| ### Directory Layout | |
| ``` | |
| contestants/ | |
| ├── alice/ | |
| │ ├── hasher.ex # Main implementation | |
| │ ├── helper.ex # Helper modules | |
| │ └── mix.exs # Optional: custom dependencies | |
| │ | |
| ├── bob/ | |
| │ ├── hasher.ex | |
| │ └── custom_math.ex | |
| │ | |
| └── charlie/ | |
| ├── hasher.ex | |
| └── README.md # Contestant description | |
| ``` | |
| ### Implementation Requirements | |
| Each contestant module must implement: | |
| ```elixir | |
| defmodule Alice.Hasher do | |
| # Required: solve/2 function | |
| def solve(:sha256_forge, input), do: sha256(input) | |
| def solve(:sum_list, input), do: sum_list(input) | |
| def solve(:factorial_5, input), do: factorial(input) | |
| # Helper functions | |
| def sha256(input), do: :crypto.hash(:sha256, input) |> Base.encode16(case: :lower) | |
| def sum_list(list), do: Enum.sum(list) | |
| def factorial(0), do: 1 | |
| def factorial(n), do: n * factorial(n - 1) | |
| end | |
| ``` | |
| ### Module Discovery | |
| The tournament system automatically discovers: | |
| - All modules in contestant directories | |
| - All exported functions | |
| - All `@moduledoc` and `@doc` documentation | |
| - Module attributes and metadata | |
| --- | |
| ## Running a Tournament | |
| ### Command Line | |
| ```bash | |
| # Run a tournament file | |
| forge tournament run examples/challenges/integer_hashing.exs | |
| # Run with custom output | |
| forge tournament run tournament.exs --format json > results.json | |
| # Run with verbose output | |
| forge tournament run tournament.exs --verbose | |
| # Run specific contestant only | |
| forge tournament run tournament.exs --contestant alice | |
| ``` | |
| ### Programmatic | |
| ```elixir | |
| # Load tournament definition | |
| {:ok, tournament} = Forge.Tournament.load("tournament.exs") | |
| # Run the tournament | |
| {:ok, results} = Forge.Tournament.run(tournament) | |
| # Get scores | |
| scores = Forge.Score.calculate_all(results) | |
| # Generate report | |
| report = Forge.Tournament.Report.generate(results, scores) | |
| ``` | |
| --- | |
| ## Tournament Results | |
| ### Result Structure | |
| ```elixir | |
| %Forge.Tournament.Result{ | |
| tournament_id: "integer_hashing_2024", | |
| timestamp: DateTime.utc_now(), | |
| contestants: [ | |
| %{ | |
| id: "alice", | |
| module: Alice.Hasher, | |
| source_hash: "abc123...", | |
| beam_path: "_build/alice/ebin/alice.hasher.beam", | |
| beam_hash: "def456...", | |
| compile_time_ms: 123, | |
| binary_size: 4567, | |
| challenges: [ | |
| %{ | |
| id: "sha256_forge", | |
| passed: true, | |
| execution_time_ms: 45.2, | |
| memory_kb: 2048, | |
| correctness: 1.0, | |
| output: "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824" | |
| } | |
| ], | |
| total_score: 0.956, | |
| rank: 1 | |
| } | |
| ], | |
| summary: %{ | |
| total_challenges: 3, | |
| total_contestants: 2, | |
| average_score: 0.892, | |
| winning_score: 0.956, | |
| winner: "alice" | |
| } | |
| } | |
| ``` | |
| ### Judge Output | |
| The judge produces detailed comparison results: | |
| ```elixir | |
| %Forge.Judge.Result{ | |
| challenge_id: "sha256_forge", | |
| contestant_id: "alice", | |
| input: "forge", | |
| expected: "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", | |
| actual: "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", | |
| passed: true, | |
| comparison: :exact_match, | |
| confidence: :high, | |
| notes: "Perfect match", | |
| timing: %{ | |
| cold_ms: 45.2, | |
| warm_ms: 32.1, | |
| iterations: 100 | |
| }, | |
| resources: %{ | |
| memory_kb: 2048, | |
| reductions: 12345 | |
| } | |
| } | |
| ``` | |
| --- | |
| ## Comparison Strategies | |
| The judge supports multiple comparison strategies: | |
| ### Exact Match | |
| ```elixir | |
| # Binary equality | |
| judge expected, actual, strategy: :exact | |
| ``` | |
| ### Float Tolerance | |
| ```elixir | |
| # Within 0.0001 | |
| judge 3.14159, 3.1415926535, strategy: :float, tolerance: 0.0001 | |
| ``` | |
| ### List Order Ignored | |
| ```elixir | |
| # [1, 2, 3] == [3, 2, 1] | |
| judge [1, 2, 3], [3, 2, 1], strategy: :unordered_list | |
| ``` | |
| ### Map Subset | |
| ```elixir | |
| # %{a: 1} is subset of %{a: 1, b: 2} | |
| judge %{a: 1}, %{a: 1, b: 2}, strategy: :map_subset | |
| ``` | |
| ### Custom Comparator | |
| ```elixir | |
| judge expected, actual, strategy: fn exp, act -> abs(exp - act) < 0.1 end | |
| ``` | |
| --- | |
| ## Advanced Features | |
| ### Dynamic Input Generation | |
| ```elixir | |
| challenge "sort_test" do | |
| input fn -> Enum.random(1..100) |> Enum.take(10) end | |
| expected fn input -> Enum.sort(input) end | |
| end | |
| ``` | |
| ### Multiple Test Vectors | |
| ```elixir | |
| challenge "edge_cases" do | |
| test_vectors [ | |
| {[], []}, | |
| {[1], [1]}, | |
| {[1, 1, 1], [1, 1, 1]}, | |
| {[3, 1, 2], [1, 2, 3]} | |
| ] | |
| end | |
| ``` | |
| ### Weighted Challenges | |
| ```elixir | |
| challenge "critical_function" do | |
| weight 2.0 # This challenge counts double | |
| end | |
| ``` | |
| ### Conditional Execution | |
| ```elixir | |
| challenge "large_input" do | |
| input fn -> Enum.to_list(1..10000) end | |
| condition :large_memory # Only run on systems with sufficient memory | |
| end | |
| ``` | |
| --- | |
| ## Tournament Manifest | |
| After running, a tournament manifest is generated: | |
| ```json | |
| { | |
| "tournament_id": "integer_hashing_2024", | |
| "timestamp": "2024-01-15T10:30:00Z", | |
| "configuration": { | |
| "compiler_version": "1.15.0", | |
| "otp_version": "25.0", | |
| "timeout_ms": 5000, | |
| "memory_limit_kb": 8192 | |
| }, | |
| "contestants": [ | |
| { | |
| "id": "alice", | |
| "source_hash": "abc123", | |
| "beam_hash": "def456", | |
| "scores": { | |
| "correctness": 1.0, | |
| "execution_time_ms": 45.2, | |
| "memory_kb": 2048, | |
| "binary_size_bytes": 8192 | |
| }, | |
| "total_score": 0.956, | |
| "rank": 1 | |
| } | |
| ], | |
| "challenges": [ | |
| { | |
| "id": "sha256_forge", | |
| "all_passed": true, | |
| "average_time_ms": 45.2 | |
| } | |
| ], | |
| "provenance": { | |
| "source_seal": "...", | |
| "beam_seal": "...", | |
| "ir_seal": "...", | |
| "tournament_seal": "..." | |
| } | |
| } | |
| ``` | |
| --- | |
| ## Best Practices | |
| ### 1. Start Small | |
| Begin with 2-3 contestants and 2-3 challenges to validate the setup. | |
| ### 2. Test Locally | |
| ```bash | |
| forge compile examples/contestants/alice/hasher.ex | |
| forge inspect _build/alice/ebin/alice.hasher.beam | |
| ``` | |
| ### 3. Use Version Control | |
| ```bash | |
| # Commit contestant source | |
| cd contestants/alice | |
| git add hasher.ex | |
| git commit -m "Alice: Initial implementation" | |
| ``` | |
| ### 4. Document Assumptions | |
| ```elixir | |
| challenge "complex_math" do | |
| description "Compute complex matrix operations" | |
| # Document any assumptions about input size, format, etc. | |
| assumptions [ | |
| "Input matrix is square", | |
| "Matrix size <= 100x100", | |
| "Elements are floats" | |
| ] | |
| end | |
| ``` | |
| ### 5. Monitor Resource Usage | |
| Use the `--profile` flag to see detailed resource consumption. | |
| ### 6. Archive Results | |
| ```bash | |
| # Save tournament results | |
| forge tournament run tournament.exs --output results/ | |
| # Verify archive integrity | |
| forge hash chain results/tournament_manifest.json | |
| ``` | |
| --- | |
| ## Troubleshooting | |
| ### Contestant Compilation Fails | |
| ```bash | |
| # Check compilation errors | |
| forge compile examples/contestants/bob/hasher.ex | |
| # View detailed compiler output | |
| forge compile examples/contestants/bob/hasher.ex --verbose | |
| ``` | |
| ### Challenge Timeout | |
| ```elixir | |
| # Increase timeout for specific challenge | |
| challenge "slow_function" do | |
| timeout 10000 # 10 seconds | |
| end | |
| ``` | |
| ### Memory Limit Exceeded | |
| ```elixir | |
| # Increase memory limit | |
| challenge "memory_intensive" do | |
| memory_limit 16 * 1024 * 1024 # 16MB | |
| end | |
| ``` | |
| ### Incorrect Output | |
| ```bash | |
| # Run with verbose judge output | |
| forge tournament run tournament.exs --verbose | |
| # Check judge comparison | |
| forge inspect examples/contestants/alice/ebin/alice.hasher.beam | |
| ``` | |
| --- | |
| ## Traditional Chinese / 繁體中文 | |
| ### 賽事系統概述 | |
| FORGE CODE 賽事系統提供了一個確定性框架,用於比較多個 Elixir 實現。參賽者提交獨立的實現,系統通過多個維度評估它們以確定最優解。 | |
| ### 核心原則 | |
| 1. **確定性**: 相同輸入始終產生相同輸出 | |
| 2. **隔離性**: 每個參賽者在獨立上下文中運行 | |
| 3. **公平性**: 所有參賽者面對相同條件 | |
| 4. **透明度**: 完整的追蹤鏈 | |
| 5. **嚴謹性**: 正確性是所有指標的門控 | |
| ### 創建賽事 | |
| ```elixir | |
| Forge.Tournament.define "整數雜湊" do | |
| challenge "sha256" do | |
| input "forge" | |
| expected "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824" | |
| end | |
| end | |
| ``` | |
| 完整文檔請參見英文版本。 | |