openinfer_simulator/graph/
var.rs1use serde::{Deserialize, Serialize};
2
3use crate::tensor::{DType, ScalarValue};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7pub enum MemoryKind {
8 Dynamic,
9 Volatile,
10 Constant,
11 Persistent,
12}
13
14#[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 pub fn model_name(&self) -> &str {
39 self.ref_name.as_deref().unwrap_or(&self.name)
40 }
41
42 pub fn is_prefix_table(&self) -> bool {
44 self.pattern.is_some()
45 }
46
47 pub fn is_cache_table(&self) -> bool {
49 self.kind == MemoryKind::Persistent && self.table && !self.table_indices.is_empty()
50 }
51
52 pub fn has_auto_dim(&self) -> bool {
54 self.kind == MemoryKind::Persistent && !self.auto_dim.is_empty()
55 }
56
57 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}