{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# RusTorch CoreML Integration - Rust Kernel\n",
"\n",
"このノートブックは、RusTorchでCoreMLを使用する方法を示します。\n",
"これはRustカーネルで実行されます。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 必要な依存関係とfeatureを確認"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"// 基本的なRusTorchの使用法\n",
"extern crate rustorch;\n",
"\n",
"use rustorch::tensor::Tensor;\n",
"use rustorch::gpu::DeviceType;\n",
"\n",
"println!(\"RusTorch version: {}\", env!(\"CARGO_PKG_VERSION\"));\n",
"println!(\"Rust version: {}\", env!(\"RUSTC_VERSION\"));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## CoreMLの可用性をチェック\n",
"\n",
"システムでCoreMLが利用可能かどうかを確認します。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#[cfg(any(feature = \"coreml\", feature = \"coreml-hybrid\", feature = \"coreml-fallback\"))]\n",
"{\n",
" use rustorch::backends::DeviceManager;\n",
" \n",
" let coreml_available = DeviceManager::is_coreml_available();\n",
" println!(\"CoreML available: {}\", coreml_available);\n",
" \n",
" if coreml_available {\n",
" println!(\"🎉 CoreMLが利用可能です!\");\n",
" println!(\"プラットフォーム: macOS\");\n",
" \n",
" // デバイス情報を表示\n",
" use rustorch::gpu::coreml::device_cache::DeviceCache;\n",
" let cache = DeviceCache::global();\n",
" cache.warmup();\n",
" \n",
" let stats = cache.get_stats();\n",
" println!(\"Cache stats: {:?}\", stats);\n",
" } else {\n",
" println!(\"⚠️ CoreMLは利用できません\");\n",
" println!(\"CPUまたは他のGPUバックエンドを使用してください\");\n",
" }\n",
"}\n",
"\n",
"#[cfg(not(any(feature = \"coreml\", feature = \"coreml-hybrid\", feature = \"coreml-fallback\")))]\n",
"{\n",
" println!(\"❌ CoreMLフィーチャーが有効になっていません\");\n",
" println!(\"--features coreml でビルドしてください\");\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 基本的なテンソル操作"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"// 基本的なテンソルを作成\n",
"let a = Tensor::zeros(&[2, 3]);\n",
"let b = Tensor::ones(&[3, 2]);\n",
"\n",
"println!(\"Tensor A shape: {:?}\", a.shape());\n",
"println!(\"Tensor B shape: {:?}\", b.shape());\n",
"\n",
"// CPU上で行列乗算\n",
"match a.matmul(&b) {\n",
" Ok(result) => {\n",
" println!(\"行列乗算結果 shape: {:?}\", result.shape());\n",
" println!(\"CPU演算完了 ✓\");\n",
" }\n",
" Err(e) => println!(\"エラー: {:?}\", e),\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## CoreMLバックエンドの使用(利用可能な場合)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#[cfg(any(feature = \"coreml\", feature = \"coreml-hybrid\", feature = \"coreml-fallback\"))]\n",
"{\n",
" use rustorch::gpu::coreml::backend::{CoreMLBackend, CoreMLBackendConfig};\n",
" use rustorch::backends::DeviceManager;\n",
" \n",
" if DeviceManager::is_coreml_available() {\n",
" println!(\"🚀 CoreMLバックエンドを初期化中...\");\n",
" \n",
" let config = CoreMLBackendConfig {\n",
" enable_caching: true,\n",
" max_cache_size: 100,\n",
" auto_fallback: true,\n",
" enable_profiling: true,\n",
" };\n",
" \n",
" match CoreMLBackend::new(config) {\n",
" Ok(backend) => {\n",
" println!(\"✅ CoreMLバックエンド初期化完了\");\n",
" println!(\"Ready: {}\", backend.is_available());\n",
" \n",
" // バックエンド統計を表示\n",
" let stats = backend.get_stats();\n",
" println!(\"Backend stats: total_operations={}, cache_hits={}, cache_misses={}\", \n",
" stats.total_operations, stats.cache_hits, stats.cache_misses);\n",
" }\n",
" Err(e) => {\n",
" println!(\"❌ CoreMLバックエンド初期化失敗: {:?}\", e);\n",
" }\n",
" }\n",
" } else {\n",
" println!(\"⚠️ CoreMLは利用できないため、CPUで実行\");\n",
" }\n",
"}\n",
"\n",
"#[cfg(not(any(feature = \"coreml\", feature = \"coreml-hybrid\", feature = \"coreml-fallback\")))]\n",
"{\n",
" println!(\"CoreMLフィーチャーが無効です。CPUで実行します。\");\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## デバイス選択のデモンストレーション"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#[cfg(any(feature = \"coreml\", feature = \"coreml-hybrid\", feature = \"coreml-fallback\"))]\n",
"{\n",
" use rustorch::gpu::coreml::smart_device_selector::{\n",
" SmartDeviceSelector, OperationProfile, OperationType\n",
" };\n",
" use rustorch::gpu::DeviceType;\n",
" \n",
" // 利用可能なデバイスを定義\n",
" let available_devices = vec![\n",
" DeviceType::CoreML(0),\n",
" DeviceType::Metal(0),\n",
" DeviceType::Cpu,\n",
" ];\n",
" \n",
" let selector = SmartDeviceSelector::new(available_devices);\n",
" \n",
" // 異なる操作プロファイルでデバイス選択をテスト\n",
" let operations = vec![\n",
" (\"小さい行列乗算\", OperationProfile::new(\n",
" OperationType::MatrixMultiplication,\n",
" &[16, 16],\n",
" 4\n",
" )),\n",
" (\"大きい行列乗算\", OperationProfile::new(\n",
" OperationType::MatrixMultiplication,\n",
" &[512, 512],\n",
" 4\n",
" )),\n",
" (\"活性化関数\", OperationProfile::new(\n",
" OperationType::Activation,\n",
" &[32, 64, 128, 128],\n",
" 4\n",
" )),\n",
" (\"複素数演算 (CoreML非対応)\", OperationProfile::new(\n",
" OperationType::ComplexNumber,\n",
" &[128, 128],\n",
" 8\n",
" )),\n",
" ];\n",
" \n",
" println!(\"🎯 スマートデバイス選択のデモ:\");\n",
" for (name, profile) in operations {\n",
" let selected_device = selector.select_device(&profile);\n",
" println!(\" {}: {:?}\", name, selected_device);\n",
" }\n",
"}\n",
"\n",
"#[cfg(not(any(feature = \"coreml\", feature = \"coreml-hybrid\", feature = \"coreml-fallback\")))]\n",
"{\n",
" println!(\"デバイス選択機能はCoreMLフィーチャーが必要です。\");\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## まとめ\n",
"\n",
"このノートブックでは以下を実証しました:\n",
"\n",
"1. ✅ **CoreML可用性チェック**: システムでCoreMLが利用可能かどうかの確認\n",
"2. ✅ **基本的なテンソル操作**: CPUでの行列乗算\n",
"3. ✅ **CoreMLバックエンド**: 利用可能な場合のCoreMLバックエンド初期化\n",
"4. ✅ **スマートデバイス選択**: 操作特性に基づく最適なデバイス選択\n",
"\n",
"### 次のステップ\n",
"\n",
"- より複雑なニューラルネットワーク操作の実装\n",
"- パフォーマンス比較(CPU vs CoreML vs Metal)\n",
"- 実際のCoreMLモデルとの統合\n",
"\n",
"### 注意事項\n",
"\n",
"- CoreMLはmacOSでのみ利用可能です\n",
"- 一部の演算はCoreMLでサポートされていないため、自動的にCPUまたは他のGPUにフォールバックされます\n",
"- パフォーマンスは具体的なハードウェアと操作の種類によって異なります"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Rust",
"language": "rust",
"name": "rust"
},
"language_info": {
"codemirror_mode": "rust",
"file_extension": ".rs",
"mimetype": "text/rust",
"name": "rust",
"nbconvert_exporter": "script",
"version": "1.76.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}