From 7a1ff0f16468ba978e587e2fea7f2147919ddcd3 Mon Sep 17 00:00:00 2001 From: co63oc Date: Fri, 25 Aug 2023 20:03:51 +0800 Subject: [PATCH] Add tests --- paconvert/api_mapping.json | 30 +++++++++++ tests/test_autocast.py | 95 ++++++++++++++++++++++++++++++++++ tests/test_cpu_amp_autocast.py | 94 +++++++++++++++++++++++++++++++++ 3 files changed, 219 insertions(+) create mode 100644 tests/test_autocast.py create mode 100644 tests/test_cpu_amp_autocast.py diff --git a/paconvert/api_mapping.json b/paconvert/api_mapping.json index 42f88efe6..75b046fe3 100644 --- a/paconvert/api_mapping.json +++ b/paconvert/api_mapping.json @@ -2985,6 +2985,22 @@ "input": "x" } }, + "torch.autocast": { + "Matcher": "GenericMatcher", + "paddle_api": "paddle.amp.auto_cast", + "args_list": [ + "device_type", + "enabled", + "dtype", + "cache_enabled" + ], + "kwargs_change": { + "device_type": "", + "enabled": "enable", + "dtype": "dtype", + "cache_enabled": "" + } + }, "torch.autograd.Function": { "Matcher": "GenericMatcher", "paddle_api": "paddle.autograd.PyLayer" @@ -3587,6 +3603,20 @@ "correction": "ddof" } }, + "torch.cpu.amp.autocast": { + "Matcher": "GenericMatcher", + "paddle_api": "paddle.amp.auto_cast", + "args_list": [ + "enabled", + "dtype", + "cache_enabled" + ], + "kwargs_change": { + "enabled": "enable", + "dtype": "dtype", + "cache_enabled": "" + } + }, "torch.cross": { "Matcher": "GenericMatcher", "paddle_api": "paddle.cross", diff --git a/tests/test_autocast.py b/tests/test_autocast.py new file mode 100644 index 000000000..316b394ed --- /dev/null +++ b/tests/test_autocast.py @@ -0,0 +1,95 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import textwrap + +from apibase import APIBase + +obj = APIBase("torch.autocast") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], + [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + with torch.autocast(device_type='cpu', enabled=False): + result = x*x + + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], + [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + with torch.autocast(device_type='cpu'): + result = x*x + + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], + [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + with torch.autocast(device_type='cpu', enabled=True, dtype=torch.bfloat16): + result = x*x + + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], + [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + # with torch.autocast('cpu', dtype=torch.bfloat16, enabled=True, cache_enabled=None): + with torch.autocast('cpu', dtype=torch.bfloat16, enabled=True, cache_enabled=None): + result = x*x + + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], + [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + with torch.autocast(device_type='cpu', dtype=torch.bfloat16, enabled=True, cache_enabled=None): + result = x*x + + """ + ) + obj.run(pytorch_code, ["result"]) diff --git a/tests/test_cpu_amp_autocast.py b/tests/test_cpu_amp_autocast.py new file mode 100644 index 000000000..46659223c --- /dev/null +++ b/tests/test_cpu_amp_autocast.py @@ -0,0 +1,94 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import textwrap + +from apibase import APIBase + +obj = APIBase("torch.cpu.amp.autocast") + + +def test_case_1(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], + [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + with torch.cpu.amp.autocast(enabled=False): + result = x*x + + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_2(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], + [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + with torch.cpu.amp.autocast(): + result = x*x + + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_3(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], + [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + with torch.cpu.amp.autocast(dtype=torch.bfloat16, enabled=True): + result = x*x + + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_4(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], + [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + with torch.cpu.amp.autocast(True, torch.bfloat16, True): + result = x*x + + """ + ) + obj.run(pytorch_code, ["result"]) + + +def test_case_5(): + pytorch_code = textwrap.dedent( + """ + import torch + import torch.nn as nn + x = torch.tensor([[[-1.3020, -0.1005, 0.5766, 0.6351, -0.8893, 0.0253, -0.1756, 1.2913], + [-0.8833, -0.1369, -0.0168, -0.5409, -0.1511, -0.1240, -1.1870, -1.8816]]]) + with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloat16, cache_enabled=True): + result = x*x + + """ + ) + obj.run(pytorch_code, ["result"])