Skip to content

Commit 83ed0a7

Browse files
committed
Add example
1 parent bfc3f4e commit 83ed0a7

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

candle-core/examples/metal_pool.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use candle_core::{DType, Device, Result, Tensor};
2+
3+
#[cfg(feature = "metal")]
4+
fn run() -> Result<()> {
5+
if !candle_core::utils::metal_is_available() {
6+
eprintln!("Metal is not available on this system.");
7+
return Ok(());
8+
}
9+
10+
let device = Device::new_metal(0)?;
11+
12+
let input = Tensor::arange(0f32, 16f32, &Device::Cpu)?
13+
.to_dtype(DType::F32)?
14+
.to_device(&device)?;
15+
16+
// Allocate a 4 MB pool for intermediate activations.
17+
let pooled = input.start_pool(4 * 1024 * 1024)?;
18+
19+
let logits = pooled.sin()?.mul(&pooled.cos()?)?;
20+
let final_tensor = logits.tanh()?.leave_pool()?;
21+
22+
println!("final tensor: {:?}", final_tensor.to_vec1::<f32>()?);
23+
24+
Ok(())
25+
}
26+
27+
#[cfg(not(feature = "metal"))]
28+
fn run() -> Result<()> {
29+
eprintln!("Rebuild candle-core with the `metal` feature to run this example.");
30+
Ok(())
31+
}
32+
33+
fn main() -> Result<()> {
34+
run()
35+
}

0 commit comments

Comments
 (0)