Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

update ios energy report

eugene4f79d8fd84a8
diff --git a/Cargo.lock b/Cargo.lock
index 93d25dad2..0216a14b3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3193,6 +3193,7 @@ dependencies = [
"serde",
"serde_json",
"sysinfo",
+ "thiserror 2.0.18",
]
[[package]]
diff --git a/apps/playground/Sources/Extensions/ReplyStatsFormat.swift b/apps/playground/Sources/Extensions/ReplyStatsFormat.swift
index b52af55e0..0c2ee6434 100644
--- a/apps/playground/Sources/Extensions/ReplyStatsFormat.swift
+++ b/apps/playground/Sources/Extensions/ReplyStatsFormat.swift
@@ -1,32 +1,21 @@
import Foundation
+import Uzu
-// Shared reply-stat formatting: fixed-unit MeasurementFormatter + ByteCountFormatter.
enum ReplyStatsFormat {
static let placeholder = "—"
- // macOS = SoC package power; iOS = whole-device charger "wall" power.
- static var powerLabel: String {
- #if os(macOS)
- return "Power (SoC):"
- #else
- return "Power (wall):"
- #endif
- }
-
- // Power/energy values are uncertain on iOS (and unset until a run completes), so the
- // caller renders these rows only when the values are present — these just format.
- static func power(average: Double) -> String {
- let avg = measurement.string(from: Measurement(value: average, unit: UnitPower.watts))
- return "\(avg) avg"
- }
-
static func energy(_ joules: Double) -> String {
measurement.string(from: Measurement(value: joules, unit: UnitEnergy.joules))
}
- static func energyPerToken(_ joulesPerToken: Double) -> String {
- let value = number.string(from: NSNumber(value: joulesPerToken)) ?? placeholder
- return "\(value) J/tok"
+ static func energyPerToken(_ stats: ChatReplyJoulesPerToken) -> String {
+ let total = formattedNumber(stats.total())
+ switch stats {
+ case .total:
+ return "\(total) J/tok"
+ case let .components(cpu, gpu, ane, dram):
+ return "CPU \(formattedNumber(cpu)), GPU \(formattedNumber(gpu)), ANE \(formattedNumber(ane)), DRAM \(formattedNumber(dram)), total \(total) J/tok"
+ }
}
static func memory(_ bytes: Int64?) -> String {
@@ -34,6 +23,10 @@ enum ReplyStatsFormat {
return byteCount.string(fromByteCount: max(bytes, 0))
}
+ private static func formattedNumber(_ value: Double) -> String {
+ number.string(from: NSNumber(value: value)) ?? placeholder
+ }
+
private static let number: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
diff --git a/apps/playground/Sources/ViewModels/ReplyStats.swift b/apps/playground/Sources/ViewModels/ReplyStats.swift
index 0812cd096..f2aa02dbb 100644
--- a/apps/playground/Sources/ViewModels/ReplyStats.swift
+++ b/apps/playground/Sources/ViewModels/ReplyStats.swift
@@ -5,18 +5,18 @@ struct ReplyStats: Equatable {
let timeToFirstToken: Double
let tokensPerSecond: Double
let memoryUsedBytes: Int64?
- let totalPower: Double?
let totalEnergy: Double?
- let joulesPerToken: Double?
+ let inputJoulesPerToken: ChatReplyJoulesPerToken?
+ let outputJoulesPerToken: ChatReplyJoulesPerToken?
let totalTime: Double
init(stats: ChatReplyStats) {
timeToFirstToken = stats.timeToFirstToken ?? 0.0
tokensPerSecond = stats.generateTokensPerSecond ?? 0.0
memoryUsedBytes = stats.memoryUsedBytes
- totalPower = stats.powerStats?.averageTotalWatts
- totalEnergy = stats.powerStats?.energyJoules
- joulesPerToken = stats.joulesPerToken()
+ totalEnergy = stats.totalJoules()
+ inputJoulesPerToken = stats.inputJoulesPerToken()
+ outputJoulesPerToken = stats.outputJoulesPerToken()
totalTime = stats.duration
}
}
diff --git a/apps/playground/Sources/Views/ReplyStatsView.swift b/apps/playground/Sources/Views/ReplyStatsView.swift
index c6c4ebc20..e5a074639 100644
--- a/apps/playground/Sources/Views/ReplyStatsView.swift
+++ b/apps/playground/Sources/Views/ReplyStatsView.swift
@@ -1,6 +1,5 @@
import SwiftUI
-// Shared stats footer shown under a chat reply, classification result, or summary.
struct ReplyStatsView: View {
let stats: ReplyStats
@@ -16,14 +15,14 @@ struct ReplyStatsView: View {
metricRow(label: "Tokens per second:", value: String(format: "%.3f t/s", stats.tokensPerSecond))
}
metricRow(label: "Memory used:", value: ReplyStatsFormat.memory(stats.memoryUsedBytes))
- if let power = stats.totalPower {
- metricRow(label: ReplyStatsFormat.powerLabel, value: ReplyStatsFormat.power(average: power))
- }
if let energy = stats.totalEnergy {
- metricRow(label: "Energy:", value: ReplyStatsFormat.energy(energy))
+ metricRow(label: "Total energy:", value: ReplyStatsFormat.energy(energy))
+ }
+ if let inputJoulesPerToken = stats.inputJoulesPerToken {
+ metricRow(label: "Input energy / token:", value: ReplyStatsFormat.energyPerToken(inputJoulesPerToken))
}
- if let joulesPerToken = stats.joulesPerToken {
- metricRow(label: "Energy / token:", value: ReplyStatsFormat.energyPerToken(joulesPerToken))
+ if let outputJoulesPerToken = stats.outputJoulesPerToken {
+ metricRow(label: "Output energy / token:", value: ReplyStatsFormat.energyPerToken(outputJoulesPerToken))
}
metricRow(label: "Total time:", value: String(format: "%.3f s", stats.totalTime))
}
diff --git a/crates/legacy/backend-remote/src/openai/stream_state.rs b/crates/legacy/backend-remote/src/openai/stream_state.rs
index 6f69cd090..79b342f73 100644
--- a/crates/legacy/backend-remote/src/openai/stream_state.rs
+++ b/crates/legacy/backend-remote/src/openai/stream_state.rs
@@ -153,7 +153,8 @@ impl StreamState {
tokens_count_output: self.tokens_output,
memory_used_bytes: None,
speculator_stats: None,
- power_stats: None,
+ input_energy: None,
+ output_energy: None,
}
}
}
diff --git a/crates/legacy/cli/src/bench/mod.rs b/crates/legacy/cli/src/bench/mod.rs
index 53a22d3b3..d904fae0f 100644
--- a/crates/legacy/cli/src/bench/mod.rs
+++ b/crates/legacy/cli/src/bench/mod.rs
@@ -32,7 +32,6 @@ pub async fn run_bench(
.map_err(|error| anyhow::anyhow!(error.to_string()))?;
progress_bar.finish();
- // Results
let results_data = serde_json::to_string_pretty(&results).context("Failed to serialize benchmark results")?;
fs::write(&output_path, results_data)
.await
@@ -53,13 +52,13 @@ pub async fn run_bench(
results.iter().map(|result| result.prompt_tokens_per_second).collect::<Vec<f64>>();
let generate_tokens_per_second_list =
results.iter().filter_map(|result| result.generate_tokens_per_second).collect::<Vec<f64>>();
- let average_total_watts_list = results
- .iter()
- .filter_map(|result| result.power_stats.as_ref().map(|power| power.average_total_watts))
- .collect::<Vec<f64>>();
let energy_joules_list = results
.iter()
- .filter_map(|result| result.power_stats.as_ref().map(|power| power.energy_joules))
+ .filter_map(|result| {
+ let mut phases = result.input_energy.iter().chain(result.output_energy.iter()).map(|energy| energy.total());
+ let first = phases.next()?;
+ Some(first + phases.sum::<f64>())
+ })
.collect::<Vec<f64>>();
let joules_per_token_list = results.iter().filter_map(|result| result.joules_per_token).collect::<Vec<f64>>();
@@ -67,7 +66,6 @@ pub async fn run_bench(
let time_to_first_token_metric = calculate_metric(&time_to_first_token_list);
let prompt_tokens_per_second_metric = calculate_metric(&prompt_tokens_per_second_list);
let generate_tokens_per_second_metric = calculate_metric(&generate_tokens_per_second_list);
- let average_total_watts_metric = calculate_metric(&average_total_watts_list);
let energy_joules_metric = calculate_metric(&energy_joules_list);
let joules_per_token_metric = calculate_metric(&joules_per_token_list);
@@ -81,7 +79,6 @@ pub async fn run_bench(
.add_row(vec!["TTFT, s", time_to_first_token_metric.as_str()])
.add_row(vec!["Prompt, t/s", prompt_tokens_per_second_metric.as_str()])
.add_row(vec!["Generate, t/s", generate_tokens_per_second_metric.as_str()])
- .add_row(vec!["Total power, W", average_total_watts_metric.as_str()])
.add_row(vec!["Total energy, J", energy_joules_metric.as_str()])
.add_row(vec!["Energy per token, J/tok", joules_per_token_metric.as_str()]);
let Some(column) = table.column_mut(1) else {
diff --git a/crates/legacy/cli/src/bench/model.rs b/crates/legacy/cli/src/bench/model.rs
index 3c499b661..8cabd10ea 100644
--- a/crates/legacy/cli/src/bench/model.rs
+++ b/crates/legacy/cli/src/bench/model.rs
@@ -1,5 +1,5 @@
use rocket::serde::{Deserialize, Serialize};
-use uzu::types::session::chat::{ChatMessage, ChatReplyPowerStats, ChatRole};
+use uzu::types::session::chat::{ChatMessage, ChatReplyEnergy, ChatRole};
use uzu_engine::data_type::DataType;
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -64,7 +64,8 @@ pub struct BenchResult {
pub time_to_first_token: f64,
pub prompt_tokens_per_second: f64,
pub generate_tokens_per_second: Option<f64>,
- pub power_stats: Option<ChatReplyPowerStats>,
+ pub input_energy: Option<ChatReplyEnergy>,
+ pub output_energy: Option<ChatReplyEnergy>,
pub joules_per_token: Option<f64>,
pub text: String,
}
diff --git a/crates/legacy/cli/src/bench/runner.rs b/crates/legacy/cli/src/bench/runner.rs
index b9f16c48c..f46695698 100644
--- a/crates/legacy/cli/src/bench/runner.rs
+++ b/crates/legacy/cli/src/bench/runner.rs
@@ -11,7 +11,7 @@ use uzu::{
types::{
basic::SamplingMethod,
model::ModelAccessibility,
- session::chat::{ChatConfig, ChatMessage, ChatReplyConfig, ChatReplyPowerStats},
+ session::chat::{ChatConfig, ChatMessage, ChatReplyConfig, ChatReplyEnergy},
},
};
use uzu_engine::{VERSION, data_type::DataType};
@@ -103,13 +103,13 @@ impl BenchRunner {
}
let generate_tokens_per_second = mean(&generate_tokens_per_second);
- let power_stats_list =
- replies.iter().filter_map(|reply| reply.stats.power_stats.as_ref()).collect::<Vec<_>>();
- let power_stats = aggregate_power_stats(&power_stats_list);
- let joules_per_token = power_stats.as_ref().and_then(|power| {
- let tokens_count = tokens_count_input + tokens_count_output;
- (tokens_count > 0).then(|| power.energy_joules / tokens_count as f64)
- });
+ let input_energy = aggregate_energy(replies.iter().filter_map(|reply| reply.stats.input_energy.as_ref()));
+ let output_energy = aggregate_energy(replies.iter().filter_map(|reply| reply.stats.output_energy.as_ref()));
+ let total_joules =
+ aggregate_energy(input_energy.iter().chain(output_energy.iter())).map(|energy| energy.total());
+ let tokens_count = tokens_count_input + tokens_count_output;
+ let joules_per_token =
+ total_joules.and_then(|joules| (tokens_count > 0).then(|| joules / tokens_count as f64));
let result = BenchResult {
task: self.task.clone(),
@@ -123,7 +123,8 @@ impl BenchRunner {
time_to_first_token,
prompt_tokens_per_second,
generate_tokens_per_second,
- power_stats,
+ input_energy,
+ output_energy,
joules_per_token,
text: text.unwrap_or("".to_string()),
};
@@ -192,17 +193,6 @@ impl BenchRunner {
}
}
-fn aggregate_power_stats(power_stats_list: &[&ChatReplyPowerStats]) -> Option<ChatReplyPowerStats> {
- let average_watts =
- |rail: fn(&ChatReplyPowerStats) -> f64| mean(&power_stats_list.iter().copied().map(rail).collect::<Vec<f64>>());
-
- Some(ChatReplyPowerStats {
- samples_count: power_stats_list.iter().map(|power| power.samples_count).sum(),
- average_cpu_watts: average_watts(|power| power.average_cpu_watts)?,
- average_gpu_watts: average_watts(|power| power.average_gpu_watts)?,
- average_ane_watts: average_watts(|power| power.average_ane_watts)?,
- average_ram_watts: average_watts(|power| power.average_ram_watts)?,
- average_total_watts: average_watts(|power| power.average_total_watts)?,
- energy_joules: power_stats_list.iter().map(|power| power.energy_joules).sum(),
- })
+fn aggregate_energy<'a>(energy: impl IntoIterator<Item = &'a ChatReplyEnergy>) -> Option<ChatReplyEnergy> {
+ energy.into_iter().cloned().reduce(|total, energy| total + energy)
}
diff --git a/crates/legacy/cli/src/interactive/components/history_cell.rs b/crates/legacy/cli/src/interactive/components/history_cell.rs
index bde546e57..bff600704 100644
--- a/crates/legacy/cli/src/interactive/components/history_cell.rs
+++ b/crates/legacy/cli/src/interactive/components/history_cell.rs
@@ -310,19 +310,13 @@ fn chat_reply_stats_component(
.map(|speculator_stats| format!("{:.2} t/f", speculator_stats.tokens_per_forward_pass))
.unwrap_or_else(|| SYMBOL_LONG_DASH.to_string());
let memory_used = stats.memory_used_bytes.map(format_memory_used).unwrap_or_else(|| SYMBOL_LONG_DASH.to_string());
- let power = stats
- .power_stats
- .as_ref()
- .map(|power| format!("{:.2} W avg", power.average_total_watts))
- .unwrap_or_else(|| SYMBOL_LONG_DASH.to_string());
- let energy = stats
- .power_stats
- .as_ref()
- .map(|power| format!("{:.2} J", power.energy_joules))
- .unwrap_or_else(|| SYMBOL_LONG_DASH.to_string());
- let energy_per_token = stats
- .joules_per_token()
- .map(|joules_per_token| format!("{joules_per_token:.3} J/tok"))
+ let energy =
+ stats.total_joules().map(|energy| format!("{energy:.2} J")).unwrap_or_else(|| SYMBOL_LONG_DASH.to_string());
+ let input_energy_per_token =
+ stats.input_joules_per_token().map(|energy| energy.to_string()).unwrap_or_else(|| SYMBOL_LONG_DASH.to_string());
+ let output_energy_per_token = stats
+ .output_joules_per_token()
+ .map(|energy| energy.to_string())
.unwrap_or_else(|| SYMBOL_LONG_DASH.to_string());
let duration = format!("{:.2} s", stats.duration);
@@ -352,15 +346,15 @@ fn chat_reply_stats_component(
color: subtitle_color,
)
Text(
- content: format!("average power: {power}"),
+ content: format!("total energy: {energy}"),
color: subtitle_color,
)
Text(
- content: format!("total energy: {energy}"),
+ content: format!("input energy per token: {input_energy_per_token}"),
color: subtitle_color,
)
Text(
- content: format!("energy per token: {energy_per_token}"),
+ content: format!("output energy per token: {output_energy_per_token}"),
color: subtitle_color,
)
Text(
diff --git a/crates/legacy/keisoku/Cargo.toml b/crates/legacy/keisoku/Cargo.toml
index 855f72635..e39893315 100644
--- a/crates/legacy/keisoku/Cargo.toml
+++ b/crates/legacy/keisoku/Cargo.toml
@@ -20,6 +20,7 @@ workspace = true
[dependencies]
bitflags.workspace = true
serde = { workspace = true, features = ["derive"] }
+thiserror.workspace = true
[target.'cfg(target_vendor = "apple")'.dependencies]
kanka.workspace = true
diff --git a/crates/legacy/keisoku/src/component.rs b/crates/legacy/keisoku/src/component.rs
index 9f4845139..965861a3b 100644
--- a/crates/legacy/keisoku/src/component.rs
+++ b/crates/legacy/keisoku/src/component.rs
@@ -44,6 +44,7 @@ impl fmt::Display for Component {
pub fn classify(name: &str) -> Component {
let name = name.to_ascii_lowercase();
+ let thermal_probe = thermal_probe_component(&name);
if name.contains("battery") || name.contains("gas gauge") || name.contains("fuel gauge") {
return Component::Battery;
@@ -60,16 +61,13 @@ pub fn classify(name: &str) -> Component {
if name.contains("ane") || name.contains("neural") {
return Component::NeuralEngine;
}
- if name.contains("gpu") || thermal_probe_block(&name) == Some('g') {
+ if name.contains("gpu") || thermal_probe == Some(Component::Gpu) {
return Component::Gpu;
}
if name.contains("cpu") || name.contains("pcore") || name.contains("ecore") {
return Component::Cpu;
}
- if name.contains("soc")
- || name.contains("tdie")
- || name.contains("tjunc")
- || thermal_probe_block(&name) == Some('s')
+ if name.contains("soc") || name.contains("tdie") || name.contains("tjunc") || thermal_probe == Some(Component::Soc)
{
return Component::Soc;
}
@@ -84,15 +82,18 @@ pub fn classify(name: &str) -> Component {
Component::Unknown
}
-fn thermal_probe_block(name: &str) -> Option<char> {
+fn thermal_probe_component(name: &str) -> Option<Component> {
name.split_whitespace().find_map(|token| {
let rest = token.strip_prefix("tp")?;
let block = rest.chars().last()?;
let digits = &rest[..rest.len() - block.len_utf8()];
- if block.is_ascii_alphabetic() && !digits.is_empty() && digits.bytes().all(|byte| byte.is_ascii_digit()) {
- Some(block)
- } else {
- None
+ if digits.is_empty() || !digits.bytes().all(|byte| byte.is_ascii_digit()) {
+ return None;
+ }
+ match block {
+ 'g' => Some(Component::Gpu),
+ 's' => Some(Component::Soc),
+ _ => None,
}
})
}
diff --git a/crates/legacy/keisoku/src/device/device.rs b/crates/legacy/keisoku/src/device/device.rs
index 43cf58347..c31916987 100644
--- a/crates/legacy/keisoku/src/device/device.rs
+++ b/crates/legacy/keisoku/src/device/device.rs
@@ -1,12 +1,16 @@
+#[cfg(target_os = "ios")]
+use std::time::Duration;
+
#[cfg(target_os = "macos")]
use super::IntervalHandle;
#[cfg(target_os = "macos")]
use crate::marker::IntervalSet;
+#[cfg(target_os = "ios")]
+use crate::units::Joules;
use crate::{
metrics::{BatteryMetrics, FanMetrics, MemoryMetrics, ThermalPressure},
sensor::Sensor,
sources::Sources,
- units::Watts,
};
/// Instantaneous device facts and gauges (chip, memory, sensors, …).
@@ -65,8 +69,12 @@ impl Device {
self.sources.current_sensors()
}
- pub fn rail_power(&mut self) -> Option<Watts> {
- self.sources.rail_power()
+ #[cfg(target_os = "ios")]
+ pub fn rail_energy(
+ &mut self,
+ elapsed: Duration,
+ ) -> Option<Joules> {
+ self.sources.rail_energy(elapsed)
}
/// Starts an IOReport interval measurement for the channels in `M`.
diff --git a/crates/legacy/keisoku/src/device/interval_handle.rs b/crates/legacy/keisoku/src/device/interval_handle.rs
index 011a9ac89..947a8236a 100644
--- a/crates/legacy/keisoku/src/device/interval_handle.rs
+++ b/crates/legacy/keisoku/src/device/interval_handle.rs
@@ -1,5 +1,4 @@
use core::marker::PhantomData;
-use std::time::Instant;
use crate::{
marker::{IntervalSet, Sample},
@@ -11,23 +10,21 @@ use crate::{
pub struct IntervalHandle<M: IntervalSet> {
engine: IntervalEngine,
session: Option<IntervalSession>,
- started: Option<Instant>,
marker: PhantomData<M>,
}
// SAFETY: `IntervalHandle` wraps CoreFoundation IOReport objects that are not `Sync`.
// Callers must use a handle only from the thread that created it (same contract as the
-// previous `Interval`/`Session` types). `Send` is asserted so `PowerMeter` can live in
-// `RefCell` behind `Send` trait objects such as nagare's `PowerRecorder`.
+// previous `Interval`/`Session` types). `Send` is asserted so the owning `PowerMeter`
+// can move with nagare's `Send` reply stream.
unsafe impl<M: IntervalSet> Send for IntervalHandle<M> {}
impl<M: IntervalSet> IntervalHandle<M> {
- pub(super) fn new() -> Self {
+ pub fn new() -> Self {
let _ = M::TYPE_MASK;
Self {
engine: IntervalEngine::new(M::GROUPS),
session: None,
- started: None,
marker: PhantomData,
}
}
@@ -36,7 +33,6 @@ impl<M: IntervalSet> IntervalHandle<M> {
pub fn start(&mut self) {
debug_assert!(self.session.is_none(), "start without stop");
drop(self.session.take());
- self.started = Some(Instant::now());
self.session = Some(self.engine.begin());
}
@@ -47,8 +43,4 @@ impl<M: IntervalSet> IntervalHandle<M> {
self.engine.fold_end::<M>(session, &mut values);
Some(Sample::new(values))
}
-
- pub(crate) fn elapsed(&self) -> std::time::Duration {
- self.started.map(|started| started.elapsed()).unwrap_or_default()
- }
}
diff --git a/crates/legacy/keisoku/src/error.rs b/crates/legacy/keisoku/src/error.rs
new file mode 100644
index 000000000..5c095b069
--- /dev/null
+++ b/crates/legacy/keisoku/src/error.rs
@@ -0,0 +1,11 @@
+#[derive(Debug, thiserror::Error)]
+pub enum KeisokuError {
+ #[error("power meter has not been started")]
+ PowerMeterNotStarted,
+ #[error("power meter did not produce a reading")]
+ PowerReadingUnavailable,
+ #[error("power meter sampling thread disconnected")]
+ SamplingTaskDisconnected,
+ #[error("power meter sampling thread panicked")]
+ SamplingTaskPanicked,
+}
diff --git a/crates/legacy/keisoku/src/lib.rs b/crates/legacy/keisoku/src/lib.rs
index 549590292..fa472c462 100644
--- a/crates/legacy/keisoku/src/lib.rs
+++ b/crates/legacy/keisoku/src/lib.rs
@@ -9,17 +9,21 @@ mod sources;
mod sys;
mod component;
+mod error;
+#[cfg(any(target_os = "macos", target_os = "ios"))]
mod power_meter;
mod sensor;
mod units;
pub use component::{Component, classify};
pub use device::Device;
+pub use error::KeisokuError;
#[cfg(target_os = "macos")]
pub use marker::{
Ane, AneBandwidth, Cons, Cpu, DramBytes, DramHistogram, DramRead, DramWrite, EnergyRail, Gpu, Nil, Ram, Sample,
};
pub use metrics::{BatteryMetrics, Fan, FanMetrics, MemoryMetrics, ThermalPressure};
+#[cfg(any(target_os = "macos", target_os = "ios"))]
pub use power_meter::{PowerMeter, PowerReading};
pub use sensor::{Sensor, SensorKind, thermal_sensors};
-pub use units::{Bytes, GigabytesPerSecond, Joules, Percent, Rpm, Watts};
+pub use units::{Bytes, GigabytesPerSecond, Joules, Percent, Rpm};
diff --git a/crates/legacy/keisoku/src/marker/mod.rs b/crates/legacy/keisoku/src/marker/mod.rs
index aad4138f6..41a6d3231 100644
--- a/crates/legacy/keisoku/src/marker/mod.rs
+++ b/crates/legacy/keisoku/src/marker/mod.rs
@@ -3,7 +3,7 @@ mod channels;
mod sample;
mod typelist;
-pub(crate) use channel_set::IntervalSet;
+pub use channel_set::IntervalSet;
pub use channels::{Ane, AneBandwidth, Cpu, DramBytes, DramHistogram, DramRead, DramWrite, EnergyRail, Gpu, Ram};
pub use sample::Sample;
pub use typelist::{Cons, Nil};
diff --git a/crates/legacy/keisoku/src/marker/sample.rs b/crates/legacy/keisoku/src/marker/sample.rs
index f2474e812..7e8738bb4 100644
--- a/crates/legacy/keisoku/src/marker/sample.rs
+++ b/crates/legacy/keisoku/src/marker/sample.rs
@@ -10,7 +10,7 @@ pub struct Sample<M: ChannelSet> {
}
impl<M: ChannelSet> Sample<M> {
- pub(crate) fn new(values: M::Value) -> Self {
+ pub fn new(values: M::Value) -> Self {
Self {
values,
marker: PhantomData,
diff --git a/crates/legacy/keisoku/src/marker/typelist.rs b/crates/legacy/keisoku/src/marker/typelist.rs
index 66840b73f..499494b70 100644
--- a/crates/legacy/keisoku/src/marker/typelist.rs
+++ b/crates/legacy/keisoku/src/marker/typelist.rs
@@ -5,12 +5,12 @@ pub struct Nil;
pub struct Cons<H, T>(PhantomData<(H, T)>);
pub struct Values<M: ChannelMetric, T> {
- pub(crate) head: M::Value,
- pub(crate) tail: T,
+ pub head: M::Value,
+ pub tail: T,
}
impl<M: ChannelMetric, T> Values<M, T> {
- pub(crate) fn new(
+ pub fn new(
head: M::Value,
tail: T,
) -> Self {
diff --git a/crates/legacy/keisoku/src/power_meter.rs b/crates/legacy/keisoku/src/power_meter.rs
index 791981690..2c64d9bb6 100644
--- a/crates/legacy/keisoku/src/power_meter.rs
+++ b/crates/legacy/keisoku/src/power_meter.rs
@@ -1,13 +1,15 @@
-use crate::units::{Joules, Watts};
-
-pub struct PowerReading {
- pub cpu: Option<Watts>,
- pub gpu: Option<Watts>,
- pub ane: Option<Watts>,
- pub ram: Option<Watts>,
- pub total: Watts,
- pub energy: Joules,
- pub samples: u64,
+use crate::{error::KeisokuError, units::Joules};
+
+pub enum PowerReading {
+ Total {
+ total: Joules,
+ },
+ Components {
+ cpu: Joules,
+ gpu: Joules,
+ ane: Joules,
+ ram: Joules,
+ },
}
pub struct PowerMeter {
@@ -21,11 +23,15 @@ impl PowerMeter {
}
}
- pub fn start(&mut self) {
- self.inner.start();
+ pub fn start(&mut self) -> Result<(), KeisokuError> {
+ self.inner.start()
}
- pub fn stop(&mut self) -> Option<PowerReading> {
+ pub fn split(&mut self) -> Result<PowerReading, KeisokuError> {
+ self.inner.split()
+ }
+
+ pub fn stop(&mut self) -> Result<PowerReading, KeisokuError> {
self.inner.stop()
}
}
@@ -38,144 +44,203 @@ impl Default for PowerMeter {
#[cfg(target_os = "macos")]
mod inner {
- use super::PowerReading;
+ use super::{KeisokuError, PowerReading};
use crate::{
Device, Select,
marker::{Ane, Cpu, EnergyRail, Gpu, Ram},
- units::{Joules, Watts},
};
type Rails = Select![EnergyRail<Cpu>, EnergyRail<Gpu>, EnergyRail<Ane>, EnergyRail<Ram>];
- pub(super) struct Inner {
+ pub struct Inner {
handle: Option<crate::device::IntervalHandle<Rails>>,
}
impl Inner {
- pub(super) fn new() -> Self {
+ pub fn new() -> Self {
Self {
handle: None,
}
}
- pub(super) fn start(&mut self) {
+ pub fn start(&mut self) -> Result<(), KeisokuError> {
let mut handle = Device::interval_measurement::<Rails>();
handle.start();
self.handle = Some(handle);
+ Ok(())
}
- pub(super) fn stop(&mut self) -> Option<PowerReading> {
- let mut handle = self.handle.take()?;
- let elapsed = handle.elapsed().as_secs_f64().max(0.001);
- let sample = handle.stop()?;
- let cpu_j = sample.get::<EnergyRail<Cpu>>().value() as f64;
- let gpu_j = sample.get::<EnergyRail<Gpu>>().value() as f64;
- let ane_j = sample.get::<EnergyRail<Ane>>().value() as f64;
- let ram_j = sample.get::<EnergyRail<Ram>>().value() as f64;
- let total_j = cpu_j + gpu_j + ane_j + ram_j;
- let to_watts = |joules: f64| Watts((joules / elapsed) as f32);
- Some(PowerReading {
- cpu: Some(to_watts(cpu_j)),
- gpu: Some(to_watts(gpu_j)),
- ane: Some(to_watts(ane_j)),
- ram: Some(to_watts(ram_j)),
- total: to_watts(total_j),
- energy: Joules(total_j as f32),
- samples: 1,
+ pub fn stop(&mut self) -> Result<PowerReading, KeisokuError> {
+ let Some(mut handle) = self.handle.take() else {
+ return Err(KeisokuError::PowerMeterNotStarted);
+ };
+ let Some(sample) = handle.stop() else {
+ return Err(KeisokuError::PowerReadingUnavailable);
+ };
+ Ok(PowerReading::Components {
+ cpu: *sample.get::<EnergyRail<Cpu>>(),
+ gpu: *sample.get::<EnergyRail<Gpu>>(),
+ ane: *sample.get::<EnergyRail<Ane>>(),
+ ram: *sample.get::<EnergyRail<Ram>>(),
})
}
+
+ pub fn split(&mut self) -> Result<PowerReading, KeisokuError> {
+ let reading = self.stop();
+ self.start()?;
+ reading
+ }
}
}
-#[cfg(not(target_os = "macos"))]
+#[cfg(target_os = "ios")]
mod inner {
use std::{
- sync::{
- Arc, Mutex,
- atomic::{AtomicBool, Ordering},
- },
+ sync::mpsc,
thread::{self, JoinHandle},
time::{Duration, Instant},
};
- use super::PowerReading;
- use crate::{
- Device,
- units::{Joules, Watts},
- };
+ use super::{KeisokuError, PowerReading};
+ use crate::{Device, units::Joules};
const SAMPLE_INTERVAL: Duration = Duration::from_millis(100);
- #[derive(Default)]
- struct Accumulator {
- energy_joules: f64,
- elapsed_seconds: f64,
- samples: u64,
+ enum Command {
+ Split {
+ boundary: Instant,
+ response: mpsc::Sender<Result<PowerReading, KeisokuError>>,
+ },
+ Stop {
+ boundary: Instant,
+ response: mpsc::Sender<Result<PowerReading, KeisokuError>>,
+ },
+ }
+
+ struct Sampler {
+ commands: mpsc::Sender<Command>,
+ worker: JoinHandle<()>,
}
- pub(super) struct Inner {
- running: Arc<AtomicBool>,
- accumulator: Arc<Mutex<Accumulator>>,
- worker: Option<JoinHandle<()>>,
+ pub struct Inner {
+ sampler: Option<Sampler>,
}
impl Inner {
- pub(super) fn new() -> Self {
+ pub fn new() -> Self {
Self {
- running: Arc::new(AtomicBool::new(false)),
- accumulator: Arc::new(Mutex::new(Accumulator::default())),
- worker: None,
+ sampler: None,
}
}
- pub(super) fn start(&mut self) {
- *self.accumulator.lock().expect("power meter accumulator poisoned") = Accumulator::default();
- self.running.store(true, Ordering::Relaxed);
- let running = self.running.clone();
- let accumulator = self.accumulator.clone();
- self.worker = Some(thread::spawn(move || {
- let mut device = Device::new();
- let mut last = Instant::now();
- while running.load(Ordering::Relaxed) {
- thread::sleep(SAMPLE_INTERVAL);
- let now = Instant::now();
- let seconds = now.duration_since(last).as_secs_f64();
- last = now;
- let Some(watts) = device.rail_power() else {
- continue;
- };
- let watts = watts.value() as f64;
- let mut accumulator = accumulator.lock().expect("power meter accumulator poisoned");
- accumulator.energy_joules += watts * seconds;
- accumulator.elapsed_seconds += seconds;
- accumulator.samples += 1;
- }
- }));
+ pub fn start(&mut self) -> Result<(), KeisokuError> {
+ if let Some(sampler) = self.sampler.take() {
+ sampler.shutdown()?;
+ }
+ let (commands, receiver) = mpsc::channel();
+ let worker = thread::spawn(move || run(receiver));
+ self.sampler = Some(Sampler {
+ commands,
+ worker,
+ });
+ Ok(())
}
- pub(super) fn stop(&mut self) -> Option<PowerReading> {
- self.running.store(false, Ordering::Relaxed);
- if let Some(worker) = self.worker.take() {
- let _ = worker.join();
- }
- let accumulator = self.accumulator.lock().expect("power meter accumulator poisoned");
- if accumulator.samples == 0 {
- return None;
+ pub fn split(&mut self) -> Result<PowerReading, KeisokuError> {
+ self.sampler.as_ref().ok_or(KeisokuError::PowerMeterNotStarted)?.split()
+ }
+
+ pub fn stop(&mut self) -> Result<PowerReading, KeisokuError> {
+ self.sampler.take().ok_or(KeisokuError::PowerMeterNotStarted)?.stop()
+ }
+ }
+
+ impl Sampler {
+ fn split(&self) -> Result<PowerReading, KeisokuError> {
+ let (response, receiver) = mpsc::channel();
+ self.commands
+ .send(Command::Split {
+ boundary: Instant::now(),
+ response,
+ })
+ .map_err(|_| KeisokuError::SamplingTaskDisconnected)?;
+ receive_reading(receiver)
+ }
+
+ fn stop(self) -> Result<PowerReading, KeisokuError> {
+ let (response, receiver) = mpsc::channel();
+ let reading = self
+ .commands
+ .send(Command::Stop {
+ boundary: Instant::now(),
+ response,
+ })
+ .map_err(|_| KeisokuError::SamplingTaskDisconnected)
+ .and_then(|()| receive_reading(receiver));
+ self.worker.join().map_err(|_| KeisokuError::SamplingTaskPanicked)?;
+ reading
+ }
+
+ fn shutdown(self) -> Result<(), KeisokuError> {
+ drop(self.commands);
+ self.worker.join().map_err(|_| KeisokuError::SamplingTaskPanicked)
+ }
+ }
+
+ fn run(commands: mpsc::Receiver<Command>) {
+ let mut device = Device::new();
+ let mut joules = None;
+ let mut last_sample = Instant::now();
+
+ loop {
+ match commands.recv_timeout(SAMPLE_INTERVAL) {
+ Ok(Command::Split {
+ boundary,
+ response,
+ }) => {
+ sample(&mut device, &mut joules, &mut last_sample, boundary);
+ let _ = response.send(reading(joules.take()));
+ },
+ Ok(Command::Stop {
+ boundary,
+ response,
+ }) => {
+ sample(&mut device, &mut joules, &mut last_sample, boundary);
+ let _ = response.send(reading(joules));
+ break;
+ },
+ Err(mpsc::RecvTimeoutError::Timeout) => {
+ sample(&mut device, &mut joules, &mut last_sample, Instant::now());
+ },
+ Err(mpsc::RecvTimeoutError::Disconnected) => break,
}
- let total = if accumulator.elapsed_seconds > 0.0 {
- (accumulator.energy_joules / accumulator.elapsed_seconds) as f32
- } else {
- 0.0
- };
- Some(PowerReading {
- cpu: None,
- gpu: None,
- ane: None,
- ram: None,
- total: Watts(total),
- energy: Joules(accumulator.energy_joules as f32),
- samples: accumulator.samples,
- })
}
}
+
+ fn sample(
+ device: &mut Device,
+ joules: &mut Option<f64>,
+ last_sample: &mut Instant,
+ boundary: Instant,
+ ) {
+ let elapsed = boundary.saturating_duration_since(*last_sample);
+ *last_sample = (*last_sample).max(boundary);
+ if let Some(energy) = device.rail_energy(elapsed) {
+ *joules.get_or_insert(0.0) += f64::from(energy.value());
+ }
+ }
+
+ fn reading(joules: Option<f64>) -> Result<PowerReading, KeisokuError> {
+ joules
+ .map(|joules| PowerReading::Total {
+ total: Joules(joules as f32),
+ })
+ .ok_or(KeisokuError::PowerReadingUnavailable)
+ }
+
+ fn receive_reading(
+ receiver: mpsc::Receiver<Result<PowerReading, KeisokuError>>
+ ) -> Result<PowerReading, KeisokuError> {
+ receiver.recv().map_err(|_| KeisokuError::SamplingTaskDisconnected)?
+ }
}
diff --git a/crates/legacy/keisoku/src/sensor/kind.rs b/crates/legacy/keisoku/src/sensor/kind.rs
index 1707ea60d..9df362616 100644
--- a/crates/legacy/keisoku/src/sensor/kind.rs
+++ b/crates/legacy/keisoku/src/sensor/kind.rs
@@ -10,7 +10,7 @@ pub enum SensorKind {
}
impl SensorKind {
- pub(crate) fn matching(self) -> (i32, i32) {
+ pub fn matching(self) -> (i32, i32) {
match self {
SensorKind::Temperature => (sys::HID_PAGE_APPLE_VENDOR, sys::HID_USAGE_TEMPERATURE_SENSOR),
SensorKind::Voltage => (sys::HID_PAGE_APPLE_VENDOR_POWER, sys::HID_USAGE_POWER_VOLTAGE),
@@ -18,7 +18,7 @@ impl SensorKind {
}
}
- pub(crate) fn event_type(self) -> i64 {
+ pub fn event_type(self) -> i64 {
match self {
SensorKind::Temperature => sys::EVENT_TYPE_TEMPERATURE,
SensorKind::Voltage | SensorKind::Current => sys::EVENT_TYPE_POWER,
diff --git a/crates/legacy/keisoku/src/sources/battery.rs b/crates/legacy/keisoku/src/sources/battery.rs
index bb5d712af..ef6e44bc8 100644
--- a/crates/legacy/keisoku/src/sources/battery.rs
+++ b/crates/legacy/keisoku/src/sources/battery.rs
@@ -1,11 +1,11 @@
use crate::metrics::BatteryMetrics;
#[cfg(target_os = "macos")]
-pub(crate) fn read_battery() -> Option<BatteryMetrics> {
+pub fn read_battery() -> Option<BatteryMetrics> {
crate::sys::read_battery()
}
#[cfg(not(target_os = "macos"))]
-pub(crate) fn read_battery() -> Option<BatteryMetrics> {
+pub fn read_battery() -> Option<BatteryMetrics> {
None
}
diff --git a/crates/legacy/keisoku/src/sources/deferred.rs b/crates/legacy/keisoku/src/sources/deferred.rs
index 891611ea8..367af96f0 100644
--- a/crates/legacy/keisoku/src/sources/deferred.rs
+++ b/crates/legacy/keisoku/src/sources/deferred.rs
@@ -1,17 +1,17 @@
-pub(super) struct Deferred<T> {
+pub struct Deferred<T> {
slot: Option<T>,
init: fn() -> T,
}
impl<T> Deferred<T> {
- pub(super) const fn new(init: fn() -> T) -> Self {
+ pub const fn new(init: fn() -> T) -> Self {
Self {
slot: None,
init,
}
}
- pub(super) fn get(&mut self) -> &mut T {
+ pub fn get(&mut self) -> &mut T {
let init = self.init;
self.slot.get_or_insert_with(init)
}
diff --git a/crates/legacy/keisoku/src/sources/interval.rs b/crates/legacy/keisoku/src/sources/interval.rs
index 55af677e3..a587fb567 100644
--- a/crates/legacy/keisoku/src/sources/interval.rs
+++ b/crates/legacy/keisoku/src/sources/interval.rs
@@ -3,28 +3,28 @@ use crate::{
sys::ioreport::{IoReport, IoReportGroups, RawIOReportSample},
};
-pub(crate) struct IntervalEngine {
+pub struct IntervalEngine {
ioreport: Option<IoReport>,
}
-pub(crate) struct IntervalSession {
+pub struct IntervalSession {
begin: Option<RawIOReportSample>,
}
impl IntervalEngine {
- pub(crate) fn new(groups: IoReportGroups) -> Self {
+ pub fn new(groups: IoReportGroups) -> Self {
Self {
ioreport: (!groups.is_empty()).then(|| IoReport::for_groups(groups)).flatten(),
}
}
- pub(crate) fn begin(&self) -> IntervalSession {
+ pub fn begin(&self) -> IntervalSession {
IntervalSession {
begin: self.ioreport.as_ref().and_then(IoReport::snapshot),
}
}
- pub(crate) fn fold_end<M: IntervalSet>(
+ pub fn fold_end<M: IntervalSet>(
&self,
session: IntervalSession,
values: &mut M::Value,
diff --git a/crates/legacy/keisoku/src/sources/memory.rs b/crates/legacy/keisoku/src/sources/memory.rs
index 6cb94f03b..9f6cb8553 100644
--- a/crates/legacy/keisoku/src/sources/memory.rs
+++ b/crates/legacy/keisoku/src/sources/memory.rs
@@ -1,12 +1,12 @@
use crate::metrics::MemoryMetrics;
#[cfg(target_os = "macos")]
-pub(crate) fn read_memory() -> Option<MemoryMetrics> {
+pub fn read_memory() -> Option<MemoryMetrics> {
crate::sys::read_memory()
}
#[cfg(not(target_os = "macos"))]
-pub(crate) fn read_memory() -> Option<MemoryMetrics> {
+pub fn read_memory() -> Option<MemoryMetrics> {
use sysinfo::System;
use crate::units::Bytes;
diff --git a/crates/legacy/keisoku/src/sources/mod.rs b/crates/legacy/keisoku/src/sources/mod.rs
index 379560d4a..b205eecb6 100644
--- a/crates/legacy/keisoku/src/sources/mod.rs
+++ b/crates/legacy/keisoku/src/sources/mod.rs
@@ -1,24 +1,29 @@
mod battery;
mod deferred;
#[cfg(target_os = "macos")]
-pub(crate) mod interval;
+pub mod interval;
mod memory;
+#[cfg(target_os = "ios")]
mod rail_power;
mod sensors;
mod thermal;
+#[cfg(target_os = "ios")]
+use std::time::Duration;
+
use deferred::Deferred;
#[cfg(target_os = "macos")]
use crate::sys::{smc::Smc, soc::SocInfo};
+#[cfg(target_os = "ios")]
+use crate::units::Joules;
use crate::{
metrics::FanMetrics,
sensor::{Sensor, SensorKind},
sys::hid::SensorReader,
- units::Watts,
};
-pub(crate) struct Sources {
+pub struct Sources {
temperature: Deferred<Option<SensorReader>>,
voltage: Deferred<Option<SensorReader>>,
current: Deferred<Option<SensorReader>>,
@@ -41,7 +46,7 @@ impl Sources {
}
}
- pub(crate) fn chip(&self) -> String {
+ pub fn chip(&self) -> String {
#[cfg(target_os = "macos")]
{
self.soc().map(|soc| soc.chip_name.clone()).unwrap_or_default()
@@ -52,7 +57,7 @@ impl Sources {
}
}
- pub(crate) fn efficiency_cores(&self) -> u8 {
+ pub fn efficiency_cores(&self) -> u8 {
#[cfg(target_os = "macos")]
{
self.soc().map(|soc| soc.ecpu_cores).unwrap_or(0)
@@ -63,7 +68,7 @@ impl Sources {
}
}
- pub(crate) fn performance_cores(&self) -> u8 {
+ pub fn performance_cores(&self) -> u8 {
#[cfg(target_os = "macos")]
{
self.soc().map(|soc| soc.pcpu_cores).unwrap_or(0)
@@ -74,7 +79,7 @@ impl Sources {
}
}
- pub(crate) fn gpu_cores(&self) -> u8 {
+ pub fn gpu_cores(&self) -> u8 {
#[cfg(target_os = "macos")]
{
self.soc().map(|soc| soc.gpu_cores).unwrap_or(0)
@@ -85,37 +90,41 @@ impl Sources {
}
}
- pub(crate) fn temperature_sensors(&mut self) -> Box<[Sensor]> {
+ pub fn temperature_sensors(&mut self) -> Box<[Sensor]> {
self.temperature.get().as_mut().map(sensors::read_reader).unwrap_or_default()
}
- pub(crate) fn voltage_sensors(&mut self) -> Box<[Sensor]> {
+ pub fn voltage_sensors(&mut self) -> Box<[Sensor]> {
self.voltage.get().as_mut().map(sensors::read_reader).unwrap_or_default()
}
- pub(crate) fn current_sensors(&mut self) -> Box<[Sensor]> {
+ pub fn current_sensors(&mut self) -> Box<[Sensor]> {
self.current.get().as_mut().map(sensors::read_reader).unwrap_or_default()
}
- pub(crate) fn memory(&mut self) -> Option<crate::metrics::MemoryMetrics> {
+ pub fn memory(&mut self) -> Option<crate::metrics::MemoryMetrics> {
memory::read_memory()
}
- pub(crate) fn battery(&mut self) -> Option<crate::metrics::BatteryMetrics> {
+ pub fn battery(&mut self) -> Option<crate::metrics::BatteryMetrics> {
battery::read_battery()
}
- pub(crate) fn thermal(&mut self) -> Option<crate::metrics::ThermalPressure> {
+ pub fn thermal(&mut self) -> Option<crate::metrics::ThermalPressure> {
thermal::read_thermal()
}
- pub(crate) fn rail_power(&mut self) -> Option<Watts> {
+ #[cfg(target_os = "ios")]
+ pub fn rail_energy(
+ &mut self,
+ elapsed: Duration,
+ ) -> Option<Joules> {
let voltage = self.voltage_sensors();
let current = self.current_sensors();
- rail_power::rail_power(&voltage, &current)
+ rail_power::rail_energy(&voltage, &current, elapsed)
}
- pub(crate) fn fans(&self) -> Option<FanMetrics> {
+ pub fn fans(&self) -> Option<FanMetrics> {
#[cfg(target_os = "macos")]
{
self.smc().map(Smc::fans)
@@ -127,12 +136,12 @@ impl Sources {
}
#[cfg(target_os = "macos")]
- pub(crate) fn soc(&self) -> Option<&SocInfo> {
+ pub fn soc(&self) -> Option<&SocInfo> {
self.soc.get_or_init(SocInfo::new).as_ref()
}
#[cfg(target_os = "macos")]
- pub(crate) fn smc(&self) -> Option<&Smc> {
+ pub fn smc(&self) -> Option<&Smc> {
self.smc.get_or_init(Smc::new).as_ref()
}
}
diff --git a/crates/legacy/keisoku/src/sources/rail_power.rs b/crates/legacy/keisoku/src/sources/rail_power.rs
index 765dd9788..929286ff6 100644
--- a/crates/legacy/keisoku/src/sources/rail_power.rs
+++ b/crates/legacy/keisoku/src/sources/rail_power.rs
@@ -1,32 +1,75 @@
-use crate::{component::Component, sensor::Sensor, units::Watts};
+use std::time::Duration;
-pub(crate) fn rail_power(
+use crate::{component::Component, sensor::Sensor, units::Joules};
+
+#[derive(Clone, Copy)]
+enum PowerFlow {
+ Charging,
+ Discharging,
+}
+
+impl PowerFlow {
+ fn parse_voltage_code(voltage_code: &str) -> Option<(Self, &str)> {
+ let (flow, rail_code) = if let Some(rail_code) = voltage_code.strip_prefix('V') {
+ (Self::Charging, rail_code)
+ } else {
+ (Self::Discharging, voltage_code.strip_prefix('W')?)
+ };
+
+ (!rail_code.is_empty()).then_some((flow, rail_code))
+ }
+
+ fn matches_current_code(
+ self,
+ current_code: &str,
+ rail_code: &str,
+ ) -> bool {
+ let prefix = match self {
+ Self::Charging => 'I',
+ Self::Discharging => 'Q',
+ };
+ current_code.strip_prefix(prefix) == Some(rail_code)
+ }
+}
+
+pub fn rail_energy(
voltage: &[Sensor],
current: &[Sensor],
-) -> Option<Watts> {
- const MAX_PLAUSIBLE_WATTS: f64 = 1000.0;
- let split_area_code = |name: &str| name.rsplit_once(' ').map(|(area, code)| (area.to_owned(), code.to_owned()));
+ elapsed: Duration,
+) -> Option<Joules> {
+ const MAX_PLAUSIBLE_VOLT_AMPS: f64 = 1000.0;
let is_battery_rail = |sensor: &&Sensor| matches!(sensor.component, Component::Charger | Component::Battery);
- let mut total_watts = 0f64;
+ let mut charging_volt_amps = 0f64;
+ let mut discharging_volt_amps = 0f64;
for voltage_sensor in voltage.iter().filter(is_battery_rail) {
- let Some((voltage_area, voltage_code)) = split_area_code(&voltage_sensor.name) else {
+ let Some((voltage_area, voltage_code)) = voltage_sensor.name.rsplit_once(' ') else {
continue;
};
- let Some(rail_code) = voltage_code.strip_prefix('V').filter(|code| !code.is_empty()) else {
+ let Some((flow, rail_code)) = PowerFlow::parse_voltage_code(voltage_code) else {
continue;
};
for current_sensor in current.iter().filter(is_battery_rail) {
- let Some((current_area, current_code)) = split_area_code(&current_sensor.name) else {
+ let Some((current_area, current_code)) = current_sensor.name.rsplit_once(' ') else {
continue;
};
- if current_area == voltage_area && current_code.strip_prefix('I') == Some(rail_code) {
- let watts = (voltage_sensor.value * current_sensor.value).abs();
- if (0.0..=MAX_PLAUSIBLE_WATTS).contains(&watts) {
- total_watts += watts;
+ if current_area == voltage_area && flow.matches_current_code(current_code, rail_code) {
+ let volt_amps = (voltage_sensor.value * current_sensor.value).abs();
+ if (0.0..=MAX_PLAUSIBLE_VOLT_AMPS).contains(&volt_amps) {
+ match flow {
+ PowerFlow::Charging => charging_volt_amps += volt_amps,
+ PowerFlow::Discharging => discharging_volt_amps += volt_amps,
+ }
}
}
}
}
- (total_watts > 0.0).then_some(Watts(total_watts as f32))
+
+ // Charging sensors stay nonzero on USB, so discharge sensors are only a fallback.
+ let total_volt_amps = if charging_volt_amps > 0.0 {
+ charging_volt_amps
+ } else {
+ discharging_volt_amps
+ };
+ (total_volt_amps > 0.0).then_some(Joules((total_volt_amps * elapsed.as_secs_f64()) as f32))
}
diff --git a/crates/legacy/keisoku/src/sources/sensors.rs b/crates/legacy/keisoku/src/sources/sensors.rs
index 8f7a90f1f..3c66fabe5 100644
--- a/crates/legacy/keisoku/src/sources/sensors.rs
+++ b/crates/legacy/keisoku/src/sources/sensors.rs
@@ -3,10 +3,10 @@ use crate::{
sys::hid::SensorReader,
};
-pub(crate) fn new_reader(kind: SensorKind) -> Option<SensorReader> {
+pub fn new_reader(kind: SensorKind) -> Option<SensorReader> {
SensorReader::new(kind)
}
-pub(crate) fn read_reader(reader: &mut SensorReader) -> Box<[Sensor]> {
+pub fn read_reader(reader: &mut SensorReader) -> Box<[Sensor]> {
reader.read()
}
diff --git a/crates/legacy/keisoku/src/sources/thermal.rs b/crates/legacy/keisoku/src/sources/thermal.rs
index ecf85c5a2..cc76b7da5 100644
--- a/crates/legacy/keisoku/src/sources/thermal.rs
+++ b/crates/legacy/keisoku/src/sources/thermal.rs
@@ -1,5 +1,5 @@
use crate::metrics::ThermalPressure;
-pub(crate) fn read_thermal() -> Option<ThermalPressure> {
+pub fn read_thermal() -> Option<ThermalPressure> {
crate::sys::read_thermal_pressure()
}
diff --git a/crates/legacy/keisoku/src/sys/battery.rs b/crates/legacy/keisoku/src/sys/battery.rs
index 473f774ae..1ab9713a6 100644
--- a/crates/legacy/keisoku/src/sys/battery.rs
+++ b/crates/legacy/keisoku/src/sys/battery.rs
@@ -6,7 +6,7 @@ use objc2_io_kit::{
use crate::{metrics::BatteryMetrics, sys::registry::dictionary_get, units::Percent};
-pub(crate) fn read_battery() -> Option<BatteryMetrics> {
+pub fn read_battery() -> Option<BatteryMetrics> {
let info = IOPSCopyPowerSourcesInfo()?;
let info_ref: &CFType = &info;
let list = unsafe { IOPSCopyPowerSourcesList(Some(info_ref)) }?;
diff --git a/crates/legacy/keisoku/src/sys/constants.rs b/crates/legacy/keisoku/src/sys/constants.rs
index 98bef9e24..1e72845e8 100644
--- a/crates/legacy/keisoku/src/sys/constants.rs
+++ b/crates/legacy/keisoku/src/sys/constants.rs
@@ -1,12 +1,12 @@
-pub(crate) const HID_PAGE_APPLE_VENDOR: i32 = 0xff00;
-pub(crate) const HID_PAGE_APPLE_VENDOR_POWER: i32 = 0xff08;
-pub(crate) const HID_USAGE_TEMPERATURE_SENSOR: i32 = 0x0005;
-pub(crate) const HID_USAGE_POWER_VOLTAGE: i32 = 0x0003;
-pub(crate) const HID_USAGE_POWER_CURRENT: i32 = 0x0002;
+pub const HID_PAGE_APPLE_VENDOR: i32 = 0xff00;
+pub const HID_PAGE_APPLE_VENDOR_POWER: i32 = 0xff08;
+pub const HID_USAGE_TEMPERATURE_SENSOR: i32 = 0x0005;
+pub const HID_USAGE_POWER_VOLTAGE: i32 = 0x0003;
+pub const HID_USAGE_POWER_CURRENT: i32 = 0x0002;
-pub(crate) const EVENT_TYPE_TEMPERATURE: i64 = 15;
-pub(crate) const EVENT_TYPE_POWER: i64 = 25;
+pub const EVENT_TYPE_TEMPERATURE: i64 = 15;
+pub const EVENT_TYPE_POWER: i64 = 25;
-pub(crate) const fn event_field_base(event_type: i64) -> i32 {
+pub const fn event_field_base(event_type: i64) -> i32 {
(event_type as i32) << 16
}
diff --git a/crates/legacy/keisoku/src/sys/hid/event_system_client.rs b/crates/legacy/keisoku/src/sys/hid/event_system_client.rs
index 2d2c06cfb..ba8f8838b 100644
--- a/crates/legacy/keisoku/src/sys/hid/event_system_client.rs
+++ b/crates/legacy/keisoku/src/sys/hid/event_system_client.rs
@@ -5,13 +5,13 @@ use objc2_core_foundation::{CFArray, CFDictionary, CFNumber, CFRetained, CFStrin
use super::{IOHIDEventSystemClient, IOHIDServiceClient, IOKit, service_client::ServiceClient};
-pub(super) struct EventSystemClient {
+pub struct EventSystemClient {
io_kit: &'static IOKit,
inner: CFRetained<IOHIDEventSystemClient>,
}
impl EventSystemClient {
- pub(super) fn new() -> Option<Self> {
+ pub fn new() -> Option<Self> {
let io_kit = IOKit::get()?;
let pointer = unsafe { (io_kit.create)(None) };
let inner = unsafe { CFRetained::from_raw(NonNull::new(pointer)?) };
@@ -21,7 +21,7 @@ impl EventSystemClient {
})
}
- pub(super) fn services(
+ pub fn services(
&self,
page: i32,
usage: i32,
diff --git a/crates/legacy/keisoku/src/sys/hid/mod.rs b/crates/legacy/keisoku/src/sys/hid/mod.rs
index 4bd1d2cb5..b475893f2 100644
--- a/crates/legacy/keisoku/src/sys/hid/mod.rs
+++ b/crates/legacy/keisoku/src/sys/hid/mod.rs
@@ -66,7 +66,7 @@ struct CachedSensor {
value: f64,
}
-pub(crate) struct SensorReader {
+pub struct SensorReader {
kind: SensorKind,
event_type: i64,
event_field: i32,
@@ -76,7 +76,7 @@ pub(crate) struct SensorReader {
}
impl SensorReader {
- pub(crate) fn new(kind: SensorKind) -> Option<Self> {
+ pub fn new(kind: SensorKind) -> Option<Self> {
let client = EventSystemClient::new()?;
let (page, usage) = kind.matching();
let event_type = kind.event_type();
@@ -112,7 +112,7 @@ impl SensorReader {
})
}
- pub(crate) fn read(&mut self) -> Box<[Sensor]> {
+ pub fn read(&mut self) -> Box<[Sensor]> {
let refresh_cold = self.last_cold_read.is_none_or(|at| at.elapsed() >= COLD_REFRESH);
if refresh_cold {
self.last_cold_read = Some(Instant::now());
diff --git a/crates/legacy/keisoku/src/sys/hid/service_client.rs b/crates/legacy/keisoku/src/sys/hid/service_client.rs
index 1a93db917..43f418bd2 100644
--- a/crates/legacy/keisoku/src/sys/hid/service_client.rs
+++ b/crates/legacy/keisoku/src/sys/hid/service_client.rs
@@ -4,7 +4,7 @@ use objc2_core_foundation::{CFNumber, CFRetained, CFString, CFType, ConcreteType
use super::{IOHIDEvent, IOHIDServiceClient, IOKit};
-pub(super) struct ServiceClient {
+pub struct ServiceClient {
io_kit: &'static IOKit,
inner: CFRetained<IOHIDServiceClient>,
}
@@ -29,21 +29,21 @@ impl ServiceClient {
value.downcast::<T>().ok()
}
- pub(super) fn string(
+ pub fn string(
&self,
key: &str,
) -> Option<String> {
self.property::<CFString>(key).map(|string| string.to_string())
}
- pub(super) fn i64_value(
+ pub fn i64_value(
&self,
key: &str,
) -> Option<i64> {
self.property::<CFNumber>(key).and_then(|number| number.as_i64())
}
- pub(super) fn f64_value(
+ pub fn f64_value(
&self,
event_type: i64,
event_field: i32,
@@ -53,7 +53,7 @@ impl ServiceClient {
Some(unsafe { (self.io_kit.get_float_value)(&event, event_field) })
}
- pub(super) fn registry_id(&self) -> u64 {
+ pub fn registry_id(&self) -> u64 {
unsafe { (self.io_kit.get_registry_id)(&self.inner) }
}
}
diff --git a/crates/legacy/keisoku/src/sys/ioreport/channel.rs b/crates/legacy/keisoku/src/sys/ioreport/channel.rs
index 345583112..74352c4ac 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/channel.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/channel.rs
@@ -9,7 +9,7 @@ use crate::sys::{
registry::{cf_string_to_string, dictionary_get},
};
-pub(super) fn for_each_channel(
+pub fn for_each_channel(
functions: &IoReportFunctions,
delta: &CFDictionary,
visit: &mut impl FnMut(Channel, &RawChannel),
diff --git a/crates/legacy/keisoku/src/sys/ioreport/channels.rs b/crates/legacy/keisoku/src/sys/ioreport/channels.rs
index 1a0c9c7f5..272f50efa 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/channels.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/channels.rs
@@ -4,10 +4,10 @@ use objc2_core_foundation::{CFMutableDictionary, CFRetained, CFString};
use super::IoReportFunctions;
use crate::sys::ioreport::IoReportGroups;
-pub(super) struct Channels(pub(super) CFRetained<CFMutableDictionary>);
+pub struct Channels(pub CFRetained<CFMutableDictionary>);
impl Channels {
- pub(super) fn from_groups(
+ pub fn from_groups(
groups: IoReportGroups,
functions: &IoReportFunctions,
) -> Option<Self> {
diff --git a/crates/legacy/keisoku/src/sys/ioreport/decode/group_id.rs b/crates/legacy/keisoku/src/sys/ioreport/decode/group_id.rs
index 13f730b5d..f10ac385f 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/decode/group_id.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/decode/group_id.rs
@@ -1,7 +1,7 @@
use obfstr::obfstr;
#[derive(Clone, Copy, PartialEq, Eq, Default)]
-pub(crate) enum GroupId {
+pub enum GroupId {
EnergyModel,
AmcStats,
Pmp,
@@ -10,7 +10,7 @@ pub(crate) enum GroupId {
}
impl GroupId {
- pub(crate) fn classify(group: &str) -> GroupId {
+ pub fn classify(group: &str) -> GroupId {
if group == obfstr!("Energy Model") {
GroupId::EnergyModel
} else if group == obfstr!("AMC Stats") {
diff --git a/crates/legacy/keisoku/src/sys/ioreport/decode/mod.rs b/crates/legacy/keisoku/src/sys/ioreport/decode/mod.rs
index e9214dfd6..69661a996 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/decode/mod.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/decode/mod.rs
@@ -8,9 +8,9 @@ mod subgroup;
mod unit;
pub use channel::Channel;
-pub(crate) use group_id::GroupId;
+pub use group_id::GroupId;
pub use raw_channel::RawChannel;
-pub(crate) use residency::{residency_active_percent, residency_weighted_gbps};
-pub(crate) use residency_state::ResidencyState;
-pub(crate) use subgroup::Subgroup;
-pub(crate) use unit::energy_joules;
+pub use residency::{residency_active_percent, residency_weighted_gbps};
+pub use residency_state::ResidencyState;
+pub use subgroup::Subgroup;
+pub use unit::energy_joules;
diff --git a/crates/legacy/keisoku/src/sys/ioreport/decode/naming.rs b/crates/legacy/keisoku/src/sys/ioreport/decode/naming.rs
index 51aa19d04..d64b851ba 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/decode/naming.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/decode/naming.rs
@@ -2,7 +2,7 @@ use obfstr::obfstr;
use crate::sys::ioreport::kinds::DramFlow;
-pub(crate) fn strip_die_prefix(channel: &str) -> &str {
+pub fn strip_die_prefix(channel: &str) -> &str {
let Some(rest) = channel.strip_prefix(obfstr!("DIE")) else {
return channel;
};
@@ -10,7 +10,7 @@ pub(crate) fn strip_die_prefix(channel: &str) -> &str {
rest.strip_prefix(' ').unwrap_or(channel)
}
-pub(crate) fn dcs_flow(aggregate: &str) -> Option<DramFlow> {
+pub fn dcs_flow(aggregate: &str) -> Option<DramFlow> {
if aggregate == obfstr!("DCS RD") {
Some(DramFlow::DramRead)
} else if aggregate == obfstr!("DCS WR") {
@@ -20,7 +20,7 @@ pub(crate) fn dcs_flow(aggregate: &str) -> Option<DramFlow> {
}
}
-pub(crate) fn read_write_flow(name: &str) -> Option<DramFlow> {
+pub fn read_write_flow(name: &str) -> Option<DramFlow> {
if name.ends_with(obfstr!(" RD")) {
Some(DramFlow::DramRead)
} else if name.ends_with(obfstr!(" WR")) {
diff --git a/crates/legacy/keisoku/src/sys/ioreport/decode/raw_channel.rs b/crates/legacy/keisoku/src/sys/ioreport/decode/raw_channel.rs
index 4a52bf0b0..d0f9a9d5f 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/decode/raw_channel.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/decode/raw_channel.rs
@@ -2,10 +2,10 @@ use super::{group_id::GroupId, residency_state::ResidencyState};
#[derive(Default)]
pub struct RawChannel {
- pub(crate) group: GroupId,
- pub(crate) subgroup: String,
- pub(crate) name: String,
- pub(crate) unit: u64,
- pub(crate) integer_value: i64,
- pub(crate) states: Vec<ResidencyState>,
+ pub group: GroupId,
+ pub subgroup: String,
+ pub name: String,
+ pub unit: u64,
+ pub integer_value: i64,
+ pub states: Vec<ResidencyState>,
}
diff --git a/crates/legacy/keisoku/src/sys/ioreport/decode/residency.rs b/crates/legacy/keisoku/src/sys/ioreport/decode/residency.rs
index da07f9b4d..0f1f18ecc 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/decode/residency.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/decode/residency.rs
@@ -2,7 +2,7 @@ use obfstr::obfstr;
use super::residency_state::ResidencyState;
-pub(crate) fn residency_active_percent(states: &[ResidencyState]) -> f32 {
+pub fn residency_active_percent(states: &[ResidencyState]) -> f32 {
let total: f64 = states.iter().map(|state| state.residency as f64).sum();
if total <= 0.0 {
return 0.0;
@@ -12,7 +12,7 @@ pub(crate) fn residency_active_percent(states: &[ResidencyState]) -> f32 {
(active / total * 100.0) as f32
}
-pub(crate) fn residency_weighted_gbps(states: &[ResidencyState]) -> f32 {
+pub fn residency_weighted_gbps(states: &[ResidencyState]) -> f32 {
let mut weighted = 0f64;
let mut total = 0f64;
for state in states {
@@ -32,7 +32,7 @@ fn parse_leading_number(name: &str) -> f64 {
digits.parse().unwrap_or(0.0)
}
-pub(crate) fn is_idle_state(name: &str) -> bool {
+pub fn is_idle_state(name: &str) -> bool {
name == obfstr!("OFF")
|| name == obfstr!("IDLE")
|| name == obfstr!("DOWN")
diff --git a/crates/legacy/keisoku/src/sys/ioreport/decode/residency_state.rs b/crates/legacy/keisoku/src/sys/ioreport/decode/residency_state.rs
index 40654c786..569158b5d 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/decode/residency_state.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/decode/residency_state.rs
@@ -1,5 +1,5 @@
#[derive(Default)]
-pub(crate) struct ResidencyState {
- pub(crate) name: String,
- pub(crate) residency: i64,
+pub struct ResidencyState {
+ pub name: String,
+ pub residency: i64,
}
diff --git a/crates/legacy/keisoku/src/sys/ioreport/decode/subgroup.rs b/crates/legacy/keisoku/src/sys/ioreport/decode/subgroup.rs
index 9af4796ea..79fb1272a 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/decode/subgroup.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/decode/subgroup.rs
@@ -1,7 +1,7 @@
use obfstr::obfstr;
#[derive(Clone, Copy, PartialEq, Eq)]
-pub(crate) enum Subgroup {
+pub enum Subgroup {
DcsBandwidth,
DramBandwidth,
Floor,
@@ -9,7 +9,7 @@ pub(crate) enum Subgroup {
}
impl Subgroup {
- pub(crate) fn classify(subgroup: &str) -> Subgroup {
+ pub fn classify(subgroup: &str) -> Subgroup {
if subgroup == obfstr!("DCS BW") {
Subgroup::DcsBandwidth
} else if subgroup == obfstr!("DRAM BW") {
diff --git a/crates/legacy/keisoku/src/sys/ioreport/decode/unit.rs b/crates/legacy/keisoku/src/sys/ioreport/decode/unit.rs
index 06f28aa2d..6838c18bf 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/decode/unit.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/decode/unit.rs
@@ -3,7 +3,7 @@ const QUANTITY_SHIFT: u32 = 56;
const SI_SCALE_SHIFT: u32 = 32;
const SI_EXPONENT_BIAS: i32 = 127;
-pub(crate) fn energy_joules(
+pub fn energy_joules(
value: i64,
unit: u64,
) -> f64 {
diff --git a/crates/legacy/keisoku/src/sys/ioreport/io_report.rs b/crates/legacy/keisoku/src/sys/ioreport/io_report.rs
index 71944cf09..442e0b55a 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/io_report.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/io_report.rs
@@ -6,7 +6,7 @@ use crate::sys::ioreport::{
decode::{Channel, RawChannel},
};
-pub(crate) struct IoReport {
+pub struct IoReport {
functions: &'static IoReportFunctions,
subscription: Subscription,
}
@@ -21,11 +21,11 @@ impl IoReport {
})
}
- pub(crate) fn snapshot(&self) -> Option<RawIOReportSample> {
+ pub fn snapshot(&self) -> Option<RawIOReportSample> {
self.subscription.snapshot(self.functions).map(RawIOReportSample)
}
- pub(crate) fn for_each_channel(
+ pub fn for_each_channel(
&self,
begin: &RawIOReportSample,
end: &RawIOReportSample,
diff --git a/crates/legacy/keisoku/src/sys/ioreport/kinds/flow/mod.rs b/crates/legacy/keisoku/src/sys/ioreport/kinds/flow/mod.rs
index 02eb72edd..6a50493f7 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/kinds/flow/mod.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/kinds/flow/mod.rs
@@ -1,4 +1,4 @@
-pub(crate) trait FlowKind: 'static {
+pub trait FlowKind: 'static {
const FLOW: DramFlow;
const TYPE_BIT_BYTES: u128;
const TYPE_BIT_HIST: u128;
diff --git a/crates/legacy/keisoku/src/sys/ioreport/kinds/mod.rs b/crates/legacy/keisoku/src/sys/ioreport/kinds/mod.rs
index d12366639..fc2ec1159 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/kinds/mod.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/kinds/mod.rs
@@ -1,7 +1,5 @@
mod flow;
mod rail;
-pub(crate) use flow::FlowKind;
-pub use flow::{DramFlow, DramRead, DramWrite};
-pub(crate) use rail::RailKind;
-pub use rail::{Ane, Cpu, Gpu, Rail, Ram};
+pub use flow::{DramFlow, DramRead, DramWrite, FlowKind};
+pub use rail::{Ane, Cpu, Gpu, Rail, RailKind, Ram};
diff --git a/crates/legacy/keisoku/src/sys/ioreport/kinds/rail/mod.rs b/crates/legacy/keisoku/src/sys/ioreport/kinds/rail/mod.rs
index 0d7fbd3fa..0347cde7d 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/kinds/rail/mod.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/kinds/rail/mod.rs
@@ -1,6 +1,6 @@
use obfstr::obfstr;
-pub(crate) trait RailKind: 'static {
+pub trait RailKind: 'static {
const RAIL: Rail;
const TYPE_BIT: u128;
}
@@ -14,7 +14,7 @@ pub enum Rail {
}
impl Rail {
- pub(crate) fn classify(name: &str) -> Option<Rail> {
+ pub fn classify(name: &str) -> Option<Rail> {
if name == obfstr!("GPU Energy") {
Some(Rail::Gpu)
} else if name.ends_with(obfstr!("CPU Energy")) {
diff --git a/crates/legacy/keisoku/src/sys/ioreport/mod.rs b/crates/legacy/keisoku/src/sys/ioreport/mod.rs
index e67b1f1d3..61ad40c3b 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/mod.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/mod.rs
@@ -3,7 +3,7 @@ use core::{ffi::c_void, ptr::NonNull};
use objc2_core_foundation::{CFDictionary, CFMutableDictionary, CFRetained, CFString, CFType, Type};
pub mod decode;
-pub(crate) mod kinds;
+pub mod kinds;
mod channel;
mod channels;
@@ -13,8 +13,8 @@ mod raw_ioreport_sample;
mod subscription;
pub use groups::IoReportGroups;
-pub(crate) use io_report::IoReport;
-pub(crate) use raw_ioreport_sample::RawIOReportSample;
+pub use io_report::IoReport;
+pub use raw_ioreport_sample::RawIOReportSample;
kanka::ffi_table! {
struct IoReportFunctions from "/usr/lib/libIOReport.dylib" {
diff --git a/crates/legacy/keisoku/src/sys/ioreport/raw_ioreport_sample.rs b/crates/legacy/keisoku/src/sys/ioreport/raw_ioreport_sample.rs
index 3d6f20992..e6ffa3421 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/raw_ioreport_sample.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/raw_ioreport_sample.rs
@@ -1,3 +1,3 @@
use objc2_core_foundation::{CFDictionary, CFRetained};
-pub(crate) struct RawIOReportSample(pub(super) CFRetained<CFDictionary>);
+pub struct RawIOReportSample(pub CFRetained<CFDictionary>);
diff --git a/crates/legacy/keisoku/src/sys/ioreport/subscription.rs b/crates/legacy/keisoku/src/sys/ioreport/subscription.rs
index 65b400935..57ba08de7 100644
--- a/crates/legacy/keisoku/src/sys/ioreport/subscription.rs
+++ b/crates/legacy/keisoku/src/sys/ioreport/subscription.rs
@@ -3,13 +3,13 @@ use objc2_core_foundation::{CFDictionary, CFRetained, CFType};
use super::{IoReportFunctions, channels::Channels};
use crate::sys::ioreport::IoReportGroups;
-pub(super) struct Subscription {
+pub struct Subscription {
handle: CFRetained<CFType>,
channels: Channels,
}
impl Subscription {
- pub(super) fn for_groups(
+ pub fn for_groups(
groups: IoReportGroups,
functions: &IoReportFunctions,
) -> Option<Self> {
@@ -28,7 +28,7 @@ impl Subscription {
})
}
- pub(super) fn snapshot(
+ pub fn snapshot(
&self,
functions: &IoReportFunctions,
) -> Option<CFRetained<CFDictionary>> {
diff --git a/crates/legacy/keisoku/src/sys/memory.rs b/crates/legacy/keisoku/src/sys/memory.rs
index dd92f45bb..09c418e37 100644
--- a/crates/legacy/keisoku/src/sys/memory.rs
+++ b/crates/legacy/keisoku/src/sys/memory.rs
@@ -1,6 +1,6 @@
use crate::{metrics::MemoryMetrics, units::Bytes};
-pub(crate) fn read_memory() -> Option<MemoryMetrics> {
+pub fn read_memory() -> Option<MemoryMetrics> {
let mut ram_total = 0u64;
unsafe {
let mut name = [libc::CTL_HW, libc::HW_MEMSIZE];
diff --git a/crates/legacy/keisoku/src/sys/mod.rs b/crates/legacy/keisoku/src/sys/mod.rs
index 0a92f016a..aa29a635e 100644
--- a/crates/legacy/keisoku/src/sys/mod.rs
+++ b/crates/legacy/keisoku/src/sys/mod.rs
@@ -8,24 +8,24 @@ mod sysctl;
mod thermal;
#[cfg(target_os = "macos")]
-pub(crate) mod ioreport;
+pub mod ioreport;
#[cfg(target_os = "macos")]
-pub(crate) mod registry;
+pub mod registry;
#[cfg(target_os = "macos")]
-pub(crate) mod smc;
+pub mod smc;
#[cfg(target_os = "macos")]
-pub(crate) mod soc;
+pub mod soc;
-pub(crate) mod hid;
+pub mod hid;
#[cfg(target_os = "macos")]
-pub(crate) use battery::read_battery;
-pub(crate) use constants::{
+pub use battery::read_battery;
+pub use constants::{
EVENT_TYPE_POWER, EVENT_TYPE_TEMPERATURE, HID_PAGE_APPLE_VENDOR, HID_PAGE_APPLE_VENDOR_POWER,
HID_USAGE_POWER_CURRENT, HID_USAGE_POWER_VOLTAGE, HID_USAGE_TEMPERATURE_SENSOR, event_field_base,
};
#[cfg(target_os = "macos")]
-pub(crate) use memory::read_memory;
+pub use memory::read_memory;
#[cfg(not(target_os = "macos"))]
-pub(crate) use sysctl::{perflevel_cores, sysctl_string};
-pub(crate) use thermal::read_thermal_pressure;
+pub use sysctl::{perflevel_cores, sysctl_string};
+pub use thermal::read_thermal_pressure;
diff --git a/crates/legacy/keisoku/src/sys/smc/fourcc.rs b/crates/legacy/keisoku/src/sys/smc/fourcc.rs
index 83928eee8..dc5392926 100644
--- a/crates/legacy/keisoku/src/sys/smc/fourcc.rs
+++ b/crates/legacy/keisoku/src/sys/smc/fourcc.rs
@@ -1,4 +1,4 @@
-pub(super) fn fourcc(key: &str) -> Option<u32> {
+pub fn fourcc(key: &str) -> Option<u32> {
let bytes = key.as_bytes();
if bytes.len() != 4 {
return None;
diff --git a/crates/legacy/keisoku/src/sys/smc/key_data.rs b/crates/legacy/keisoku/src/sys/smc/key_data.rs
index 6f67d5fb8..319c0b98e 100644
--- a/crates/legacy/keisoku/src/sys/smc/key_data.rs
+++ b/crates/legacy/keisoku/src/sys/smc/key_data.rs
@@ -1,6 +1,6 @@
#[repr(C)]
#[derive(Clone, Copy, Default)]
-pub(super) struct SmcVersion {
+pub struct SmcVersion {
major: u8,
minor: u8,
build: u8,
@@ -10,7 +10,7 @@ pub(super) struct SmcVersion {
#[repr(C)]
#[derive(Clone, Copy, Default)]
-pub(super) struct SmcLimitData {
+pub struct SmcLimitData {
version: u16,
length: u16,
cpu_plimit: u32,
@@ -20,24 +20,24 @@ pub(super) struct SmcLimitData {
#[repr(C)]
#[derive(Clone, Copy, Default)]
-pub(super) struct SmcKeyInfo {
- pub(super) data_size: u32,
- pub(super) data_type: u32,
- pub(super) data_attributes: u8,
+pub struct SmcKeyInfo {
+ pub data_size: u32,
+ pub data_type: u32,
+ pub data_attributes: u8,
}
#[repr(C)]
#[derive(Clone, Copy, Default)]
-pub(super) struct SmcKeyData {
- pub(super) key: u32,
- pub(super) version: SmcVersion,
- pub(super) limit: SmcLimitData,
- pub(super) key_info: SmcKeyInfo,
- pub(super) result: u8,
- pub(super) status: u8,
- pub(super) data8: u8,
- pub(super) data32: u32,
- pub(super) bytes: [u8; 32],
+pub struct SmcKeyData {
+ pub key: u32,
+ pub version: SmcVersion,
+ pub limit: SmcLimitData,
+ pub key_info: SmcKeyInfo,
+ pub result: u8,
+ pub status: u8,
+ pub data8: u8,
+ pub data32: u32,
+ pub bytes: [u8; 32],
}
const _: () = assert!(core::mem::size_of::<SmcKeyData>() == 80);
diff --git a/crates/legacy/keisoku/src/sys/sysctl.rs b/crates/legacy/keisoku/src/sys/sysctl.rs
index 2958c1da0..c83e011d8 100644
--- a/crates/legacy/keisoku/src/sys/sysctl.rs
+++ b/crates/legacy/keisoku/src/sys/sysctl.rs
@@ -1,4 +1,4 @@
-pub(crate) fn sysctl_string(name: &str) -> Option<String> {
+pub fn sysctl_string(name: &str) -> Option<String> {
let name = std::ffi::CString::new(name).ok()?;
let mut len = 0usize;
let probe = unsafe { libc::sysctlbyname(name.as_ptr(), std::ptr::null_mut(), &mut len, std::ptr::null_mut(), 0) };
@@ -27,7 +27,7 @@ fn sysctl_u32(name: &str) -> Option<u32> {
(read == 0).then_some(value)
}
-pub(crate) fn perflevel_cores() -> (u8, u8) {
+pub fn perflevel_cores() -> (u8, u8) {
let performance = sysctl_u32("hw.perflevel0.logicalcpu").unwrap_or(0);
let efficiency = if sysctl_u32("hw.nperflevels").unwrap_or(1) > 1 {
sysctl_u32("hw.perflevel1.logicalcpu").unwrap_or(0)
diff --git a/crates/legacy/keisoku/src/sys/thermal.rs b/crates/legacy/keisoku/src/sys/thermal.rs
index 6afed1ed6..8fb2c76ce 100644
--- a/crates/legacy/keisoku/src/sys/thermal.rs
+++ b/crates/legacy/keisoku/src/sys/thermal.rs
@@ -15,7 +15,7 @@ impl ThermalPressure {
}
}
-pub(crate) fn read_thermal_pressure() -> Option<ThermalPressure> {
+pub fn read_thermal_pressure() -> Option<ThermalPressure> {
unsafe extern "C" {
fn notify_register_check(
name: *const c_char,
diff --git a/crates/legacy/keisoku/src/units.rs b/crates/legacy/keisoku/src/units.rs
index fe0eea516..b5e9f7544 100644
--- a/crates/legacy/keisoku/src/units.rs
+++ b/crates/legacy/keisoku/src/units.rs
@@ -93,7 +93,6 @@ macro_rules! unit {
};
}
-unit!(float Watts(f32) = "W");
unit!(float Joules(f32) = "J");
unit!(float Percent(f32) = "%");
unit!(float GigabytesPerSecond(f32) = "GB/s");
diff --git a/crates/legacy/keisoku/tests/telemetry.rs b/crates/legacy/keisoku/tests/telemetry.rs
index d71686332..f5293c6b8 100644
--- a/crates/legacy/keisoku/tests/telemetry.rs
+++ b/crates/legacy/keisoku/tests/telemetry.rs
@@ -14,7 +14,6 @@ fn available_telemetry() {
let sensors = device.temperature_sensors();
let voltage = device.voltage_sensors();
let current = device.current_sensors();
- let rail_power = device.rail_power();
println!("--- keisoku available telemetry ---");
println!("device {} {}E+{}P {} GPU", chip, efficiency_cores, performance_cores, gpu_cores);
@@ -27,7 +26,6 @@ fn available_telemetry() {
println!("sensors {}", sensors.len());
println!("voltage {}", voltage.len());
println!("current {}", current.len());
- println!("rail_power {}", rail_power.map_or("--".into(), |watts| format!("{:.2} W", watts.value())));
if let Some(memory) = &memory {
println!(
diff --git a/crates/legacy/nagare/Cargo.toml b/crates/legacy/nagare/Cargo.toml
index 371206bb0..7f67c5526 100644
--- a/crates/legacy/nagare/Cargo.toml
+++ b/crates/legacy/nagare/Cargo.toml
@@ -84,7 +84,7 @@ tsify = { workspace = true, optional = true }
[target.'cfg(not(target_family = "wasm"))'.dependencies]
uuid.workspace = true
-[target.'cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "visionos"))'.dependencies]
+[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
keisoku.workspace = true
[build-dependencies]
diff --git a/crates/legacy/nagare/src/chat/error.rs b/crates/legacy/nagare/src/chat/error.rs
index 41aa5c5b2..94e7bd584 100644
--- a/crates/legacy/nagare/src/chat/error.rs
+++ b/crates/legacy/nagare/src/chat/error.rs
@@ -1,5 +1,8 @@
use serde::{Deserialize, Serialize};
+#[cfg(any(target_os = "macos", target_os = "ios"))]
+use crate::util::power::Error as EnergyError;
+
#[bindings::export(Error)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, thiserror::Error)]
#[non_exhaustive]
@@ -23,3 +26,12 @@ pub enum ChatSessionError {
limit: u32,
},
}
+
+#[cfg(any(target_os = "macos", target_os = "ios"))]
+impl From<EnergyError> for ChatSessionError {
+ fn from(error: EnergyError) -> Self {
+ Self::Backend {
+ message: error.to_string(),
+ }
+ }
+}
diff --git a/crates/legacy/nagare/src/chat/mod.rs b/crates/legacy/nagare/src/chat/mod.rs
index 89a12ffef..75f0c0168 100644
--- a/crates/legacy/nagare/src/chat/mod.rs
+++ b/crates/legacy/nagare/src/chat/mod.rs
@@ -23,8 +23,8 @@ use shoji::{
basic::{CancelToken, SamplingParameters, ToolCall, ToolDescription, ToolNamespace, Value, parse_lenient_json},
model::Model,
session::chat::{
- ChatConfig, ChatContentBlock, ChatMessage, ChatReply, ChatReplyConfig, ChatReplyFinishReason,
- ChatReplyPowerStats, ChatReplySpeculatorStats, ChatReplyStats, ChatRole,
+ ChatConfig, ChatContentBlock, ChatMessage, ChatReply, ChatReplyConfig, ChatReplyEnergy,
+ ChatReplyFinishReason, ChatReplySpeculatorStats, ChatReplyStats, ChatRole,
},
},
};
@@ -724,14 +724,15 @@ fn aggregate_stats(
ChatReplyStats {
duration: stats.iter().map(|stats| stats.duration).sum(),
time_to_first_token: stats.iter().find_map(|stats| stats.time_to_first_token),
- prefill_tokens_per_second: stats.iter().find_map(|stats| stats.prefill_tokens_per_second),
+ prefill_tokens_per_second: aggregate_prefill_rate(&stats),
generate_tokens_per_second: aggregate_generate_rate(&stats),
tokens_count_input: sum_optional_u32(&stats, |stats| stats.tokens_count_input),
tokens_count_input_cached: sum_optional_u32(&stats, |stats| stats.tokens_count_input_cached),
tokens_count_output: sum_optional_u32(&stats, |stats| stats.tokens_count_output),
memory_used_bytes: stats.iter().filter_map(|stats| stats.memory_used_bytes).max(),
speculator_stats,
- power_stats: aggregate_power_stats(&stats),
+ input_energy: aggregate_input_energy(&stats),
+ output_energy: aggregate_output_energy(&stats),
}
}
@@ -772,6 +773,25 @@ fn aggregate_generate_rate(stats: &[&ChatReplyStats]) -> Option<f64> {
(token_intervals > 0 && duration > 0.0).then(|| token_intervals as f64 / duration)
}
+fn aggregate_prefill_rate(stats: &[&ChatReplyStats]) -> Option<f64> {
+ if let [stats] = stats {
+ return stats.prefill_tokens_per_second;
+ }
+
+ let (tokens, duration) = stats.iter().try_fold((0_u64, 0.0), |(tokens, duration), stats| {
+ let input_tokens = stats.tokens_count_input?;
+ if input_tokens == 0 {
+ return Some((tokens, duration));
+ }
+ let prefill_rate = stats.prefill_tokens_per_second?;
+ if !prefill_rate.is_finite() || prefill_rate <= 0.0 {
+ return None;
+ }
+ Some((tokens + u64::from(input_tokens), duration + f64::from(input_tokens) / prefill_rate))
+ })?;
+ (tokens > 0 && duration > 0.0).then(|| tokens as f64 / duration)
+}
+
fn sum_optional_u32(
stats: &[&ChatReplyStats],
value: impl Fn(&ChatReplyStats) -> Option<u32>,
@@ -779,34 +799,28 @@ fn sum_optional_u32(
stats.iter().try_fold(0_u32, |sum, stats| sum.checked_add(value(stats)?))
}
-fn aggregate_power_stats(stats: &[&ChatReplyStats]) -> Option<ChatReplyPowerStats> {
- let stats =
- stats.iter().filter_map(|stats| stats.power_stats.as_ref().map(|power| (*stats, power))).collect::<Vec<_>>();
- if stats.is_empty() {
- return None;
- }
-
- Some(ChatReplyPowerStats {
- samples_count: stats.iter().map(|(_, power)| power.samples_count).sum(),
- average_cpu_watts: duration_weighted_power(&stats, |power| power.average_cpu_watts),
- average_gpu_watts: duration_weighted_power(&stats, |power| power.average_gpu_watts),
- average_ane_watts: duration_weighted_power(&stats, |power| power.average_ane_watts),
- average_ram_watts: duration_weighted_power(&stats, |power| power.average_ram_watts),
- average_total_watts: duration_weighted_power(&stats, |power| power.average_total_watts),
- energy_joules: stats.iter().map(|(_, power)| power.energy_joules).sum(),
- })
+fn aggregate_input_energy(stats: &[&ChatReplyStats]) -> Option<ChatReplyEnergy> {
+ let mut phases = Vec::with_capacity(stats.len());
+ for stats in stats {
+ phases.push(stats.input_energy.as_ref()?);
+ }
+ aggregate_energy(phases)
}
-fn duration_weighted_power(
- stats: &[(&ChatReplyStats, &ChatReplyPowerStats)],
- value: impl Fn(&ChatReplyPowerStats) -> f64,
-) -> f64 {
- let duration = stats.iter().map(|(stats, _)| stats.duration).sum::<f64>();
- if duration > 0.0 {
- stats.iter().map(|(stats, power)| value(power) * stats.duration).sum::<f64>() / duration
- } else {
- stats.iter().map(|(_, power)| value(power)).sum::<f64>() / stats.len() as f64
+fn aggregate_output_energy(stats: &[&ChatReplyStats]) -> Option<ChatReplyEnergy> {
+ let mut phases = Vec::with_capacity(stats.len());
+ for stats in stats {
+ let tokens = stats.tokens_count_output?;
+ if tokens == 0 {
+ continue;
+ }
+ phases.push(stats.output_energy.as_ref()?);
}
+ aggregate_energy(phases)
+}
+
+fn aggregate_energy<'a>(energy: impl IntoIterator<Item = &'a ChatReplyEnergy>) -> Option<ChatReplyEnergy> {
+ energy.into_iter().cloned().reduce(|total, energy| total + energy)
}
fn find_tools_definitions(messages: &mut [ChatMessage]) -> Option<&mut Vec<ToolNamespace>> {
diff --git a/crates/legacy/nagare/src/chat/token.rs b/crates/legacy/nagare/src/chat/token.rs
index 02502ac4f..99ccc7540 100644
--- a/crates/legacy/nagare/src/chat/token.rs
+++ b/crates/legacy/nagare/src/chat/token.rs
@@ -9,6 +9,8 @@ use hanashi::{
Encoding as EncodingTrait,
chat::{Encoding, EncodingConfig, TokenizerLocation, hanashi::HanashiEncodingImpl, harmony::HarmonyEncodingImpl},
};
+#[cfg(any(target_os = "macos", target_os = "ios"))]
+use keisoku::KeisokuError;
use shoji::{
traits::{
State,
@@ -24,17 +26,16 @@ use shoji::{
basic::{SamplingParameters, TokenId},
model::Model,
session::chat::{
- ChatConfig, ChatContentBlock, ChatMessage, ChatReplyConfig, ChatReplyFinishReason,
+ ChatConfig, ChatContentBlock, ChatMessage, ChatReplyConfig, ChatReplyEnergy, ChatReplyFinishReason,
ChatReplySpeculatorStats, ChatReplyStats,
},
},
};
use tokio_util::sync::CancellationToken;
-use crate::{
- chat::ChatSessionError,
- util::{helpers::error_stream, power::PowerRecorder},
-};
+#[cfg(any(target_os = "macos", target_os = "ios"))]
+use crate::util::power::{EnergyRecorder, Error as EnergyError};
+use crate::{chat::ChatSessionError, util::helpers::error_stream};
pub struct Session {
instance: Arc<dyn ChatTokenBackendInstance>,
@@ -42,7 +43,8 @@ pub struct Session {
encoding: Encoding,
input_tokens: Vec<u64>,
stop_token_ids: Box<[u64]>,
- power_recorder: Box<dyn PowerRecorder>,
+ #[cfg(any(target_os = "macos", target_os = "ios"))]
+ energy_recorder: EnergyRecorder,
}
impl Session {
@@ -108,7 +110,8 @@ impl Session {
encoding,
input_tokens: Vec::new(),
stop_token_ids,
- power_recorder: <dyn PowerRecorder>::create(),
+ #[cfg(any(target_os = "macos", target_os = "ios"))]
+ energy_recorder: EnergyRecorder::new(),
})
}
@@ -127,7 +130,6 @@ impl Session {
cancel_token: CancellationToken,
) -> Pin<Box<dyn Stream<Item = Result<Output, ChatSessionError>> + Send + 'a>> {
let time_start = Instant::now();
- self.power_recorder.begin();
let curr_all_tokens = self.encoding.state().tokens.clone();
let new_all_tokens = match self.build_input(input) {
@@ -173,6 +175,13 @@ impl Session {
};
let instance = self.instance.as_ref();
+ let time_prefill_start = Instant::now();
+ #[cfg(any(target_os = "macos", target_os = "ios"))]
+ {
+ if let Err(error) = self.energy_recorder.begin() {
+ return error_stream(error.into());
+ }
+ }
let stream = instance.stream(&self.input_tokens, self.state.as_mut(), config.clone(), cancel_token.clone());
let stream_state = StreamingState {
@@ -181,12 +190,14 @@ impl Session {
encoding: &mut self.encoding,
max_context_length: self.instance.max_context_length(),
stop_token_ids: self.stop_token_ids.clone(),
- power_recorder: &mut *self.power_recorder,
+ #[cfg(any(target_os = "macos", target_os = "ios"))]
+ energy_recorder: &mut self.energy_recorder,
time_start,
time_last_token: None,
- time_prefill_start: Instant::now(),
+ time_prefill_start,
time_first_token: None,
+ input_energy: None,
total_tokens_input: self.input_tokens.len(),
cached_tokens_input,
total_tokens_output: 0,
@@ -211,7 +222,7 @@ impl Session {
},
None => {
if !terminated && state.cancel_token.is_cancelled() {
- let output = Ok(Self::render_output(&state, Some(ChatReplyFinishReason::Cancelled)));
+ let output = Self::render_output(&mut state, Some(ChatReplyFinishReason::Cancelled));
Some((output, (inner, state, true, true)))
} else {
None
@@ -250,7 +261,7 @@ impl Session {
fn build_output(
event: Result<StreamOutput, Error>,
- state: &mut StreamingState,
+ state: &mut StreamingState<'_>,
) -> Result<Output, ChatSessionError> {
let now = Instant::now();
let result = event.map_err(|err| ChatSessionError::Backend {
@@ -258,10 +269,18 @@ impl Session {
})?;
match result {
- StreamOutput::LimitReached => Ok(Self::render_output(state, Some(ChatReplyFinishReason::Length))),
+ StreamOutput::LimitReached => Self::render_output(state, Some(ChatReplyFinishReason::Length)),
StreamOutput::Token(token) => {
if state.total_tokens_output == 0 {
- state.time_first_token = Some(now)
+ state.time_first_token = Some(now);
+ #[cfg(any(target_os = "macos", target_os = "ios"))]
+ {
+ match state.energy_recorder.split() {
+ Ok(energy) => state.input_energy = Some(energy),
+ Err(EnergyError::Keisoku(KeisokuError::PowerReadingUnavailable)) => {},
+ Err(error) => return Err(error.into()),
+ }
+ }
}
state.total_tokens_output += 1;
state.time_last_token = Some(now);
@@ -273,22 +292,23 @@ impl Session {
}
let finish_reason = state.get_finish_reason(token);
- Ok(Self::render_output(state, finish_reason))
+ Self::render_output(state, finish_reason)
},
}
}
fn render_output(
- state: &StreamingState,
+ state: &mut StreamingState<'_>,
finish_reason: Option<ChatReplyFinishReason>,
- ) -> Output {
+ ) -> Result<Output, ChatSessionError> {
let have_finish_reason = finish_reason.is_some();
+ let stats = state.get_stats(have_finish_reason)?;
let Some(message) = state.encoding.state().messages.last() else {
- return Output {
+ return Ok(Output {
finish_reason,
- stats: state.get_stats(have_finish_reason),
+ stats,
..Default::default()
- };
+ });
};
let tool_calls = message
@@ -316,13 +336,13 @@ impl Session {
finish_reason
};
- Output {
+ Ok(Output {
reasoning: message.reasoning(),
text: message.text(),
tool_calls,
finish_reason,
- stats: state.get_stats(have_finish_reason),
- }
+ stats,
+ })
}
pub fn supports_tool_calls(&self) -> bool {
@@ -344,12 +364,14 @@ struct StreamingState<'a> {
encoding: &'a mut Encoding,
max_context_length: Option<usize>,
stop_token_ids: Box<[u64]>,
- power_recorder: &'a mut dyn PowerRecorder,
+ #[cfg(any(target_os = "macos", target_os = "ios"))]
+ energy_recorder: &'a mut EnergyRecorder,
time_start: Instant,
time_last_token: Option<Instant>,
time_prefill_start: Instant,
time_first_token: Option<Instant>,
+ input_energy: Option<ChatReplyEnergy>,
total_tokens_input: usize,
cached_tokens_input: usize,
total_tokens_output: usize,
@@ -381,9 +403,9 @@ impl StreamingState<'_> {
}
fn get_stats(
- &self,
+ &mut self,
last_stat: bool,
- ) -> ChatReplyStats {
+ ) -> Result<ChatReplyStats, ChatSessionError> {
let speculator_stats = if let Some(metrics) = self.metrics.as_ref() {
let num_forward_passes = metrics.num_prefill_forward_passes + metrics.num_decode_forward_passes;
(num_forward_passes > 0).then(|| ChatReplySpeculatorStats {
@@ -394,8 +416,6 @@ impl StreamingState<'_> {
None
};
- let power_stats = last_stat.then(|| self.power_recorder.finish()).flatten();
-
let total_duration = self.time_last_token.unwrap_or(Instant::now()).duration_since(self.time_start);
let ttft_duration =
self.time_first_token.map(|time_first_token| time_first_token.duration_since(self.time_prefill_start));
@@ -411,7 +431,24 @@ impl StreamingState<'_> {
};
let generate_tps = calculate_rate(self.total_tokens_output, generate_duration);
- ChatReplyStats {
+ #[cfg(any(target_os = "macos", target_os = "ios"))]
+ let completed_energy = if last_stat {
+ match self.energy_recorder.finish() {
+ Ok(energy) => Some(energy),
+ Err(EnergyError::Keisoku(KeisokuError::PowerReadingUnavailable)) => None,
+ Err(error) => return Err(error.into()),
+ }
+ } else {
+ None
+ };
+ #[cfg(not(any(target_os = "macos", target_os = "ios")))]
+ let completed_energy = None;
+ let (input_energy, output_energy) = if self.time_first_token.is_some() {
+ (self.input_energy.clone(), completed_energy)
+ } else {
+ (completed_energy, None)
+ };
+ Ok(ChatReplyStats {
duration: total_duration.as_secs_f64(),
time_to_first_token: ttft_duration.map(|time| time.as_secs_f64()),
prefill_tokens_per_second: prefill_tps,
@@ -421,8 +458,9 @@ impl StreamingState<'_> {
tokens_count_output: Some(self.total_tokens_output as u32),
memory_used_bytes: last_stat.then(|| self.memory_usage.map(|bytes| bytes as i64)).flatten(),
speculator_stats,
- power_stats,
- }
+ input_energy,
+ output_energy,
+ })
}
}
diff --git a/crates/legacy/nagare/src/telemetry/event.rs b/crates/legacy/nagare/src/telemetry/event.rs
index 5fa9a2642..9ffc394cc 100644
--- a/crates/legacy/nagare/src/telemetry/event.rs
+++ b/crates/legacy/nagare/src/telemetry/event.rs
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
-use shoji::types::session::chat::ChatReplyStats;
+use shoji::types::session::chat::{ChatReplyJoulesPerToken, ChatReplyStats};
/// Reply stats plus the metrics `ChatReplyStats` exposes as getters, which serde
/// would otherwise leave out of the reported payload.
@@ -8,13 +8,15 @@ use shoji::types::session::chat::ChatReplyStats;
pub struct TelemetryStats {
#[serde(flatten)]
pub stats: ChatReplyStats,
- pub joules_per_token: Option<f64>,
+ pub input_joules_per_token: Option<ChatReplyJoulesPerToken>,
+ pub output_joules_per_token: Option<ChatReplyJoulesPerToken>,
}
impl From<ChatReplyStats> for TelemetryStats {
fn from(stats: ChatReplyStats) -> Self {
Self {
- joules_per_token: stats.joules_per_token(),
+ input_joules_per_token: stats.input_joules_per_token(),
+ output_joules_per_token: stats.output_joules_per_token(),
stats,
}
}
diff --git a/crates/legacy/nagare/src/util/mod.rs b/crates/legacy/nagare/src/util/mod.rs
index adf0e9387..252badd5b 100644
--- a/crates/legacy/nagare/src/util/mod.rs
+++ b/crates/legacy/nagare/src/util/mod.rs
@@ -1,2 +1,3 @@
pub mod helpers;
+#[cfg(any(target_os = "macos", target_os = "ios"))]
pub mod power;
diff --git a/crates/legacy/nagare/src/util/power/apple.rs b/crates/legacy/nagare/src/util/power/apple.rs
deleted file mode 100644
index c3442ad68..000000000
--- a/crates/legacy/nagare/src/util/power/apple.rs
+++ /dev/null
@@ -1,43 +0,0 @@
-#![cfg(target_vendor = "apple")]
-
-use std::cell::RefCell;
-
-use keisoku::{PowerMeter, PowerReading, Watts};
-use shoji::types::session::chat::ChatReplyPowerStats;
-
-use crate::util::power::PowerRecorder;
-
-pub struct ApplePowerRecorder {
- meter: RefCell<PowerMeter>,
-}
-
-impl ApplePowerRecorder {
- pub fn new() -> Self {
- Self {
- meter: RefCell::new(PowerMeter::new()),
- }
- }
-}
-
-impl PowerRecorder for ApplePowerRecorder {
- fn begin(&self) {
- self.meter.borrow_mut().start();
- }
-
- fn finish(&self) -> Option<ChatReplyPowerStats> {
- self.meter.borrow_mut().stop().map(stats)
- }
-}
-
-fn stats(reading: PowerReading) -> ChatReplyPowerStats {
- let watts = |value: Option<Watts>| value.map_or(0.0, |watts| watts.value() as f64);
- ChatReplyPowerStats {
- samples_count: reading.samples as i64,
- average_cpu_watts: watts(reading.cpu),
- average_gpu_watts: watts(reading.gpu),
- average_ane_watts: watts(reading.ane),
- average_ram_watts: watts(reading.ram),
- average_total_watts: reading.total.value() as f64,
- energy_joules: reading.energy.value() as f64,
- }
-}
diff --git a/crates/legacy/nagare/src/util/power/error.rs b/crates/legacy/nagare/src/util/power/error.rs
new file mode 100644
index 000000000..7b885bce6
--- /dev/null
+++ b/crates/legacy/nagare/src/util/power/error.rs
@@ -0,0 +1,5 @@
+#[derive(Debug, thiserror::Error)]
+pub enum Error {
+ #[error(transparent)]
+ Keisoku(#[from] keisoku::KeisokuError),
+}
diff --git a/crates/legacy/nagare/src/util/power/mod.rs b/crates/legacy/nagare/src/util/power/mod.rs
index 2f9d3901d..a6da8a858 100644
--- a/crates/legacy/nagare/src/util/power/mod.rs
+++ b/crates/legacy/nagare/src/util/power/mod.rs
@@ -1,27 +1,51 @@
-#![allow(dead_code)]
+mod error;
-mod apple;
-mod stub;
+pub use error::Error;
+use keisoku::{PowerMeter, PowerReading};
+use shoji::types::session::chat::ChatReplyEnergy;
-use shoji::types::session::chat::ChatReplyPowerStats;
-
-pub trait PowerRecorder: Send {
- fn begin(&self);
- fn finish(&self) -> Option<ChatReplyPowerStats>;
+pub struct EnergyRecorder {
+ meter: PowerMeter,
}
-impl dyn PowerRecorder {
- pub fn create() -> Box<dyn PowerRecorder> {
- #[cfg(target_vendor = "apple")]
- {
- use crate::util::power::apple::ApplePowerRecorder;
- Box::new(ApplePowerRecorder::new()) as Box<dyn PowerRecorder>
+impl EnergyRecorder {
+ pub fn new() -> Self {
+ Self {
+ meter: PowerMeter::new(),
}
+ }
- #[cfg(not(target_vendor = "apple"))]
- {
- use crate::util::power::stub::StubPowerRecorder;
- Box::new(StubPowerRecorder::new()) as Box<dyn PowerRecorder>
- }
+ pub fn begin(&mut self) -> Result<(), Error> {
+ self.meter.start()?;
+ Ok(())
+ }
+
+ pub fn split(&mut self) -> Result<ChatReplyEnergy, Error> {
+ Ok(energy(self.meter.split()?))
+ }
+
+ pub fn finish(&mut self) -> Result<ChatReplyEnergy, Error> {
+ Ok(energy(self.meter.stop()?))
+ }
+}
+
+fn energy(reading: PowerReading) -> ChatReplyEnergy {
+ match reading {
+ PowerReading::Total {
+ total,
+ } => ChatReplyEnergy::Total {
+ total: total.value() as f64,
+ },
+ PowerReading::Components {
+ cpu,
+ gpu,
+ ane,
+ ram,
+ } => ChatReplyEnergy::Components {
+ cpu: cpu.value() as f64,
+ gpu: gpu.value() as f64,
+ ane: ane.value() as f64,
+ dram: ram.value() as f64,
+ },
}
}
diff --git a/crates/legacy/nagare/src/util/power/stub.rs b/crates/legacy/nagare/src/util/power/stub.rs
deleted file mode 100644
index 95bbe8a3f..000000000
--- a/crates/legacy/nagare/src/util/power/stub.rs
+++ /dev/null
@@ -1,19 +0,0 @@
-use shoji::types::session::chat::ChatReplyPowerStats;
-
-use crate::util::power::PowerRecorder;
-
-pub struct StubPowerRecorder {}
-
-impl StubPowerRecorder {
- pub fn new() -> Self {
- Self {}
- }
-}
-
-impl PowerRecorder for StubPowerRecorder {
- fn begin(&self) {}
-
- fn finish(&self) -> Option<ChatReplyPowerStats> {
- None
- }
-}
diff --git a/crates/legacy/shoji/src/types/session/chat/mod.rs b/crates/legacy/shoji/src/types/session/chat/mod.rs
index d86c0cd64..9083c674a 100644
--- a/crates/legacy/shoji/src/types/session/chat/mod.rs
+++ b/crates/legacy/shoji/src/types/session/chat/mod.rs
@@ -10,6 +10,7 @@ pub use config::ChatConfig;
pub use content_block::{ChatContentBlock, ChatContentBlockType};
pub use message::{ChatMessage, ChatMessageList, ChatMessageMetadata};
pub use reply::{
- ChatReply, ChatReplyConfig, ChatReplyFinishReason, ChatReplyPowerStats, ChatReplySpeculatorStats, ChatReplyStats,
+ ChatReply, ChatReplyConfig, ChatReplyEnergy, ChatReplyFinishReason, ChatReplyJoulesPerToken,
+ ChatReplySpeculatorStats, ChatReplyStats,
};
pub use role::ChatRole;
diff --git a/crates/legacy/shoji/src/types/session/chat/reply/mod.rs b/crates/legacy/shoji/src/types/session/chat/reply/mod.rs
index b5ad09a89..4fbdadf15 100644
--- a/crates/legacy/shoji/src/types/session/chat/reply/mod.rs
+++ b/crates/legacy/shoji/src/types/session/chat/reply/mod.rs
@@ -5,7 +5,7 @@ mod stats;
pub use config::ChatReplyConfig;
pub use finish_reason::ChatReplyFinishReason;
use serde::{Deserialize, Serialize};
-pub use stats::{ChatReplyPowerStats, ChatReplySpeculatorStats, ChatReplyStats};
+pub use stats::{ChatReplyEnergy, ChatReplyJoulesPerToken, ChatReplySpeculatorStats, ChatReplyStats};
use crate::types::session::chat::ChatMessage;
diff --git a/crates/legacy/shoji/src/types/session/chat/reply/stats.rs b/crates/legacy/shoji/src/types/session/chat/reply/stats.rs
index 19d94097d..22c375217 100644
--- a/crates/legacy/shoji/src/types/session/chat/reply/stats.rs
+++ b/crates/legacy/shoji/src/types/session/chat/reply/stats.rs
@@ -1,15 +1,157 @@
+use std::{
+ fmt::{self, Display},
+ ops::Add,
+};
+
use serde::{Deserialize, Serialize};
-#[bindings::export(Structure(Class))]
-#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
-pub struct ChatReplyPowerStats {
- pub samples_count: i64,
- pub average_cpu_watts: f64,
- pub average_gpu_watts: f64,
- pub average_ane_watts: f64,
- pub average_ram_watts: f64,
- pub average_total_watts: f64,
- pub energy_joules: f64,
+#[bindings::export(Enumeration)]
+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
+#[serde(tag = "type", rename_all = "snake_case")]
+pub enum ChatReplyEnergy {
+ Total {
+ total: f64,
+ },
+ Components {
+ cpu: f64,
+ gpu: f64,
+ ane: f64,
+ dram: f64,
+ },
+}
+
+#[cfg_attr(feature = "bindings-uniffi", uniffi::export)]
+impl ChatReplyEnergy {
+ pub fn total(&self) -> f64 {
+ match self {
+ Self::Total {
+ total,
+ } => *total,
+ Self::Components {
+ cpu,
+ gpu,
+ ane,
+ dram,
+ } => cpu + gpu + ane + dram,
+ }
+ }
+}
+
+impl ChatReplyEnergy {
+ fn per_token(
+ &self,
+ tokens_count: u32,
+ ) -> Option<ChatReplyJoulesPerToken> {
+ if tokens_count == 0 {
+ return None;
+ }
+ let tokens_count = f64::from(tokens_count);
+
+ Some(match self {
+ Self::Total {
+ total,
+ } => ChatReplyJoulesPerToken::Total {
+ total: total / tokens_count,
+ },
+ Self::Components {
+ cpu,
+ gpu,
+ ane,
+ dram,
+ } => ChatReplyJoulesPerToken::Components {
+ cpu: cpu / tokens_count,
+ gpu: gpu / tokens_count,
+ ane: ane / tokens_count,
+ dram: dram / tokens_count,
+ },
+ })
+ }
+}
+
+impl Add for ChatReplyEnergy {
+ type Output = Self;
+
+ fn add(
+ self,
+ other: Self,
+ ) -> Self {
+ match (self, other) {
+ (
+ Self::Components {
+ cpu: left_cpu,
+ gpu: left_gpu,
+ ane: left_ane,
+ dram: left_dram,
+ },
+ Self::Components {
+ cpu: right_cpu,
+ gpu: right_gpu,
+ ane: right_ane,
+ dram: right_dram,
+ },
+ ) => Self::Components {
+ cpu: left_cpu + right_cpu,
+ gpu: left_gpu + right_gpu,
+ ane: left_ane + right_ane,
+ dram: left_dram + right_dram,
+ },
+ (left, right) => Self::Total {
+ total: left.total() + right.total(),
+ },
+ }
+ }
+}
+
+#[bindings::export(Enumeration)]
+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
+#[serde(tag = "type", rename_all = "snake_case")]
+pub enum ChatReplyJoulesPerToken {
+ Total {
+ total: f64,
+ },
+ Components {
+ cpu: f64,
+ gpu: f64,
+ ane: f64,
+ dram: f64,
+ },
+}
+
+impl Display for ChatReplyJoulesPerToken {
+ fn fmt(
+ &self,
+ formatter: &mut fmt::Formatter<'_>,
+ ) -> fmt::Result {
+ let total = self.total();
+ match self {
+ Self::Total {
+ ..
+ } => write!(formatter, "{total:.3} J/tok"),
+ Self::Components {
+ cpu,
+ gpu,
+ ane,
+ dram,
+ } => write!(formatter, "CPU {cpu:.3}, GPU {gpu:.3}, ANE {ane:.3}, DRAM {dram:.3}, total {total:.3} J/tok"),
+ }
+ }
+}
+
+#[cfg_attr(feature = "bindings-uniffi", uniffi::export)]
+impl ChatReplyJoulesPerToken {
+ pub fn total(&self) -> f64 {
+ match self {
+ Self::Total {
+ total,
+ } => *total,
+ Self::Components {
+ cpu,
+ gpu,
+ ane,
+ dram,
+ } => cpu + gpu + ane + dram,
+ }
+ }
}
#[bindings::export(Structure(Class))]
@@ -31,7 +173,8 @@ pub struct ChatReplyStats {
pub tokens_count_output: Option<u32>,
pub memory_used_bytes: Option<i64>,
pub speculator_stats: Option<ChatReplySpeculatorStats>,
- pub power_stats: Option<ChatReplyPowerStats>,
+ pub input_energy: Option<ChatReplyEnergy>,
+ pub output_energy: Option<ChatReplyEnergy>,
}
#[bindings::export(Implementation)]
@@ -41,11 +184,18 @@ impl ChatReplyStats {
self.tokens_count_input.and_then(|input| self.tokens_count_output.map(|output| input + output))
}
- /// Energy spent per processed token, counting both input and output tokens.
#[bindings::export(Method(Getter))]
- pub fn joules_per_token(&self) -> Option<f64> {
- let energy_joules = self.power_stats.as_ref()?.energy_joules;
- let tokens_count = self.tokens_count()?;
- (tokens_count > 0).then(|| energy_joules / f64::from(tokens_count))
+ pub fn total_joules(&self) -> Option<f64> {
+ self.input_energy.iter().chain(self.output_energy.iter()).map(ChatReplyEnergy::total).reduce(f64::add)
+ }
+
+ #[bindings::export(Method(Getter))]
+ pub fn input_joules_per_token(&self) -> Option<ChatReplyJoulesPerToken> {
+ self.input_energy.as_ref()?.per_token(self.tokens_count_input?)
+ }
+
+ #[bindings::export(Method(Getter))]
+ pub fn output_joules_per_token(&self) -> Option<ChatReplyJoulesPerToken> {
+ self.output_energy.as_ref()?.per_token(self.tokens_count_output?.checked_sub(1)?)
}
}
diff --git a/crates/legacy/uzu/bindings/python/uzu/__init__.pyi b/crates/legacy/uzu/bindings/python/uzu/__init__.pyi
index 5095c7b94..95c6d64c3 100644
--- a/crates/legacy/uzu/bindings/python/uzu/__init__.pyi
+++ b/crates/legacy/uzu/bindings/python/uzu/__init__.pyi
@@ -17,8 +17,9 @@ __all__ = [
"ChatModelCapabilities",
"ChatReply",
"ChatReplyConfig",
+ "ChatReplyEnergy",
"ChatReplyFinishReason",
- "ChatReplyPowerStats",
+ "ChatReplyJoulesPerToken",
"ChatReplySpeculatorStats",
"ChatReplyStats",
"ChatRole",
@@ -345,23 +346,51 @@ class ChatReplyConfig:
def with_tool_turn_limit(self, tool_turn_limit: typing.Optional[builtins.int]) -> ChatReplyConfig: ...
def with_grammar(self, grammar: typing.Optional[Grammar]) -> ChatReplyConfig: ...
-class ChatReplyPowerStats:
- @property
- def samples_count(self) -> builtins.int: ...
- @property
- def average_cpu_watts(self) -> builtins.float: ...
- @property
- def average_gpu_watts(self) -> builtins.float: ...
- @property
- def average_ane_watts(self) -> builtins.float: ...
- @property
- def average_ram_watts(self) -> builtins.float: ...
- @property
- def average_total_watts(self) -> builtins.float: ...
- @property
- def energy_joules(self) -> builtins.float: ...
- def __new__(cls, samples_count: builtins.int, average_cpu_watts: builtins.float, average_gpu_watts: builtins.float, average_ane_watts: builtins.float, average_ram_watts: builtins.float, average_total_watts: builtins.float, energy_joules: builtins.float) -> ChatReplyPowerStats: ...
+class ChatReplyEnergy:
+ @typing.final
+ class Total(ChatReplyEnergy):
+ __match_args__ = ("total",)
+ @property
+ def total(self) -> builtins.float: ...
+ def __new__(cls, total: builtins.float) -> ChatReplyEnergy.Total: ...
+
+ @typing.final
+ class Components(ChatReplyEnergy):
+ __match_args__ = ("cpu", "gpu", "ane", "dram",)
+ @property
+ def cpu(self) -> builtins.float: ...
+ @property
+ def gpu(self) -> builtins.float: ...
+ @property
+ def ane(self) -> builtins.float: ...
+ @property
+ def dram(self) -> builtins.float: ...
+ def __new__(cls, cpu: builtins.float, gpu: builtins.float, ane: builtins.float, dram: builtins.float) -> ChatReplyEnergy.Components: ...
+
+ ...
+
+class ChatReplyJoulesPerToken:
+ @typing.final
+ class Total(ChatReplyJoulesPerToken):
+ __match_args__ = ("total",)
+ @property
+ def total(self) -> builtins.float: ...
+ def __new__(cls, total: builtins.float) -> ChatReplyJoulesPerToken.Total: ...
+
+ @typing.final
+ class Components(ChatReplyJoulesPerToken):
+ __match_args__ = ("cpu", "gpu", "ane", "dram",)
+ @property
+ def cpu(self) -> builtins.float: ...
+ @property
+ def gpu(self) -> builtins.float: ...
+ @property
+ def ane(self) -> builtins.float: ...
+ @property
+ def dram(self) -> builtins.float: ...
+ def __new__(cls, cpu: builtins.float, gpu: builtins.float, ane: builtins.float, dram: builtins.float) -> ChatReplyJoulesPerToken.Components: ...
+
+ ...
@typing.final
class ChatReplySpeculatorStats:
@@ -392,12 +421,18 @@ class ChatReplyStats:
@property
def speculator_stats(self) -> typing.Optional[ChatReplySpeculatorStats]: ...
@property
- def power_stats(self) -> typing.Optional[ChatReplyPowerStats]: ...
+ def input_energy(self) -> typing.Optional[ChatReplyEnergy]: ...
+ @property
+ def output_energy(self) -> typing.Optional[ChatReplyEnergy]: ...
@property
def tokens_count(self) -> typing.Optional[builtins.int]: ...
@property
- def joules_per_token(self) -> typing.Optional[builtins.float]: ...
- def __new__(cls, duration: builtins.float, time_to_first_token: typing.Optional[builtins.float], prefill_tokens_per_second: typing.Optional[builtins.float], generate_tokens_per_second: typing.Optional[builtins.float], tokens_count_input: typing.Optional[builtins.int], tokens_count_input_cached: typing.Optional[builtins.int], tokens_count_output: typing.Optional[builtins.int], memory_used_bytes: typing.Optional[builtins.int], speculator_stats: typing.Optional[ChatReplySpeculatorStats], power_stats: typing.Optional[ChatReplyPowerStats]) -> ChatReplyStats: ...
+ def total_joules(self) -> typing.Optional[builtins.float]: ...
+ @property
+ def input_joules_per_token(self) -> typing.Optional[ChatReplyJoulesPerToken]: ...
+ @property
+ def output_joules_per_token(self) -> typing.Optional[ChatReplyJoulesPerToken]: ...
+ def __new__(cls, duration: builtins.float, time_to_first_token: typing.Optional[builtins.float], prefill_tokens_per_second: typing.Optional[builtins.float], generate_tokens_per_second: typing.Optional[builtins.float], tokens_count_input: typing.Optional[builtins.int], tokens_count_input_cached: typing.Optional[builtins.int], tokens_count_output: typing.Optional[builtins.int], memory_used_bytes: typing.Optional[builtins.int], speculator_stats: typing.Optional[ChatReplySpeculatorStats], input_energy: typing.Optional[ChatReplyEnergy], output_energy: typing.Optional[ChatReplyEnergy]) -> ChatReplyStats: ...
class ChatRole:
@typing.final
diff --git a/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/shoji.swift b/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/shoji.swift
index 78e42a92f..e095452fc 100644
--- a/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/shoji.swift
+++ b/crates/legacy/uzu/bindings/swift/Sources/Uzu/Generated/uzu/shoji.swift
@@ -1181,80 +1181,6 @@ public func FfiConverterTypeChatReplyConfig_lower(_ value: ChatReplyConfig) -> R
}
-public struct ChatReplyPowerStats: Equatable, Hashable, Codable {
- public var samplesCount: Int64
- public var averageCpuWatts: Double
- public var averageGpuWatts: Double
- public var averageAneWatts: Double
- public var averageRamWatts: Double
- public var averageTotalWatts: Double
- public var energyJoules: Double
-
- // Default memberwise initializers are never public by default, so we
- // declare one manually.
- public init(samplesCount: Int64, averageCpuWatts: Double, averageGpuWatts: Double, averageAneWatts: Double, averageRamWatts: Double, averageTotalWatts: Double, energyJoules: Double) {
- self.samplesCount = samplesCount
- self.averageCpuWatts = averageCpuWatts
- self.averageGpuWatts = averageGpuWatts
- self.averageAneWatts = averageAneWatts
- self.averageRamWatts = averageRamWatts
- self.averageTotalWatts = averageTotalWatts
- self.energyJoules = energyJoules
- }
-
-
-
-
-}
-
-#if compiler(>=6)
-extension ChatReplyPowerStats: Sendable {}
-#endif
-
-#if swift(>=5.8)
-@_documentation(visibility: private)
-#endif
-public struct FfiConverterTypeChatReplyPowerStats: FfiConverterRustBuffer {
- public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> ChatReplyPowerStats {
- return
- try ChatReplyPowerStats(
- samplesCount: FfiConverterInt64.read(from: &buf),
- averageCpuWatts: FfiConverterDouble.read(from: &buf),
- averageGpuWatts: FfiConverterDouble.read(from: &buf),
- averageAneWatts: FfiConverterDouble.read(from: &buf),
- averageRamWatts: FfiConverterDouble.read(from: &buf),
- averageTotalWatts: FfiConverterDouble.read(from: &buf),
- energyJoules: FfiConverterDouble.read(from: &buf)
- )
- }
-
- public static func write(_ value: ChatReplyPowerStats, into buf: inout [UInt8]) {
- FfiConverterInt64.write(value.samplesCount, into: &buf)
- FfiConverterDouble.write(value.averageCpuWatts, into: &buf)
- FfiConverterDouble.write(value.averageGpuWatts, into: &buf)
- FfiConverterDouble.write(value.averageAneWatts, into: &buf)
- FfiConverterDouble.write(value.averageRamWatts, into: &buf)
- FfiConverterDouble.write(value.averageTotalWatts, into: &buf)
- FfiConverterDouble.write(value.energyJoules, into: &buf)
- }
-}
-
-
-#if swift(>=5.8)
-@_documentation(visibility: private)
-#endif
-public func FfiConverterTypeChatReplyPowerStats_lift(_ buf: RustBuffer) throws -> ChatReplyPowerStats {
- return try FfiConverterTypeChatReplyPowerStats.lift(buf)
-}
-
-#if swift(>=5.8)
-@_documentation(visibility: private)
-#endif
-public func FfiConverterTypeChatReplyPowerStats_lower(_ value: ChatReplyPowerStats) -> RustBuffer {
- return FfiConverterTypeChatReplyPowerStats.lower(value)
-}
-
-
public struct ChatReplySpeculatorStats: Equatable, Hashable, Codable {
public var tokensPerForwardPass: Double
public var numDecodeForwardPasses: UInt32
@@ -1319,11 +1245,12 @@ public struct ChatReplyStats: Equatable, Hashable, Codable {
public var tokensCountOutput: UInt32?
public var memoryUsedBytes: Int64?
public var speculatorStats: ChatReplySpeculatorStats?
- public var powerStats: ChatReplyPowerStats?
+ public var inputEnergy: ChatReplyEnergy?
+ public var outputEnergy: ChatReplyEnergy?
// Default memberwise initializers are never public by default, so we
// declare one manually.
- public init(duration: Double, timeToFirstToken: Double?, prefillTokensPerSecond: Double?, generateTokensPerSecond: Double?, tokensCountInput: UInt32?, tokensCountInputCached: UInt32?, tokensCountOutput: UInt32?, memoryUsedBytes: Int64?, speculatorStats: ChatReplySpeculatorStats?, powerStats: ChatReplyPowerStats?) {
+ public init(duration: Double, timeToFirstToken: Double?, prefillTokensPerSecond: Double?, generateTokensPerSecond: Double?, tokensCountInput: UInt32?, tokensCountInputCached: UInt32?, tokensCountOutput: UInt32?, memoryUsedBytes: Int64?, speculatorStats: ChatReplySpeculatorStats?, inputEnergy: ChatReplyEnergy?, outputEnergy: ChatReplyEnergy?) {
self.duration = duration
self.timeToFirstToken = timeToFirstToken
self.prefillTokensPerSecond = prefillTokensPerSecond
@@ -1333,16 +1260,22 @@ public struct ChatReplyStats: Equatable, Hashable, Codable {
self.tokensCountOutput = tokensCountOutput
self.memoryUsedBytes = memoryUsedBytes
self.speculatorStats = speculatorStats
- self.powerStats = powerStats
+ self.inputEnergy = inputEnergy
+ self.outputEnergy = outputEnergy
}
- /**
- * Energy spent per processed token, counting both input and output tokens.
- */
-public func joulesPerToken() -> Double? {
- return try! FfiConverterOptionDouble.lift(try! rustCall() {
- uniffi_shoji_fn_method_chatreplystats_joules_per_token(
+public func inputJoulesPerToken() -> ChatReplyJoulesPerToken? {
+ return try! FfiConverterOptionTypeChatReplyJoulesPerToken.lift(try! rustCall() {
+ uniffi_shoji_fn_method_chatreplystats_input_joules_per_token(
+ FfiConverterTypeChatReplyStats_lower(self),$0
+ )
+})
+}
+
+public func outputJoulesPerToken() -> ChatReplyJoulesPerToken? {
+ return try! FfiConverterOptionTypeChatReplyJoulesPerToken.lift(try! rustCall() {
+ uniffi_shoji_fn_method_chatreplystats_output_joules_per_token(
FfiConverterTypeChatReplyStats_lower(self),$0
)
})
@@ -1356,6 +1289,14 @@ public func tokensCount() -> UInt32? {
})
}
+public func totalJoules() -> Double? {
+ return try! FfiConverterOptionDouble.lift(try! rustCall() {
+ uniffi_shoji_fn_method_chatreplystats_total_joules(
+ FfiConverterTypeChatReplyStats_lower(self),$0
+ )
+})
+}
+
}
@@ -1380,7 +1321,8 @@ public struct FfiConverterTypeChatReplyStats: FfiConverterRustBuffer {
tokensCountOutput: FfiConverterOptionUInt32.read(from: &buf),
memoryUsedBytes: FfiConverterOptionInt64.read(from: &buf),
speculatorStats: FfiConverterOptionTypeChatReplySpeculatorStats.read(from: &buf),
- powerStats: FfiConverterOptionTypeChatReplyPowerStats.read(from: &buf)
+ inputEnergy: FfiConverterOptionTypeChatReplyEnergy.read(from: &buf),
+ outputEnergy: FfiConverterOptionTypeChatReplyEnergy.read(from: &buf)
)
}
@@ -1394,7 +1336,8 @@ public struct FfiConverterTypeChatReplyStats: FfiConverterRustBuffer {
FfiConverterOptionUInt32.write(value.tokensCountOutput, into: &buf)
FfiConverterOptionInt64.write(value.memoryUsedBytes, into: &buf)
FfiConverterOptionTypeChatReplySpeculatorStats.write(value.speculatorStats, into: &buf)
- FfiConverterOptionTypeChatReplyPowerStats.write(value.powerStats, into: &buf)
+ FfiConverterOptionTypeChatReplyEnergy.write(value.inputEnergy, into: &buf)
+ FfiConverterOptionTypeChatReplyEnergy.write(value.outputEnergy, into: &buf)
}
}
@@ -3666,6 +3609,90 @@ public func FfiConverterTypeChatContentBlockType_lower(_ value: ChatContentBlock
}
+// Note that we don't yet support `indirect` for enums.
+// See https://github.com/mozilla/uniffi-rs/issues/396 for further discussion.
+
+public enum ChatReplyEnergy: Equatable, Hashable, Codable {
+
+ case total(total: Double
+ )
+ case components(cpu: Double, gpu: Double, ane: Double, dram: Double
+ )
+
+
+
+public func total() -> Double {
+ return try! FfiConverterDouble.lift(try! rustCall() {
+ uniffi_shoji_fn_method_chatreplyenergy_total(
+ FfiConverterTypeChatReplyEnergy_lower(self),$0
+ )
+})
+}
+
+
+
+}
+
+#if compiler(>=6)
+extension ChatReplyEnergy: Sendable {}
+#endif
+
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+public struct FfiConverterTypeChatReplyEnergy: FfiConverterRustBuffer {
+ typealias SwiftType = ChatReplyEnergy
+
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> ChatReplyEnergy {
+ let variant: Int32 = try readInt(&buf)
+ switch variant {
+
+ case 1: return .total(total: try FfiConverterDouble.read(from: &buf)
+ )
+
+ case 2: return .components(cpu: try FfiConverterDouble.read(from: &buf), gpu: try FfiConverterDouble.read(from: &buf), ane: try FfiConverterDouble.read(from: &buf), dram: try FfiConverterDouble.read(from: &buf)
+ )
+
+ default: throw UniffiInternalError.unexpectedEnumCase
+ }
+ }
+
+ public static func write(_ value: ChatReplyEnergy, into buf: inout [UInt8]) {
+ switch value {
+
+
+ case let .total(total):
+ writeInt(&buf, Int32(1))
+ FfiConverterDouble.write(total, into: &buf)
+
+
+ case let .components(cpu,gpu,ane,dram):
+ writeInt(&buf, Int32(2))
+ FfiConverterDouble.write(cpu, into: &buf)
+ FfiConverterDouble.write(gpu, into: &buf)
+ FfiConverterDouble.write(ane, into: &buf)
+ FfiConverterDouble.write(dram, into: &buf)
+
+ }
+ }
+}
+
+
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+public func FfiConverterTypeChatReplyEnergy_lift(_ buf: RustBuffer) throws -> ChatReplyEnergy {
+ return try FfiConverterTypeChatReplyEnergy.lift(buf)
+}
+
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+public func FfiConverterTypeChatReplyEnergy_lower(_ value: ChatReplyEnergy) -> RustBuffer {
+ return FfiConverterTypeChatReplyEnergy.lower(value)
+}
+
+
// Note that we don't yet support `indirect` for enums.
// See https://github.com/mozilla/uniffi-rs/issues/396 for further discussion.
@@ -3761,6 +3788,90 @@ public func FfiConverterTypeChatReplyFinishReason_lower(_ value: ChatReplyFinish
}
+// Note that we don't yet support `indirect` for enums.
+// See https://github.com/mozilla/uniffi-rs/issues/396 for further discussion.
+
+public enum ChatReplyJoulesPerToken: Equatable, Hashable, Codable {
+
+ case total(total: Double
+ )
+ case components(cpu: Double, gpu: Double, ane: Double, dram: Double
+ )
+
+
+
+public func total() -> Double {
+ return try! FfiConverterDouble.lift(try! rustCall() {
+ uniffi_shoji_fn_method_chatreplyjoulespertoken_total(
+ FfiConverterTypeChatReplyJoulesPerToken_lower(self),$0
+ )
+})
+}
+
+
+
+}
+
+#if compiler(>=6)
+extension ChatReplyJoulesPerToken: Sendable {}
+#endif
+
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+public struct FfiConverterTypeChatReplyJoulesPerToken: FfiConverterRustBuffer {
+ typealias SwiftType = ChatReplyJoulesPerToken
+
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> ChatReplyJoulesPerToken {
+ let variant: Int32 = try readInt(&buf)
+ switch variant {
+
+ case 1: return .total(total: try FfiConverterDouble.read(from: &buf)
+ )
+
+ case 2: return .components(cpu: try FfiConverterDouble.read(from: &buf), gpu: try FfiConverterDouble.read(from: &buf), ane: try FfiConverterDouble.read(from: &buf), dram: try FfiConverterDouble.read(from: &buf)
+ )
+
+ default: throw UniffiInternalError.unexpectedEnumCase
+ }
+ }
+
+ public static func write(_ value: ChatReplyJoulesPerToken, into buf: inout [UInt8]) {
+ switch value {
+
+
+ case let .total(total):
+ writeInt(&buf, Int32(1))
+ FfiConverterDouble.write(total, into: &buf)
+
+
+ case let .components(cpu,gpu,ane,dram):
+ writeInt(&buf, Int32(2))
+ FfiConverterDouble.write(cpu, into: &buf)
+ FfiConverterDouble.write(gpu, into: &buf)
+ FfiConverterDouble.write(ane, into: &buf)
+ FfiConverterDouble.write(dram, into: &buf)
+
+ }
+ }
+}
+
+
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+public func FfiConverterTypeChatReplyJoulesPerToken_lift(_ buf: RustBuffer) throws -> ChatReplyJoulesPerToken {
+ return try FfiConverterTypeChatReplyJoulesPerToken.lift(buf)
+}
+
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+public func FfiConverterTypeChatReplyJoulesPerToken_lower(_ value: ChatReplyJoulesPerToken) -> RustBuffer {
+ return FfiConverterTypeChatReplyJoulesPerToken.lower(value)
+}
+
+
// Note that we don't yet support `indirect` for enums.
// See https://github.com/mozilla/uniffi-rs/issues/396 for further discussion.
@@ -5154,30 +5265,6 @@ fileprivate struct FfiConverterOptionString: FfiConverterRustBuffer {
}
}
-#if swift(>=5.8)
-@_documentation(visibility: private)
-#endif
-fileprivate struct FfiConverterOptionTypeChatReplyPowerStats: FfiConverterRustBuffer {
- typealias SwiftType = ChatReplyPowerStats?
-
- public static func write(_ value: SwiftType, into buf: inout [UInt8]) {
- guard let value = value else {
- writeInt(&buf, Int8(0))
- return
- }
- writeInt(&buf, Int8(1))
- FfiConverterTypeChatReplyPowerStats.write(value, into: &buf)
- }
-
- public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {
- switch try readInt(&buf) as Int8 {
- case 0: return nil
- case 1: return try FfiConverterTypeChatReplyPowerStats.read(from: &buf)
- default: throw UniffiInternalError.unexpectedOptionalTag
- }
- }
-}
-
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
@@ -5322,6 +5409,30 @@ fileprivate struct FfiConverterOptionTypeValue: FfiConverterRustBuffer {
}
}
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+fileprivate struct FfiConverterOptionTypeChatReplyEnergy: FfiConverterRustBuffer {
+ typealias SwiftType = ChatReplyEnergy?
+
+ public static func write(_ value: SwiftType, into buf: inout [UInt8]) {
+ guard let value = value else {
+ writeInt(&buf, Int8(0))
+ return
+ }
+ writeInt(&buf, Int8(1))
+ FfiConverterTypeChatReplyEnergy.write(value, into: &buf)
+ }
+
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {
+ switch try readInt(&buf) as Int8 {
+ case 0: return nil
+ case 1: return try FfiConverterTypeChatReplyEnergy.read(from: &buf)
+ default: throw UniffiInternalError.unexpectedOptionalTag
+ }
+ }
+}
+
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
@@ -5346,6 +5457,30 @@ fileprivate struct FfiConverterOptionTypeChatReplyFinishReason: FfiConverterRust
}
}
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+fileprivate struct FfiConverterOptionTypeChatReplyJoulesPerToken: FfiConverterRustBuffer {
+ typealias SwiftType = ChatReplyJoulesPerToken?
+
+ public static func write(_ value: SwiftType, into buf: inout [UInt8]) {
+ guard let value = value else {
+ writeInt(&buf, Int8(0))
+ return
+ }
+ writeInt(&buf, Int8(1))
+ FfiConverterTypeChatReplyJoulesPerToken.write(value, into: &buf)
+ }
+
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {
+ switch try readInt(&buf) as Int8 {
+ case 0: return nil
+ case 1: return try FfiConverterTypeChatReplyJoulesPerToken.read(from: &buf)
+ default: throw UniffiInternalError.unexpectedOptionalTag
+ }
+ }
+}
+
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
diff --git a/crates/legacy/uzu/bindings/typescript/src/napi/index.d.mts b/crates/legacy/uzu/bindings/typescript/src/napi/index.d.mts
index 1dfb65555..96d38b508 100644
--- a/crates/legacy/uzu/bindings/typescript/src/napi/index.d.mts
+++ b/crates/legacy/uzu/bindings/typescript/src/napi/index.d.mts
@@ -289,15 +289,30 @@ export declare class ChatReplyConfig {
withGrammar(grammar?: Grammar | undefined | null): ChatReplyConfig
}
-export declare class ChatReplyPowerStats {
- samplesCount: number
- averageCpuWatts: number
- averageGpuWatts: number
- averageAneWatts: number
- averageRamWatts: number
- averageTotalWatts: number
- energyJoules: number
- constructor(samplesCount: number, averageCpuWatts: number, averageGpuWatts: number, averageAneWatts: number, averageRamWatts: number, averageTotalWatts: number, energyJoules: number)
+export declare class ChatReplyEnergyComponents {
+ cpu: number
+ gpu: number
+ ane: number
+ dram: number
+ constructor(cpu: number, gpu: number, ane: number, dram: number)
+}
+
+export declare class ChatReplyEnergyTotal {
+ total: number
+ constructor(total: number)
+}
+
+export declare class ChatReplyJoulesPerTokenComponents {
+ cpu: number
+ gpu: number
+ ane: number
+ dram: number
+ constructor(cpu: number, gpu: number, ane: number, dram: number)
+}
+
+export declare class ChatReplyJoulesPerTokenTotal {
+ total: number
+ constructor(total: number)
}
export declare class ChatReplySpeculatorStats {
@@ -316,11 +331,13 @@ export declare class ChatReplyStats {
tokensCountOutput?: number
memoryUsedBytes?: number
speculatorStats?: ChatReplySpeculatorStats
- powerStats?: ChatReplyPowerStats
- constructor(duration: number, timeToFirstToken?: number, prefillTokensPerSecond?: number, generateTokensPerSecond?: number, tokensCountInput?: number, tokensCountInputCached?: number, tokensCountOutput?: number, memoryUsedBytes?: number, speculatorStats?: ChatReplySpeculatorStats, powerStats?: ChatReplyPowerStats)
+ inputEnergy?: ChatReplyEnergy
+ outputEnergy?: ChatReplyEnergy
+ constructor(duration: number, timeToFirstToken?: number, prefillTokensPerSecond?: number, generateTokensPerSecond?: number, tokensCountInput?: number, tokensCountInputCached?: number, tokensCountOutput?: number, memoryUsedBytes?: number, speculatorStats?: ChatReplySpeculatorStats, inputEnergy?: ChatReplyEnergy, outputEnergy?: ChatReplyEnergy)
get tokensCount(): number | null
- /** Energy spent per processed token, counting both input and output tokens. */
- get joulesPerToken(): number | null
+ get totalJoules(): number | null
+ get inputJoulesPerToken(): ChatReplyJoulesPerToken | null
+ get outputJoulesPerToken(): ChatReplyJoulesPerToken | null
}
export declare class ChatRoleAssistant {
@@ -749,6 +766,9 @@ export interface ChatMessageMetadata {
values: Record<string, any>
}
+export type ChatReplyEnergy =
+ ChatReplyEnergyTotal | ChatReplyEnergyComponents
+
export declare const enum ChatReplyFinishReason {
Stop = 'Stop',
Length = 'Length',
@@ -758,6 +778,9 @@ export declare const enum ChatReplyFinishReason {
Rejected = 'Rejected'
}
+export type ChatReplyJoulesPerToken =
+ ChatReplyJoulesPerTokenTotal | ChatReplyJoulesPerTokenComponents
+
export type ChatRole =
ChatRoleUser | ChatRoleAssistant | ChatRoleSystem | ChatRoleDeveloper | ChatRoleTool | ChatRoleCustom
diff --git a/crates/legacy/uzu/bindings/typescript/src/napi/index.d.ts b/crates/legacy/uzu/bindings/typescript/src/napi/index.d.ts
index 1dfb65555..96d38b508 100644
--- a/crates/legacy/uzu/bindings/typescript/src/napi/index.d.ts
+++ b/crates/legacy/uzu/bindings/typescript/src/napi/index.d.ts
@@ -289,15 +289,30 @@ export declare class ChatReplyConfig {
withGrammar(grammar?: Grammar | undefined | null): ChatReplyConfig
}
-export declare class ChatReplyPowerStats {
- samplesCount: number
- averageCpuWatts: number
- averageGpuWatts: number
- averageAneWatts: number
- averageRamWatts: number
- averageTotalWatts: number
- energyJoules: number
- constructor(samplesCount: number, averageCpuWatts: number, averageGpuWatts: number, averageAneWatts: number, averageRamWatts: number, averageTotalWatts: number, energyJoules: number)
+export declare class ChatReplyEnergyComponents {
+ cpu: number
+ gpu: number
+ ane: number
+ dram: number
+ constructor(cpu: number, gpu: number, ane: number, dram: number)
+}
+
+export declare class ChatReplyEnergyTotal {
+ total: number
+ constructor(total: number)
+}
+
+export declare class ChatReplyJoulesPerTokenComponents {
+ cpu: number
+ gpu: number
+ ane: number
+ dram: number
+ constructor(cpu: number, gpu: number, ane: number, dram: number)
+}
+
+export declare class ChatReplyJoulesPerTokenTotal {
+ total: number
+ constructor(total: number)
}
export declare class ChatReplySpeculatorStats {
@@ -316,11 +331,13 @@ export declare class ChatReplyStats {
tokensCountOutput?: number
memoryUsedBytes?: number
speculatorStats?: ChatReplySpeculatorStats
- powerStats?: ChatReplyPowerStats
- constructor(duration: number, timeToFirstToken?: number, prefillTokensPerSecond?: number, generateTokensPerSecond?: number, tokensCountInput?: number, tokensCountInputCached?: number, tokensCountOutput?: number, memoryUsedBytes?: number, speculatorStats?: ChatReplySpeculatorStats, powerStats?: ChatReplyPowerStats)
+ inputEnergy?: ChatReplyEnergy
+ outputEnergy?: ChatReplyEnergy
+ constructor(duration: number, timeToFirstToken?: number, prefillTokensPerSecond?: number, generateTokensPerSecond?: number, tokensCountInput?: number, tokensCountInputCached?: number, tokensCountOutput?: number, memoryUsedBytes?: number, speculatorStats?: ChatReplySpeculatorStats, inputEnergy?: ChatReplyEnergy, outputEnergy?: ChatReplyEnergy)
get tokensCount(): number | null
- /** Energy spent per processed token, counting both input and output tokens. */
- get joulesPerToken(): number | null
+ get totalJoules(): number | null
+ get inputJoulesPerToken(): ChatReplyJoulesPerToken | null
+ get outputJoulesPerToken(): ChatReplyJoulesPerToken | null
}
export declare class ChatRoleAssistant {
@@ -749,6 +766,9 @@ export interface ChatMessageMetadata {
values: Record<string, any>
}
+export type ChatReplyEnergy =
+ ChatReplyEnergyTotal | ChatReplyEnergyComponents
+
export declare const enum ChatReplyFinishReason {
Stop = 'Stop',
Length = 'Length',
@@ -758,6 +778,9 @@ export declare const enum ChatReplyFinishReason {
Rejected = 'Rejected'
}
+export type ChatReplyJoulesPerToken =
+ ChatReplyJoulesPerTokenTotal | ChatReplyJoulesPerTokenComponents
+
export type ChatRole =
ChatRoleUser | ChatRoleAssistant | ChatRoleSystem | ChatRoleDeveloper | ChatRoleTool | ChatRoleCustom
diff --git a/crates/legacy/uzu/bindings/typescript/src/napi/index.js b/crates/legacy/uzu/bindings/typescript/src/napi/index.js
index c09b5695c..8794eec38 100644
--- a/crates/legacy/uzu/bindings/typescript/src/napi/index.js
+++ b/crates/legacy/uzu/bindings/typescript/src/napi/index.js
@@ -545,7 +545,10 @@ module.exports.ChatMessage = nativeBinding.ChatMessage
module.exports.ChatModelCapabilities = nativeBinding.ChatModelCapabilities
module.exports.ChatReply = nativeBinding.ChatReply
module.exports.ChatReplyConfig = nativeBinding.ChatReplyConfig
-module.exports.ChatReplyPowerStats = nativeBinding.ChatReplyPowerStats
+module.exports.ChatReplyEnergyComponents = nativeBinding.ChatReplyEnergyComponents
+module.exports.ChatReplyEnergyTotal = nativeBinding.ChatReplyEnergyTotal
+module.exports.ChatReplyJoulesPerTokenComponents = nativeBinding.ChatReplyJoulesPerTokenComponents
+module.exports.ChatReplyJoulesPerTokenTotal = nativeBinding.ChatReplyJoulesPerTokenTotal
module.exports.ChatReplySpeculatorStats = nativeBinding.ChatReplySpeculatorStats
module.exports.ChatReplyStats = nativeBinding.ChatReplyStats
module.exports.ChatRoleAssistant = nativeBinding.ChatRoleAssistant
diff --git a/crates/legacy/uzu/bindings/typescript/src/napi/index.mjs b/crates/legacy/uzu/bindings/typescript/src/napi/index.mjs
index fead56beb..624762937 100644
--- a/crates/legacy/uzu/bindings/typescript/src/napi/index.mjs
+++ b/crates/legacy/uzu/bindings/typescript/src/napi/index.mjs
@@ -42,7 +42,10 @@ export const ChatMessage = cjs.ChatMessage;
export const ChatModelCapabilities = cjs.ChatModelCapabilities;
export const ChatReply = cjs.ChatReply;
export const ChatReplyConfig = cjs.ChatReplyConfig;
-export const ChatReplyPowerStats = cjs.ChatReplyPowerStats;
+export const ChatReplyEnergyComponents = cjs.ChatReplyEnergyComponents;
+export const ChatReplyEnergyTotal = cjs.ChatReplyEnergyTotal;
+export const ChatReplyJoulesPerTokenComponents = cjs.ChatReplyJoulesPerTokenComponents;
+export const ChatReplyJoulesPerTokenTotal = cjs.ChatReplyJoulesPerTokenTotal;
export const ChatReplySpeculatorStats = cjs.ChatReplySpeculatorStats;
export const ChatReplyStats = cjs.ChatReplyStats;
export const ChatRoleAssistant = cjs.ChatRoleAssistant;