forked from StarxSky/ANE-GPT-New
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_transformer.py
148 lines (118 loc) · 4.8 KB
/
test_transformer.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#
# For licensing see accompanying LICENSE.md file.
# Copyright (C) 2022 Apple Inc. All Rights Reserved.
#
import time
import torch
import unittest
import numpy as np
import collections
import coremltools as ct
from Core import testing_utils
from Core import ANEGPT as ane_gpt
torch.set_grad_enabled(False)
import logging
logger = logging.getLogger(__name__)
logger.setLevel('INFO')
# Testing configuration
PSNR_THRESHOLD = 20
SANITY_CHECK_CPUGPU2ANE_SPEEDUP_FACTOR = 10
TEST_INPUTS = collections.OrderedDict({
'inputs':
torch.LongTensor([[1,2,3,8], [2,3,5,6], [3,3,8,3]]),
})
class TestGPTReferenceImplementation(unittest.TestCase):
"""
Test conversion success and ANE speed-up of the reference Transformer implementation
"""
@classmethod
def setUpClass(cls):
cls.model = ane_gpt.AppleNeuralEngineGPT()
cls.inputs = TEST_INPUTS
cls.ref_outputs = cls.model(**cls.inputs)
@classmethod
def tearDownClass(cls):
cls.model = None
cls.inputs = None
def test_coreml_conversion_and_speedup(self):
# Conversion from PyTorch module to Torchscript module
try:
module_traced = torch.jit.trace(self.model,
list(self.inputs.values()))
except Exception as e:
raise RuntimeError("Torchscript tracing failed!") from e
logger.info("Torchscript tracing is successful")
# Conversion from Torchscript module to CoreML Model Package
# Targeting (primarily) ANE without GPU+CPU fallback
try:
ane_mlpackage_obj = ct.convert(
module_traced,
convert_to='mlprogram',
inputs=[
ct.TensorType(
name,
shape=tensor.shape,
dtype=np.float32,
) for name, tensor in self.inputs.items()
],
compute_units=ct.ComputeUnit.CPU_ONLY #ComputeUnit.ALL,
)
except Exception as e:
raise RuntimeError(
"CoreML conversion targeting ANE failed!") from e
logger.info("CoreML conversion targeting ANE is successful")
# Targeting GPU+CPU but not the ANE
#try:
#noane_mlpackage_obj = ct.convert(
#module_traced,
#convert_to='mlprogram',
#inputs=[
#ct.TensorType(
#name,
#shape=tensor.shape,
#dtype=np.float32,
#) for name, tensor in self.inputs.items()
#],
#compute_units=ct.ComputeUnit.CPU_AND_GPU,
#)
#except Exception as e:
#raise RuntimeError(
#"CoreML conversion targeting GPU+CPU failed!") from e
#logger.info("CoreML conversion targeting GPU+CPU is successful")
# CoreML inference on test inputs
coreml_out = list(
ane_mlpackage_obj.predict(
{k: v.numpy()
for k, v in self.inputs.items()}).values())[0]
coreml_test_outputs = torch.from_numpy(coreml_out).numpy()
# Test end-to-end parity for the conversion pipeline
psnr = testing_utils.compute_psnr(coreml_test_outputs,
self.ref_outputs[0].numpy())
logger.info(
f"PSNR between original PyTorch module and optimized CoreML ANE forward pass: {psnr:.2f}"
)
assert psnr > PSNR_THRESHOLD
# Speed-up lower bound test
ane_latency = testing_utils.rough_timeit(
lambda: ane_mlpackage_obj.predict(
{k: v.numpy()
for k, v in self.inputs.items()}),
n=13)
#noane_latency = testing_utils.rough_timeit(
#lambda: noane_mlpackage_obj.predict(
#{k: v.numpy()
#for k, v in self.inputs.items()}),
# n=13)
#speedup_factor = noane_latency / ane_latency
#logger.info(
#f"Speed-up factor from GPU+CPU to ANE is at least {speedup_factor:.2f}"
#)
#if speedup_factor < SANITY_CHECK_CPUGPU2ANE_SPEEDUP_FACTOR:
#logger.error(
#f"Expected speed-up (Expected >{SANITY_CHECK_CPUGPU2ANE_SPEEDUP_FACTOR:.2f}, observed {speedup_factor:.2f}) was not observed " \
#"between coremltools.ComputeUnit.ALL and coremltools.ComputeUnit.CPU_AND_GPU. The reason might be, among other things that your Mac " \
# "does not have Apple Silicon (e.g. M1) so ANE is unavailable for this test. This model will still work as efficiently as expected on " \
#"on devices with A14 and newer or M1 or newer chips."
# )
if __name__ == "__main__":
unittest.main()