File size: 4,160 Bytes
bdd9175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""TinyCast: an attention-free, 146,505-parameter dilated-convolution
time-series foundation model.

Forecast:
    >>> from tinycast import TinyCastPredictor, load_checkpoint
    >>> predictor = TinyCastPredictor(
    ...     prediction_length=48,
    ...     checkpoint_path="model.safetensors",
    ...     freq="H", domain="Energy", device="cpu",
    ...     force_flip_invariance=True,
    ... )
    >>> forecasts = predictor.predict(gluonts_test_input)

Evaluate:
    >>> from tinycast import summarize_by_freq_bin
    >>> summarize_by_freq_bin("all_results.csv")["overall"]["ncrps"]

Train, then release:
    >>> from tinycast import (TinyCastConfig, train, average_checkpoints,
    ...                       export_safetensors)
    >>> result = train(TinyCastConfig(), data=windows, max_steps=1000,
    ...                output_dir="run/", batch_size=32, checkpoint_every=100)
    >>> weights = average_checkpoints(result.checkpoints[-8:])
    >>> export_safetensors(weights, "release/", expect_parameters=146_505)

Rebuild the synthetic corpus (needs CUDA):
    >>> from tinycast import build_shard, verify_shard
    >>> build_shard()                       # published shard 0
    >>> verify_shard("synth4096_0", shard=0)

``tinycast.train`` is the training function, not the module: the two share a
name and the function wins. Module-level recipe constants are reachable as
``from tinycast.train import AR_CHUNKS``. A submodule the package does not
import itself, ``tinycast.backbone`` and ``tinycast.periodogram`` among them,
becomes an attribute only after ``import tinycast.backbone``.
"""

from .config import TinyCastConfig
from .model import TinyCastForPrediction, TinyCastBackbone, PredictionOutput
from .checkpoint import load_checkpoint, load_model
from .predictor import TinyCastPredictor, ARRolloutPredictor

# Training and the objectives it optimizes.
from .losses import committing_loss, pinball_loss, seasonal_copy_baseline
from .train import TrainResult, train, training_window_width

# The synthetic pretraining corpus generators.
from .synth import generate_gp, generate_spikes, generate_tsi

# eval, export and corpus each carry a ``python -m`` entry point, so the package
# must not import them eagerly: that puts them in sys.modules before runpy runs
# them as __main__, which warns and executes the module body twice. Resolving
# them on first attribute access (PEP 562) keeps ``from tinycast import
# evaluate`` working and leaves the command line quiet.
_LAZY_MODULES = ("eval", "export", "corpus")
_LAZY_EXPORTS = {
    "evaluate": "eval",
    "summarize_by_freq_bin": "eval",
    "export_safetensors": "export",
    "average_checkpoints": "export",
    "check_export_roundtrip": "export",
    "ExportError": "export",
    "build_shard": "corpus",
    "verify_shard": "corpus",
    "iter_shard_series": "corpus",
}


def __getattr__(name: str):
    from importlib import import_module

    if name in _LAZY_MODULES:
        value = import_module(f".{name}", __name__)
    else:
        module = _LAZY_EXPORTS.get(name)
        if module is None:
            raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
        value = getattr(import_module(f".{module}", __name__), name)
    globals()[name] = value          # resolve once, then it is a plain global
    return value


def __dir__() -> list:
    return sorted(set(globals()) | set(_LAZY_EXPORTS) | set(_LAZY_MODULES))


__all__ = [
    # model and inference
    "TinyCastConfig",
    "TinyCastForPrediction",
    "TinyCastBackbone",
    "PredictionOutput",
    "load_checkpoint",
    "load_model",
    "TinyCastPredictor",
    "ARRolloutPredictor",
    # training
    "train",
    "TrainResult",
    "training_window_width",
    "pinball_loss",
    "committing_loss",
    "seasonal_copy_baseline",
    # export
    "export_safetensors",
    "average_checkpoints",
    "check_export_roundtrip",
    "ExportError",
    # evaluation
    "evaluate",
    "summarize_by_freq_bin",
    # synthetic corpus
    "build_shard",
    "verify_shard",
    "iter_shard_series",
    "generate_gp",
    "generate_spikes",
    "generate_tsi",
]

__version__ = "1.0.0"