Skip to content

Commit

Permalink
improve logging of cpu feautres
Browse files Browse the repository at this point in the history
  • Loading branch information
kali committed Oct 3, 2023
1 parent ecc9516 commit ae17ea2
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 9 deletions.
1 change: 1 addition & 0 deletions linalg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ walkdir.workspace = true

[dev-dependencies]
criterion.workspace = true
env_logger.workspace = true
nu-ansi-term.workspace = true
proptest.workspace = true
core_affinity.workspace = true
Expand Down
28 changes: 19 additions & 9 deletions linalg/src/arm64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ use crate::Ops;
use crate::frame::element_wise::ElementWiseKer;
use crate::frame::mmm::kernel::MatMatMulKer;

lazy_static::lazy_static! {
static ref KIND: Kind = Kind::choose();
}

// https://en.wikipedia.org/wiki/Comparison_of_ARMv8-A_cores
const PART_A53: &str = "0xd03";
const PART_A55: &str = "0xd05";
Expand All @@ -38,13 +34,23 @@ fn max_cpuid() -> std::io::Result<String> {
}

lazy_static::lazy_static! {
static ref KIND: Kind = Kind::choose();

static ref CPU_FEATURES: Vec<String> = {
let cpu_info = std::fs::read_to_string("/proc/cpuinfo").unwrap();
let line = cpu_info
#[cfg(test)] crate::setup_test_logger();
let Ok(cpu_info) = std::fs::read_to_string("/proc/cpuinfo") else {
log::warn!("Could not read /proc/cpuinfo. CPU Features detection may be impaired.");
return vec!();
};
if let Some(line) = cpu_info
.lines()
.filter(|line| line.starts_with("Features"))
.next().unwrap();
line.split_once(":").unwrap().1.split_whitespace().map(|s| s.to_string()).collect()
.next() {
line.split_once(":").unwrap().1.split_whitespace().map(|s| s.to_string()).collect()
} else {
log::warn!("Could not find \"Features :\" lines in /proc/cpuinfo. CPU Features detection may be impaired.");
vec!()
}
};

static ref HAS_FP16: bool = {
Expand All @@ -54,7 +60,7 @@ lazy_static::lazy_static! {

#[inline]
pub fn has_fp16() -> bool {
*HAS_FP16
cfg!(feature_cpu = "fp16") || *KIND == Kind::CortexA55 || *KIND == Kind::CortexA75 || *HAS_FP16
}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
Expand All @@ -70,6 +76,8 @@ enum Kind {

impl Kind {
fn choose() -> Kind {
#[cfg(test)]
crate::setup_test_logger();
let kind = if let Ok(kind) = std::env::var("TRACT_CPU_AARCH64_KIND") {
log::info!("CPU kind forced with TRACT_CPU_AARCH64_KIND: {}", kind);
let kind = kind.to_lowercase();
Expand Down Expand Up @@ -195,6 +203,8 @@ pub fn plug(ops: &mut Ops) {
log::info!("ARMv8.2 tanh_f16 and sigmoid_f16 activated");
ops.tanh_f16 = Box::new(|| arm64fp16_tanh_f16_8n::ew());
ops.sigmoid_f16 = Box::new(|| arm64fp16_sigmoid_f16_8n::ew());
} else {
log::info!("No native fp16 support");
}
#[cfg(target_os = "macos")]
{
Expand Down
1 change: 1 addition & 0 deletions linalg/src/frame/element_wise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub mod test {
values: &[T],
reference: F,
) -> TestCaseResult {
crate::setup_test_logger();
let op = ElementWiseImpl::<K, T>::new();
let mut values = values.to_vec();
while values.len() < K::nr() {
Expand Down
3 changes: 3 additions & 0 deletions linalg/src/frame/mmm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ where
i32: AsPrimitive<TI>,
usize: AsPrimitive<TI>,
{
crate::setup_test_logger();
assert_eq!(a.datum_type(), TA::datum_type());
let op = MatMatMulImpl::<K, TI>::default();
unsafe {
Expand Down Expand Up @@ -281,6 +282,7 @@ where
i32: AsPrimitive<TI>,
usize: AsPrimitive<TI>,
{
crate::setup_test_logger();
unsafe {
let op = MatMatMulImpl::<K, TI>::default();
let mut packed_a =
Expand Down Expand Up @@ -327,6 +329,7 @@ where
i32: AsPrimitive<TI>,
usize: AsPrimitive<TI>,
{
crate::setup_test_logger();
let op = MatMatMulImpl::<K, TI>::default();

let mut found = Tensor::zero::<TC>(&[m, n]).unwrap();
Expand Down
1 change: 1 addition & 0 deletions linalg/src/frame/sigmoid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub mod test {
f32: AsPrimitive<T>,
T: AsPrimitive<f32>,
{
crate::setup_test_logger();
let values: Vec<T> = values.iter().copied().map(|x| x.as_()).collect();
crate::frame::element_wise::test::test_element_wise::<K, _, _>(&values, |x| {
(1f32).as_() / (1f32.as_() + (-x).exp())
Expand Down
1 change: 1 addition & 0 deletions linalg/src/frame/tanh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub mod test {
f32: AsPrimitive<T>,
T: AsPrimitive<f32>,
{
crate::setup_test_logger();
let values: Vec<T> = values.iter().copied().map(|x| x.as_()).collect();
crate::frame::element_wise::test::test_element_wise::<K, _, _>(&values, |x| x.tanh())
}
Expand Down
6 changes: 6 additions & 0 deletions linalg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,9 @@ impl LADatum for i32 {
any::<i32>().boxed()
}
}

#[cfg(test)]
#[allow(dead_code)]
fn setup_test_logger() {
let _ = env_logger::Builder::from_env("TRACT_LOG").try_init();
}

0 comments on commit ae17ea2

Please sign in to comment.