|
| 1 | +# Copyright (c) 2022 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 os |
| 16 | +import yaml |
| 17 | +import argparse |
| 18 | +import re |
| 19 | + |
| 20 | +from sparse_api_gen import SparseAPI |
| 21 | +from backward_api_gen import BackwardAPI |
| 22 | + |
| 23 | + |
| 24 | +class SparseBackwardAPI(SparseAPI, BackwardAPI): |
| 25 | + def __init__(self, bw_api_item_yaml): |
| 26 | + BackwardAPI.__init__(self, bw_api_item_yaml) |
| 27 | + |
| 28 | + def get_api_name(self, api_item_yaml): |
| 29 | + return api_item_yaml['sparse_bw_api'] |
| 30 | + |
| 31 | + def get_api_func_name(self): |
| 32 | + return self.api |
| 33 | + |
| 34 | + def get_return_type(self, out_type_list): |
| 35 | + return BackwardAPI.get_return_type(self, out_type_list) |
| 36 | + |
| 37 | + def gene_api_declaration(self): |
| 38 | + return SparseAPI.gene_api_declaration(self) |
| 39 | + |
| 40 | + def gene_output(self, |
| 41 | + output_type_list, |
| 42 | + set_out_func, |
| 43 | + code_indent, |
| 44 | + inplace_flag=False): |
| 45 | + kernel_output = "" |
| 46 | + output_names = [] |
| 47 | + output_create = "" |
| 48 | + |
| 49 | + if len(output_type_list) == 1: |
| 50 | + kernel_output = 'kernel_out' |
| 51 | + output_names.append('kernel_out') |
| 52 | + inplace_assign = " = " + self.inplace_map[self.outputs['names'][ |
| 53 | + 0]] if inplace_flag and self.inplace_map is not None and self.outputs[ |
| 54 | + 'names'][0] in self.inplace_map else "" |
| 55 | + output_create = f""" |
| 56 | + {self.outputs['return_type']} out{inplace_assign}; |
| 57 | + auto kernel_out = {set_out_func}(&out, {self.get_kernel_tensor_out_type(self.outputs['names'][0])});""" |
| 58 | + |
| 59 | + elif len(output_type_list) > 1: |
| 60 | + output_create = f""" |
| 61 | + {self.outputs['return_type']} out({len(output_type_list)});""" |
| 62 | + |
| 63 | + for i, out_type_item in enumerate(output_type_list): |
| 64 | + kernel_output = kernel_output + f'kernel_out_{i}, ' |
| 65 | + output_names.append(f'kernel_out_{i}') |
| 66 | + if out_type_item == 'Tensor': |
| 67 | + get_out_code = f'&out[{i}][0]' |
| 68 | + if inplace_flag and self.inplace_map is not None and self.outputs[ |
| 69 | + 'names'][i] in self.inplace_map: |
| 70 | + output_create = output_create + f""" |
| 71 | + out[{i}].emplace_back({self.inplace_map[self.outputs['names'][i]]});""" |
| 72 | + |
| 73 | + else: |
| 74 | + output_create = output_create + f""" |
| 75 | + out[{i}].emplace_back();""" |
| 76 | + |
| 77 | + else: |
| 78 | + get_out_code = f'&out[{i}]' |
| 79 | + if inplace_flag and self.inplace_map is not None and self.outputs[ |
| 80 | + 'names'][i] in self.inplace_map: |
| 81 | + output_create = output_create + f""" |
| 82 | + out[{i}] = {self.inplace_map[self.outputs['names'][i]]};""" |
| 83 | + |
| 84 | + output_create = output_create + f""" |
| 85 | + auto kernel_out_{i} = {set_out_func}({get_out_code}, {self.get_kernel_tensor_out_type(self.outputs['names'][i])});""" |
| 86 | + |
| 87 | + kernel_output = kernel_output[:-2] |
| 88 | + else: |
| 89 | + raise ValueError( |
| 90 | + "{} : Output error: the output should not be empty.".format( |
| 91 | + self.api)) |
| 92 | + |
| 93 | + return kernel_output, output_names, output_create |
| 94 | + |
| 95 | + |
| 96 | +def header_include(): |
| 97 | + return """ |
| 98 | +#include "paddle/phi/api/include/tensor.h" |
| 99 | +#include "paddle/phi/common/scalar.h" |
| 100 | +#include "paddle/phi/common/scalar_array.h" |
| 101 | +#include "paddle/utils/optional.h" |
| 102 | +""" |
| 103 | + |
| 104 | + |
| 105 | +def source_include(header_file_path): |
| 106 | + return f""" |
| 107 | +#include "{header_file_path}" |
| 108 | +#include <memory> |
| 109 | +
|
| 110 | +#include "glog/logging.h" |
| 111 | +
|
| 112 | +#include "paddle/phi/api/lib/api_registry.h" |
| 113 | +#include "paddle/phi/api/lib/api_gen_utils.h" |
| 114 | +#include "paddle/phi/api/lib/kernel_dispatch.h" |
| 115 | +#include "paddle/phi/api/lib/sparse_api_custom_impl.h" |
| 116 | +#include "paddle/phi/core/kernel_registry.h" |
| 117 | +#include "paddle/phi/kernels/declarations.h" |
| 118 | +""" |
| 119 | + |
| 120 | + |
| 121 | +def api_register(): |
| 122 | + return """ |
| 123 | +PD_REGISTER_API(Test); |
| 124 | +""" |
| 125 | + |
| 126 | + |
| 127 | +def api_namespace(): |
| 128 | + return (""" |
| 129 | +namespace paddle { |
| 130 | +namespace experimental { |
| 131 | +namespace sparse { |
| 132 | +
|
| 133 | +""", """ |
| 134 | +
|
| 135 | +} // namespace sparse |
| 136 | +} // namespace experimental |
| 137 | +} // namespace paddle |
| 138 | +""") |
| 139 | + |
| 140 | + |
| 141 | +def generate_api(api_yaml_path, header_file_path, source_file_path): |
| 142 | + |
| 143 | + with open(api_yaml_path, 'r') as f: |
| 144 | + apis = yaml.load(f, Loader=yaml.FullLoader) |
| 145 | + header_file = open(header_file_path, 'w') |
| 146 | + source_file = open(source_file_path, 'w') |
| 147 | + |
| 148 | + namespace = api_namespace() |
| 149 | + |
| 150 | + header_file.write("#pragma once\n") |
| 151 | + header_file.write(header_include()) |
| 152 | + header_file.write(namespace[0]) |
| 153 | + |
| 154 | + include_header_file = "paddle/phi/api/backward/sparse_bw_api.h" |
| 155 | + source_file.write(source_include(include_header_file)) |
| 156 | + source_file.write(namespace[0]) |
| 157 | + |
| 158 | + for api in apis: |
| 159 | + sparse_bw_api = SparseBackwardAPI(api) |
| 160 | + header_file.write(sparse_bw_api.gene_api_declaration()) |
| 161 | + source_file.write(sparse_bw_api.gene_api_code()) |
| 162 | + |
| 163 | + header_file.write(namespace[1]) |
| 164 | + source_file.write(namespace[1]) |
| 165 | + |
| 166 | + source_file.write(api_register()) |
| 167 | + |
| 168 | + header_file.close() |
| 169 | + source_file.close() |
| 170 | + |
| 171 | + |
| 172 | +def main(): |
| 173 | + parser = argparse.ArgumentParser( |
| 174 | + description='Generate PaddlePaddle C++ Sparse API files') |
| 175 | + parser.add_argument( |
| 176 | + '--api_yaml_path', |
| 177 | + help='path to sparse api yaml file', |
| 178 | + default='python/paddle/utils/code_gen/sparse_bw_api.yaml') |
| 179 | + |
| 180 | + parser.add_argument( |
| 181 | + '--api_header_path', |
| 182 | + help='output of generated api header code file', |
| 183 | + default='paddle/phi/api/backward/sparse_bw_api.h') |
| 184 | + |
| 185 | + parser.add_argument( |
| 186 | + '--api_source_path', |
| 187 | + help='output of generated api source code file', |
| 188 | + default='paddle/phi/api/lib/sparse_bw_api.cc') |
| 189 | + |
| 190 | + options = parser.parse_args() |
| 191 | + |
| 192 | + api_yaml_path = options.api_yaml_path |
| 193 | + header_file_path = options.api_header_path |
| 194 | + source_file_path = options.api_source_path |
| 195 | + |
| 196 | + generate_api(api_yaml_path, header_file_path, source_file_path) |
| 197 | + |
| 198 | + |
| 199 | +if __name__ == '__main__': |
| 200 | + main() |
0 commit comments