torsh-jit 0.1.3

JIT compilation and kernel fusion for ToRSh deep learning framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
//! Code generation backend for JIT compilation

use crate::graph::{ComputationGraph, Node, NodeId, Operation};
use crate::{CompiledKernel, JitError, JitResult, KernelMetadata, TensorDesc};
use torsh_core::DeviceType;

#[cfg(feature = "cranelift-backend")]
use cranelift::prelude::*;

/// Code generator for different backends
pub struct CodeGenerator {
    device: DeviceType,
    #[cfg(feature = "cranelift-backend")]
    cranelift: Option<CraneliftBackend>,
}

impl CodeGenerator {
    /// Create a new code generator for the target device
    pub fn new(device: DeviceType) -> Self {
        Self {
            device,
            #[cfg(feature = "cranelift-backend")]
            cranelift: match device {
                DeviceType::Cpu => Some(CraneliftBackend::new()),
                _ => None,
            },
        }
    }

    /// Generate code for the computation graph
    pub fn generate(&self, graph: &ComputationGraph) -> JitResult<Vec<CompiledKernel>> {
        match self.device {
            DeviceType::Cpu => self.generate_cpu(graph),
            DeviceType::Cuda(_) => self.generate_cuda(graph),
            DeviceType::Metal(_) => self.generate_metal(graph),
            _ => Err(JitError::UnsupportedOp(format!(
                "Code generation not supported for {:?}",
                self.device
            ))),
        }
    }

    /// Generate CPU code
    fn generate_cpu(&self, graph: &ComputationGraph) -> JitResult<Vec<CompiledKernel>> {
        #[cfg(feature = "cranelift-backend")]
        if let Some(ref backend) = self.cranelift {
            return backend.generate(graph);
        }

        // Fallback to interpreter mode
        self.generate_interpreter(graph)
    }

    /// Generate CUDA code
    ///
    /// Future implementation will support:
    /// - PTX code generation for NVIDIA GPUs
    /// - Kernel fusion for memory bandwidth optimization
    /// - Tensor core utilization for matrix operations
    /// - Automatic memory coalescing
    /// - Multi-stream execution support
    fn generate_cuda(&self, graph: &ComputationGraph) -> JitResult<Vec<CompiledKernel>> {
        let node_count = graph.nodes().count();
        let operation_types: Vec<_> = graph
            .nodes()
            .map(|(_, node)| format!("{:?}", node.op))
            .collect();

        Err(JitError::UnsupportedOp(format!(
            "CUDA code generation not yet implemented. \
             Graph contains {} nodes with operations: {}. \
             To enable CUDA support: \
             1. Install CUDA toolkit (>=11.0) \
             2. Enable 'cuda' feature flag \
             3. Set CUDA_PATH environment variable \
             \nFallback: Use CPU backend or interpreter mode.",
            node_count,
            operation_types.join(", ")
        )))
    }

    /// Generate Metal code
    ///
    /// Future implementation will support:
    /// - Metal Shading Language (MSL) generation
    /// - Metal Performance Shaders (MPS) integration
    /// - Unified memory architecture optimization
    /// - Apple Neural Engine (ANE) acceleration
    /// - Multi-GPU support for Mac Pro
    fn generate_metal(&self, graph: &ComputationGraph) -> JitResult<Vec<CompiledKernel>> {
        let node_count = graph.nodes().count();
        let has_matmul = graph
            .nodes()
            .any(|(_, node)| matches!(node.op, Operation::MatMul));
        let has_conv = graph
            .nodes()
            .any(|(_, node)| matches!(node.op, Operation::Conv2d { .. }));

        let recommendations = if has_matmul || has_conv {
            "Consider using Metal Performance Shaders (MPS) backend for matrix/convolution operations."
        } else {
            "For element-wise operations, CPU backend may provide sufficient performance."
        };

        Err(JitError::UnsupportedOp(format!(
            "Metal code generation not yet implemented. \
             Graph contains {} nodes. \
             Detected: {} \
             To enable Metal support: \
             1. Ensure macOS 10.15+ or iOS 13+ \
             2. Enable 'metal' feature flag \
             3. Install Metal developer tools \
             \n{} \
             \nFallback: Use CPU backend or interpreter mode.",
            node_count,
            if has_matmul {
                "matrix multiplication"
            } else if has_conv {
                "convolutions"
            } else {
                "element-wise ops"
            },
            recommendations
        )))
    }

    /// Generate code from IR module  
    pub fn generate_from_ir(
        &self,
        ir_module: &crate::ir::IrModule,
    ) -> JitResult<Vec<CompiledKernel>> {
        // For now, convert IR back to graph-like representation and use existing logic
        // In a real implementation, this would generate code directly from IR
        self.generate_interpreter_from_ir(ir_module)
    }

    /// Generate interpreter kernels from IR
    pub fn generate_interpreter_from_ir(
        &self,
        ir_module: &crate::ir::IrModule,
    ) -> JitResult<Vec<CompiledKernel>> {
        let mut kernels = Vec::new();

        // For each basic block, create a kernel
        for (block_id, block) in &ir_module.blocks {
            let kernel_id = format!("ir_kernel_{}", block_id);

            // Create simple metadata
            let metadata = KernelMetadata {
                inputs: ir_module
                    .inputs
                    .iter()
                    .filter_map(|&input| self.ir_value_to_tensor_desc(ir_module, input))
                    .collect(),
                outputs: ir_module
                    .outputs
                    .iter()
                    .filter_map(|&output| self.ir_value_to_tensor_desc(ir_module, output))
                    .collect(),
                shared_memory: 0,
                block_size: (1, 1, 1),
                grid_size: (1, 1, 1),
            };

            // Encode the instructions
            let mut code = Vec::new();
            for instruction in &block.instructions {
                let opcode = self.encode_ir_instruction(instruction)?;
                code.push(opcode);
            }

            let kernel = CompiledKernel {
                id: kernel_id,
                source_nodes: Vec::new(), // Would need mapping from IR to original nodes
                code,
                metadata,
            };

            kernels.push(kernel);
        }

        Ok(kernels)
    }

    /// Convert IR value to tensor descriptor
    fn ir_value_to_tensor_desc(
        &self,
        ir_module: &crate::ir::IrModule,
        ir_value: crate::ir::IrValue,
    ) -> Option<TensorDesc> {
        if let Some(value_def) = ir_module.get_value(ir_value) {
            if let Some(type_def) = ir_module.get_type(value_def.ty) {
                match &type_def.kind {
                    crate::ir::TypeKind::Tensor { shape, .. } => {
                        Some(TensorDesc {
                            dtype: torsh_core::DType::F32, // Simplified
                            shape: shape.clone(),
                            strides: self.compute_strides(shape),
                            offset: 0,
                        })
                    }
                    _ => None,
                }
            } else {
                None
            }
        } else {
            None
        }
    }

    /// Encode an IR instruction
    fn encode_ir_instruction(&self, instruction: &crate::ir::Instruction) -> JitResult<u8> {
        let opcode = match &instruction.opcode {
            crate::ir::IrOpcode::Add => 1,
            crate::ir::IrOpcode::Sub => 2,
            crate::ir::IrOpcode::Mul => 3,
            crate::ir::IrOpcode::Div => 4,
            crate::ir::IrOpcode::Neg => 5,
            crate::ir::IrOpcode::Abs => 6,
            crate::ir::IrOpcode::Exp => 7,
            crate::ir::IrOpcode::Log => 8,
            crate::ir::IrOpcode::Sqrt => 9,
            crate::ir::IrOpcode::Sin => 10,
            crate::ir::IrOpcode::Cos => 11,
            crate::ir::IrOpcode::Tanh => 12,
            crate::ir::IrOpcode::Sigmoid => 13,
            crate::ir::IrOpcode::Relu => 14,
            crate::ir::IrOpcode::Gelu => 15,
            crate::ir::IrOpcode::MatMul => 16,
            crate::ir::IrOpcode::Conv2d => 17,
            crate::ir::IrOpcode::Pool2d => 18,
            crate::ir::IrOpcode::Reshape => 19,
            crate::ir::IrOpcode::Transpose => 20,
            crate::ir::IrOpcode::Sum => 21,
            crate::ir::IrOpcode::Mean => 22,
            crate::ir::IrOpcode::Max => 23,
            crate::ir::IrOpcode::Min => 24,
            crate::ir::IrOpcode::Load => 25,
            crate::ir::IrOpcode::Store => 26,
            crate::ir::IrOpcode::Const => 27,
            _ => {
                return Err(JitError::UnsupportedOp(format!(
                    "IR opcode {:?} not supported in interpreter",
                    instruction.opcode
                )))
            }
        };

        Ok(opcode)
    }

    /// Generate interpreter-based kernels (fallback)
    pub fn generate_interpreter(&self, graph: &ComputationGraph) -> JitResult<Vec<CompiledKernel>> {
        let mut kernels = Vec::new();

        // Get topological order
        let order = graph
            .topological_sort()
            .map_err(|e| JitError::GraphError(format!("{:?}", e)))?;

        // Generate a kernel for each node (simple approach)
        for node_id in order {
            if let Some(node) = graph.node(node_id) {
                let kernel = self.generate_interpreter_kernel(graph, node_id, node)?;
                kernels.push(kernel);
            }
        }

        Ok(kernels)
    }

    /// Generate an interpreter kernel for a single node
    fn generate_interpreter_kernel(
        &self,
        graph: &ComputationGraph,
        node_id: NodeId,
        node: &Node,
    ) -> JitResult<CompiledKernel> {
        // Populate inputs from graph
        let input_tensors: Vec<TensorDesc> = graph
            .get_node_inputs(node_id)
            .iter()
            .filter_map(|&input_id| {
                graph.node(input_id).map(|input_node| TensorDesc {
                    dtype: input_node.dtype,
                    shape: input_node.output_shape.dims().to_vec(),
                    strides: self.compute_strides(input_node.output_shape.dims()),
                    offset: 0,
                })
            })
            .collect();

        // Generate metadata
        let metadata = KernelMetadata {
            inputs: input_tensors,
            outputs: vec![TensorDesc {
                dtype: node.dtype,
                shape: node.output_shape.dims().to_vec(),
                strides: self.compute_strides(node.output_shape.dims()),
                offset: 0,
            }],
            shared_memory: 0,
            block_size: (1, 1, 1),
            grid_size: (1, 1, 1),
        };

        // Encode operation as "code"
        let code = self.encode_operation(&node.op)?;

        Ok(CompiledKernel {
            id: format!("kernel_{:?}", node_id),
            source_nodes: vec![node_id],
            code,
            metadata,
        })
    }

    /// Compute strides for a shape
    fn compute_strides(&self, shape: &[usize]) -> Vec<usize> {
        let mut strides = vec![1; shape.len()];
        for i in (0..shape.len() - 1).rev() {
            strides[i] = strides[i + 1] * shape[i + 1];
        }
        strides
    }

    /// Encode an operation for interpreter execution
    fn encode_operation(&self, op: &Operation) -> JitResult<Vec<u8>> {
        // Simple encoding scheme for interpreter
        let op_code = match op {
            Operation::Add => 1,
            Operation::Sub => 2,
            Operation::Mul => 3,
            Operation::Div => 4,
            Operation::Relu => 5,
            Operation::Sigmoid => 6,
            Operation::Tanh => 7,
            Operation::MatMul => 8,
            // ... more operations
            _ => {
                return Err(JitError::UnsupportedOp(format!(
                    "Operation {:?} not supported in interpreter",
                    op
                )))
            }
        };

        Ok(vec![op_code])
    }
}

#[cfg(feature = "cranelift-backend")]
struct CraneliftBackend {
    _builder_context: FunctionBuilderContext,
    _ctx: codegen::Context,
}

#[cfg(feature = "cranelift-backend")]
impl CraneliftBackend {
    fn new() -> Self {
        let mut flag_builder = settings::builder();
        flag_builder
            .set("use_colocated_libcalls", "false")
            .expect("setting should be valid");
        flag_builder
            .set("is_pic", "false")
            .expect("setting should be valid");
        let isa_builder = cranelift_native::builder().expect("native builder should succeed");
        let isa = isa_builder
            .finish(settings::Flags::new(flag_builder))
            .expect("ISA creation should succeed");

        let mut ctx = codegen::Context::new();
        ctx.func.signature.call_conv = isa.default_call_conv();

        Self {
            _builder_context: FunctionBuilderContext::new(),
            _ctx: ctx,
        }
    }

    fn generate(&self, graph: &ComputationGraph) -> JitResult<Vec<CompiledKernel>> {
        let mut kernels = Vec::new();

        // Group nodes into kernels based on fusion information
        let kernel_groups = self.identify_kernel_groups(graph)?;

        // Generate code for each kernel
        for (kernel_id, nodes) in kernel_groups.iter().enumerate() {
            let kernel = self.generate_kernel(graph, kernel_id, nodes)?;
            kernels.push(kernel);
        }

        Ok(kernels)
    }

    fn identify_kernel_groups(&self, graph: &ComputationGraph) -> JitResult<Vec<Vec<NodeId>>> {
        // For now, each node is its own kernel
        // In a real implementation, this would use fusion information
        let order = graph
            .topological_sort()
            .map_err(|e| JitError::GraphError(format!("{:?}", e)))?;

        Ok(order.into_iter().map(|n| vec![n]).collect())
    }

    fn generate_kernel(
        &self,
        _graph: &ComputationGraph,
        kernel_id: usize,
        nodes: &[NodeId],
    ) -> JitResult<CompiledKernel> {
        // TODO: Implement actual Cranelift code generation

        // For now, return a placeholder
        Ok(CompiledKernel {
            id: format!("cranelift_kernel_{}", kernel_id),
            source_nodes: nodes.to_vec(),
            code: vec![],
            metadata: KernelMetadata {
                inputs: vec![],
                outputs: vec![],
                shared_memory: 0,
                block_size: (1, 1, 1),
                grid_size: (1, 1, 1),
            },
        })
    }
}

/// CUDA kernel generator
///
/// Generates PTX (Parallel Thread Execution) code for NVIDIA GPUs.
/// Supports compute capabilities from 5.0 (Maxwell) to 9.0 (Hopper).
pub struct CudaKernelGenerator {
    compute_capability: (u32, u32),
    /// Enable tensor core usage for matrix operations (compute capability >= 7.0)
    enable_tensor_cores: bool,
    /// Target PTX ISA version
    ptx_version: (u32, u32),
    /// Enable cooperative groups
    enable_cooperative_groups: bool,
}

impl CudaKernelGenerator {
    /// Create a new CUDA kernel generator
    ///
    /// # Arguments
    /// * `compute_capability` - GPU compute capability (e.g., (7, 5) for sm_75)
    pub fn new(compute_capability: (u32, u32)) -> Self {
        let enable_tensor_cores = compute_capability.0 >= 7;
        let enable_cooperative_groups = compute_capability.0 >= 6;

        Self {
            compute_capability,
            enable_tensor_cores,
            ptx_version: (7, 0), // Default to PTX 7.0
            enable_cooperative_groups,
        }
    }

    /// Enable or disable tensor core usage
    pub fn set_tensor_cores(&mut self, enable: bool) {
        self.enable_tensor_cores = enable && self.compute_capability.0 >= 7;
    }

    /// Generate PTX assembly code for the computation graph
    ///
    /// Future implementation will:
    /// - Analyze graph for optimal thread block configuration
    /// - Generate fused kernels for element-wise operation chains
    /// - Emit specialized tensor core instructions (WMMA) for matrix ops
    /// - Apply memory coalescing patterns
    /// - Generate multi-kernel launches for large graphs
    pub fn generate_ptx(&self, graph: &ComputationGraph) -> JitResult<String> {
        let node_count = graph.nodes().count();
        let matmul_count = graph
            .nodes()
            .filter(|(_, n)| matches!(n.op, Operation::MatMul))
            .count();
        let conv_count = graph
            .nodes()
            .filter(|(_, n)| matches!(n.op, Operation::Conv2d { .. }))
            .count();

        let capability_str = format!(
            "sm_{}{}",
            self.compute_capability.0, self.compute_capability.1
        );
        let features = if self.enable_tensor_cores {
            "tensor cores (WMMA), "
        } else {
            ""
        };

        Err(JitError::UnsupportedOp(format!(
            "PTX generation not yet implemented.\n\
             Target: {} (compute capability {}.{})\n\
             Graph statistics:\n\
             - Total nodes: {}\n\
             - MatMul operations: {} {}\n\
             - Conv2D operations: {} {}\n\
             Features: {}cooperative groups\n\
             \n\
             Future PTX generation will support:\n\
             - Automatic kernel fusion for {:.1}x speedup potential\n\
             - Memory coalescing optimization\n\
             - Shared memory tiling for matrix operations\n\
             - Warp-level primitives for reduction operations\n\
             \nFallback: Use CPU backend with BLAS/MKL for good performance.",
            capability_str,
            self.compute_capability.0,
            self.compute_capability.1,
            node_count,
            matmul_count,
            if self.enable_tensor_cores {
                "(tensor core eligible)"
            } else {
                ""
            },
            conv_count,
            if conv_count > 0 {
                "(cudnn eligible)"
            } else {
                ""
            },
            features,
            (matmul_count + conv_count).max(1) as f64 * 1.5 // Estimated fusion speedup
        )))
    }

    /// Estimate kernel launch configuration for a graph
    pub fn estimate_launch_config(&self, graph: &ComputationGraph) -> LaunchConfiguration {
        let total_ops: usize = graph
            .nodes()
            .map(|(_, node)| node.output_shape.dims().iter().product::<usize>())
            .sum();

        // Simple heuristic for block size
        let threads_per_block = if total_ops < 1024 {
            128
        } else if total_ops < 1024 * 1024 {
            256
        } else {
            512
        };

        let blocks = (total_ops + threads_per_block - 1) / threads_per_block;

        LaunchConfiguration {
            grid_dim: (blocks.min(65535), 1, 1),
            block_dim: (threads_per_block, 1, 1),
            shared_memory_bytes: 0, // Would be calculated based on kernel
            stream_id: 0,
        }
    }
}

/// CUDA kernel launch configuration
#[derive(Debug, Clone)]
pub struct LaunchConfiguration {
    /// Grid dimensions (number of blocks)
    pub grid_dim: (usize, usize, usize),
    /// Block dimensions (threads per block)
    pub block_dim: (usize, usize, usize),
    /// Shared memory per block in bytes
    pub shared_memory_bytes: usize,
    /// CUDA stream ID
    pub stream_id: i32,
}

/// Metal kernel generator
///
/// Generates Metal Shading Language (MSL) code for Apple GPUs.
/// Supports macOS 10.15+, iOS 13+, and Apple Silicon.
pub struct MetalKernelGenerator {
    device_family: String,
    /// Enable Metal Performance Shaders (MPS) integration
    enable_mps: bool,
    /// Metal language version
    metal_version: (u32, u32),
    /// Target Apple Neural Engine (ANE) when available
    enable_ane: bool,
}

impl MetalKernelGenerator {
    /// Create a new Metal kernel generator
    ///
    /// # Arguments
    /// * `device_family` - Metal GPU family (e.g., "apple7" for M1)
    pub fn new(device_family: String) -> Self {
        // Detect if ANE is available (A11+ or Apple Silicon)
        let enable_ane = device_family.starts_with("apple")
            && device_family[5..].parse::<u32>().unwrap_or(0) >= 7;

        Self {
            device_family,
            enable_mps: true,      // MPS available on all modern devices
            metal_version: (2, 4), // Metal 2.4 for macOS 12+
            enable_ane,
        }
    }

    /// Enable or disable Metal Performance Shaders integration
    pub fn set_mps(&mut self, enable: bool) {
        self.enable_mps = enable;
    }

    /// Generate Metal Shading Language code for the computation graph
    ///
    /// Future implementation will:
    /// - Generate optimized MSL kernels for each operation
    /// - Integrate with Metal Performance Shaders for standard ops
    /// - Utilize tile memory for data reuse
    /// - Emit SIMD-group operations for reduction
    /// - Generate ANE-compatible operations when possible
    pub fn generate_metal(&self, graph: &ComputationGraph) -> JitResult<String> {
        let node_count = graph.nodes().count();
        let matmul_count = graph
            .nodes()
            .filter(|(_, n)| matches!(n.op, Operation::MatMul))
            .count();
        let conv_count = graph
            .nodes()
            .filter(|(_, n)| matches!(n.op, Operation::Conv2d { .. }))
            .count();
        let elementwise_count = node_count - matmul_count - conv_count;

        let mps_eligible = matmul_count + conv_count;
        let ane_hints = if self.enable_ane && conv_count > 0 {
            format!(
                "\n- {} convolution ops are ANE-eligible for ultra-low power inference",
                conv_count
            )
        } else {
            String::new()
        };

        Err(JitError::UnsupportedOp(format!(
            "Metal shader generation not yet implemented.\n\
             Target: {} (Metal {}. {})\n\
             Graph statistics:\n\
             - Total nodes: {}\n\
             - Element-wise ops: {}\n\
             - MatMul operations: {}\n\
             - Conv2D operations: {}\n\
             - MPS-eligible ops: {}{}\n\
             \n\
             Future Metal generation will support:\n\
             - Metal Performance Shaders integration for {:.0}% of operations\n\
             - Unified memory optimization (zero-copy on Apple Silicon)\n\
             - Tile memory usage for {:.1}x bandwidth reduction\n\
             - SIMD-group operations for efficient reduction\n\
             - Concurrent kernel execution across multiple command buffers\n\
             \nFallback: Use CPU backend with Accelerate framework for good performance.",
            self.device_family,
            self.metal_version.0,
            self.metal_version.1,
            node_count,
            elementwise_count,
            matmul_count,
            conv_count,
            mps_eligible,
            ane_hints,
            (mps_eligible as f64 / node_count as f64) * 100.0,
            2.5 // Estimated bandwidth reduction from tile memory
        )))
    }

    /// Estimate threadgroup size for a graph
    pub fn estimate_threadgroup_size(&self, graph: &ComputationGraph) -> ThreadgroupSize {
        let total_ops: usize = graph
            .nodes()
            .map(|(_, node)| node.output_shape.dims().iter().product::<usize>())
            .sum();

        // Metal recommends threadgroup sizes in multiples of SIMD width (32)
        let threads_per_threadgroup = if total_ops < 1024 {
            128
        } else if total_ops < 1024 * 1024 {
            256
        } else {
            512
        };

        ThreadgroupSize {
            width: threads_per_threadgroup,
            height: 1,
            depth: 1,
        }
    }
}

/// Metal threadgroup size configuration
#[derive(Debug, Clone)]
pub struct ThreadgroupSize {
    pub width: usize,
    pub height: usize,
    pub depth: usize,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_code_generator_creation() {
        let _gen = CodeGenerator::new(DeviceType::Cpu);
        // Basic creation test
        assert!(true);
    }

    #[test]
    fn test_stride_computation() {
        let gen = CodeGenerator::new(DeviceType::Cpu);

        let strides = gen.compute_strides(&[2, 3, 4]);
        assert_eq!(strides, vec![12, 4, 1]);

        let strides = gen.compute_strides(&[10]);
        assert_eq!(strides, vec![1]);
    }
}