Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import math
 
 
class _LRScheduler:
    r"""Base class for implementing custom learning rate schedulers
 
    Schedulers can be either stateful or stateless.
    Stateless implementation can only rely on information available at
    :py:class:`.TrainStepInfo`.
    Stateful implementation, on the other hand, can store additional parameters
    by overriding the constructor.
 
    In both cases, once the scheduler is configured, no user code is needed
    to update learning rate during each train step.
 
    NOTE: Current implementation doesn't support 'lr' within :py:attr:`param_groups` entries.
    """
 
    def __init__(self):
        self._last_lr = []
 
    def _step(self, train_step_info):
        r"""Internal method called to compute learning rate"""
 
        # Store last lr for future inquiry
        new_lr = self.get_lr(train_step_info)
        self._last_lr = new_lr
 
        return new_lr
 
    def get_lr(self, train_step_info):
        r"""Returns a list of learning rate
 
        Args:
            train_step_info (:py:class:`.TrainStepInfo`): runtime info for current training step
 
        Returns:
            ordered :py:obj:`list` of learning rates.
                The first entry is the default learning rate and
                    the remaining refer to each parameter group.
            NOTE: Currently, only default learning rate is supported and a single-valued list must be returned.
        """
        raise NotImplementedError
 
    def get_last_lr(self):
        r"""Return last computed learning rate by LR Scheduler"""
        return self._last_lr
 
 
class ConstantWarmupLRScheduler(_LRScheduler):
    r"""Constant warmup strategy for learning rate update based on HuggingFace's Transformers implementation
 
    Creates a schedule with constant learning rate preceded by a warmup period during which the learning rate
    increases linearly between 0 and the initial lr set in the optimizer.
 
    Learning rate update strategy:
        When current_step < warmup
            lr = base_lr * (current_step / max(1, num_warmup_steps))
        Otherwise,
            lr = base_lr
 
    Args:
        total_steps (int): total training steps for learning.
        warmup (float, default is 0.002): portion of total steps for warmup. Range is (0, 1]
 
    Example:
        .. code-block:: python
 
            # Initialize lr scheduler
            lr_scheduler = ConstantWarmupLRScheduler(total_steps=512, warmup=0.002)
 
            # Initialize ORTTrainer with lr scheduler
            opts = ORTTrainerOptions({
                lr_scheduler: lr_scheduler
            })
            ort_trainer = ORTTrainer(..., options=opts)
 
            # Call step() in every batch update
            for inputs in batch_inputs:
                outputs = ort_trainer.train_step(**inputs)
    """
 
    def __init__(self, total_steps, warmup=0.002):
        super().__init__()
        assert isinstance(total_steps, int) and total_steps > 0, "total_steps must be a strict positive number"
        assert isinstance(warmup, float) and warmup >= 0 and warmup < 1, "warmup must be a float between (0, 1]"
        assert total_steps > warmup, "total_steps must be greater than warmup"
 
        self.total_steps = total_steps
        self.warmup = warmup
        self._num_warmup_steps = warmup * total_steps
 
    def _warmup_constant(self, train_step_info):
        if train_step_info.optimization_step < self._num_warmup_steps:
            return float(train_step_info.optimization_step) / float(max(1, self._num_warmup_steps))
        return 1.0
 
    def get_lr(self, train_step_info):
        return [train_step_info.optimizer_config.lr * self._warmup_constant(train_step_info)]
 
 
class CosineWarmupLRScheduler(_LRScheduler):
    r"""Cosine warmup strategy for learning rate update based on HuggingFace's Transformers implementation
 
    Creates a schedule with learning rate that decreases following the values of the cosine function between the
    initial lr set in the :py:class`.optim._OptimizerConfig` to 0, after a warmup period during which it increases
    linearly between 0 and the initial lr set in the :py:class`.optim._OptimizerConfig`.
 
    Learning rate update strategy:
        When current_step < warmup
            lr = base_lr * (current_step / max(1, num_warmup_steps)), when
        Otherwise
            lr = base_lr * max(0, 0.5 * (1.0 + math.cos(math.pi * cycles * 2.0 * progress))), where
                progress = current_step - num_warmup_steps / max(1, total_steps - num_warmup_steps)
 
    Args:
        total_steps (int): total training steps for learning.
        cycles (float, default is 0.5): number of waves in the cosine schedule.
            The default decreases from max value to 0, following a half-cosine
        warmup (float, default is 0.002): portion of total steps for warmup. Range is (0, 1]
 
    Example:
        .. code-block:: python
 
            # Initialize lr scheduler
            lr_scheduler = CosineWarmupLRScheduler(total_steps=512, warmup=0.002)
 
            # Initialize ORTTrainer with lr scheduler
            opts = ORTTrainerOptions({
                lr_scheduler: lr_scheduler
            })
            ort_trainer = ORTTrainer(..., options=opts)
 
            # Call step() in every batch update
            for inputs in batch_inputs:
                outputs = ort_trainer.train_step(**inputs)
    """
 
    def __init__(self, total_steps, cycles=0.5, warmup=0.002):
        super().__init__()
        assert isinstance(total_steps, int) and total_steps > 0, "total_steps must be a strict positive number"
        assert isinstance(cycles, float) and cycles > 0, "cycles must be a positive float"
        assert isinstance(warmup, float) and warmup >= 0 and warmup < 1, "warmup must be a float between (0, 1]"
        assert total_steps > warmup, "total_steps must be greater than warmup"
 
        self.total_steps = total_steps
        self.cycles = cycles
        self.warmup = warmup
        self._num_warmup_steps = warmup * total_steps
 
    def _warmup_cosine(self, train_step_info):
        if train_step_info.optimization_step < self._num_warmup_steps:
            return float(train_step_info.optimization_step) / float(max(1, self._num_warmup_steps))
        progress = float(train_step_info.optimization_step - self._num_warmup_steps) / float(
            max(1, self.total_steps - self._num_warmup_steps)
        )
        return max(0.0, 0.5 * (1.0 + math.cos(math.pi * float(self.cycles) * 2.0 * progress)))
 
    def get_lr(self, train_step_info):
        return [train_step_info.optimizer_config.lr * self._warmup_cosine(train_step_info)]
 
 
class LinearWarmupLRScheduler(_LRScheduler):
    r"""Linear warmup strategy for learning rate update based on HuggingFace's Transformers implementation
 
    Creates a schedule with a learning rate that decreases linearly from the initial lr
    set in the :py:class`.optim._OptimizerConfig` to 0, after a warmup period during which
    it increases linearly from 0 to the initial lr set in the :py:class`.optim._OptimizerConfig`.
 
    Learning rate update strategy:
        When current_step < warmup
            lr = base_lr * (current_step / max(1, num_warmup_steps))
        Otherwise
            lr = base_lr * (max(0, total_steps - current_step) / max(1, total_steps - num_warmup_steps)))
 
    Args:
        total_steps (int): total training steps for learning.
        warmup (float, default is 0.002): portion of total steps for warmup. Range is (0, 1]
 
    Example:
        .. code-block:: python
 
            # Initialize lr scheduler
            lr_scheduler = LinearWarmupLRScheduler(total_steps=512, warmup=0.002)
 
            # Initialize ORTTrainer with lr scheduler
            opts = ORTTrainerOptions({
                lr_scheduler: lr_scheduler
            })
            ort_trainer = ORTTrainer(..., options=opts)
 
            # Call step() in every batch update
            for inputs in batch_inputs:
                outputs = ort_trainer.train_step(**inputs)
    """
 
    def __init__(self, total_steps, warmup=0.002):
        super().__init__()
        assert isinstance(total_steps, int) and total_steps > 0, "total_steps must be a strict positive number"
        assert isinstance(warmup, float) and warmup >= 0 and warmup < 1, "warmup must be a float between (0, 1]"
        assert total_steps > warmup, "total_steps must be greater than warmup"
 
        self.total_steps = total_steps
        self.warmup = warmup
        self._num_warmup_steps = warmup * total_steps
 
    def _warmup_linear(self, train_step_info):
        if train_step_info.optimization_step < self._num_warmup_steps:
            return float(train_step_info.optimization_step) / float(max(1, self._num_warmup_steps))
        return max(
            0.0,
            float(self.total_steps - train_step_info.optimization_step)
            / float(max(1, self.total_steps - self._num_warmup_steps)),
        )
 
    def get_lr(self, train_step_info):
        return [train_step_info.optimizer_config.lr * self._warmup_linear(train_step_info)]
 
 
class PolyWarmupLRScheduler(_LRScheduler):
    r"""Polynomial warmup strategy for learning rate update based on HuggingFace's Transformers implementation
 
    Creates a schedule with a learning rate that decreases as a polynomial decay
    from the initial lr set in the :py:class`.optim._OptimizerConfig` to  lr_end,
    after a warmup period during which it increases linearly from 0 to the
    initial lr set in the :py:class`.optim._OptimizerConfig`
 
    Learning rate update strategy:
        When current_step < warmup
            lr = base_lr * (current_step / max(1, num_warmup_steps))
        When current_step > total_steps
            lr = lr_end / lr
        Otherwise
            lr =  decay / lr, where decay is
                (lr - lr_end) * (1 - (current_step - num_warmup_steps) / (total_steps - num_warmup_steps)) ** power + lr_end
 
    Args:
        total_steps (int): total training steps for learning.
        lr_end (float, default 1e-7): final learning rate value.
            Applies to the default lr and parameter groups in :py:class:`.optim._OptimizerConfig`
        power (float, default is 1.0): polynomial factor
        warmup (float, default is 0.002): portion of total steps for warmup. Range is (0, 1]
 
    Example:
        .. code-block:: python
 
            # Initialize lr scheduler
            lr_scheduler = PolyWarmupLRScheduler(total_steps=512, warmup=0.002, degree=0.5)
 
            # Initialize ORTTrainer with lr scheduler
            opts = ORTTrainerOptions({
                lr_scheduler: lr_scheduler
            })
            ort_trainer = ORTTrainer(..., options=opts)
 
            # Call step() in every batch update
            for inputs in batch_inputs:
                outputs = ort_trainer.train_step(**inputs)
    """
 
    def __init__(self, total_steps, lr_end=1e-7, power=1.0, warmup=0.002):
        super().__init__()
        assert isinstance(total_steps, int) and total_steps > 0, "total_steps must be a strict positive number"
        assert isinstance(lr_end, float) and lr_end >= 0, "lr_end must be a positive float"
        assert isinstance(warmup, float) and warmup >= 0 and warmup < 1, "warmup must be a float between (0, 1]"
        assert isinstance(power, float) and power >= 0, "power must be a positive float"
        assert total_steps > warmup, "total_steps must be greater than warmup"
 
        self.total_steps = total_steps
        self.lr_end = lr_end
        self.power = power
        self.warmup = warmup
        self._num_warmup_steps = warmup * total_steps
 
    def _warmup_poly(self, train_step_info):
        assert train_step_info.optimizer_config.lr > self.lr_end, (
            f"lr_end ({self.lr_end}) must be be smaller than initial lr ({train_step_info.optimizer_config.lr})"
        )
 
        if train_step_info.optimization_step < self._num_warmup_steps:
            return float(train_step_info.optimization_step) / float(max(1, self._num_warmup_steps))
        elif train_step_info.optimization_step > self.total_steps:
            return self.lr_end / train_step_info.optimizer_config.lr
        else:
            lr_range = train_step_info.optimizer_config.lr - self.lr_end
            decay_steps = self.total_steps - self._num_warmup_steps
            pct_remaining = 1 - (train_step_info.optimization_step - self._num_warmup_steps) / decay_steps
            decay = lr_range * pct_remaining**self.power + self.lr_end
            return decay / train_step_info.optimizer_config.lr
 
    def get_lr(self, train_step_info):
        return [train_step_info.optimizer_config.lr * self._warmup_poly(train_step_info)]