File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments