kronos-mql5/Scripts/Kronos/KronosProfile.mq5

109 lines
5 KiB
MQL5
Raw Permalink Normal View History

2026-07-05 05:06:39 +00:00
//+------------------------------------------------------------------+
//| KronosProfile.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 config (config_tokenizer.json)
#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 config (config_predictor.json)
#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
//--- real config, matching KronosForecast.mq5 defaults
input string InpRefDir = "kronos_refs\\";
input int InpLookback = 256; // context bars
input int InpPredLen = 16; // forecast horizon
input double InpTemperature = 1.0; // sampling temperature
input int InpTopK = 0; // top-k (0 = off)
input double InpTopP = 0.9; // nucleus top-p
input int InpSampleCount = 5; // averaged sample paths (the EA default)
input bool InpGreedy = false; // EA runs stochastic; profile the real cost
//+------------------------------------------------------------------+
//| 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: one full Predict(), timed, then exit. |
//+------------------------------------------------------------------+
void OnStart()
{
const int L = InpLookback, P = InpPredLen;
Print("============ Kronos profile (one full Predict, real config) ============");
PrintFormat("config: L=%d pred_len=%d sample_count=%d greedy=%s topP=%.2f",
L, P, InpSampleCount, (InpGreedy ? "true" : "false"), InpTopP);
if(L > 256)
{ Print("lookback>256 not available from refs"); return; }
//--- reference window (already normalized) reused as Predict's raw input; Predict
//--- re-normalizes internally — fine for profiling (compute path is value-agnostic)
matrix raw, x_stamp, y_stamp;
if(!LoadF32Matrix(InpRefDir + "x_norm.bin", (ulong)L, KR_NFEAT, raw)) { 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;
uint t0 = GetTickCount();
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; }
PrintFormat("weight load: %u ms", GetTickCount() - t0);
//--- the exact call KronosForecast.mq5 makes, once
uint t1 = GetTickCount();
matrix forecast;
if(!model.Predict(raw, full_stamp, P, InpTemperature, InpTopK, InpTopP,
InpSampleCount, InpGreedy, forecast))
{ Print("ABORT: Predict failed"); return; }
uint dt = GetTickCount() - t1;
PrintFormat("Predict (L=%d, P=%d, samples=%d): %u ms total -> ~%.1f ms/step/path",
L, P, InpSampleCount, dt, (double)dt / (P * InpSampleCount));
PrintFormat("forecast rows=%I64u cols=%I64u, first close=%.5f last close=%.5f",
forecast.Rows(), forecast.Cols(), forecast[0][3], forecast[P - 1][3]);
Print("=========================================================================");
}
//+------------------------------------------------------------------+