File size: 768 Bytes
56238f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import logging


class BaseAE(torch.nn.Module):
    def __init__(self, scale=1.0, shift=0.0):
        super().__init__()
        self.scale = scale
        self.shift = shift

    @torch.autocast("cuda", dtype=torch.bfloat16)
    def encode(self, x):
        return self._impl_encode(x).to(torch.bfloat16)

    @torch.autocast("cuda", dtype=torch.bfloat16)
    def decode(self, x):
        return self._impl_decode(x).to(torch.bfloat16)

    def _impl_encode(self, x):
        raise NotImplementedError

    def _impl_decode(self, x):
        raise NotImplementedError

def uint82fp(x):
    x = x.to(torch.float32)
    x = (x - 127.5) / 127.5
    return x

def fp2uint8(x):
    x = torch.clip_((x + 1) * 127.5 + 0.5, 0, 255).to(torch.uint8)
    return x