What does __constants__ = ['reduction'] mean in class L1Loss()

Can anybody please tell me what does this statement __constants__ = ['reduction'] mean, and how does it work ? Below is the class definition of L1Loss.

class L1Loss(_Loss):
    __constants__ = ['reduction']

    def __init__(self, size_average=None, reduce=None, reduction='mean'):
        super(L1Loss, self).__init__(size_average, reduce, reduction)

    def forward(self, input, target):
        return F.l1_loss(input, target, reduction=self.reduction)

__constant__ is used for the JIT to define this value as a constant value to allow more potential optimizations as explained here.
Alternatively, you could use the new API with Final.

2 Likes