145 lines
5.4 KiB
MQL5
145 lines
5.4 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| KronosVerifyInference.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>
|
|
|
|
//--- tokenizer
|
|
#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
|
|
//--- predictor
|
|
#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\\";
|
|
input int InpLookback = 256;
|
|
input int InpPredLen = 16;
|
|
input int InpExpectS1 = 943; // verified decode_s1 last-step argmax
|
|
input int InpExpectS2 = 462; // verified decode_s2 last-step argmax
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| 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;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Script entry point. |
|
|
//+------------------------------------------------------------------+
|
|
void OnStart()
|
|
{
|
|
Print("============ Kronos AR inference verification (greedy) ============");
|
|
const int L = InpLookback, P = InpPredLen;
|
|
|
|
matrix x_norm, x_stamp, y_stamp;
|
|
if(!LoadF32Matrix(InpRefDir + "x_norm.bin", (ulong)L, KR_NFEAT, x_norm))
|
|
{
|
|
Print("ABORT x_norm");
|
|
return;
|
|
}
|
|
if(!LoadF32Matrix(InpRefDir + "x_stamp.bin", (ulong)L, 5, x_stamp))
|
|
{
|
|
Print("ABORT x_stamp");
|
|
return;
|
|
}
|
|
if(!LoadF32Matrix(InpRefDir + "y_stamp.bin", (ulong)P, 5, y_stamp))
|
|
{
|
|
Print("ABORT y_stamp");
|
|
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, 512))
|
|
{ Print("ABORT: model Init failed"); return; }
|
|
Print("All components loaded (encoder, decoder, predictor s1/s2).");
|
|
|
|
//--- greedy path (deterministic): returns normalized preds + token sequence
|
|
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; }
|
|
|
|
//--- STEP-1 deterministic check
|
|
PrintFormat("step-1 tokens: gen_s1[0]=%d (expect %d %s), gen_s2[0]=%d (expect %d %s)",
|
|
gen_s1[0], InpExpectS1, (gen_s1[0] == InpExpectS1 ? "OK" : "FAIL"),
|
|
gen_s2[0], InpExpectS2, (gen_s2[0] == InpExpectS2 ? "OK" : "FAIL"));
|
|
|
|
//--- full token sequence + finiteness
|
|
string seq1 = "", seq2 = "";
|
|
for(int i = 0; i < P; i++)
|
|
{
|
|
seq1 += StringFormat("%d ", gen_s1[i]);
|
|
seq2 += StringFormat("%d ", gen_s2[i]);
|
|
}
|
|
PrintFormat("gen_s1: %s", seq1);
|
|
PrintFormat("gen_s2: %s", seq2);
|
|
|
|
bool finite = true;
|
|
for(ulong i = 0; i < pred_norm.Rows(); i++)
|
|
for(ulong j = 0; j < pred_norm.Cols(); j++)
|
|
if(!MathIsValidNumber(pred_norm[i][j]))
|
|
finite = false;
|
|
|
|
PrintFormat("forecast %I64u x %I64u, all finite: %s", pred_norm.Rows(), pred_norm.Cols(), finite ? "yes" : "NO");
|
|
Print("normalized forecast, first 4 steps:");
|
|
for(int i = 0; i < (int)MathMin(4, P); i++)
|
|
{
|
|
string r = "";
|
|
for(int j = 0; j < KR_NFEAT; j++)
|
|
r += StringFormat("%9.5f ", pred_norm[i][j]);
|
|
PrintFormat(" step %2d: %s", i, r);
|
|
}
|
|
|
|
bool step1_ok = (gen_s1[0] == InpExpectS1 && gen_s2[0] == InpExpectS2);
|
|
if(step1_ok && finite)
|
|
Print(">>> AR LOOP VERIFIED: step-1 greedy picks match verified argmaxes, output finite. <<<");
|
|
else
|
|
Print(">>> AR LOOP CHECK FAILED: see step-1 tokens / finiteness above. <<<");
|
|
Print("==================================================================");
|
|
}
|
|
//+------------------------------------------------------------------+
|