File size: 2,814 Bytes
ab8be8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import random
from pathlib import Path

import numpy as np
import pandas as pd
import torch

from model import Kronos, KronosPredictor, KronosTokenizer


TEST_DATA_ROOT = Path(__file__).parent
INPUT_DATA_PATH = TEST_DATA_ROOT / "regression_input.csv"
OUTPUT_DATA_DIR = TEST_DATA_ROOT
MAX_CTX_LEN = 512
TEST_CTX_LEN = [512, 256]
PRED_LEN = 8
FEATURE_NAMES = ["open", "high", "low", "close", "volume", "amount"]

MODEL_REVISION = "901c26c1332695a2a8f243eb2f37243a37bea320"
TOKENIZER_REVISION = "0e0117387f39004a9016484a186a908917e22426"
SEED = 123

DEVICE = "cpu"


def set_seed(seed: int) -> None:
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.backends.cudnn.is_available():
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False


def generate_output(ctx_len: int) -> None:
    if ctx_len > MAX_CTX_LEN:
        raise ValueError(
            f"Context length for output generation ({ctx_len}) "
            f"cannot exceed maximum context length ({MAX_CTX_LEN})."
        )

    context_df = df.iloc[:ctx_len].copy()
    future_timestamps = df["timestamps"].iloc[
        ctx_len : ctx_len + PRED_LEN
    ].reset_index(drop=True)

    tokenizer = KronosTokenizer.from_pretrained("NeoQuasar/Kronos-Tokenizer-base", revision=TOKENIZER_REVISION)
    model = Kronos.from_pretrained("NeoQuasar/Kronos-small", revision=MODEL_REVISION)
    tokenizer.eval()
    model.eval()

    predictor = KronosPredictor(
        model, tokenizer, device=DEVICE, max_context=MAX_CTX_LEN
    )

    with torch.no_grad():
        pred_df = predictor.predict(
            df=context_df[FEATURE_NAMES].reset_index(drop=True),
            x_timestamp=context_df["timestamps"].reset_index(drop=True),
            y_timestamp=future_timestamps,
            pred_len=PRED_LEN,
            T=1.0,
            top_k=1,
            top_p=1.0,
            verbose=False,
            sample_count=1,
        )

    if pred_df.shape != (PRED_LEN, len(FEATURE_NAMES)):
        raise ValueError(f"Unexpected prediction shape: {pred_df.shape}")

    output_df = pred_df.reset_index(drop=True)
    output_df["timestamps"] = future_timestamps
    output_df = output_df[["timestamps"] + FEATURE_NAMES]
    output_df.to_csv(OUTPUT_DATA_DIR / f"regression_output_{ctx_len}.csv", index=False)
    print(f"Saved {ctx_len} fixture to {OUTPUT_DATA_DIR / f'regression_output_{ctx_len}.csv'}")


if __name__ == "__main__":
    set_seed(SEED)


    df = pd.read_csv(INPUT_DATA_PATH, parse_dates=["timestamps"])
    if df.shape[0] < MAX_CTX_LEN + PRED_LEN:
        raise ValueError(
            f"Input data must have at least {MAX_CTX_LEN + PRED_LEN} rows, "
            f"found {df.shape[0]} instead."
        )

    for ctx_len in TEST_CTX_LEN:
        generate_output(ctx_len)