Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
co63oc committed Aug 25, 2023
1 parent a8d6094 commit 7a1ff0f
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 0 deletions.
30 changes: 30 additions & 0 deletions paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
95 changes: 95 additions & 0 deletions tests/test_autocast.py
Original file line number Diff line number Diff line change
@@ -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"])
94 changes: 94 additions & 0 deletions tests/test_cpu_amp_autocast.py
Original file line number Diff line number Diff line change
@@ -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"])

0 comments on commit 7a1ff0f

Please sign in to comment.