Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mistral run with metal feature and current-thread runtimes support #11

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 2 additions & 57 deletions mistralrs-core/src/attention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ use crate::{

use candle_core::{Device, Result, Tensor};

#[cfg(feature = "metal")]
/// Initial, sentinel value is usize::MAX
static METAL_VERSION_CACHE: AtomicUsize = AtomicUsize::new(usize::MAX);

#[cfg(feature = "flash-attn")]
fn flash_attn(
Expand Down Expand Up @@ -100,60 +97,8 @@ fn naive_sdpa(
sdpa_params: &SdpaParams,
) -> Result<Tensor> {
#[cfg(feature = "metal")]
let supports_attn_softmax = {
use std::sync::atomic::Ordering;
let cache = METAL_VERSION_CACHE.load(Ordering::Relaxed);

let version = if cache != usize::MAX {
cache
} else {
// echo "__METAL_VERSION__" | xcrun -sdk macosx metal -E -x metal -P -

use std::process::{Command, Stdio};

// Create the `echo` command and pipe its output into `xcrun`
let mut echo = Command::new("echo")
.arg("__METAL_VERSION__")
.stdout(Stdio::piped())
.spawn()
.expect("Failed to start echo command");

echo.wait()?;

// Run the `xcrun` command, taking input from the `echo` command's output
let output = Command::new("xcrun")
.arg("-sdk")
.arg("macosx")
.arg("metal")
.arg("-E")
.arg("-x")
.arg("metal")
.arg("-P")
.arg("-")
.stdin(echo.stdout.unwrap())
.output()
.expect("Failed to run xcrun command");

// Handle the output
if output.status.success() {
let version = String::from_utf8_lossy(&output.stdout)
.split('\n')
.nth(1)
.unwrap()
.trim()
.to_string()
.parse::<usize>()
.unwrap();
METAL_VERSION_CACHE.store(version, Ordering::Relaxed);
version
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
panic!("Error:\n{}", stderr);
}
};
// Attn softmax is only supported for metal >= 310
version >= 310
};
// macOS 13 (Ventura) and later support softmax with attention mask: https://support.apple.com/en-us/102894
let supports_attn_softmax = true;

#[cfg(not(feature = "metal"))]
let supports_attn_softmax = true;
Expand Down
87 changes: 46 additions & 41 deletions mistralrs-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,48 +363,53 @@ impl MistralRs {

let engine_id = ENGINE_ID.fetch_add(1, atomic::Ordering::SeqCst);

// Do a dummy run
if matches!(category, ModelCategory::Text | ModelCategory::Vision { .. }) {
let clone_sender = sender.read().unwrap().clone();
tokio::task::block_in_place(|| {
let (tx, mut rx) = channel(1);
let req = Request::Normal(NormalRequest {
id: 0,
messages: RequestMessage::Completion {
text: "dummy".to_string(),
echo_prompt: false,
best_of: None,
},
sampling_params: SamplingParams {
max_len: Some(1),
..SamplingParams::deterministic()
},
response: tx,
return_logprobs: false,
is_streaming: true,
constraint: Constraint::None,
suffix: None,
adapters: None,
tool_choice: None,
tools: None,
logits_processors: None,
return_raw_logits: false,
// Skip the dummy run in Single Threaded mode as blocking operations are not allowed
if tokio::runtime::Handle::try_current()
.is_ok_and(|h| h.runtime_flavor() != tokio::runtime::RuntimeFlavor::CurrentThread)
{
// Do a dummy run
if matches!(category, ModelCategory::Text | ModelCategory::Vision { .. }) {
let clone_sender = sender.read().unwrap().clone();
tokio::task::block_in_place(|| {
let (tx, mut rx) = channel(1);
let req = Request::Normal(NormalRequest {
id: 0,
messages: RequestMessage::Completion {
text: "dummy".to_string(),
echo_prompt: false,
best_of: None,
},
sampling_params: SamplingParams {
max_len: Some(1),
..SamplingParams::deterministic()
},
response: tx,
return_logprobs: false,
is_streaming: true,
constraint: Constraint::None,
suffix: None,
adapters: None,
tool_choice: None,
tools: None,
logits_processors: None,
return_raw_logits: false,
});
info!("Beginning dummy run.");
let start = Instant::now();
clone_sender.blocking_send(req).unwrap();
sgrebnov marked this conversation as resolved.
Show resolved Hide resolved

if let Some(_resp) = rx.blocking_recv() {
let end = Instant::now();
info!(
"Dummy run completed in {}s.",
end.duration_since(start).as_secs_f64()
);
} else {
warn!("Dummy run failed!");
}
});
info!("Beginning dummy run.");
let start = Instant::now();
clone_sender.blocking_send(req).unwrap();

if let Some(_resp) = rx.blocking_recv() {
let end = Instant::now();
info!(
"Dummy run completed in {}s.",
end.duration_since(start).as_secs_f64()
);
} else {
warn!("Dummy run failed!");
}
});
}
}
};

Arc::new(Self {
engine_id,
Expand Down
Loading