Mayo commited on
refactor: simplify device name logic
Browse files- koharu-ml/src/lib.rs +2 -32
- koharu-pipeline/src/lib.rs +2 -2
- koharu-pipeline/src/operations.rs +5 -1
- koharu/src/app.rs +2 -3
koharu-ml/src/lib.rs
CHANGED
|
@@ -8,42 +8,12 @@ pub mod llm;
|
|
| 8 |
pub mod manga_ocr;
|
| 9 |
|
| 10 |
use anyhow::Result;
|
| 11 |
-
use candle_core::
|
| 12 |
|
|
|
|
| 13 |
pub use koharu_http::hf_hub::set_cache_dir;
|
| 14 |
pub use llm::{language_from_tag, set_default_locale, set_locale, supported_locales};
|
| 15 |
|
| 16 |
-
/// Name of the compute device being used.
|
| 17 |
-
#[derive(Debug, Clone, PartialEq, Eq)]
|
| 18 |
-
pub enum DeviceName {
|
| 19 |
-
Cpu,
|
| 20 |
-
Cuda,
|
| 21 |
-
Metal,
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
-
impl std::fmt::Display for DeviceName {
|
| 25 |
-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
| 26 |
-
match self {
|
| 27 |
-
DeviceName::Cpu => write!(f, "CPU"),
|
| 28 |
-
DeviceName::Cuda => write!(f, "CUDA"),
|
| 29 |
-
DeviceName::Metal => write!(f, "Metal"),
|
| 30 |
-
}
|
| 31 |
-
}
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
/// Returns the name of the device that would be selected.
|
| 35 |
-
pub fn device_name(cpu: bool) -> DeviceName {
|
| 36 |
-
if cpu {
|
| 37 |
-
DeviceName::Cpu
|
| 38 |
-
} else if cuda_is_available() {
|
| 39 |
-
DeviceName::Cuda
|
| 40 |
-
} else if metal_is_available() {
|
| 41 |
-
DeviceName::Metal
|
| 42 |
-
} else {
|
| 43 |
-
DeviceName::Cpu
|
| 44 |
-
}
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
pub fn device(cpu: bool) -> Result<Device> {
|
| 48 |
if cpu {
|
| 49 |
Ok(Device::Cpu)
|
|
|
|
| 8 |
pub mod manga_ocr;
|
| 9 |
|
| 10 |
use anyhow::Result;
|
| 11 |
+
use candle_core::utils::metal_is_available;
|
| 12 |
|
| 13 |
+
pub use candle_core::Device;
|
| 14 |
pub use koharu_http::hf_hub::set_cache_dir;
|
| 15 |
pub use llm::{language_from_tag, set_default_locale, set_locale, supported_locales};
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
pub fn device(cpu: bool) -> Result<Device> {
|
| 18 |
if cpu {
|
| 19 |
Ok(Device::Cpu)
|
koharu-pipeline/src/lib.rs
CHANGED
|
@@ -3,7 +3,7 @@ pub mod pipeline;
|
|
| 3 |
|
| 4 |
use std::sync::Arc;
|
| 5 |
|
| 6 |
-
use koharu_ml::
|
| 7 |
use koharu_renderer::facade::Renderer;
|
| 8 |
use koharu_types::AppState;
|
| 9 |
use tokio::sync::RwLock;
|
|
@@ -14,7 +14,7 @@ pub struct AppResources {
|
|
| 14 |
pub ml: Arc<koharu_ml::facade::Model>,
|
| 15 |
pub llm: Arc<koharu_ml::llm::facade::Model>,
|
| 16 |
pub renderer: Arc<Renderer>,
|
| 17 |
-
pub
|
| 18 |
pub pipeline: Arc<RwLock<Option<pipeline::PipelineHandle>>>,
|
| 19 |
pub version: &'static str,
|
| 20 |
}
|
|
|
|
| 3 |
|
| 4 |
use std::sync::Arc;
|
| 5 |
|
| 6 |
+
use koharu_ml::Device;
|
| 7 |
use koharu_renderer::facade::Renderer;
|
| 8 |
use koharu_types::AppState;
|
| 9 |
use tokio::sync::RwLock;
|
|
|
|
| 14 |
pub ml: Arc<koharu_ml::facade::Model>,
|
| 15 |
pub llm: Arc<koharu_ml::llm::facade::Model>,
|
| 16 |
pub renderer: Arc<Renderer>,
|
| 17 |
+
pub device: Device,
|
| 18 |
pub pipeline: Arc<RwLock<Option<pipeline::PipelineHandle>>>,
|
| 19 |
pub version: &'static str,
|
| 20 |
}
|
koharu-pipeline/src/operations.rs
CHANGED
|
@@ -28,7 +28,11 @@ pub struct DeviceInfo {
|
|
| 28 |
|
| 29 |
pub async fn device(state: AppResources) -> anyhow::Result<DeviceInfo> {
|
| 30 |
Ok(DeviceInfo {
|
| 31 |
-
ml_device: state.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
wgpu: state.renderer.wgpu_device_info(),
|
| 33 |
})
|
| 34 |
}
|
|
|
|
| 28 |
|
| 29 |
pub async fn device(state: AppResources) -> anyhow::Result<DeviceInfo> {
|
| 30 |
Ok(DeviceInfo {
|
| 31 |
+
ml_device: match state.device {
|
| 32 |
+
koharu_ml::Device::Cpu => "CPU".to_string(),
|
| 33 |
+
koharu_ml::Device::Cuda(_) => "CUDA".to_string(),
|
| 34 |
+
koharu_ml::Device::Metal(_) => "Metal".to_string(),
|
| 35 |
+
},
|
| 36 |
wgpu: state.renderer.wgpu_device_info(),
|
| 37 |
})
|
| 38 |
}
|
koharu/src/app.rs
CHANGED
|
@@ -8,7 +8,7 @@ use tauri::Manager;
|
|
| 8 |
use tokio::{net::TcpListener, sync::RwLock};
|
| 9 |
use tracing_subscriber::fmt::format::FmtSpan;
|
| 10 |
|
| 11 |
-
use koharu_ml::{cuda_is_available,
|
| 12 |
use koharu_pipeline::AppResources;
|
| 13 |
use koharu_renderer::facade::Renderer;
|
| 14 |
use koharu_rpc::{SharedResources, server};
|
|
@@ -131,7 +131,6 @@ async fn build_resources(cpu: bool) -> Result<AppResources> {
|
|
| 131 |
);
|
| 132 |
}
|
| 133 |
|
| 134 |
-
let ml_device = device_name(cpu);
|
| 135 |
let ml = Arc::new(
|
| 136 |
koharu_ml::facade::Model::new(cpu)
|
| 137 |
.await
|
|
@@ -146,7 +145,7 @@ async fn build_resources(cpu: bool) -> Result<AppResources> {
|
|
| 146 |
ml,
|
| 147 |
llm,
|
| 148 |
renderer,
|
| 149 |
-
|
| 150 |
pipeline: Arc::new(RwLock::new(None)),
|
| 151 |
version: crate::version::current(),
|
| 152 |
})
|
|
|
|
| 8 |
use tokio::{net::TcpListener, sync::RwLock};
|
| 9 |
use tracing_subscriber::fmt::format::FmtSpan;
|
| 10 |
|
| 11 |
+
use koharu_ml::{cuda_is_available, device};
|
| 12 |
use koharu_pipeline::AppResources;
|
| 13 |
use koharu_renderer::facade::Renderer;
|
| 14 |
use koharu_rpc::{SharedResources, server};
|
|
|
|
| 131 |
);
|
| 132 |
}
|
| 133 |
|
|
|
|
| 134 |
let ml = Arc::new(
|
| 135 |
koharu_ml::facade::Model::new(cpu)
|
| 136 |
.await
|
|
|
|
| 145 |
ml,
|
| 146 |
llm,
|
| 147 |
renderer,
|
| 148 |
+
device: device(cpu)?,
|
| 149 |
pipeline: Arc::new(RwLock::new(None)),
|
| 150 |
version: crate::version::current(),
|
| 151 |
})
|