File size: 11,864 Bytes
bc2cdff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from contextlib import contextmanager
from collections import defaultdict
import drjit as dr
from mitsuba import TensorXf

class Optimizer:
    """
    Base class of all gradient-based optimizers.
    """
    def __init__(self, lr, params: dict):
        """
        Parameter ``lr``:
            learning rate

        Parameter ``params`` (:py:class:`dict`):
            Dictionary-like object containing parameters to optimize.
        """
        self.lr   = defaultdict(lambda: self.lr_default)
        self.lr_v = defaultdict(lambda: self.lr_default_v)

        self.set_learning_rate(lr)
        self.variables = {}
        self.state = {}

        if params is not None:
            for k, v in params.items():
                self.__setitem__(k, v)

    def __contains__(self, key: str):
        return self.variables.__contains__(key)

    def __getitem__(self, key: str):
        return self.variables[key]

    def __setitem__(self, key: str, value):
        """
        Overwrite the value of a parameter.

        This method can also be used to load a scene parameters to optimize.

        Note that the state of the optimizer (e.g. momentum) associated with the
        parameter is preserved, unless the parameter's dimensions changed. When
        assigning a substantially different parameter value (e.g. as part of a
        different optimization run), the previous momentum value is meaningless
        and calling `reset()` is advisable.
        """
        if not (dr.is_diff_v(value) and dr.is_float_v(value)):
            raise Exception('Optimizer.__setitem__(): value should be differentiable!')
        needs_reset = (key not in self.variables) or dr.shape(self.variables[key]) != dr.shape(value)

        self.variables[key] = dr.detach(value, True)
        dr.enable_grad(self.variables[key])
        if needs_reset:
            self.reset(key)

    def __delitem__(self, key: str) -> None:
        del self.variables[key]

    def __len__(self) -> int:
        return len(self.variables)

    def keys(self):
        return self.variables.keys()

    def items(self):
        class OptimizerItemIterator:
            def __init__(self, items):
                self.items = items
                self.it = items.keys().__iter__()

            def __iter__(self):
                return self

            def __next__(self):
                key = next(self.it)
                return (key, self.items[key])

        return OptimizerItemIterator(self.variables)

    def set_learning_rate(self, lr) -> None:
        """
        Set the learning rate.

        Parameter ``lr`` (``float``, ``dict``):
            The new learning rate. A ``dict`` can be provided instead to
            specify the learning rate for specific parameters.
        """

        # We use `dr.opaque` so the that the JIT compiler does not include
        # the learning rate as a scalar literal into generated code, which
        # would defeat kernel caching when updating learning rates.
        if isinstance(lr, float) or isinstance(lr, int):
            self.lr_default = float(lr)
            #self.lr_default_v = dr.opaque(dr.detached_t(Float), lr, shape=1)
            self.lr_default_v = dr.opaque(dr.detached_t(TensorXf), lr, shape=())
        elif isinstance(lr, dict):
            for k, v in lr.items():
                self.lr[k] = v
                #self.lr_v[k] = dr.opaque(dr.detached_t(Float), v, shape=1)
                self.lr_v[k] = dr.opaque(dr.detached_t(TensorXf), v, shape=())
        else:
            raise Exception('Optimizer.set_learning_rate(): value should be a float or a dict!')

    def reset(self, key):
        """
        Resets the internal state associated with a parameter, if any (e.g. momentum).
        """
        pass


class SGD(Optimizer):
    """
    Implements basic stochastic gradient descent with a fixed learning rate
    and, optionally, momentum :cite:`Sutskever2013Importance` (0.9 is a typical
    parameter value for the ``momentum`` parameter).

    The momentum-based SGD uses the update equation

    .. math::

        v_{i+1} = \\mu \\cdot v_i +  g_{i+1}

    .. math::
        p_{i+1} = p_i + \\varepsilon \\cdot v_{i+1},

    where :math:`v` is the velocity, :math:`p` are the positions,
    :math:`\\varepsilon` is the learning rate, and :math:`\\mu` is
    the momentum parameter.
    """
    def __init__(self, lr, momentum=0, mask_updates=False, params:dict=None):
        """
        Parameter ``lr``:
            learning rate

        Parameter ``momentum``:
            momentum factor

        Parameter ``mask_updates``:
            if enabled, parameters and state variables will only be updated
            in a given iteration if it received nonzero gradients in that iteration.
            This only has an effect if momentum is enabled.
            See :py:class:`mitsuba.optimizers.Adam`'s documentation for more details.

        Parameter ``params`` (:py:class:`dict`):
            Optional dictionary-like object containing parameters to optimize.
        """
        assert momentum >= 0 and momentum < 1
        assert lr > 0
        self.momentum = momentum
        self.mask_updates = mask_updates
        super().__init__(lr, params)

    def step(self):
        """Take a gradient step"""
        for k, p in self.variables.items():
            g_p = dr.grad(p)
            shape = dr.shape(g_p)
            if shape == 0:
                continue



            if self.momentum != 0:
                if shape != dr.shape(self.state[k]):
                    # Reset state if data size has changed
                    self.reset(k)

                next_state = self.momentum * self.state[k] + g_p
                step = self.lr_v[k] * self.state[k]
                if self.mask_updates:
                    nonzero = dr.neq(g_p, 0.)
                    next_state = dr.select(nonzero, next_state, self.state[k])
                    step = dr.select(nonzero, step, 0)
                self.state[k] = next_state
                value = dr.detach(p) - step
                dr.schedule(self.state[k])
            else:
                value = dr.detach(p) - self.lr_v[k] * g_p


            value = type(p)(value)
            dr.enable_grad(value)
            self.variables[k] = value
            dr.schedule(self.variables[k])

        dr.eval()

    def reset(self, key):
        """Zero-initializes the internal state associated with a parameter"""
        if self.momentum == 0:
            return
        p = self.variables[key]
        #shape = dr.shape(p) if p.IsTensor else dr.width(p)
        shape = dr.shape(p)
        self.state[key] = dr.zeros(dr.detached_t(p), shape)

    def __repr__(self):
        return ('SGD[\n'
                '  variables = %s,\n'
                '  lr = %s,\n'
                '  momentum = %.2g\n'
                ']' % (list(self.keys()), dict(self.lr, default=self.lr_default),
                       self.momentum))


class Adam(Optimizer):
    """
    Implements the Adam optimizer presented in the paper *Adam: A Method for
    Stochastic Optimization* by Kingman and Ba, ICLR 2015.

    When optimizing many variables (e.g. a high resolution texture) with
    momentum enabled, it may be beneficial to restrict state and variable
    updates to the entries that received nonzero gradients in the current
    iteration (``mask_updates=True``).
    In the context of differentiable Monte Carlo simulations, many of those
    variables may not be observed at each iteration, e.g. when a surface is
    not visible from the current camera. Gradients for unobserved variables
    will remain at zero by default.
    If we do not take special care, at each new iteration:

    1. Momentum accumulated at previous iterations (potentially very noisy)
       will keep being applied to the variable.
    2. The optimizer's state will be updated to incorporate ``gradient = 0``,
       even though it is not an actual gradient value but rather lack of one.

    Enabling ``mask_updates`` avoids these two issues. This is similar to
    `PyTorch's SparseAdam optimizer <https://pytorch.org/docs/1.9.0/generated/torch.optim.SparseAdam.html>`_.
    """
    def __init__(self, lr, beta_1=0.9, beta_2=0.999, epsilon=1e-8,
                 mask_updates=False, uniform=False, params: dict=None):
        """
        Parameter ``lr``:
            learning rate

        Parameter ``beta_1``:
            controls the exponential averaging of first order gradient moments

        Parameter ``beta_2``:
            controls the exponential averaging of second order gradient moments

        Parameter ``mask_updates``:
            if enabled, parameters and state variables will only be updated in a
            given iteration if it received nonzero gradients in that iteration

        Parameter ``uniform``:
            if enabled, the optimizer will use the 'UniformAdam' variant of Adam
            [Nicolet et al. 2021], where the update rule uses the *maximum* of
            the second moment estimates at the current step instead of the
            per-element second moments.

        Parameter ``params`` (:py:class:`dict`):
            Optional dictionary-like object containing parameters to optimize.
        """
        assert 0 <= beta_1 < 1 and 0 <= beta_2 < 1 \
            and lr > 0 and epsilon > 0

        self.beta_1 = beta_1
        self.beta_2 = beta_2
        self.epsilon = epsilon
        self.mask_updates = mask_updates
        self.uniform = uniform
        self.t = defaultdict(lambda: 0)
        super().__init__(lr, params)

    def step(self):
        """Take a gradient step"""
        for k, p in self.variables.items():
            self.t[k] += 1
            lr_scale = dr.sqrt(1 - self.beta_2 ** self.t[k]) / (1 - self.beta_1 ** self.t[k])
            #lr_scale = dr.opaque(dr.detached_t(Float), lr_scale, shape=1)
            lr_scale = dr.opaque(dr.detached_t(TensorXf), lr_scale, shape=())

            lr_t = self.lr_v[k] * lr_scale
            g_p = dr.grad(p)
            shape = dr.shape(g_p)

            if shape == 0:
                continue
            elif shape != dr.shape(self.state[k][0]):
                # Reset state if data size has changed
                self.reset(k)

            m_tp, v_tp = self.state[k]
            m_t = self.beta_1 * m_tp + (1 - self.beta_1) * g_p
            v_t = self.beta_2 * v_tp + (1 - self.beta_2) * dr.square(g_p)
            if self.mask_updates:
                nonzero = dr.neq(g_p, 0.)
                m_t = dr.select(nonzero, m_t, m_tp)
                v_t = dr.select(nonzero, v_t, v_tp)
            self.state[k] = (m_t, v_t)
            dr.schedule(self.state[k])

            if self.uniform:
                step = lr_t * m_t / (dr.sqrt(dr.max(v_t)) + self.epsilon)
            else:
                step = lr_t * m_t / (dr.sqrt(v_t) + self.epsilon)
            if self.mask_updates:
                step = dr.select(nonzero, step, 0.)
            u = dr.detach(p) - step
            u = type(p)(u)
            dr.enable_grad(u)
            self.variables[k] = u
            dr.schedule(self.variables[k])

        dr.eval()

    def reset(self, key):
        """Zero-initializes the internal state associated with a parameter"""
        p = self.variables[key]
        #shape = dr.shape(p) if p.IsTensor else dr.width(p)
        shape = dr.shape(p)
        self.state[key] = (dr.zeros(dr.detached_t(p), shape),
                           dr.zeros(dr.detached_t(p), shape))
        self.t[key] = 0

    def __repr__(self):
        return ('Adam[\n'
                '  variables = %s,\n'
                '  lr = %s,\n'
                '  betas = (%g, %g),\n'
                '  eps = %g\n'
                ']' % (list(self.keys()), dict(self.lr, default=self.lr_default),
                       self.beta_1, self.beta_2, self.epsilon))