custom
code
sovereign-compute
File size: 6,493 Bytes
e92f76f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// ============================================================
// FSLOps.td β€” Operation definitions for the FSL dialect
// ============================================================
// Covers: MambaStep, SelectiveMambaStep, OutputProjection, FSMTransition.
// Hybrid continuous-discrete semantics.

#ifndef FSL_OPS
#define FSL_OPS

include "FSLDialect.td"
include "FSLTypes.td"
include "mlir/Interfaces/SideEffectInterfaces.td"

// ============================================================
// MambaStepOp β€” Basic SSM state transition
// ============================================================

def FSL_MambaStepOp : FSL_Op<"mamba_step", [
    NoMemoryEffect
  ]> {
  let summary = "Linear SSM state transition (fixed A, B)";
  let description = [{
    Executes one step of the state-space model recurrence:
      s_{t+1} = A * s_t + B * u_t

    This is the non-selective version where A and B are fixed
    matrices provided as explicit operands. The output is
    zeroed (output_projection is a separate op).

    Parameters from YAML:
      n = d_state = 16 (state dimension)
      m = d_model = 512 (model dimension)
  }];

  let arguments = (ins
    FSL_StateVectorType:$state,           // s_t ∈ R^n
    FSL_TokenVectorType:$input,           // u_t ∈ R^m (convolved)
    AnyType:$matrix_a,                    // A ∈ R^{nΓ—n}
    AnyType:$matrix_b                     // B ∈ R^{nΓ—m}
  );
  let results = (outs
    FSL_StateVectorType:$next_state,      // s_{t+1} ∈ R^n
    FSL_TokenVectorType:$output           // y_t = 0_m (placeholder)
  );

  let assemblyFormat = [{
    $state `,` $input `,` $matrix_a `,` $matrix_b
    attr-dict `:` functional-type(operands, results)
  }];

  let hasVerifier = 1;
}

// ============================================================
// SelectiveMambaStepOp β€” Selective SSM (Mamba-2)
// ============================================================

def FSL_SelectiveMambaStepOp : FSL_Op<"selective_mamba_step", [
    NoMemoryEffect
  ]> {
  let summary = "Selective SSM state transition (Mamba-2 architecture)";
  let description = [{
    Executes one step of the selective state-space model:
      s_{t+1} = A * s_t + B * u_t

    where u_t is computed from the raw input via:
      1. Depthwise convolution: z_t = Conv_{d_c}(x_t; W)
      2. Split: z1 = z_t[:, :m/2], z2 = z_t[:, m/2:]
      3. SiLU gating: u_t = z1 βŠ™ silu(z2)

    A is diagonal: A = diag(-exp(A_log))
    B is fixed (provided as full nΓ—m matrix or low-rank factors)

    This implements the Mamba-2 selectivity mechanism where
    input-dependence flows through u_t, not through A/B.

    Parameters from YAML:
      n = d_state = 16
      m = d_model = 512
      d_c = d_conv = 4
  }];

  let arguments = (ins
    FSL_StateVectorType:$state,           // s_t ∈ R^n
    FSL_TokenVectorType:$input,           // x_t ∈ R^m (raw token)
    FSL_SSMMatricesType:$params           // A_log, B, W_conv, V, U
  );
  let results = (outs
    FSL_StateVectorType:$next_state,      // s_{t+1} ∈ R^n
    FSL_TokenVectorType:$output           // y_t = 0_m (placeholder)
  );

  let assemblyFormat = [{
    $state `,` $input `,` $params
    attr-dict `:` functional-type(operands, results)
  }];

  let hasVerifier = 1;
}

// ============================================================
// OutputProjectionOp β€” Emit output from SSM state
// ============================================================

def FSL_OutputProjectionOp : FSL_Op<"output_projection", [
    NoMemoryEffect
  ]> {
  let summary = "Project SSM state to output token";
  let description = [{
    Projects the SSM state to an output token:
      y_t = C * s_t + D * u_t

    In Mamba-2, C and D are fixed matrices. This op is
    executed in the S1_EMIT state (per YAML FSM).

    Note: This op is separate from mamba_step to enable
    hybrid FSM semantics where emission is gated by
    discrete state transitions.
  }];

  let arguments = (ins
    FSL_StateVectorType:$state,           // s_t ∈ R^n
    FSL_TokenVectorType:$input,           // u_t ∈ R^m (optional)
    AnyType:$matrix_c,                    // C ∈ R^{mΓ—n}
    AnyType:$matrix_d                     // D ∈ R^{mΓ—m}
  );
  let results = (outs
    FSL_TokenVectorType:$output           // y_t ∈ R^m
  );

  let assemblyFormat = [{
    $state `,` $input `,` $matrix_c `,` $matrix_d
    attr-dict `:` functional-type(operands, results)
  }];

  let hasVerifier = 1;
}

// ============================================================
// FSMTransitionOp β€” Discrete state transition
// ============================================================

def FSL_FSMTransitionOp : FSL_Op<"transition", [
    NoMemoryEffect
  ]> {
  let summary = "Discrete FSM state transition (gated by condition)";
  let description = [{
    Evaluates a transition condition and updates the FSM state.

    The condition is a boolean flag derived from the SSM state:
      condition(s) = ||s||_2 > theta  (threshold)
      condition(s) = scan_complete      (external signal)

    If the condition is true, the FSM transitions from
    from_state to to_state. Otherwise, it stays in from_state.

    This enables hybrid continuous-discrete semantics:
      - Continuous: SSM state evolves via mamba_step
      - Discrete: FSM state gates which actions are executed
  }];

  let arguments = (ins
    FSL_FSMStateType:$from_state,
    FSL_FSMStateType:$to_state,
    IntegerAttr<I1>:$condition            // boolean flag
  );
  let results = (outs
    FSL_FSMStateType:$new_state           // updated FSM state
  );

  let assemblyFormat = [{
    $from_state `->` $to_state `if` $condition
    attr-dict `:` type($new_state)
  }];
}

// ============================================================
// ScanCompleteOp β€” Generate scan_complete flag
// ============================================================

def FSL_ScanCompleteOp : FSL_Op<"scan_complete", [
    Pure
  ]> {
  let summary = "Check if SSM scan is complete";
  let description = [{
    Evaluates whether the SSM scan is complete based on
    the state vector. Returns a boolean flag.

    Common conditions:
      - ||s_t||_2 < epsilon (state converged)
      - t >= T_max (maximum timesteps reached)
      - External trigger (e.g., end-of-sequence token)
  }];

  let arguments = (ins
    FSL_StateVectorType:$state
  );
  let results = (outs
    I1:$is_complete
  );

  let assemblyFormat = [{
    $state attr-dict `:` type($is_complete)
  }];
}

#endif // FSL_OPS