kronos-mql5/Scripts/Kronos/KronosVerifySlide.mq5
2026-07-05 05:06:39 +00:00

181 lines
6.3 KiB
MQL5

//+------------------------------------------------------------------+
//| KronosVerifySlide.mq5 |
//| MMQ — Muhammad Minhas Qamar |
//| www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "MMQ — Muhammad Minhas Qamar"
#property link "https://www.mql5.com"
#property version "1.00"
#property script_show_inputs
#property strict
#include <Kronos\KronosInference.mqh>
#define KR_TOK_DIR "kronos_weights\\tokenizer\\"
#define KR_TOK_DM 256
#define KR_TOK_HEADS 4
#define KR_TOK_ENC 4
#define KR_TOK_DEC 4
#define KR_TOK_FF 512
#define KR_PRED_DIR "kronos_weights\\predictor\\"
#define KR_PRED_DM 512
#define KR_PRED_HEADS 8
#define KR_PRED_LAYERS 8
#define KR_PRED_FF 1024
input string InpRefDir = "kronos_refs_slide\\";
input int InpLookback = 500;
input int InpPredLen = 30;
input int InpMaxCtx = 512;
//+------------------------------------------------------------------+
//| Read a [rows,cols] float32 .bin into a matrix(double). |
//+------------------------------------------------------------------+
bool LoadF32Matrix(const string fname, ulong rows, ulong cols, matrix &out)
{
int h = FileOpen(fname, FILE_READ | FILE_BIN);
if(h == INVALID_HANDLE)
{
PrintFormat("open fail %s (%d)", fname, GetLastError());
return false;
}
ulong n = rows * cols;
float buf[];
ArrayResize(buf, (int)n);
uint got = FileReadArray(h, buf, 0, (int)n);
FileClose(h);
if(got != n)
{
PrintFormat("short read %s", fname);
return false;
}
out = matrix::Zeros(rows, cols);
for(ulong r = 0; r < rows; r++)
for(ulong c = 0; c < cols; c++)
out[r][c] = (double)buf[r*cols+c];
return true;
}
//+------------------------------------------------------------------+
//| Read N int32 values from a .bin into an int[]. |
//+------------------------------------------------------------------+
bool LoadI32Array(const string fname, int n, int &out[])
{
int h = FileOpen(fname, FILE_READ | FILE_BIN);
if(h == INVALID_HANDLE)
{
PrintFormat("open fail %s (%d)", fname, GetLastError());
return false;
}
ArrayResize(out, n);
uint got = FileReadArray(h, out, 0, n);
FileClose(h);
if(got != (uint)n)
{
PrintFormat("short read %s", fname);
return false;
}
return true;
}
//+------------------------------------------------------------------+
//| Script entry point. |
//+------------------------------------------------------------------+
void OnStart()
{
Print("============ Kronos AR slide-branch verification (greedy) ============");
const int L = InpLookback, P = InpPredLen;
matrix x_norm, x_stamp, y_stamp;
if(!LoadF32Matrix(InpRefDir + "x_norm_slide.bin", (ulong)L, KR_NFEAT, x_norm))
{
Print("ABORT x_norm");
return;
}
if(!LoadF32Matrix(InpRefDir + "x_stamp_slide.bin", (ulong)L, 5, x_stamp))
{
Print("ABORT x_stamp");
return;
}
if(!LoadF32Matrix(InpRefDir + "y_stamp_slide.bin", (ulong)P, 5, y_stamp))
{
Print("ABORT y_stamp");
return;
}
int ref_s1[], ref_s2[];
if(!LoadI32Array(InpRefDir + "gen_s1_slide.bin", P, ref_s1))
{
Print("ABORT gen_s1");
return;
}
if(!LoadI32Array(InpRefDir + "gen_s2_slide.bin", P, ref_s2))
{
Print("ABORT gen_s2");
return;
}
matrix full_stamp = matrix::Zeros((ulong)(L + P), 5);
for(int i = 0; i < L; i++)
for(int j = 0; j < 5; j++)
full_stamp[i][j] = x_stamp[i][j];
for(int i = 0; i < P; i++)
for(int j = 0; j < 5; j++)
full_stamp[L + i][j] = y_stamp[i][j];
CKronosModel model;
if(!model.Init(KR_TOK_DIR, KR_TOK_ENC, KR_TOK_DEC, KR_TOK_DM, KR_TOK_HEADS, KR_TOK_FF,
KR_PRED_DIR, KR_PRED_LAYERS, KR_PRED_DM, KR_PRED_HEADS, KR_PRED_FF, InpMaxCtx))
{ Print("ABORT: model Init failed"); return; }
Print("All components loaded.");
matrix pred_norm;
int gen_s1[], gen_s2[];
if(!model.GeneratePathNorm(x_norm, full_stamp, P, 1.0, 0, 1.0, true, pred_norm, gen_s1, gen_s2))
{ Print("ABORT: GeneratePathNorm failed"); return; }
//--- exact integer comparison across all P steps
int bad1 = 0, bad2 = 0, first = -1;
for(int i = 0; i < P; i++)
{
if(gen_s1[i] != ref_s1[i])
{
bad1++;
if(first < 0)
first = i;
}
if(gen_s2[i] != ref_s2[i])
{
bad2++;
if(first < 0)
first = i;
}
}
PrintFormat("s1 tokens: %d/%d match | s2 tokens: %d/%d match", P - bad1, P, P - bad2, P);
//--- highlight the slide region (steps where L0+i >= max_context)
int slide0 = InpMaxCtx - L; // first step where current_seq_len reaches max_context
PrintFormat("slide branch fires from generation step ~%d onward (L0+i >= %d)", slide0, InpMaxCtx);
string a = "", b = "", ra = "", rb = "";
for(int i = 0; i < P; i++)
{ a += StringFormat("%d ", gen_s1[i]); ra += StringFormat("%d ", ref_s1[i]); }
PrintFormat("mql gen_s1: %s", a);
PrintFormat("ref gen_s1: %s", ra);
if(bad1 > 0 || bad2 > 0)
{
PrintFormat("first mismatch at step %d: s1 mql=%d ref=%d | s2 mql=%d ref=%d",
first, gen_s1[first], ref_s1[first], gen_s2[first], ref_s2[first]);
int shown = 0;
for(int i = 0; i < P && shown < 8; i++)
if(gen_s1[i] != ref_s1[i] || gen_s2[i] != ref_s2[i])
{ PrintFormat(" step %d: s1 %d/%d s2 %d/%d", i, gen_s1[i], ref_s1[i], gen_s2[i], ref_s2[i]); shown++; }
}
if(bad1 == 0 && bad2 == 0)
Print(">>> SLIDE BRANCH VERIFIED: exact token match through the 512 crossing. <<<");
else
Print(">>> SLIDE BRANCH MISMATCH: check buffer roll, window offset, or stamp slice. <<<");
Print("======================================================================");
}
//+------------------------------------------------------------------+