未验证 提交 cf825da3 编写于 作者: W Wilber 提交者: GitHub

test_convert_to_mixed_precision.py use tempfile for temporary models/params (#48819)

上级 9aef0e3e
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import os
import tempfile
import unittest import unittest
import paddle import paddle
...@@ -30,69 +32,56 @@ from paddle.vision.models import resnet50 ...@@ -30,69 +32,56 @@ from paddle.vision.models import resnet50
'should compile with cuda.', 'should compile with cuda.',
) )
class TestConvertToMixedPrecision(unittest.TestCase): class TestConvertToMixedPrecision(unittest.TestCase):
def test_convert_to_fp16(self): def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
model = resnet50(True) model = resnet50(True)
net = to_static( net = to_static(
model, input_spec=[InputSpec(shape=[None, 3, 224, 224], name='x')] model, input_spec=[InputSpec(shape=[None, 3, 224, 224], name='x')]
) )
paddle.jit.save(net, 'resnet50/inference') paddle.jit.save(
convert_to_mixed_precision( net, os.path.join(self.temp_dir.name, 'resnet50/inference')
'resnet50/inference.pdmodel',
'resnet50/inference.pdiparams',
'mixed/inference.pdmodel',
'mixed/inference.pdiparams',
PrecisionType.Half,
PlaceType.GPU,
True,
) )
def test_convert_to_fp16_with_fp16_input(self): def tearDown(self):
model = resnet50(True) self.temp_dir.cleanup()
net = to_static(
model, input_spec=[InputSpec(shape=[None, 3, 224, 224], name='x')]
)
paddle.jit.save(net, 'resnet50/inference')
convert_to_mixed_precision(
'resnet50/inference.pdmodel',
'resnet50/inference.pdiparams',
'mixed1/inference.pdmodel',
'mixed1/inference.pdiparams',
PrecisionType.Half,
PlaceType.GPU,
False,
)
def test_convert_to_fp16_with_blacklist(self): def test_convert_to_mixed_precision(self):
model = resnet50(True) mixed_precision_options = [
net = to_static(
model, input_spec=[InputSpec(shape=[None, 3, 224, 224], name='x')]
)
paddle.jit.save(net, 'resnet50/inference')
convert_to_mixed_precision(
'resnet50/inference.pdmodel',
'resnet50/inference.pdiparams',
'mixed2/inference.pdmodel',
'mixed2/inference.pdiparams',
PrecisionType.Half, PrecisionType.Half,
PlaceType.GPU, PrecisionType.Half,
False, PrecisionType.Half,
set('conv2d'), PrecisionType.Bfloat16,
) ]
keep_io_types_options = [True, False, False, True]
black_list_options = [set(), set(), set(['conv2d']), set()]
def test_convert_to_bf16(self): test_configs = zip(
model = resnet50(True) mixed_precision_options, keep_io_types_options, black_list_options
net = to_static(
model, input_spec=[InputSpec(shape=[None, 3, 224, 224], name='x')]
) )
paddle.jit.save(net, 'resnet50/inference') for mixed_precision, keep_io_types, black_list in test_configs:
config = f'mixed_precision={mixed_precision}-keep_io_types={keep_io_types}-black_list={black_list}'
with self.subTest(
mixed_precision=mixed_precision,
keep_io_types=keep_io_types,
black_list=black_list,
):
convert_to_mixed_precision( convert_to_mixed_precision(
'resnet50/inference.pdmodel', os.path.join(
'resnet50/inference.pdiparams', self.temp_dir.name, 'resnet50/inference.pdmodel'
'mixed3/inference.pdmodel', ),
'mixed3/inference.pdiparams', os.path.join(
PrecisionType.Bfloat16, self.temp_dir.name, 'resnet50/inference.pdiparams'
PlaceType.GPU, ),
True, os.path.join(
self.temp_dir.name, f'{config}/inference.pdmodel'
),
os.path.join(
self.temp_dir.name, f'{config}/inference.pdiparams'
),
backend=PlaceType.GPU,
mixed_precision=mixed_precision,
keep_io_types=keep_io_types,
black_list=black_list,
) )
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册