Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
146 changes: 8 additions & 138 deletions paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -8497,39 +8497,7 @@
}
},
"torch.optim.Adagrad": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.optimizer.Adagrad",
"min_input_args": 1,
"args_list": [
"params",
"lr",
"lr_decay",
"weight_decay",
"initial_accumulator_value",
"eps",
"foreach",
"*",
"maximize",
"differentiable",
"fused"
],
"unsupport_args": [
"lr_decay",
"foreach",
"maximize",
"differentiable",
"fused"
],
"kwargs_change": {
"params": "parameters",
"lr": "learning_rate",
"eps": "epsilon"
},
"paddle_default_kwargs": {
"weight_decay": 0.0,
"epsilon": 1e-10,
"learning_rate": 0.01
}
"Matcher": "ChangePrefixMatcher"
},
"torch.optim.Adam": {
"Matcher": "OptimAdamMatcher",
Expand Down Expand Up @@ -8569,39 +8537,7 @@
}
},
"torch.optim.AdamW": {
"Matcher": "OptimAdamMatcher",
"paddle_api": "paddle.optimizer.AdamW",
"min_input_args": 1,
"args_list": [
"params",
"lr",
"betas",
"eps",
"weight_decay",
"amsgrad",
"*",
"maximize",
"foreach",
"capturable",
"differentiable",
"fused"
],
"unsupport_args": [
"amsgrad",
"maximize",
"foreach",
"capturable",
"differentiable",
"fused"
],
"kwargs_change": {
"params": "parameters",
"lr": "learning_rate",
"eps": "epsilon"
},
"paddle_default_kwargs": {
"weight_decay": 0.0
}
"Matcher": "ChangePrefixMatcher"
},
"torch.optim.Adamax": {
"Matcher": "OptimAdamMatcher",
Expand Down Expand Up @@ -8683,13 +8619,7 @@
}
},
"torch.optim.Optimizer": {
"Matcher": "OptimOptimizerMatcher",
"paddle_api": "paddle.optimizer.Optimizer",
"min_input_args": 2,
"args_list": [
"params",
"defaults"
]
"Matcher": "ChangePrefixMatcher"
},
"torch.optim.Optimizer.add_param_group": {
"Matcher": "ChangeAPIMatcher",
Expand Down Expand Up @@ -8812,38 +8742,7 @@
]
},
"torch.optim.SGD": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.optimizer.SGD",
"min_input_args": 1,
"args_list": [
"params",
"lr",
"momentum",
"dampening",
"weight_decay",
"nesterov",
"*",
"maximize",
"foreach",
"differentiable",
"fused"
],
"unsupport_args": [
"momentum",
"dampening",
"nesterov",
"maximize",
"foreach",
"differentiable",
"fused"
],
"kwargs_change": {
"params": "parameters",
"lr": "learning_rate"
},
"paddle_default_kwargs": {
"weight_decay": 0.0
}
"Matcher": "ChangePrefixMatcher"
},
"torch.optim.lr_scheduler.ConstantLR": {
"Matcher": "ConstantLRMatcher",
Expand Down Expand Up @@ -10240,39 +10139,7 @@
"Matcher": "ChangePrefixMatcher"
},
"torch.utils.data.DataLoader": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.io.DataLoader",
"min_input_args": 1,
"args_list": [
"dataset",
"batch_size",
"shuffle",
"sampler",
"batch_sampler",
"num_workers",
"collate_fn",
"pin_memory",
"drop_last",
"timeout",
"worker_init_fn",
"multiprocessing_context",
"generator",
"*",
"prefetch_factor",
"persistent_workers",
"pin_memory_device"
],
"kwargs_change": {
"pin_memory": "",
"multiprocessing_context": "",
"generator": "",
"persistent_workers": "",
"pin_memory_device": ""
},
"unsupport_args": [
"sampler",
"prefetch_factor"
]
"Matcher": "ChangePrefixMatcher"
},
"torch.utils.data.Dataset": {
"Matcher": "ChangePrefixMatcher"
Expand Down Expand Up @@ -10321,6 +10188,9 @@
"torch.utils.data._utils.collate.default_collate": {
"Matcher": "ChangePrefixMatcher"
},
"torch.utils.data.dataloader.DataLoader": {
"Matcher": "ChangePrefixMatcher"
},
"torch.utils.data.dataloader.default_collate": {
"Matcher": "ChangePrefixMatcher"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import paddle


class PT_Optimizer(paddle.optimizer.Optimizer):
class PT_Optimizer(paddle.optim.Optimizer):
pass


Expand Down
56 changes: 56 additions & 0 deletions tests/distributed/utils_data_distributed_DistributedSampler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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 os

import numpy as np
import torch
import torch.distributed as dist
from torch.utils.data import Dataset
from torch.utils.data.distributed import DistributedSampler

dist.init_process_group(backend="nccl")
rank = dist.get_rank()
torch.cuda.set_device(rank)


class RandomDataset(Dataset):
def __init__(self, num_samples):
self.num_samples = num_samples

def __getitem__(self, idx):
image = np.random.random([16]).astype("float32")
label = np.random.randint(0, 9, (1,)).astype("int64")
return image, label

def __len__(self):
return self.num_samples


dataset = RandomDataset(16)
sampler = DistributedSampler(
dataset=dataset,
num_replicas=None,
rank=None,
shuffle=False,
seed=0,
drop_last=False,
)

data = [i for i in sampler]
data = torch.tensor(data).squeeze()
if rank == 0:
print(data)
torch.save(data, os.environ["DUMP_FILE"])
71 changes: 71 additions & 0 deletions tests/test_nn_modules_module__IncompatibleKeys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright (c) 2026 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.nn.modules.module._IncompatibleKeys")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
model = torch.nn.Linear(1, 2)
incompatible_keys = model.load_state_dict({"a": 2.0}, strict=False)
"""
)
obj.run(pytorch_code, ["incompatible_keys"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
model = torch.nn.Linear(2, 3)
missing, unexpected = model.load_state_dict({"b": -2.0}, strict=False)
"""
)
obj.run(pytorch_code, ["missing", "unexpected"])


def test_case_3():
pytorch_code = textwrap.dedent(
"""
import torch
model = torch.nn.Linear(6, 2)
result = model.load_state_dict({"c": -6.0}, strict=False)
result_0 = result[0]
result_1 = result[1]
result_missing = result.missing_keys
result_unexpected = result.unexpected_keys
"""
)
obj.run(
pytorch_code, ["result_0", "result_1", "result_missing", "result_unexpected"]
)


def test_case_4():
pytorch_code = textwrap.dedent(
"""
import torch
from collections import namedtuple
model = torch.nn.Linear(6, 2)
result = model.load_state_dict({"c": -6.0}, strict=False)
is_tuple = isinstance(result, tuple)
"""
)
obj.run(pytorch_code, ["is_tuple"])
24 changes: 6 additions & 18 deletions tests/test_optim_Adagrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,37 +82,25 @@ def test_case_7():
def test_case_8():
pytorch_code = textwrap.dedent(
generate_optimizer_test_code(
"torch.optim.Adagrad(params=conv.parameters(), lr=0.01, lr_decay=0, weight_decay=0, initial_accumulator_value=0, eps=1e-10, foreach=None, maximize=False, differentiable=False)"
"torch.optim.Adagrad(params=conv.parameters(), lr=0.01, lr_decay=0.1, weight_decay=0, initial_accumulator_value=0, eps=1e-10, foreach=None, maximize=False, differentiable=False)"
)
)
obj.run(
pytorch_code,
unsupport=True,
reason="`lr_decay`, `foreach`, 'maximize` and `differentiable` is not supported.",
)
obj.run(pytorch_code, ["result"])


def test_case_9():
pytorch_code = textwrap.dedent(
generate_optimizer_test_code(
"torch.optim.Adagrad(conv.parameters(), 0.01, 0, 0, 0, 1e-10, None, maximize=False, differentiable=False)"
"torch.optim.Adagrad(conv.parameters(), 0.01, 0.1, 0.01, 0, 1e-10, None, maximize=True, differentiable=False)"
)
)
obj.run(
pytorch_code,
unsupport=True,
reason="`lr_decay`, `foreach`, 'maximize` and `differentiable` is not supported.",
)
obj.run(pytorch_code, ["result"])


def test_case_10():
pytorch_code = textwrap.dedent(
generate_optimizer_test_code(
"torch.optim.Adagrad(params=conv.parameters(), lr_decay=0, lr=0.01, initial_accumulator_value=0, weight_decay=0, eps=1e-10, foreach=None, maximize=False, differentiable=False)"
"torch.optim.Adagrad(params=conv.parameters(), lr_decay=0, lr=0.01, initial_accumulator_value=0, weight_decay=0, eps=1e-10, foreach=None, maximize=True, differentiable=False)"
)
)
obj.run(
pytorch_code,
unsupport=True,
reason="`lr_decay`, `foreach`, 'maximize` and `differentiable` is not supported.",
)
obj.run(pytorch_code, ["result"])
Loading
Loading