{"slug":"domain-ml","title":"domain-ml","summary":"Use when building ML/AI apps in Rust. Keywords: machine learning, ML, AI, tensor, model, inference, neural network, deep learning, training, prediction, ndarray, tch-rs, burn, candle, 机器学习, 人工智能, 模型推理","platform":"Cursor","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-09-07T18:43:35.905171Z","repo":{"url":"https://github.com/moeru-ai/auv","stars":51,"forks":6,"license":"Apache-2.0","updatedAt":"2026-09-25T05:45:21Z"},"bodyHtml":"<hr>\n<h2>name: domain-ml\ndescription: \"Use when building ML/AI apps in Rust. Keywords: machine learning, ML, AI, tensor, model, inference, neural network, deep learning, training, prediction, ndarray, tch-rs, burn, candle, 机器学习, 人工智能, 模型推理\"\nuser-invocable: false</h2>\n<h1>Machine Learning Domain</h1>\n<blockquote>\n<p><strong>Layer 3: Domain Constraints</strong></p>\n</blockquote>\n<h2>Domain Constraints → Design Implications</h2>\n<table>\n<thead>\n<tr>\n<th>Domain Rule</th>\n<th>Design Constraint</th>\n<th>Rust Implication</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Large data</td>\n<td>Efficient memory</td>\n<td>Zero-copy, streaming</td>\n</tr>\n<tr>\n<td>GPU acceleration</td>\n<td>CUDA/Metal support</td>\n<td>candle, tch-rs</td>\n</tr>\n<tr>\n<td>Model portability</td>\n<td>Standard formats</td>\n<td>ONNX</td>\n</tr>\n<tr>\n<td>Batch processing</td>\n<td>Throughput over latency</td>\n<td>Batched inference</td>\n</tr>\n<tr>\n<td>Numerical precision</td>\n<td>Float handling</td>\n<td>ndarray, careful f32/f64</td>\n</tr>\n<tr>\n<td>Reproducibility</td>\n<td>Deterministic</td>\n<td>Seeded random, versioning</td>\n</tr>\n</tbody>\n</table>\n<hr>\n<h2>Critical Constraints</h2>\n<h3>Memory Efficiency</h3>\n<pre><code>RULE: Avoid copying large tensors\nWHY: Memory bandwidth is bottleneck\nRUST: References, views, in-place ops\n</code></pre>\n<h3>GPU Utilization</h3>\n<pre><code>RULE: Batch operations for GPU efficiency\nWHY: GPU overhead per kernel launch\nRUST: Batch sizes, async data loading\n</code></pre>\n<h3>Model Portability</h3>\n<pre><code>RULE: Use standard model formats\nWHY: Train in Python, deploy in Rust\nRUST: ONNX via tract or candle\n</code></pre>\n<hr>\n<h2>Trace Down ↓</h2>\n<p>From constraints to design (Layer 2):</p>\n<pre><code>\"Need efficient data pipelines\"\n    ↓ m10-performance: Streaming, batching\n    ↓ polars: Lazy evaluation\n\n\"Need GPU inference\"\n    ↓ m07-concurrency: Async data loading\n    ↓ candle/tch-rs: CUDA backend\n\n\"Need model loading\"\n    ↓ m12-lifecycle: Lazy init, caching\n    ↓ tract: ONNX runtime\n</code></pre>\n<hr>\n<h2>Use Case → Framework</h2>\n<table>\n<thead>\n<tr>\n<th>Use Case</th>\n<th>Recommended</th>\n<th>Why</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Inference only</td>\n<td>tract (ONNX)</td>\n<td>Lightweight, portable</td>\n</tr>\n<tr>\n<td>Training + inference</td>\n<td>candle, burn</td>\n<td>Pure Rust, GPU</td>\n</tr>\n<tr>\n<td>PyTorch models</td>\n<td>tch-rs</td>\n<td>Direct bindings</td>\n</tr>\n<tr>\n<td>Data pipelines</td>\n<td>polars</td>\n<td>Fast, lazy eval</td>\n</tr>\n</tbody>\n</table>\n<h2>Key Crates</h2>\n<table>\n<thead>\n<tr>\n<th>Purpose</th>\n<th>Crate</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Tensors</td>\n<td>ndarray</td>\n</tr>\n<tr>\n<td>ONNX inference</td>\n<td>tract</td>\n</tr>\n<tr>\n<td>ML framework</td>\n<td>candle, burn</td>\n</tr>\n<tr>\n<td>PyTorch bindings</td>\n<td>tch-rs</td>\n</tr>\n<tr>\n<td>Data processing</td>\n<td>polars</td>\n</tr>\n<tr>\n<td>Embeddings</td>\n<td>fastembed</td>\n</tr>\n</tbody>\n</table>\n<h2>Design Patterns</h2>\n<table>\n<thead>\n<tr>\n<th>Pattern</th>\n<th>Purpose</th>\n<th>Implementation</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Model loading</td>\n<td>Once, reuse</td>\n<td><code>OnceLock&lt;Model&gt;</code></td>\n</tr>\n<tr>\n<td>Batching</td>\n<td>Throughput</td>\n<td>Collect then process</td>\n</tr>\n<tr>\n<td>Streaming</td>\n<td>Large data</td>\n<td>Iterator-based</td>\n</tr>\n<tr>\n<td>GPU async</td>\n<td>Parallelism</td>\n<td>Data loading parallel to compute</td>\n</tr>\n</tbody>\n</table>\n<h2>Code Pattern: Inference Server</h2>\n<pre><code>use std::sync::OnceLock;\nuse tract_onnx::prelude::*;\n\nstatic MODEL: OnceLock&lt;SimplePlan&lt;TypedFact, Box&lt;dyn TypedOp&gt;, Graph&lt;TypedFact, Box&lt;dyn TypedOp&gt;&gt;&gt;&gt; = OnceLock::new();\n\nfn get_model() -&gt; &amp;'static SimplePlan&lt;...&gt; {\n    MODEL.get_or_init(|| {\n        tract_onnx::onnx()\n            .model_for_path(\"model.onnx\")\n            .unwrap()\n            .into_optimized()\n            .unwrap()\n            .into_runnable()\n            .unwrap()\n    })\n}\n\nasync fn predict(input: Vec&lt;f32&gt;) -&gt; anyhow::Result&lt;Vec&lt;f32&gt;&gt; {\n    let model = get_model();\n    let input = tract_ndarray::arr1(&amp;input).into_shape((1, input.len()))?;\n    let result = model.run(tvec!(input.into()))?;\n    Ok(result[0].to_array_view::&lt;f32&gt;()?.iter().copied().collect())\n}\n</code></pre>\n<h2>Code Pattern: Batched Inference</h2>\n<pre><code>async fn batch_predict(inputs: Vec&lt;Vec&lt;f32&gt;&gt;, batch_size: usize) -&gt; Vec&lt;Vec&lt;f32&gt;&gt; {\n    let mut results = Vec::with_capacity(inputs.len());\n\n    for batch in inputs.chunks(batch_size) {\n        // Stack inputs into batch tensor\n        let batch_tensor = stack_inputs(batch);\n\n        // Run inference on batch\n        let batch_output = model.run(batch_tensor).await;\n\n        // Unstack results\n        results.extend(unstack_outputs(batch_output));\n    }\n\n    results\n}\n</code></pre>\n<hr>\n<h2>Common Mistakes</h2>\n<table>\n<thead>\n<tr>\n<th>Mistake</th>\n<th>Domain Violation</th>\n<th>Fix</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Clone tensors</td>\n<td>Memory waste</td>\n<td>Use views</td>\n</tr>\n<tr>\n<td>Single inference</td>\n<td>GPU underutilized</td>\n<td>Batch processing</td>\n</tr>\n<tr>\n<td>Load model per request</td>\n<td>Slow</td>\n<td>Singleton pattern</td>\n</tr>\n<tr>\n<td>Sync data loading</td>\n<td>GPU idle</td>\n<td>Async pipeline</td>\n</tr>\n</tbody>\n</table>\n<hr>\n<h2>Trace to Layer 1</h2>\n<table>\n<thead>\n<tr>\n<th>Constraint</th>\n<th>Layer 2 Pattern</th>\n<th>Layer 1 Implementation</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Memory efficiency</td>\n<td>Zero-copy</td>\n<td>ndarray views</td>\n</tr>\n<tr>\n<td>Model singleton</td>\n<td>Lazy init</td>\n<td>OnceLock</td>\n</tr>\n<tr>\n<td>Batch processing</td>\n<td>Chunked iteration</td>\n<td>chunks() + parallel</td>\n</tr>\n<tr>\n<td>GPU async</td>\n<td>Concurrent loading</td>\n<td>tokio::spawn + GPU</td>\n</tr>\n</tbody>\n</table>\n<hr>\n<h2>Related Skills</h2>\n<table>\n<thead>\n<tr>\n<th>When</th>\n<th>See</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Performance</td>\n<td>m10-performance</td>\n</tr>\n<tr>\n<td>Lazy initialization</td>\n<td>m12-lifecycle</td>\n</tr>\n<tr>\n<td>Async patterns</td>\n<td>m07-concurrency</td>\n</tr>\n<tr>\n<td>Memory efficiency</td>\n<td>m01-ownership</td>\n</tr>\n</tbody>\n</table>\n","files":[{"path":"SKILL.md","sizeBytes":4711,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-09-07T18:43:46.536836Z","sha256":"31C9D82F1CB554AD6651BF37530770D04625172E13DD4C91ABEEB6C12397307F","sizeBytes":2173},"review":null,"source":{"repositoryUrl":"https://github.com/moeru-ai/auv","path":".agents/skills/domain-ml","license":"Apache-2.0","commit":"f986875427f770607918bff38dec171a0ed3d040","subtreeSha":"658B26C7FD793882DE5FBC46AB297BF7638D0E82E1615D6063E1F81CF49B2AB3","lastSyncedAt":"2026-09-25T06:49:27.585891Z"},"reviewedAt":"2026-09-07T18:50:42.909004Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/moeru-ai/auv/tree/main/.agents/skills/domain-ml"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install moeru-ai-auv@llmmart"},{"target":"git","command":"git clone https://github.com/moeru-ai/auv.git"}]}