use std::fmt::Debug;
use scirs2_core::ndarray::Array1;
use scirs2_core::numeric::Float;
use std::collections::HashMap;
use std::time::Instant;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ComputationId(pub u64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct OperationId(pub usize);
#[derive(Debug, Clone)]
pub struct XLAOperation<T: Float + Debug + Send + Sync + 'static> {
pub id: OperationId,
pub op_type: OperationType,
pub inputs: Vec<Operand<T>>,
pub outputs: Vec<OperandType<T>>,
pub attributes: OperationAttributes,
pub source_location: Option<SourceLocation>,
pub performance_characteristics: OperationPerformanceCharacteristics,
pub memory_requirements: OperationMemoryRequirements,
}
#[derive(Debug, Clone, PartialEq)]
pub enum OperationType {
Add,
Subtract,
Multiply,
Divide,
Power,
Sqrt,
Exp,
Log,
Sin,
Cos,
Tanh,
Dot,
MatMul,
Transpose,
Reduce(ReduceOperation),
AllReduce(AllReduceOperation),
Reshape,
Broadcast,
Slice,
Concatenate,
ReLU,
Sigmoid,
GELU,
Swish,
BatchNorm,
LayerNorm,
Convolution(ConvolutionConfig),
Conditional,
While,
Call,
AllGather,
AllToAll,
CollectivePermute,
Custom(CustomOperation),
OptimizerUpdate(OptimizerUpdateType),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ReduceOperation {
pub reduce_function: ReduceFunction,
pub dimensions: Vec<usize>,
pub keep_dims: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ReduceFunction {
Sum,
Product,
Min,
Max,
Mean,
And,
Or,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AllReduceOperation {
pub reduce_function: ReduceFunction,
pub replica_groups: Vec<Vec<usize>>,
pub channel_id: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ConvolutionConfig {
pub strides: Vec<usize>,
pub padding: PaddingConfig,
pub dilation: Vec<usize>,
pub feature_group_count: usize,
pub batch_group_count: usize,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PaddingConfig {
Same,
Valid,
Explicit(Vec<(usize, usize)>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct CustomOperation {
pub name: String,
pub version: u32,
pub attributes: HashMap<String, AttributeValue>,
pub has_side_effects: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum OptimizerUpdateType {
SGD,
Adam,
RMSprop,
AdaGrad,
Custom(String),
}
#[derive(Debug, Clone)]
pub struct Operand<T: Float + Debug + Send + Sync + 'static> {
pub id: OperandId,
pub operand_type: OperandType<T>,
pub source_operation: Option<OperationId>,
pub metadata: OperandMetadata,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct OperandId(pub usize);
#[derive(Debug, Clone)]
pub enum OperandType<T: Float + Debug + Send + Sync + 'static> {
Tensor {
shape: TensorShape,
element_type: ElementType,
layout: Option<Layout>,
},
Scalar { value: T, element_type: ElementType },
Constant {
values: Array1<T>,
shape: TensorShape,
element_type: ElementType,
},
Tuple(Vec<OperandType<T>>),
Token,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TensorShape {
pub dimensions: Vec<usize>,
pub is_dynamic: Vec<bool>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ElementType {
F16,
F32,
F64,
BF16,
S8,
S16,
S32,
S64,
U8,
U16,
U32,
U64,
C64,
C128,
Bool,
Token,
}
#[derive(Debug, Clone)]
pub struct Layout {
pub minor_to_major: Vec<usize>,
pub tiles: Vec<Tile>,
pub element_size_in_bits: usize,
pub memory_space: MemorySpace,
}
#[derive(Debug, Clone)]
pub struct Tile {
pub dimensions: Vec<usize>,
}
#[derive(Debug, Clone, Copy)]
pub enum MemorySpace {
Default,
Host,
Device,
Unified,
}
#[derive(Debug, Clone)]
pub struct OperationAttributes {
pub attributes: HashMap<String, AttributeValue>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum AttributeValue {
Bool(bool),
Int(i64),
Float(f64),
String(String),
IntList(Vec<i64>),
FloatList(Vec<f64>),
StringList(Vec<String>),
}
#[derive(Debug, Clone)]
pub struct SourceLocation {
pub file: String,
pub line: u32,
pub column: u32,
pub function: String,
}
#[derive(Debug, Clone)]
pub struct OperationPerformanceCharacteristics {
pub flops: u64,
pub execution_time_us: u64,
pub memory_bandwidth: f64,
pub compute_intensity: f64,
pub parallelization_potential: f64,
pub tpu_utilization: f64,
}
#[derive(Debug, Clone)]
pub struct OperationMemoryRequirements {
pub input_memory: usize,
pub output_memory: usize,
pub temp_memory: usize,
pub peak_memory: usize,
pub access_pattern: MemoryAccessPattern,
}
#[derive(Debug, Clone, Copy)]
pub enum MemoryAccessPattern {
Sequential,
Random,
Strided,
Broadcast,
Gather,
Scatter,
}
#[derive(Debug, Clone)]
pub struct InputSpecification<T: Float + Debug + Send + Sync + 'static> {
pub name: String,
pub operand_type: OperandType<T>,
pub is_parameter: bool,
}
#[derive(Debug, Clone)]
pub struct OutputSpecification<T: Float + Debug + Send + Sync + 'static> {
pub name: String,
pub operand_type: OperandType<T>,
pub operand_id: OperandId,
}
#[derive(Debug, Clone)]
pub struct ComputationMetadata {
pub creation_time: Instant,
pub estimated_flops: u64,
pub estimated_memory: usize,
pub complexity_score: f64,
pub optimization_opportunities: Vec<OptimizationOpportunity>,
}
#[derive(Debug, Clone)]
pub struct XLAComputation<T: Float + Debug + Send + Sync + 'static> {
pub id: ComputationId,
pub operations: Vec<XLAOperation<T>>,
pub inputs: Vec<InputSpecification<T>>,
pub outputs: Vec<OutputSpecification<T>>,
pub metadata: ComputationMetadata,
pub operands: HashMap<OperandId, Operand<T>>,
pub dependencies: HashMap<OperationId, Vec<OperationId>>,
}
#[derive(Debug, Clone)]
pub struct OptimizationOpportunity {
pub opportunity_type: OptimizationOpportunityType,
pub estimated_benefit: f64,
pub implementation_cost: f64,
pub description: String,
}
#[derive(Debug, Clone, Copy)]
pub enum OptimizationOpportunityType {
OperatorFusion,
LayoutOptimization,
MemoryOptimization,
ParallelizationOpportunity,
ConstantFolding,
DeadCodeElimination,
CommonSubexpressionElimination,
LoopOptimization,
}
#[derive(Debug, Clone)]
pub struct PerformanceHint {
pub hint_type: PerformanceHintType,
pub target_operations: Vec<OperationId>,
pub parameters: HashMap<String, AttributeValue>,
}
#[derive(Debug, Clone, Copy)]
pub enum PerformanceHintType {
PreferTensorCores,
MinimizeMemoryBandwidth,
MaximizeParallelism,
OptimizeForLatency,
OptimizeForThroughput,
PreferLocalMemory,
AvoidSynchronization,
}
#[derive(Debug, Clone)]
pub struct LayoutHint {
pub target_operand: OperandId,
pub preferred_layout: Layout,
pub priority: LayoutPriority,
}
#[derive(Debug, Clone, Copy)]
pub enum LayoutPriority {
Low,
Medium,
High,
Critical,
}
#[derive(Debug, Clone)]
pub struct OperandMetadata {
pub name: Option<String>,
pub description: Option<String>,
pub source_info: Option<SourceLocation>,
pub usage_hints: Vec<UsageHint>,
}
#[derive(Debug, Clone)]
pub struct UsageHint {
pub hint_type: UsageHintType,
pub confidence: f64,
}
#[derive(Debug, Clone, Copy)]
pub enum UsageHintType {
HighFrequencyAccess,
SequentialAccess,
RandomAccess,
ReadOnly,
WriteOnly,
ReadWrite,
Temporary,
Persistent,
}