Skip to content

Commit

Permalink
Fixed smooth quant's UTs (#1631)
Browse files Browse the repository at this point in the history
Signed-off-by: yiliu30 <yi4.liu@intel.com>
  • Loading branch information
yiliu30 authored Feb 27, 2024
1 parent 2a86aea commit 8ddd755
Showing 1 changed file with 37 additions and 47 deletions.
84 changes: 37 additions & 47 deletions test/algorithm/test_smooth_quant.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ def __iter__(self):


class TestSqDepthwiseConv(unittest.TestCase):

@classmethod
def setUpClass(self):
def setUpClass(cls):
class RandDataloader:
def __init__(self):
self.batch_size = 1

def __iter__(self):
yield torch.rand((1, 3, 1, 1))

self.conv_dl = RandDataloader()
cls.conv_dl = RandDataloader()

@classmethod
def test_sq_dw_conv_relu6_auto(self):
datasets = Datasets("pytorch")
dummy_dataset = datasets["dummy"](shape=(10, 3, 1, 1), low=0.0, high=1.0)
Expand Down Expand Up @@ -106,7 +106,6 @@ def forward(self, x):
assert torch.sum(torch.abs(output - output_sq)) < 1e-3
assert len(sq.absorb_to_layer) == 1

@classmethod
def test_sq_dw_conv_relu6(self):
datasets = Datasets("pytorch")
dummy_dataset = datasets["dummy"](shape=(10, 3, 1, 1), low=0.0, high=1.0)
Expand Down Expand Up @@ -140,18 +139,18 @@ def forward(self, x):


class TestSqConvOpFuseAuto(unittest.TestCase):

@classmethod
def setUpClass(self):
def setUpClass(cls):
class RandDataloader:
def __init__(self):
self.batch_size = 1

def __iter__(self):
yield torch.rand((1, 3, 1, 1))

self.conv_dl = RandDataloader()
cls.conv_dl = RandDataloader()

@classmethod
def test_sq_conv_relu6(self):
datasets = Datasets("pytorch")
dummy_dataset = datasets["dummy"](shape=(10, 3, 2, 2), low=0.0, high=1.0)
Expand Down Expand Up @@ -180,18 +179,18 @@ def forward(self, x):


class TestSqConvOpFuse(unittest.TestCase):

@classmethod
def setUpClass(self):
def setUpClass(cls):
class RandDataloader:
def __init__(self):
self.batch_size = 1

def __iter__(self):
yield torch.rand((1, 3, 1, 1))

self.conv_dl = RandDataloader()
cls.conv_dl = RandDataloader()

@classmethod
def test_sq_conv_relu6(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand All @@ -214,7 +213,6 @@ def forward(self, x):
sq.transform(alpha=0.5, folding=True)
assert len(sq.absorb_to_layer) == 1

@classmethod
def test_sq_conv_relu(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand All @@ -237,7 +235,6 @@ def forward(self, x):
sq.transform(alpha=0.5, calib_iter=2, folding=True)
assert len(sq.absorb_to_layer) == 1

@classmethod
def test_sq_conv_gelu(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand All @@ -260,7 +257,6 @@ def forward(self, x):
sq.transform(alpha=0.5, calib_iter=2, folding=True)
assert len(sq.absorb_to_layer) == 0

@classmethod
def test_sq_conv_bn(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand Down Expand Up @@ -385,8 +381,9 @@ def forward(self, hidden_states):


class TestSqListInput(unittest.TestCase):

@classmethod
def setUpClass(self):
def setUpClass(cls):
class ListDataloader:
def __init__(self):
self.batch_size = 1
Expand All @@ -410,11 +407,10 @@ def __iter__(self):
input2 = torch.rand((1, 3))
yield [input1, ((input2, input1)), input2]

self.list_dl = ListDataloader()
self.tuple_dl = TupleDataloader()
self.list_tuple_dl = ListTupleDataLoader()
cls.list_dl = ListDataloader()
cls.tuple_dl = TupleDataloader()
cls.list_tuple_dl = ListTupleDataLoader()

@classmethod
def test_sq_linear_LlamaRMSNorm(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand All @@ -437,7 +433,6 @@ def forward(self, x):
sq.transform(alpha=0.5, calib_iter=1, folding=True)
assert len(sq.absorb_to_layer) == 1

@classmethod
def test_sq_linear_LlamaRMSNorm_tuple(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand All @@ -460,7 +455,6 @@ def forward(self, x):
sq.transform(alpha=0.5, calib_iter=1, folding=True)
assert len(sq.absorb_to_layer) == 1

@classmethod
def test_sq_linear_LlamaRMSNorm_list_tuple(self):
class Model(torch.nn.Module):
def __init__(self):
Expand Down Expand Up @@ -498,18 +492,18 @@ def test_device(self):


class TestAlphaAutoLinear(unittest.TestCase):

@classmethod
def setUpClass(self):
def setUpClass(cls):
class RandDataloader:
def __init__(self):
self.batch_size = 1

def __iter__(self):
yield torch.rand((1, 3))

self.linear_dl = RandDataloader()
cls.linear_dl = RandDataloader()

@classmethod
def test_sq_linear_LlamaRMSNorm_auto(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand All @@ -534,18 +528,18 @@ def forward(self, x):


class TestSqLinearOpFuse(unittest.TestCase):

@classmethod
def setUpClass(self):
def setUpClass(cls):
class RandDataloader:
def __init__(self):
self.batch_size = 1

def __iter__(self):
yield torch.rand((1, 3))

self.linear_dl = RandDataloader()
cls.linear_dl = RandDataloader()

@classmethod
def test_sq_linear_LlamaRMSNorm(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand All @@ -568,7 +562,6 @@ def forward(self, x):
sq.transform(alpha=0.5, calib_iter=1, folding=True)
assert len(sq.absorb_to_layer) == 1

@classmethod
def test_sq_linear_T5Norm(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand All @@ -591,7 +584,6 @@ def forward(self, x):
sq.transform(alpha=0.5, calib_iter=1, folding=True)
assert len(sq.absorb_to_layer) == 1

@classmethod
def test_sq_linear_relu6(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand All @@ -614,7 +606,6 @@ def forward(self, x):
sq.transform(alpha=0.5, calib_iter=1, folding=True)
assert len(sq.absorb_to_layer) == 1

@classmethod
def test_sq_linear_norm(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand All @@ -637,7 +628,6 @@ def forward(self, x):
sq.transform(alpha=0.5, calib_iter=1, folding=True)
assert len(sq.absorb_to_layer) == 1

@classmethod
def test_sq_linear_norm_linear(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand All @@ -662,7 +652,6 @@ def forward(self, x):
sq.transform(alpha=0.5, calib_iter=1, folding=True)
assert len(sq.absorb_to_layer) == 2

@classmethod
def test_sq_linear_gelu_norm(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand Down Expand Up @@ -957,18 +946,18 @@ def __iter__(self):


class TestSqSkipOp(unittest.TestCase):

@classmethod
def setUpClass(self):
def setUpClass(cls):
class RandDataloader:
def __init__(self):
self.batch_size = 1

def __iter__(self):
yield torch.rand((1, 4))

self.linear_dl = RandDataloader()
cls.linear_dl = RandDataloader()

@classmethod
def test_sq_skip_op_auto(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand Down Expand Up @@ -1014,18 +1003,18 @@ def test_sq_no_skip_op_auto(self):


class TestSqSkipOp_attn(unittest.TestCase):

@classmethod
def setUpClass(self):
def setUpClass(cls):
class RandDataloader:
def __init__(self):
self.batch_size = 1

def __iter__(self):
yield torch.rand((1, 4))

self.linear_dl = RandDataloader()
cls.linear_dl = RandDataloader()

@classmethod
def test_sq_skip_op_attn_auto(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand Down Expand Up @@ -1068,9 +1057,10 @@ def forward(self, x):


class TestTuneSqAlpha(unittest.TestCase):

@classmethod
def setUpClass(self):
self.ns_workspace = "./nc_workspace"
def setUpClass(cls):
cls.ns_workspace = "./nc_workspace"

@classmethod
def tearDownClass(self):
Expand Down Expand Up @@ -1236,11 +1226,12 @@ def fake_eval(model, eval_result_lst):


class TestTextGeneration(unittest.TestCase):

@classmethod
def setUpClass(self):
def setUpClass(cls):
from modeling_gptj import GPTJForCausalLM

self.clm_model = GPTJForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gptj", torchscript=True)
cls.clm_model = GPTJForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gptj", torchscript=True)

def test_text_generation(self):
input_ids = torch.tensor([[531, 574, 658, 492, 156], [309, 296, 471, 817, 435], [182, 176, 756, 944, 768]])
Expand Down Expand Up @@ -1445,7 +1436,7 @@ def calib_func(model):
) # Linear that is not called in calibration

@unittest.skipIf(
IPEX_VERSION.release <= Version("2.1.0").release and ipex.__version__ != "2.1.0+cpu",
(not TEST_IPEX) or (IPEX_VERSION.release <= Version("2.1.0").release and ipex.__version__ != "2.1.0+cpu"),
"Please use Intel extension for Pytorch version higher or equal to 2.1.0",
)
def test_peft_model_quantization_ipex(self):
Expand Down Expand Up @@ -1480,18 +1471,18 @@ def calib_func(model):


class TestInputConfig(unittest.TestCase):

@classmethod
def setUpClass(self):
def setUpClass(cls):
class RandDataloader:
def __init__(self):
self.batch_size = 1

def __iter__(self):
yield torch.rand((1, 3))

self.linear_dl = RandDataloader()
cls.linear_dl = RandDataloader()

@classmethod
def test_sq_weight_clipping(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand All @@ -1514,7 +1505,6 @@ def forward(self, x):
sq.transform(alpha="auto", calib_iter=1, folding=True, weight_clip=False)
assert sq.weight_clip is False

@classmethod
def test_sq_auto_alpha_arg(self):
class Model(torch.nn.Module):
device = torch.device("cpu")
Expand Down Expand Up @@ -1546,7 +1536,7 @@ def forward(self, x):


class TestAlphaAutoLinearBlockwise(unittest.TestCase):
@classmethod

def test_sq_linear_Blockwise_auto(self):
model = transformers.AutoModelForCausalLM.from_pretrained(
"facebook/opt-125m",
Expand Down

0 comments on commit 8ddd755

Please sign in to comment.