Skip to content

Commit f07ca44

Browse files
authored
[AP] Enhance builtin modules and add apy code. (#72698)
* [AP] Enhance builtin modules and add apy code. * Minor modification.
1 parent 2c7a583 commit f07ca44

17 files changed

+375
-13
lines changed

paddle/ap/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ if(WITH_ONEDNN)
33
list(APPEND AP_COMMON_DEPS onednn)
44
endif()
55

6-
file(GLOB_RECURSE axpr_srcs "src/axpr/*.cc")
6+
file(GLOB_RECURSE axpr_srcs "src/axpr/*.cc" "src/fs/*.cc")
77
set(axpr_deps common)
88
cc_library(
99
axpr

paddle/ap/include/axpr/builtin_frame_util.h

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "paddle/ap/include/axpr/builtin_symbol.h"
2121
#include "paddle/ap/include/axpr/exception_method_class.h"
2222
#include "paddle/ap/include/axpr/module_mgr_helper.h"
23+
#include "paddle/ap/include/fs/builtin_functions.h"
2324

2425
namespace ap::axpr {
2526

@@ -38,9 +39,14 @@ void VisitEachBuiltinFrameAttr(const YieldT& Yield) {
3839
Yield("setattr", &SetAttr);
3940
ForEachExceptionConstructor(Yield);
4041
Yield("raise", &Raise);
42+
Yield("__builtin__raise", &Raise);
4143
Yield("__builtin_not__", &BuiltinNot);
4244

45+
Yield("__builtin__sorted", &Sorted);
4346
Yield("__builtin__foreach", &ForEach);
47+
Yield("__builtin__registry", &GetRegistry);
48+
Yield("__builtin__dirname", &fs::DirName);
49+
Yield("__builtin__basename", &fs::BaseName);
4450

4551
auto YieldTwice = [&](const auto& name, const auto& value) {
4652
Yield(name, value);

paddle/ap/include/axpr/module_mgr.h

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class ModuleMgr {
7777
return iter->second;
7878
}
7979
auto frame_object = std::make_shared<AttrMapImpl<SerializableValue>>();
80+
frame_object->Set("__file__", file_path);
8081
const auto& frame =
8182
Frame<SerializableValue>::Make(circlable_ref_list(), frame_object);
8283
ADT_LET_CONST_REF(lambda, GetLambdaByFilePath(file_path));

paddle/ap/include/axpr/mutable_global_environment.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class MutableGlobalEnvironment : public Environment<ValueT> {
7474
}
7575

7676
bool IsTempVar(const std::string& var) const {
77-
static std::string tmp_var_prefix("__");
77+
static std::string tmp_var_prefix("___");
7878
return var.substr(0, tmp_var_prefix.size()) == tmp_var_prefix;
7979
}
8080

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
#include "paddle/ap/include/adt/adt.h"
17+
#include "paddle/ap/include/axpr/core_expr.h"
18+
#include "paddle/ap/include/axpr/value.h"
19+
20+
namespace ap::fs {
21+
22+
adt::Result<axpr::Value> DirName(const axpr::Value&,
23+
const std::vector<axpr::Value>& args);
24+
25+
adt::Result<axpr::Value> BaseName(const axpr::Value&,
26+
const std::vector<axpr::Value>& args);
27+
28+
void ForceLink();
29+
30+
} // namespace ap::fs

paddle/ap/src/axpr/fs.cc

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <functional>
16+
#include <sstream>
17+
#include <stdexcept>
18+
#include "paddle/ap/include/axpr/abstract_list.h"
19+
#include "paddle/ap/include/axpr/bool_helper.h"
20+
#include "paddle/ap/include/axpr/bool_int_double_helper.h"
21+
#include "paddle/ap/include/axpr/builtin_frame_util.h"
22+
#include "paddle/ap/include/axpr/builtin_high_order_func_type.h"
23+
#include "paddle/ap/include/axpr/callable_helper.h"
24+
#include "paddle/ap/include/axpr/data_value_util.h"
25+
#include "paddle/ap/include/axpr/exception_method_class.h"
26+
#include "paddle/ap/include/axpr/interpreter.h"
27+
#include "paddle/ap/include/axpr/lambda_expr_builder.h"
28+
#include "paddle/ap/include/axpr/method_class.h"
29+
#include "paddle/ap/include/axpr/string_util.h"
30+
#include "paddle/ap/include/axpr/value.h"
31+
#include "paddle/ap/include/axpr/value_method_class.h"
32+
#include "paddle/ap/include/fs/builtin_functions.h"
33+
#include "paddle/ap/include/memory/guard.h"
34+
35+
namespace ap::axpr {
36+
37+
adt::Result<axpr::Value> DirName(const axpr::Value&,
38+
const std::vector<axpr::Value>& args) {
39+
ADT_CHECK(args.size() == 1)
40+
<< adt::errors::TypeError{"dirname() takes 1 argument, but " +
41+
std::to_string(args.size()) + "were given."};
42+
ADT_LET_CONST_REF(filepath, args.at(0).template CastTo<std::string>());
43+
std::size_t pos = filepath.find_last_of("/\\");
44+
if (pos == std::string::npos) return std::string{};
45+
return filepath.substr(0, pos);
46+
}
47+
48+
adt::Result<axpr::Value> BaseName(const axpr::Value&,
49+
const std::vector<axpr::Value>& args) {
50+
ADT_CHECK(args.size() == 1)
51+
<< adt::errors::TypeError{"basename() takes 1 argument, but " +
52+
std::to_string(args.size()) + "were given."};
53+
ADT_LET_CONST_REF(filepath, args.at(0).template CastTo<std::string>());
54+
std::size_t pos = filepath.find_last_of("/\\");
55+
if (pos == std::string::npos) return filepath;
56+
return filepath.substr(pos + 1);
57+
}
58+
59+
void ForceLink() {}
60+
61+
} // namespace ap::axpr

paddle/ap/src/axpr/interpreter.cc

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "paddle/ap/include/axpr/interpreter.h"
1616
#include "paddle/ap/include/axpr/cps_interpreter.h"
17+
#include "paddle/ap/include/fs/builtin_functions.h"
1718

1819
namespace ap::axpr {
1920

paddle/ap/src/fs/builtin_functions.cc

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/ap/include/fs/builtin_functions.h"
16+
#include <functional>
17+
#include <sstream>
18+
#include <stdexcept>
19+
#include "paddle/ap/include/axpr/abstract_list.h"
20+
#include "paddle/ap/include/axpr/bool_helper.h"
21+
#include "paddle/ap/include/axpr/bool_int_double_helper.h"
22+
#include "paddle/ap/include/axpr/builtin_frame_util.h"
23+
#include "paddle/ap/include/axpr/builtin_high_order_func_type.h"
24+
#include "paddle/ap/include/axpr/callable_helper.h"
25+
#include "paddle/ap/include/axpr/data_value_util.h"
26+
#include "paddle/ap/include/axpr/exception_method_class.h"
27+
#include "paddle/ap/include/axpr/interpreter.h"
28+
#include "paddle/ap/include/axpr/lambda_expr_builder.h"
29+
#include "paddle/ap/include/axpr/method_class.h"
30+
#include "paddle/ap/include/axpr/string_util.h"
31+
#include "paddle/ap/include/axpr/value.h"
32+
#include "paddle/ap/include/axpr/value_method_class.h"
33+
#include "paddle/ap/include/memory/guard.h"
34+
35+
namespace ap::fs {
36+
37+
adt::Result<axpr::Value> DirName(const axpr::Value&,
38+
const std::vector<axpr::Value>& args) {
39+
ADT_CHECK(args.size() == 1)
40+
<< adt::errors::TypeError{"dirname() takes 1 argument, but " +
41+
std::to_string(args.size()) + "were given."};
42+
ADT_LET_CONST_REF(filepath, args.at(0).template CastTo<std::string>());
43+
std::size_t pos = filepath.find_last_of("/\\");
44+
if (pos == std::string::npos) return std::string{};
45+
return filepath.substr(0, pos);
46+
}
47+
48+
adt::Result<axpr::Value> BaseName(const axpr::Value&,
49+
const std::vector<axpr::Value>& args) {
50+
ADT_CHECK(args.size() == 1)
51+
<< adt::errors::TypeError{"basename() takes 1 argument, but " +
52+
std::to_string(args.size()) + "were given."};
53+
ADT_LET_CONST_REF(filepath, args.at(0).template CastTo<std::string>());
54+
std::size_t pos = filepath.find_last_of("/\\");
55+
if (pos == std::string::npos) return filepath;
56+
return filepath.substr(pos + 1);
57+
}
58+
59+
} // namespace ap::fs

paddle/ap/src/paddle/phi/ap_variadic_kernel.cc

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "paddle/common/enforce.h"
2222

2323
#include "paddle/ap/include/axpr/anf_expr_util.h"
24+
#include "paddle/ap/include/fs/builtin_functions.h"
2425
#include "paddle/ap/include/kernel_dispatch/builtin_frame_util.h"
2526
#include "paddle/ap/include/paddle/phi/kernel_define_helper.h"
2627
#include "paddle/ap/include/paddle/phi/kernel_dispatch_helper.h"

python/paddle/apy/sys/__builtin__.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
DataType = __builtin__DataType # noqa: F821
16+
DataValue = __builtin__DataValue # noqa: F821
17+
PointerType = __builtin__PointerType # noqa: F821
18+
PointerValue = __builtin__PointerValue # noqa: F821
19+
MutableList = __builtin__MutableList # noqa: F821
20+
OrderedDict = __builtin__OrderedDict # noqa: F821
21+
MutableOrderedDict = __builtin__MutableOrderedDict # noqa: F821
22+
AttrMap = __builtin__AttrMap # noqa: F821
23+
SerializableAttrMap = __builtin__BuiltinSerializableAttrMap # noqa: F821
24+
25+
_raise = __builtin__raise # noqa: F821
26+
27+
foreach = __builtin__foreach # noqa: F821
28+
range = __builtin__range # noqa: F821
29+
map = __builtin__map # noqa: F821
30+
reduce = __builtin__reduce # noqa: F821
31+
filter = __builtin__filter # noqa: F821
32+
zip = __builtin__zip # noqa: F821
33+
flat_map = __builtin__flat_map # noqa: F821
34+
apply = __builtin__apply # noqa: F821
35+
replace_or_trim_left_comma = __builtin__replace_or_trim_left_comma # noqa: F821
36+
37+
sorted = __builtin__sorted # noqa: F821
38+
39+
dirname = __builtin__dirname # noqa: F821
40+
basename = __builtin__basename # noqa: F821
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import __builtin__
16+
17+
18+
class RegistryEntry:
19+
20+
def __init__(self):
21+
self.__tag_name__ = None
22+
self.__nice__ = None
23+
self.__values__ = __builtin__.MutableList()
24+
self.__child_register_item_name2value__ = (
25+
__builtin__.MutableOrderedDict()
26+
)
27+
28+
# tag_name: str
29+
# nice: int
30+
def __getattr__(self, attrname):
31+
def contains():
32+
return self.__child_register_item_name2value__.contains(attrname)
33+
34+
def find():
35+
return self.__child_register_item_name2value__[attrname]
36+
37+
def create():
38+
register_entry = RegistryEntry()
39+
self.__child_register_item_name2value__[attrname] = register_entry
40+
return register_entry
41+
42+
return find() if contains() else create()
43+
44+
def __call__(self, tag_name, nice):
45+
registry_obj = RegistryObject(tag_name, nice)
46+
self.__values__.append(registry_obj)
47+
return RegisterItemDecorator(registry_obj)
48+
49+
50+
class RegistryObject:
51+
52+
def __init__(self, tag_name, nice):
53+
self.tag_name = tag_name
54+
self.nice = nice
55+
self.value = None
56+
57+
58+
class RegisterItemDecorator:
59+
60+
def __init__(self, register_obj):
61+
self.register_obj = register_obj
62+
63+
def __call__(self, value):
64+
self.register_obj.value = value
65+
return value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def GetGroupedTrivialOpNames():
17+
return [
18+
"pd_op.sin",
19+
"pd_op.add",
20+
"pd_op.relu",
21+
]

python/paddle/apy/sys/ap.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import __builtin__
16+
17+
DataType = __builtin__.DataType
18+
DataValue = __builtin__.DataValue
19+
PointerType = __builtin__.PointerType
20+
PointerValue = __builtin__.PointerValue
21+
MutableList = __builtin__.MutableList
22+
OrderedDict = __builtin__.OrderedDict
23+
MutableOrderedDict = __builtin__.MutableOrderedDict
24+
AttrMap = __builtin__.AttrMap
25+
SerializableAttrMap = __builtin__.SerializableAttrMap
26+
27+
_raise = __builtin__._raise
28+
29+
foreach = __builtin__.foreach
30+
range = __builtin__.range
31+
map = __builtin__.map
32+
reduce = __builtin__.reduce
33+
filter = __builtin__.filter
34+
zip = __builtin__.zip
35+
flat_map = __builtin__.flat_map
36+
apply = __builtin__.apply
37+
replace_or_trim_left_comma = __builtin__.replace_or_trim_left_comma
38+
39+
registry = __builtin__registry # noqa: F821
40+
41+
sorted = __builtin__.sorted
42+
43+
dirname = __builtin__.dirname
44+
basename = __builtin__.basename

0 commit comments

Comments
 (0)