check,this,file,import numpy as np import torch import torch.nn.functional as F from…
- User
check,this,file,import numpy as np import torch import torch.nn.functional as F from torch import nn from collections import namedtuple from torch.nn import Linear, Conv2d, BatchNorm2d, PReLU, ReLU, Sigmoid, MaxPool2d, AdaptiveAvgPool2d, Sequential, Module
code borrowed from https://github.com/eladrich/pixel2style2pixel
from Generator import EqualLinear
class Flatten(Module): def forward(self, input): return input.view(input.size(0), -1)
class Bottleneck(namedtuple('Block', ['in_channel', 'depth', 'stride'])): """ A named tuple describing a ResNet block. """
def get_block(in_channel, depth, num_units, stride=2): return [Bottleneck(in_channel, depth, stride)] + [Bottleneck(depth, depth, 1) for i in range(num_units - 1)]
def get_blocks(num_layers): if num_layers == 50: blocks = [ get_block(in_channel=64, depth=64, num_units=3), get_block(in_channel=64, depth=128, num_units=4), get_block(in_channel=128, depth=256, num_units=14), get_block(in_channel=256, depth=512, num_units=3) ] elif num_layers == 100: blocks = [ get_block(in_channel=64, depth=64, num_units=3), get_block(in_channel=64, depth=128, num_units=13), get_block(in_channel=128, depth=256, num_units=30), get_block(in_channel=256, depth=512, num_units=3) ] elif num_layers == 152: blocks = [ get_block(in_channel=64, depth=64, num_units=3), get_block(in_channel=64, depth=128, num_units=8), get_block(in_channel=128, depth=256, num_units=36), get_block(in_channel=256, depth=512, num_units=3) ] else: raise ValueError("Invalid number of layers: {}. Must be one of [50, 100, 152]".format(num_layers)) return blocks
import torch.quantization as quant class SEModule(Module): def init(self, channels, reduction): super(SEModule, self).init() self.avg_pool = AdaptiveAvgPool2d(1) self.fc1 = Conv2d(channels, channels // reduction, kernel_size=1, padding=0, bias=False) self.relu = ReLU(inplace=True) self.fc2 = Conv2d(channels // reduction, channels, kernel_size=1, padding=0, bias=False) self.sigmoid = Sigmoid() # Add quantization stubs self.quant = quant.QuantStub() self.dequant = quant.DeQuantStub()
def forward(self, x): # Dequantize before performing unsupported operations if x.is_quantized: x = self.dequant(x)
module_input = x.clone() x = self.avg_pool(x) x = self.fc1(x) x = self.relu(x) x = self.fc2(x) x = self.sigmoid(x) # Use element-wise multiplication in float space output = module_input * x return outputclass bottleneck_IR(Module): def init(self, in_channel, depth, stride): super(bottleneck_IR, self).init() if in_channel == depth: self.shortcut_layer = MaxPool2d(1, stride) else: self.shortcut_layer = Sequential( Conv2d(in_channel, depth, (1, 1), stride, bias=False), BatchNorm2d(depth) ) self.res_layer = Sequential( BatchNorm2d(in_channel), Conv2d(in_channel, depth, (3, 3), (1, 1), 1, bias=False), PReLU(depth), Conv2d(depth, depth, (3, 3), stride, 1, bias=False), BatchNorm2d(depth) )
- Assistant
- User
the,error,still,same,it,point,to, 67 if x.is_quantized: ---> 68 x = self.dequant(x)
- Assistant
- User
Using BackboneEncoderUsingLastLayerIntoWPlus Using BackboneEncoderUsingLastLayerIntoWPlus Loaded pSp from checkpoint: /content/drive/MyDrive/StyleDreamer/best_model.pt Using BackboneEncoderUsingLastLayerIntoWPlus Using BackboneEncoderUsingLastLayerIntoWPlus :65: FutureWarning: You are using
torch.loadwithweights_only=False(the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for will be flipped to . This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via . We recommend you start setting for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature. model_quantized_for_loading.load_state_dict(torch.load('quantized_mypSp_cpu.pth')) - Assistant
- User
now,it,become,NotImplementedError: Could not run 'quantized::conv2d.new' with arguments from the 'CPU' backend. This could be because the operator doesn't exist for this backend, or was omitted during the selective/custom build process (if using custom build). If you are a Facebook employee using PyTorch on mobile, please visit https://fburl.com/ptmfixes for possible resolutions. 'quantized::conv2d.new' is only available for these backends: [Meta, QuantizedCPU, QuantizedCUDA, BackendSelect, Python, FuncTorchDynamicLayerBackMode, Functionalize, Named, Conjugate, Negative, ZeroTensor, ADInplaceOrView, AutogradOther, AutogradCPU, AutogradCUDA, AutogradXLA, AutogradMPS, AutogradXPU, AutogradHPU, AutogradLazy, AutogradMeta, Tracer, AutocastCPU, AutocastXPU, AutocastCUDA, FuncTorchBatched, BatchedNestedTensor, FuncTorchVmapMode, Batched, VmapMode, FuncTorchGradWrapper, PythonTLSSnapshot, FuncTorchDynamicLayerFrontMode, PreDispatch, PythonDispatcher].
- Assistant
- User
Quantized engine: x86
- Assistant
- User
i,use,this,quantization_config = quant.QConfig( activation=quant.MinMaxObserver.with_args(dtype=torch.quint8, qscheme=torch.per_tensor_affine), weight=quant.default_weight_observer )
- Assistant
- User
---> 75 output = module_input * x 76 77 return output
RuntimeError: empty_strided not supported on quantized tensors yet see https://github.com/pytorch/pytorch/issues/74540
- Assistant
- User
i,do,def forward(self, x):
module_input = x x = self.avg_pool(x) x = self.fc1(x) x = self.relu(x) x = self.fc2(x) x = self.sigmoid(x) # Dequantization x = self.dequant(x) # Perform the multiplication in dequantized form output = module_input * x # Quantize output if needed by subsequent model parts return self.quant(output)# if module_input.is_quantized else output,RuntimeError: promoteTypes with quantized numbers is not handled yet; figure out what the correct rules should be, offending types: QUInt8 Float
- Assistant
- User
is,it,possible,to,do,elemen,wise,multiplication,on,quant,tensor
- Assistant
- User
now,move,to, def forward(self, x): shortcut = self.shortcut_layer(x) res = self.res_layer(x) return res + shortcut ,notimplemented,also
- Assistant