Skip to main content

openinfer_simulator/graph/
var.rs

1use serde::{Deserialize, Serialize};
2
3use crate::tensor::{DType, ScalarValue};
4
5/// Variable storage classification.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7pub enum MemoryKind {
8    Dynamic,
9    Volatile,
10    Constant,
11    Persistent,
12}
13
14/// Variable declaration within a graph.
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct VarDecl {
17    pub name: String,
18    #[serde(default)]
19    pub ref_name: Option<String>,
20    #[serde(default)]
21    pub pattern: Option<String>,
22    #[serde(default)]
23    pub table_indices: Vec<String>,
24    #[serde(default)]
25    pub table: bool,
26    #[serde(default)]
27    pub auto_dim: Vec<String>,
28    #[serde(default)]
29    pub fixed: Vec<(String, usize)>,
30    pub dtype: DType,
31    pub dims: Vec<String>,
32    pub kind: MemoryKind,
33    pub init: Option<ScalarValue>,
34}
35
36impl VarDecl {
37    /// Return the model-facing name (alias or own name).
38    pub fn model_name(&self) -> &str {
39        self.ref_name.as_deref().unwrap_or(&self.name)
40    }
41
42    /// True if this variable is a prefix table.
43    pub fn is_prefix_table(&self) -> bool {
44        self.pattern.is_some()
45    }
46
47    /// True if this variable represents a cache table.
48    pub fn is_cache_table(&self) -> bool {
49        self.kind == MemoryKind::Persistent && self.table && !self.table_indices.is_empty()
50    }
51
52    /// True if this variable has auto-dimension entries.
53    pub fn has_auto_dim(&self) -> bool {
54        self.kind == MemoryKind::Persistent && !self.auto_dim.is_empty()
55    }
56
57    /// Cache table indices excluding auto-dimension indices.
58    pub fn cache_table_indices(&self) -> Vec<String> {
59        if !self.is_cache_table() {
60            return Vec::new();
61        }
62        self.table_indices
63            .iter()
64            .filter(|index| !self.auto_dim.contains(index))
65            .cloned()
66            .collect()
67    }
68}