From b313592653e653cd03ef3e193d2dddeefa175d8e Mon Sep 17 00:00:00 2001 From: Yifan Zhao <yifanz16@illinois.edu> Date: Mon, 12 Jul 2021 01:56:45 -0500 Subject: [PATCH] Moved miniera model files together --- hpvm/test/epoch_dnn/main.py | 3 +- hpvm/test/epoch_dnn/torch_dnn/__init__.py | 2 - .../epoch_dnn/torch_dnn/miniera/__init__.py | 2 + .../{datasets.py => miniera/dataset.py} | 54 ++++++++----------- .../{miniera.py => miniera/model.py} | 4 +- hpvm/test/epoch_dnn/torch_dnn/quantizer.py | 6 +-- 6 files changed, 33 insertions(+), 38 deletions(-) create mode 100644 hpvm/test/epoch_dnn/torch_dnn/miniera/__init__.py rename hpvm/test/epoch_dnn/torch_dnn/{datasets.py => miniera/dataset.py} (89%) rename hpvm/test/epoch_dnn/torch_dnn/{miniera.py => miniera/model.py} (94%) diff --git a/hpvm/test/epoch_dnn/main.py b/hpvm/test/epoch_dnn/main.py index 221c1e7019..b89f847d68 100644 --- a/hpvm/test/epoch_dnn/main.py +++ b/hpvm/test/epoch_dnn/main.py @@ -10,7 +10,8 @@ from torch2hpvm import BinDataset, ModelExporter self_folder = Path(__file__).parent.absolute() site.addsitedir(self_folder.as_posix()) -from torch_dnn import CIFAR, MiniERA, quantize +from torch_dnn import quantize +from torch_dnn.miniera import CIFAR, MiniERA # Consts (don't change) BUFFER_NAME = "hpvm-mod.nvdla" diff --git a/hpvm/test/epoch_dnn/torch_dnn/__init__.py b/hpvm/test/epoch_dnn/torch_dnn/__init__.py index a6fb99465f..89b65549ba 100644 --- a/hpvm/test/epoch_dnn/torch_dnn/__init__.py +++ b/hpvm/test/epoch_dnn/torch_dnn/__init__.py @@ -1,3 +1 @@ -from .datasets import CIFAR -from .miniera import MiniERA from .quantizer import quantize diff --git a/hpvm/test/epoch_dnn/torch_dnn/miniera/__init__.py b/hpvm/test/epoch_dnn/torch_dnn/miniera/__init__.py new file mode 100644 index 0000000000..d135d1bd40 --- /dev/null +++ b/hpvm/test/epoch_dnn/torch_dnn/miniera/__init__.py @@ -0,0 +1,2 @@ +from .dataset import CIFAR +from .model import MiniERA diff --git a/hpvm/test/epoch_dnn/torch_dnn/datasets.py b/hpvm/test/epoch_dnn/torch_dnn/miniera/dataset.py similarity index 89% rename from hpvm/test/epoch_dnn/torch_dnn/datasets.py rename to hpvm/test/epoch_dnn/torch_dnn/miniera/dataset.py index ac519bfe26..b235a7ca5c 100644 --- a/hpvm/test/epoch_dnn/torch_dnn/datasets.py +++ b/hpvm/test/epoch_dnn/torch_dnn/miniera/dataset.py @@ -7,39 +7,16 @@ import torch from torch.utils.data.dataset import Dataset RetT = Tuple[torch.Tensor, torch.Tensor] -msg_logger = logging.getLogger(__name__) - PathLike = Union[Path, str] +msg_logger = logging.getLogger(__name__) class SingleFileDataset(Dataset): + image_shape = None + def __init__(self, inputs: torch.Tensor, outputs: torch.Tensor): self.inputs, self.outputs = inputs, outputs - @classmethod - def from_file(cls, *args, **kwargs): - pass - - @property - def sample_input(self): - inputs, outputs = next(iter(self)) - return inputs - - def __len__(self) -> int: - return len(self.inputs) - - def __getitem__(self, idx) -> RetT: - return self.inputs[idx], self.outputs[idx] - - def __iter__(self) -> Iterator[RetT]: - for i in range(len(self)): - yield self[i] - - -class DNNDataset(SingleFileDataset): - image_shape = None - label_ty = np.int32 - @classmethod def from_file( cls, @@ -61,8 +38,8 @@ class DNNDataset(SingleFileDataset): labels = read_tensor_from_file( labels_file, -1, - read_ty=cls.label_ty, - cast_ty=np.long, + read_ty=np.int32, + cast_ty=np.int64, count=count, offset=offset, ) @@ -71,16 +48,31 @@ class DNNDataset(SingleFileDataset): msg_logger.info(f"%d entries loaded from dataset.", inputs.shape[0]) return cls(inputs, labels) + @property + def sample_input(self): + inputs, outputs = next(iter(self)) + return inputs + + def __len__(self) -> int: + return len(self.inputs) + + def __getitem__(self, idx) -> RetT: + return self.inputs[idx], self.outputs[idx] + + def __iter__(self) -> Iterator[RetT]: + for i in range(len(self)): + yield self[i] + -class MNIST(DNNDataset): +class MNIST(SingleFileDataset): image_shape = 1, 28, 28 -class CIFAR(DNNDataset): +class CIFAR(SingleFileDataset): image_shape = 3, 32, 32 -class ImageNet(DNNDataset): +class ImageNet(SingleFileDataset): image_shape = 3, 224, 224 diff --git a/hpvm/test/epoch_dnn/torch_dnn/miniera.py b/hpvm/test/epoch_dnn/torch_dnn/miniera/model.py similarity index 94% rename from hpvm/test/epoch_dnn/torch_dnn/miniera.py rename to hpvm/test/epoch_dnn/torch_dnn/miniera/model.py index 3e20067318..0f6fe455d8 100644 --- a/hpvm/test/epoch_dnn/torch_dnn/miniera.py +++ b/hpvm/test/epoch_dnn/torch_dnn/miniera/model.py @@ -36,7 +36,9 @@ class MiniERA(Module): for conv in self.convs: if not isinstance(conv, Conv2d): continue - weight_np = np.fromfile(prefix / f"conv2d_{count+1}_w.bin", dtype=np.float32) + weight_np = np.fromfile( + prefix / f"conv2d_{count+1}_w.bin", dtype=np.float32 + ) bias_np = np.fromfile(prefix / f"conv2d_{count+1}_b.bin", dtype=np.float32) conv.weight.data = torch.tensor(weight_np).reshape(conv.weight.shape) conv.bias.data = torch.tensor(bias_np).reshape(conv.bias.shape) diff --git a/hpvm/test/epoch_dnn/torch_dnn/quantizer.py b/hpvm/test/epoch_dnn/torch_dnn/quantizer.py index 729e8bd356..85ee9c479b 100644 --- a/hpvm/test/epoch_dnn/torch_dnn/quantizer.py +++ b/hpvm/test/epoch_dnn/torch_dnn/quantizer.py @@ -1,17 +1,17 @@ import os from copy import deepcopy from pathlib import Path -from typing import Union from shutil import move +from typing import Union import distiller import torch -from torch.utils.data.dataset import Dataset import yaml from distiller.data_loggers import collect_quant_stats from distiller.quantization import PostTrainLinearQuantizer from torch import nn from torch.utils.data import DataLoader +from torch.utils.data.dataset import Dataset PathLike = Union[str, Path] STATS_FILENAME = "acts_quantization_stats.yaml" @@ -40,7 +40,7 @@ def quantize( strat: str = "NONE", working_dir: PathLike = ".", output_name: str = "calib.txt", - eval_batchsize: int = 128 + eval_batchsize: int = 128, ): # possible quant strats ['NONE', 'AVG', 'N_STD', 'GAUSS', 'LAPLACE'] print("Quantizing...") -- GitLab