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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from typing import Sequence
|
| 6 |
+
import torch
|
| 7 |
+
import torch.nn as nn
|
| 8 |
+
|
| 9 |
+
from cidm_v3_product_model import (
|
| 10 |
+
CapacityScaledSingleStateCIDM,
|
| 11 |
+
ProductModelConfig,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
class LeadVariableIncrementAdapter(nn.Module):
|
| 15 |
+
def __init__(
|
| 16 |
+
self,
|
| 17 |
+
variable_to_group: Sequence[int],
|
| 18 |
+
steps: int = 60,
|
| 19 |
+
maximum_scale_deviation: float = 0.65,
|
| 20 |
+
maximum_bias: float = 0.12,
|
| 21 |
+
):
|
| 22 |
+
super().__init__()
|
| 23 |
+
mapping = torch.as_tensor(
|
| 24 |
+
variable_to_group,
|
| 25 |
+
dtype=torch.long,
|
| 26 |
+
)
|
| 27 |
+
self.register_buffer(
|
| 28 |
+
"variable_to_group",
|
| 29 |
+
mapping,
|
| 30 |
+
persistent=True,
|
| 31 |
+
)
|
| 32 |
+
self.steps = int(steps)
|
| 33 |
+
self.group_count = int(mapping.max().item() + 1)
|
| 34 |
+
self.maximum_scale_deviation = float(
|
| 35 |
+
maximum_scale_deviation
|
| 36 |
+
)
|
| 37 |
+
self.maximum_bias = float(maximum_bias)
|
| 38 |
+
|
| 39 |
+
self.raw_scale = nn.Parameter(
|
| 40 |
+
torch.zeros(self.steps, self.group_count)
|
| 41 |
+
)
|
| 42 |
+
self.raw_bias = nn.Parameter(
|
| 43 |
+
torch.zeros(self.steps, self.group_count)
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
bias_mask = torch.ones(
|
| 47 |
+
len(mapping),
|
| 48 |
+
dtype=torch.float32,
|
| 49 |
+
)
|
| 50 |
+
bias_mask[[12, 13]] = 0.0
|
| 51 |
+
self.register_buffer(
|
| 52 |
+
"variable_bias_mask",
|
| 53 |
+
bias_mask,
|
| 54 |
+
persistent=True,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
def forward(
|
| 58 |
+
self,
|
| 59 |
+
base_prediction,
|
| 60 |
+
persistence,
|
| 61 |
+
mask,
|
| 62 |
+
lead_index: int,
|
| 63 |
+
):
|
| 64 |
+
group_scale = (
|
| 65 |
+
1.0
|
| 66 |
+
+ self.maximum_scale_deviation
|
| 67 |
+
* torch.tanh(
|
| 68 |
+
self.raw_scale[lead_index]
|
| 69 |
+
)
|
| 70 |
+
)
|
| 71 |
+
group_bias = (
|
| 72 |
+
self.maximum_bias
|
| 73 |
+
* torch.tanh(
|
| 74 |
+
self.raw_bias[lead_index]
|
| 75 |
+
)
|
| 76 |
+
)
|
| 77 |
+
scale = group_scale[
|
| 78 |
+
self.variable_to_group
|
| 79 |
+
][None, :, None, None]
|
| 80 |
+
bias = (
|
| 81 |
+
group_bias[
|
| 82 |
+
self.variable_to_group
|
| 83 |
+
]
|
| 84 |
+
* self.variable_bias_mask
|
| 85 |
+
)[None, :, None, None]
|
| 86 |
+
|
| 87 |
+
return (
|
| 88 |
+
persistence
|
| 89 |
+
+ scale
|
| 90 |
+
* (
|
| 91 |
+
base_prediction
|
| 92 |
+
- persistence
|
| 93 |
+
)
|
| 94 |
+
+ bias
|
| 95 |
+
) * mask
|
| 96 |
+
|
| 97 |
+
class TimeVariableDistilledCIDM(nn.Module):
|
| 98 |
+
def __init__(
|
| 99 |
+
self,
|
| 100 |
+
base_model,
|
| 101 |
+
adapter,
|
| 102 |
+
):
|
| 103 |
+
super().__init__()
|
| 104 |
+
self.base_model = base_model
|
| 105 |
+
self.adapter = adapter
|
| 106 |
+
|
| 107 |
+
def forward(
|
| 108 |
+
self,
|
| 109 |
+
history,
|
| 110 |
+
mask,
|
| 111 |
+
steps=60,
|
| 112 |
+
gradient_checkpointing=False,
|
| 113 |
+
):
|
| 114 |
+
outputs = self.base_model(
|
| 115 |
+
history,
|
| 116 |
+
mask,
|
| 117 |
+
steps,
|
| 118 |
+
gradient_checkpointing=gradient_checkpointing,
|
| 119 |
+
)
|
| 120 |
+
persistence = history[:, -1]
|
| 121 |
+
return [
|
| 122 |
+
self.adapter(
|
| 123 |
+
output,
|
| 124 |
+
persistence,
|
| 125 |
+
mask,
|
| 126 |
+
lead_index,
|
| 127 |
+
)
|
| 128 |
+
for lead_index, output
|
| 129 |
+
in enumerate(outputs)
|
| 130 |
+
]
|
| 131 |
+
|
| 132 |
+
def load_distilled_checkpoint(
|
| 133 |
+
checkpoint_path,
|
| 134 |
+
map_location="cpu",
|
| 135 |
+
):
|
| 136 |
+
try:
|
| 137 |
+
payload = torch.load(
|
| 138 |
+
checkpoint_path,
|
| 139 |
+
map_location=map_location,
|
| 140 |
+
weights_only=False,
|
| 141 |
+
)
|
| 142 |
+
except TypeError:
|
| 143 |
+
payload = torch.load(
|
| 144 |
+
checkpoint_path,
|
| 145 |
+
map_location=map_location,
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
config = ProductModelConfig(
|
| 149 |
+
**payload["model_config"]
|
| 150 |
+
)
|
| 151 |
+
base_model = CapacityScaledSingleStateCIDM(
|
| 152 |
+
config
|
| 153 |
+
)
|
| 154 |
+
base_model.load_state_dict(
|
| 155 |
+
payload["model_state"],
|
| 156 |
+
strict=True,
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
adapter_config = payload["adapter_config"]
|
| 160 |
+
adapter = LeadVariableIncrementAdapter(
|
| 161 |
+
adapter_config["variable_to_group"],
|
| 162 |
+
adapter_config["steps"],
|
| 163 |
+
adapter_config[
|
| 164 |
+
"maximum_scale_deviation"
|
| 165 |
+
],
|
| 166 |
+
adapter_config["maximum_bias"],
|
| 167 |
+
)
|
| 168 |
+
adapter.load_state_dict(
|
| 169 |
+
payload["adapter_state"],
|
| 170 |
+
strict=True,
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
model = TimeVariableDistilledCIDM(
|
| 174 |
+
base_model,
|
| 175 |
+
adapter,
|
| 176 |
+
)
|
| 177 |
+
return model, payload
|