Skip to content

Commit

Permalink
Rename device_index to devfile_index, and deprecate get_device()
Browse files Browse the repository at this point in the history
  • Loading branch information
n0gu-furiosa committed Apr 16, 2024
1 parent abc3937 commit e171f6b
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 118 deletions.
4 changes: 2 additions & 2 deletions bindings/python/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl DevicePy {

/// Returns the device index (e.g., 0 for npu0).
fn device_index(&self) -> u8 {
self.inner.device_index()
self.inner.devfile_index()
}

/// Returns `Arch` of the device(e.g., `Warboy`).
Expand Down Expand Up @@ -361,7 +361,7 @@ impl DeviceFilePy {

/// Returns the device index (e.g., 1 for npu1pe0).
fn device_index(&self) -> u8 {
self.inner.device_index()
self.inner.devfile_index()
}

/// Returns the range of cores this device file may occupy.
Expand Down
25 changes: 1 addition & 24 deletions bindings/python/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use furiosa_device::{find_device_files, get_device, get_device_file, list_devices};
use furiosa_device::{find_device_files, get_device_file, list_devices};
use hwmon::FetcherPy;
use pyo3::prelude::*;

Expand Down Expand Up @@ -46,28 +46,6 @@ fn list_devices_python(py: Python<'_>) -> PyResult<&PyAny> {
})
}

/// `get_device` returns a specific Furiosa NPU device in the system.
/// One can simply call as below:
/// ```python
/// import asyncio
/// from furiosa_device import get_device
///
/// async def main():
/// device = await furiosa_device.get_device(0)
/// asyncio.run(main())
/// ```
///
/// `Device` offers methods for further information of each device.
#[pyfunction(name = "get_device")]
fn get_device_python(py: Python<'_>, arch: ArchPy, idx: u8) -> PyResult<&PyAny> {
pyo3_asyncio::tokio::future_into_py(py, async move {
get_device(arch.into(), idx)
.await
.map(DevicePy::new)
.map_err(to_py_err)
})
}

/// If you have a desired configuration, call `find_device_files` with your device configuration
/// described by a `DeviceConfig`. `find_device_files` will return a list of
/// `DeviceFile`s if there are matched devices.
Expand Down Expand Up @@ -118,7 +96,6 @@ fn get_device_file_python(py: Python<'_>, device_name: String) -> PyResult<&PyAn
#[pyo3(name = "furiosa_native_device")]
fn furiosa_device_python(py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(list_devices_python, m)?)?;
m.add_function(wrap_pyfunction!(get_device_python, m)?)?;
m.add_function(wrap_pyfunction!(find_device_files_python, m)?)?;
m.add_function(wrap_pyfunction!(get_device_file_python, m)?)?;
m.add_class::<DevicePy>()?;
Expand Down
12 changes: 1 addition & 11 deletions bindings/python/src/sync.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::collections::HashMap;

use furiosa_device::blocking::{find_device_files, get_device, get_device_file, list_devices};
use furiosa_device::blocking::{find_device_files, get_device_file, list_devices};
use pyo3::prelude::*;
use tokio::runtime::Runtime;

use crate::device::{CoreStatusPy, DeviceFilePy, DevicePy};
use crate::errors::to_py_err;
use crate::hwmon::{FetcherPy, SensorValuePy};
use crate::ArchPy;
use crate::DeviceConfigPy;

#[pyclass(extends=DevicePy, name="DeviceSync")]
Expand Down Expand Up @@ -129,14 +128,6 @@ fn list_devices_python_sync(py: Python<'_>) -> PyResult<Vec<Py<PyAny>>> {
Ok(device_syncs)
}

/// This is sync version of get_device
#[pyfunction(name = "get_device")]
fn get_device_python_sync(arch: ArchPy, idx: u8) -> PyResult<DevicePy> {
get_device(arch.into(), idx)
.map(DevicePy::new)
.map_err(to_py_err)
}

/// This is sync version of find_device_files
#[pyfunction(name = "find_device_files")]
fn find_device_files_python_sync(config: DeviceConfigPy) -> PyResult<Vec<DeviceFilePy>> {
Expand All @@ -161,7 +152,6 @@ fn get_device_file_python_sync(device_name: String) -> PyResult<DeviceFilePy> {
#[pyo3(name = "furiosa_native_device_sync")]
pub fn furiosa_device_python_sync(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(list_devices_python_sync, m)?)?;
m.add_function(wrap_pyfunction!(get_device_python_sync, m)?)?;
m.add_function(wrap_pyfunction!(find_device_files_python_sync, m)?)?;
m.add_function(wrap_pyfunction!(get_device_file_python_sync, m)?)?;
m.add_class::<DeviceSyncPy>()?;
Expand Down
2 changes: 1 addition & 1 deletion device-api/bin/list_clock_frequency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use furiosa_device::{list_devices, DeviceError};
#[tokio::main]
async fn main() -> Result<(), DeviceError> {
for device in list_devices().await? {
println!("-- npu{} --", device.device_index());
println!("-- npu{} --", device.devfile_index());
for frequency in device.clock_frequency()? {
println!(
"{:15}: {} {}",
Expand Down
2 changes: 1 addition & 1 deletion device-api/bin/list_hwmon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ async fn main() -> Result<(), DeviceError> {
for device in list_devices().await? {
let fetcher = device.get_hwmon_fetcher();

println!("-- npu{} --", device.device_index());
println!("-- npu{} --", device.devfile_index());
println!("Current");
for sensor_value in fetcher.read_currents().await? {
println!(
Expand Down
10 changes: 5 additions & 5 deletions device-api/src/arch/renegade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::DeviceError;
#[derive(Clone)]
pub struct RenegadeInner {
arch: Arch,
device_index: u8,
devfile_index: u8,
sysfs: PathBuf,
mgmt_root: PathBuf,
mgmt_cache: MgmtCache<StaticMgmtFile>,
Expand All @@ -29,7 +29,7 @@ impl RenegadeInner {

Ok(RenegadeInner {
arch,
device_index,
devfile_index: device_index,
sysfs,
mgmt_root,
mgmt_cache,
Expand Down Expand Up @@ -73,8 +73,8 @@ impl DeviceMgmt for RenegadeInner {
&self.sysfs
}

fn device_index(&self) -> u8 {
self.device_index
fn devfile_index(&self) -> u8 {
self.devfile_index
}

#[inline]
Expand Down Expand Up @@ -169,7 +169,7 @@ mod tests {
let device =
RenegadeInner::new(Arch::Renegade, 0, PathBuf::from("../test_data/test-1/sys"))?;

assert_eq!(device.device_index(), 0);
assert_eq!(device.devfile_index(), 0);
assert_eq!(device.arch(), Arch::Renegade);
assert!(device.alive()?);
assert_eq!(device.atr_error()?.len(), 9);
Expand Down
10 changes: 5 additions & 5 deletions device-api/src/arch/warboy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{Arch, ClockFrequency, DeviceError, DeviceFile};
#[derive(Clone)]
pub struct WarboyInner {
arch: Arch,
device_index: u8,
devfile_index: u8,
sysfs: PathBuf,
mgmt_root: PathBuf,
mgmt_cache: MgmtCache<StaticMgmtFile>,
Expand All @@ -26,7 +26,7 @@ impl WarboyInner {

Ok(WarboyInner {
arch,
device_index,
devfile_index: device_index,
sysfs,
mgmt_root,
mgmt_cache,
Expand Down Expand Up @@ -70,8 +70,8 @@ impl DeviceMgmt for WarboyInner {
&self.sysfs
}

fn device_index(&self) -> u8 {
self.device_index
fn devfile_index(&self) -> u8 {
self.devfile_index
}

fn arch(&self) -> Arch {
Expand Down Expand Up @@ -173,7 +173,7 @@ mod tests {
fn test_warboy_inner_functionality() -> eyre::Result<()> {
let device = WarboyInner::new(Arch::WarboyB0, 0, PathBuf::from("../test_data/test-1/sys"))?;

assert_eq!(device.device_index(), 0);
assert_eq!(device.devfile_index(), 0);
assert_eq!(device.arch(), Arch::WarboyB0);
assert!(device.alive()?);
assert_eq!(device.atr_error()?.len(), 9);
Expand Down
19 changes: 0 additions & 19 deletions device-api/src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ pub(crate) fn list_devices_with_arch(
Ok(devices)
}

/// Return a specific Furiosa NPU device in the system.
pub fn get_device(arch: Arch, idx: u8) -> DeviceResult<Device> {
get_device_with(arch, idx, "/dev", "/sys")
}

/// Find a set of devices with specific configuration.
pub fn find_device_files(config: &DeviceConfig) -> DeviceResult<Vec<DeviceFile>> {
let devices = expand_status(list_devices()?)?;
Expand Down Expand Up @@ -95,20 +90,6 @@ pub(crate) fn get_file_with(devfs: &str, device_name: &str) -> DeviceResult<Devi
DeviceFile::try_from(&path)
}

pub(crate) fn get_device_with(
arch: Arch,
idx: u8,
devfs: &str,
sysfs: &str,
) -> DeviceResult<Device> {
let mut npu_dev_files = filter_dev_files(list_dev_files(arch.devfile_path(devfs))?)?;
if let Some(paths) = npu_dev_files.remove(&idx) {
get_device_inner(arch, idx, paths, devfs, sysfs)
} else {
Err(DeviceError::device_not_found(format!("npu{idx}")))
}
}

pub(crate) fn get_device_inner(
arch: Arch,
idx: u8,
Expand Down
2 changes: 1 addition & 1 deletion device-api/src/config/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Config {
device_id,
core_range,
} => {
device_file.device_index() == *device_id && device_file.core_range() == *core_range
device_file.devfile_index() == *device_id && device_file.core_range() == *core_range
}
Self::Unnamed {
arch: config_arch,
Expand Down
Loading

0 comments on commit e171f6b

Please sign in to comment.