Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Hackathon 7th No.36】为 Paddle 代码转换工具新增 API 转换规则(第 3 组)-part #484

Merged
merged 28 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -4869,6 +4869,28 @@
"other": "y"
}
},
"torch.blackman_window": {
"Matcher": "BHHWindowMatcher",
"paddle_api": "paddle.audio.functional.get_window",
"min_input_args": 1,
"args_list": [
"window_length",
"periodic",
"*",
"dtype",
"layout",
"device",
"requires_grad"
],
"kwargs_change": {
"window_length": "win_length",
"periodic": "fftbins",
"dtype": "dtype"
},
"paddle_default_kwargs": {
"dtype": "paddle.float64"
}
},
"torch.bmm": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.bmm",
Expand Down Expand Up @@ -7311,6 +7333,17 @@
"ndarray": "data"
}
},
"torch.frombuffer": {
"Matcher": "FromBufferMatcher",
"args_list": [
"buffer",
"*",
"dtype",
"count",
"offset",
"requires_grad"
]
},
"torch.full": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.full",
Expand Down Expand Up @@ -7416,6 +7449,12 @@
"Matcher": "GenericMatcher",
"paddle_api": "paddle.get_default_dtype"
},
"torch.get_num_interop_threads": {
"Matcher": "GetNumInteropThreadsMatcher"
},
"torch.get_num_threads": {
"Matcher": "GetNumThreadsMatcher"
},
"torch.get_rng_state": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.get_rng_state",
Expand Down Expand Up @@ -7454,6 +7493,56 @@
"out"
]
},
"torch.hamming_window": {
"Matcher": "BHHWindowMatcher",
"paddle_api": "paddle.audio.functional.get_window",
"min_input_args": 1,
"args_list": [
"window_length",
"periodic",
"alpha",
"beta",
"*",
"dtype",
"layout",
"device",
"requires_grad"
],
"unsupport_args": [
"alpha",
"beta"
],
"kwargs_change": {
"window_length": "win_length",
"periodic": "fftbins",
"dtype": "dtype"
},
"paddle_default_kwargs": {
"dtype": "paddle.float64"
}
},
"torch.hann_window": {
"Matcher": "BHHWindowMatcher",
"paddle_api": "paddle.audio.functional.get_window",
"min_input_args": 1,
"args_list": [
"window_length",
"periodic",
"*",
"dtype",
"layout",
"device",
"requires_grad"
],
"kwargs_change": {
"window_length": "win_length",
"periodic": "fftbins",
"dtype": "dtype"
},
"paddle_default_kwargs": {
"dtype": "paddle.float64"
}
},
"torch.heaviside": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.heaviside",
Expand Down Expand Up @@ -14518,6 +14607,20 @@
"mode"
]
},
"torch.set_num_interop_threads": {
"Matcher": "SetNumInteropThreadsMatcher",
"min_input_args": 1,
"args_list": [
"int"
]
},
"torch.set_num_threads": {
"Matcher": "SetNumThreadsMatcher",
"min_input_args": 1,
"args_list": [
"int"
]
},
"torch.set_printoptions": {
"Matcher": "SetPrintOptionsMatcher",
"paddle_api": "paddle.set_printoptions",
Expand Down
102 changes: 102 additions & 0 deletions paconvert/api_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4792,3 +4792,105 @@ def generate_code(self, kwargs):
self.kwargs_to_str(kwargs_bin_edges),
)
return code


class BHHWindowMatcher(BaseMatcher):
enkilee marked this conversation as resolved.
Show resolved Hide resolved
def generate_code(self, kwargs):
new_kwargs = {}
if "blackman_window" in self.torch_api:
new_kwargs["window"] = "'blackman'"
if "hann_window" in self.torch_api:
new_kwargs["window"] = "'hann'"
if "hamming_window" in self.torch_api:
new_kwargs["window"] = "'hamming'"
if "periodic" in kwargs:
new_kwargs["fftbins"] = f"not {kwargs.pop('periodic')}"
new_kwargs.update(kwargs)
return GenericMatcher.generate_code(self, new_kwargs)


class FromBufferMatcher(BaseMatcher):
def generate_aux_code(self):
CODE_TEMPLATE = textwrap.dedent(
"""
import numpy
enkilee marked this conversation as resolved.
Show resolved Hide resolved
def _frombuffer(**kwargs):
buffer = kwargs.pop("buffer")
return paddle.to_tensor(numpy.frombuffer(numpy.array(buffer), dtype = numpy.int32))
"""
)
return CODE_TEMPLATE

def generate_code(self, kwargs):
self.write_aux_code()
return "paddle_aux._frombuffer({})".format(self.kwargs_to_str(kwargs))
enkilee marked this conversation as resolved.
Show resolved Hide resolved


class GetNumThreadsMatcher(BaseMatcher):
def generate_code(self, kwargs):
API_TEMPLATE = textwrap.dedent(
"""
import multiprocessing
multiprocessing.cpu_count()
"""
)
code = API_TEMPLATE.format()
return code


class GetNumInteropThreadsMatcher(BaseMatcher):
def generate_code(self, kwargs):
API_TEMPLATE = textwrap.dedent(
"""
import os
os.environ['OMP_NUM_THREADS']
"""
)
code = API_TEMPLATE.format()
return code


class SetNumInteropThreadsMatcher(BaseMatcher):
def generate_aux_code(self):
CODE_TEMPLATE = textwrap.dedent(
"""
import os
def _set_num_interop_threads(int):
os.environ['OMP_NUM_THREADS'] = ("int")
"""
)
return CODE_TEMPLATE

def generate_code(self, kwargs):
enkilee marked this conversation as resolved.
Show resolved Hide resolved
self.write_aux_code()
API_TEMPLATE = textwrap.dedent(
"""
paddle_aux._set_num_interop_threads({})
"""
)
code = API_TEMPLATE.format(kwargs["int"])

return code


class SetNumThreadsMatcher(BaseMatcher):
def generate_aux_code(self):
enkilee marked this conversation as resolved.
Show resolved Hide resolved
CODE_TEMPLATE = textwrap.dedent(
"""
import os
def _set_num_threads(int):
os.environ['CPU_NUM'] = ("int")
"""
)
return CODE_TEMPLATE

def generate_code(self, kwargs):
self.write_aux_code()
API_TEMPLATE = textwrap.dedent(
"""
paddle_aux._set_num_threads({})
"""
)
code = API_TEMPLATE.format(kwargs["int"])

return code
79 changes: 79 additions & 0 deletions tests/test_blackman_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright (c) 2024 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.blackman_window")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
result = torch.blackman_window(5)
"""
)
obj.run(pytorch_code, ["result"], check_value=False, check_dtype=False)


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
result = torch.blackman_window(5, dtype=torch.float64)
"""
)
obj.run(pytorch_code, ["result"], check_value=False)


def test_case_3():
pytorch_code = textwrap.dedent(
"""
import torch
result = torch.blackman_window(5, dtype=torch.float64, requires_grad=True)
"""
)
obj.run(pytorch_code, ["result"], check_value=False)


def test_case_4():
pytorch_code = textwrap.dedent(
"""
import torch
result = torch.blackman_window(5, dtype=torch.float64, layout=torch.strided, requires_grad=True)
"""
)
obj.run(pytorch_code, ["result"], check_value=False)


def test_case_5():
pytorch_code = textwrap.dedent(
"""
import torch
result = torch.blackman_window(5, dtype=torch.float64, layout=torch.strided, device=torch.device('cpu'), requires_grad=True)
"""
)
obj.run(pytorch_code, ["result"], check_value=False)
enkilee marked this conversation as resolved.
Show resolved Hide resolved


def test_case_6():
pytorch_code = textwrap.dedent(
"""
import torch
result = torch.blackman_window(5, periodic=False, dtype=torch.float64)
"""
)
obj.run(pytorch_code, ["result"], check_value=False)
43 changes: 43 additions & 0 deletions tests/test_frombuffer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright (c) 2024 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.frombuffer", is_aux_api=True)


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
import array
a = array.array('i', [1, 2, 3])
result = torch.frombuffer(a, dtype=torch.int32)
enkilee marked this conversation as resolved.
Show resolved Hide resolved
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
import array
a =array.array('b', [-1, 0, 0, 0])
result = torch.frombuffer(a, dtype=torch.int32)
"""
)
obj.run(pytorch_code, ["result"])
29 changes: 29 additions & 0 deletions tests/test_get_num_interop_threads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2024 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.get_num_interop_threads")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
result = torch.get_num_threads()
"""
)
obj.run(pytorch_code, ["result"], check_value=False)
Loading