Skip to content

Commit c36c719

Browse files
authored
[Autogen Tensor API] Autogen tensor_api.cc (#50642)
* polish tensor operants implementation * change year, 2021->2023 * autogen tensor.h and tensor_api.cc * polish CMakeLists logic * cancel tensor.h auto-gen * clean useless parameter * delete tensor_api.cc
1 parent 43622a2 commit c36c719

File tree

5 files changed

+129
-66
lines changed

5 files changed

+129
-66
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ paddle/phi/api/lib/operants_manager.cc
2121
paddle/phi/api/lib/sparse_api.cc
2222
paddle/phi/api/lib/strings_api.cc
2323
paddle/phi/api/lib/sparse_bw_api.cc
24+
paddle/phi/api/lib/tensor_api.cc
2425
paddle/phi/api/lib/tensor_operants.cc
2526
paddle/phi/extension.h
2627
paddle/phi/include/*

paddle/phi/api/include/tensor.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,13 @@ class PADDLE_API Tensor final {
645645
* in the development of new dygraph. It may be removed in the future.
646646
*/
647647
std::string name_{""};
648+
649+
// Tensor C++ APIs
650+
// Example: Tensor add(const Tensor& other) const;
651+
Tensor add(const Tensor& y) const;
652+
Tensor divide(const Tensor& y) const;
653+
Tensor multiply(const Tensor& y) const;
654+
Tensor subtract(const Tensor& y) const;
648655
};
649656

650657
} // namespace experimental

paddle/phi/api/lib/CMakeLists.txt

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ set(tensor_gen_file
102102
${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/tensor_gen.py)
103103
set(operants_base_file
104104
${CMAKE_SOURCE_DIR}/paddle/phi/api/include/operants_base.h)
105+
set(tensor_api_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/tensor_api.cc)
105106
set(phi_tensor_operants_header_file
106107
${CMAKE_SOURCE_DIR}/paddle/phi/api/include/tensor_operants.h)
107108
set(phi_tensor_operants_source_file
@@ -111,6 +112,7 @@ set(operants_manager_header_file
111112
set(operants_manager_source_file
112113
${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/operants_manager.cc)
113114
set(operants_base_file_tmp ${operants_base_file}.tmp)
115+
set(tensor_api_source_file_tmp ${tensor_api_source_file}.tmp)
114116
set(phi_tensor_operants_header_file_tmp ${phi_tensor_operants_header_file}.tmp)
115117
set(phi_tensor_operants_source_file_tmp ${phi_tensor_operants_source_file}.tmp)
116118
set(operants_manager_header_file_tmp ${operants_manager_header_file}.tmp)
@@ -235,35 +237,43 @@ add_custom_command(
235237
VERBATIM)
236238

237239
# generate tensor and tensor operants file
238-
add_custom_command(
239-
OUTPUT ${operants_base_file} ${phi_tensor_operants_header_file}
240-
${phi_tensor_operants_source_file} ${operants_manager_header_file}
241-
${operants_manager_source_file}
242-
COMMAND ${PYTHON_EXECUTABLE} -m pip install pyyaml
240+
message("create or copy auto-geneated tensor files")
241+
execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install pyyaml)
242+
execute_process(
243+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator
243244
COMMAND
244245
${PYTHON_EXECUTABLE} ${tensor_gen_file} --api_yaml_path ${api_yaml_file}
245246
${legacy_api_yaml_file} --operants_base_path ${operants_base_file_tmp}
247+
--tensor_api_source_path ${tensor_api_source_file_tmp}
246248
--phi_tensor_operants_header_path ${phi_tensor_operants_header_file_tmp}
247249
--phi_tensor_operants_source_path ${phi_tensor_operants_source_file_tmp}
248250
--operants_manager_header_path ${operants_manager_header_file_tmp}
249251
--operants_manager_source_path ${operants_manager_source_file_tmp}
250-
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${operants_base_file_tmp}
251-
${operants_base_file}
252-
COMMAND
253-
${CMAKE_COMMAND} -E copy_if_different ${phi_tensor_operants_header_file_tmp}
254-
${phi_tensor_operants_header_file}
255-
COMMAND
256-
${CMAKE_COMMAND} -E copy_if_different ${phi_tensor_operants_source_file_tmp}
257-
${phi_tensor_operants_source_file}
258-
COMMAND ${CMAKE_COMMAND} -E copy_if_different
259-
${operants_manager_header_file_tmp} ${operants_manager_header_file}
260-
COMMAND ${CMAKE_COMMAND} -E copy_if_different
261-
${operants_manager_source_file_tmp} ${operants_manager_source_file}
262-
COMMENT
263-
"copy_if_different ${phi_tensor_operants_header_file} ${phi_tensor_operants_source_file}"
264-
DEPENDS ${api_yaml_file} ${legacy_api_yaml_file} ${tensor_gen_file}
265-
${api_gen_base} ${api_gen_file}
266-
VERBATIM)
252+
RESULT_VARIABLE _result)
253+
if(${_result})
254+
message(FATAL_ERROR "tensor codegen failed, exiting.")
255+
endif()
256+
257+
set(generated_tensor_files
258+
"${operants_base_file}" "${tensor_api_source_file}"
259+
"${phi_tensor_operants_header_file}" "${phi_tensor_operants_source_file}"
260+
"${operants_manager_header_file}" "${operants_manager_source_file}")
261+
262+
foreach(generated_tensor_file ${generated_tensor_files})
263+
if(EXISTS "${generated_tensor_file}.tmp" AND EXISTS
264+
"${generated_tensor_file}")
265+
execute_process(
266+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
267+
"${generated_tensor_file}.tmp" "${generated_tensor_file}")
268+
message(
269+
"copy if different ${generated_tensor_file}.tmp ${generated_tensor_file}")
270+
elseif(EXISTS "${generated_tensor_file}.tmp")
271+
execute_process(
272+
COMMAND ${CMAKE_COMMAND} -E copy "${generated_tensor_file}.tmp"
273+
"${generated_tensor_file}")
274+
message("copy ${generated_tensor_file}.tmp ${generated_tensor_file}")
275+
endif()
276+
endforeach()
267277

268278
cc_library(
269279
op_meta_info
@@ -388,5 +398,5 @@ cc_library(
388398
DEPS phi_enforce)
389399
cc_library(
390400
tensor_api
391-
SRCS tensor_api.cc
401+
SRCS ${tensor_api_source_file}
392402
DEPS operants_manager)

paddle/phi/api/lib/tensor_api.cc

Lines changed: 0 additions & 43 deletions
This file was deleted.

paddle/phi/api/yaml/generator/tensor_gen.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,43 @@ class TensorOperantsBase {
5858
5959
"""
6060

61+
tensor_api_source_include = """// Generated by paddle/phi/api/yaml/generator/tensor_gen.py
62+
63+
#include "paddle/phi/api/include/tensor.h"
64+
65+
#include "paddle/phi/api/include/operants_manager.h"
66+
67+
"""
68+
69+
tensor_api_source_start = """
70+
namespace paddle {
71+
72+
namespace experimental {
73+
74+
Tensor Tensor::operator+(const Tensor &other) const {
75+
return add(other);
76+
}
77+
78+
Tensor Tensor::operator-(const Tensor &other) const {
79+
return subtract(other);
80+
}
81+
82+
Tensor Tensor::operator*(const Tensor &other) const {
83+
return multiply(other);
84+
}
85+
86+
Tensor Tensor::operator/(const Tensor &other) const {
87+
return divide(other);
88+
}
89+
"""
90+
91+
92+
tensor_api_source_end = """
93+
} // namespace experimental
94+
} // namespace paddle
95+
96+
"""
97+
6198

6299
operants_header_include = """// Generated by paddle/phi/api/yaml/generator/tensor_gen.py
63100
@@ -231,6 +268,40 @@ def gene_operants_base(self):
231268
else:
232269
return f"""
233270
{indent}virtual {self.get_return_type(inplace_flag=True)} {api_func_name}({self.get_declare_args(inplace_flag=True)}) = 0;
271+
"""
272+
273+
def get_define_args_without_first_tensor(self, inplace_flag=False):
274+
# NOTE(HongyuJia): consider vector<Tensor> becomes first input argument.
275+
define_args = self.get_input_tensor_args(inplace_flag)
276+
assert (
277+
len(define_args) > 1
278+
), "Can't use tensor api without Tensor inputs"
279+
for name in self.attrs['names']:
280+
define_args.append(self.attrs['attr_info'][name][0] + ' ' + name)
281+
# remove first Tensor argument
282+
return ", ".join(define_args[1:])
283+
284+
def gene_tensor_api_implementation(self):
285+
func_name = self.get_api_func_name()
286+
assert (
287+
len(self.inputs['names']) > 1
288+
), "Can't use tensor api without Tensor inputs"
289+
# remove first Tensor argument
290+
func_args = self.inputs['names'][1:] + self.attrs['names']
291+
func_args_code = ", ".join(func_args)
292+
# func decalaration
293+
if func_name[-1] != '_':
294+
return f"""
295+
{self.get_return_type()} Tensor::{func_name}({self.get_define_args_without_first_tensor()}) const {{
296+
{indent}return paddle::OperantsManager::Instance().{func_name}(static_cast<const Tensor &>(*this), {func_args_code});
297+
}}
298+
"""
299+
else:
300+
return f"""
301+
{self.get_return_type(inplace_flag=True)} Tensor::{func_name}({self.get_define_args_without_first_tensor(inplace_flag=True)}) const {{
302+
{indent}return paddle::OperantsManager::Instance().{func_name}(static_cast<const Tensor &>(*this), {func_args_code});
303+
}}
304+
234305
"""
235306

236307
def gene_operants_declaration(self):
@@ -318,6 +389,7 @@ def gene_operants_manager_implementation(self):
318389
def generate_tensor_operants_api(
319390
api_yaml_path,
320391
operants_base_path,
392+
tensor_api_source_path,
321393
operants_header_path,
322394
operants_source_path,
323395
operants_manager_header_path,
@@ -332,13 +404,16 @@ def generate_tensor_operants_api(
332404
apis.extend(api_list)
333405

334406
operants_base_file = open(operants_base_path, 'w')
407+
tensor_api_source_file = open(tensor_api_source_path, 'w')
335408
operants_header_file = open(operants_header_path, 'w')
336409
operants_source_file = open(operants_source_path, 'w')
337410
operants_manager_header_file = open(operants_manager_header_path, 'w')
338411
operants_manager_source_file = open(operants_manager_source_path, 'w')
339412

340413
operants_base_file.write(operants_base_include)
341414
operants_base_file.write(operants_base_start)
415+
tensor_api_source_file.write(tensor_api_source_include)
416+
tensor_api_source_file.write(tensor_api_source_start)
342417
operants_header_file.write(operants_header_include)
343418
operants_header_file.write(operants_header_start)
344419
operants_source_file.write(operants_source_include)
@@ -355,6 +430,9 @@ def generate_tensor_operants_api(
355430
operants_api = OperantsAPI(api, api_prims)
356431
if operants_api.is_prim_api:
357432
operants_base_file.write(operants_api.gene_operants_base())
433+
tensor_api_source_file.write(
434+
operants_api.gene_tensor_api_implementation()
435+
)
358436
operants_header_file.write(operants_api.gene_operants_declaration())
359437
operants_source_file.write(
360438
operants_api.gene_operants_implementation()
@@ -367,12 +445,14 @@ def generate_tensor_operants_api(
367445
)
368446

369447
operants_base_file.write(operants_base_end)
448+
tensor_api_source_file.write(tensor_api_source_end)
370449
operants_header_file.write(operants_header_end)
371450
operants_source_file.write(operants_source_end)
372451
operants_manager_header_file.write(operants_manager_header_end)
373452
operants_manager_source_file.write(operants_manager_source_end)
374453

375454
operants_base_file.close()
455+
tensor_api_source_file.close()
376456
operants_header_file.close()
377457
operants_source_file.close()
378458
operants_manager_header_file.close()
@@ -396,6 +476,12 @@ def main():
396476
default='paddle/phi/api/include/operants_base.h',
397477
)
398478

479+
parser.add_argument(
480+
'--tensor_api_source_path',
481+
help='output of generated tensor_api source code file',
482+
default='paddle/phi/api/lib/tensor_api.cc',
483+
)
484+
399485
parser.add_argument(
400486
'--phi_tensor_operants_header_path',
401487
help='output of generated phi_tensor_operants header code file',
@@ -424,6 +510,7 @@ def main():
424510

425511
api_yaml_path = options.api_yaml_path
426512
operants_base_path = options.operants_base_path
513+
tensor_api_source_path = options.tensor_api_source_path
427514
operants_header_path = options.phi_tensor_operants_header_path
428515
operants_source_path = options.phi_tensor_operants_source_path
429516
operants_manager_header_path = options.operants_manager_header_path
@@ -432,6 +519,7 @@ def main():
432519
generate_tensor_operants_api(
433520
api_yaml_path,
434521
operants_base_path,
522+
tensor_api_source_path,
435523
operants_header_path,
436524
operants_source_path,
437525
operants_manager_header_path,

0 commit comments

Comments
 (0)