strings_api_gen.py 13.8 KB
Newer Older
J
Jack Zhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# Copyright (c) 2022 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 yaml
import argparse
import re

from api_gen import ForwardAPI
21

J
Jack Zhou 已提交
22 23 24 25 26
PREFIX_TENSOR_NAME = 'input_'
PREFIX_META_TENSOR_NAME = 'meta_'


class StringsAPI(ForwardAPI):
27

J
Jack Zhou 已提交
28 29 30 31 32 33 34 35 36
    def __init__(self, api_item_yaml):
        super(StringsAPI, self).__init__(api_item_yaml)

    def get_api_func_name(self):
        return self.api

    def gene_api_declaration(self):
        return f"""
// {", ".join(self.outputs['names'])}
37
{super(StringsAPI, self).gene_api_declaration()}
J
Jack Zhou 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
"""

    def get_kernel_tensor_out_type(self, output_name):
        strings_type = 'TensorType::DENSE_TENSOR'
        if output_name.endswith('@StringTensor'):
            strings_type = 'TensorType::STRING_TENSOR'
        return strings_type

    def get_tensor_type(self, kernel_tensor_out_type):
        tensor_type_dict = {
            "TensorType::DENSE_TENSOR": "phi::DenseTensor",
            "TensorType::STRING_TENSOR": "phi::StringTensor",
        }
        return tensor_type_dict[kernel_tensor_out_type]

    def gene_output(self,
54 55 56
                    out_dtype_list,
                    out_tensor_type_list=None,
                    code_indent='',
J
Jack Zhou 已提交
57
                    inplace_flag=False):
58
        kernel_output = []
J
Jack Zhou 已提交
59 60
        output_names = []
        output_create = ""
61
        return_type = self.get_return_type(inplace_flag)
J
Jack Zhou 已提交
62

63
        if len(out_dtype_list) == 1:
64
            kernel_output.append('kernel_out')
J
Jack Zhou 已提交
65 66 67 68 69 70 71 72
            output_names.append('kernel_out')
            kernel_tensor_out_type = self.get_kernel_tensor_out_type(
                self.outputs['names'][0])
            tensor_type = self.get_tensor_type(kernel_tensor_out_type)
            inplace_assign = " = " + self.inplace_map[self.outputs['names'][
                0]] if inplace_flag and self.inplace_map is not None and self.outputs[
                    'names'][0] in self.inplace_map else ""
            output_create = f"""
73
  {return_type} api_output{inplace_assign};
74
  {tensor_type}* kernel_out = dynamic_cast<{tensor_type}*>(SetStringsKernelOutput(kernel_backend, &api_output, {kernel_tensor_out_type}));"""
J
Jack Zhou 已提交
75

76
        elif len(out_dtype_list) > 1:
J
Jack Zhou 已提交
77
            output_create = f"""
78
  {return_type} api_output;"""
J
Jack Zhou 已提交
79

80
            for i in range(len(out_dtype_list)):
81
                kernel_output.append(f'kernel_out_{i}')
J
Jack Zhou 已提交
82 83 84 85 86 87 88 89 90 91
                output_names.append(f'kernel_out_{i}')
                kernel_tensor_out_type = self.get_kernel_tensor_out_type(
                    self.outputs['names'][i])
                tensor_type = self.get_tensor_type(kernel_tensor_out_type)
                if inplace_flag and self.inplace_map is not None and self.outputs[
                        'names'][i] in self.inplace_map:
                    output_create = output_create + f"""
  std::get<{i}>(api_output) = {self.inplace_map[self.outputs['names'][i]]};"""

                output_create = output_create + f"""
92
  {tensor_type}* kernel_out_{i} = dynamic_cast<{tensor_type}*>(SetStringsKernelOutput(&std::get<{i}>(api_output), {kernel_tensor_out_type}));"""
J
Jack Zhou 已提交
93 94 95 96 97 98 99 100 101 102

        else:
            raise ValueError(
                "{} : Output error: the output should not be empty.".format(
                    self.api))

        return kernel_output, output_names, output_create

    def get_kernel_args(self, code_indent):
        input_trans_map = {
103 104
            'const Tensor&':
            'const phi::StringTensor&',
J
Jack Zhou 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
            'const std::vector<Tensor>&':
            'const std::vector<const phi::StringTensor*>&',
            'const paddle::optional<Tensor>&':
            'paddle::optional<const phi::StringTensor&>',
            'const paddle::optional<std::vector<Tensor>>&':
            'paddle::optional<const std::vector<phi::StringTensor>&>'
        }
        out_trans_map = {
            'Tensor': 'phi::StringTensor*',
            'std::vector<Tensor>': 'std::vector<phi::StringTensor*>&'
        }
        input_names = self.inputs['names']
        input_infos = self.inputs['input_info']
        kernel_args_type_list = ['const platform::DeviceContext&']

        attr_names = self.attrs['names']
        kernel_param = self.kernel['param']
        if kernel_param is None:
            kernel_param = input_names + attr_names
        input_tensor_code = ""
        # set input_tensor_code
        for i, input_name in enumerate(input_names):
            input_tensor_code = input_tensor_code + f"""
{code_indent}  auto {PREFIX_TENSOR_NAME}{input_name} = TensorToStringTensor({input_name});"""

        # set kernel_args
        kernel_args = "*dev_ctx, "
        for param in kernel_param:
            if param in input_names:
                if param in self.optional_vars:
                    kernel_args = kernel_args + PREFIX_TENSOR_NAME + param + ", "
                else:
                    if self.inputs['input_info'][param] == "const Tensor&":
                        kernel_args = kernel_args + "*" + PREFIX_TENSOR_NAME + param + ", "
                    elif self.inputs['input_info'][
                            input_name] == "const std::vector<Tensor>&":
                        kernel_args = kernel_args + PREFIX_TENSOR_NAME + param + ", "
                    else:
                        # do nothing
                        pass
                kernel_in_type = input_trans_map[input_infos[param]]
                kernel_args_type_list.append(kernel_in_type)
            elif param in attr_names:
                # set attr for kernel_context
149 150 151
                if 'IntArray' in self.attrs['attr_info'][param][0]:
                    kernel_args_type_list.append('const phi::IntArray&')
                    param = 'phi::IntArray(' + param + ')'
J
Jack Zhou 已提交
152 153 154 155
                elif 'Scalar' in self.attrs['attr_info'][param][0]:
                    kernel_args_type_list.append('const phi::Scalar&')
                    param = 'phi::Scalar(' + param + ')'
                else:
156 157
                    kernel_args_type_list.append(
                        self.attrs['attr_info'][param][0])
J
Jack Zhou 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
                kernel_args = kernel_args + param + ", "
            elif isinstance(param, bool):
                kernel_args = kernel_args + str(param).lower() + ", "
            else:
                kernel_args = kernel_args + str(param) + ", "

        for out_type in self.outputs['types']:
            kernel_args_type_list.append(out_trans_map[out_type])

        # set kernel_signature
        kernel_signature = "void(*)(" + ", ".join(kernel_args_type_list) + ")"

        return input_tensor_code, kernel_args[:-2], kernel_signature

    def gen_string_tensor_kernel_code(self, inplace_flag=False, code_indent=""):
        input_tensors, kernel_args, kernel_signature = self.get_kernel_args(
            code_indent)
        outputs_args, kernel_output_names, output_create = self.gene_output(
176
            self.outputs['types'], None, '', inplace_flag)
J
Jack Zhou 已提交
177 178 179 180

        return f"""
  // 1. Get kernel signature and kernel
  VLOG(6) << "{self.api} api strings kernel key: [" << kernel_backend << ", " << kernel_layout << ", "<< kernel_data_type << "]";
181 182 183
  auto kernel_result = phi::KernelFactory::Instance().SelectKernelOrThrowError(
      "{self.kernel['func'][0]}", {{kernel_backend, kernel_layout, kernel_data_type}});
  const auto& kernel = kernel_result.kernel;
J
Jack Zhou 已提交
184 185 186
  VLOG(6) << "{self.api} api strings kernel: " << kernel;

  // 2. Get Device Context and input
187
  auto* dev_ctx = GetDeviceContextByBackend(kernel_result.has_fallback_cpu ? Backend::CPU : kernel_backend);
J
Jack Zhou 已提交
188 189 190 191 192 193 194 195 196 197
  {input_tensors}

  //  3. Set output
  {output_create}
{self.gene_infer_meta(kernel_output_names, code_indent)}

  // 4. run kernel

{code_indent}  using kernel_signature = {kernel_signature};
{code_indent}  auto* kernel_fn = kernel.GetVariadicKernelFn<kernel_signature>();
198
{code_indent}  (*kernel_fn)({kernel_args}, {", ".join(outputs_args)});
J
Jack Zhou 已提交
199

200
{code_indent}  {self.gene_return_code()}"""
J
Jack Zhou 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

    def gene_kernel_select(self) -> str:
        api = self.api
        input_names = self.inputs['names']
        attrs = self.attrs
        kernel = self.kernel

        kernel_key_item_init = """
  Backend kernel_backend = Backend::UNDEFINED;
  DataLayout kernel_layout = DataLayout::PSTRING_UNION;
  DataType kernel_data_type = DataType::PSTRING;
"""
        # Check the tensor options
        attr_backend_count = 0
        attr_layout_count = 0
        attr_data_type_count = 0
        for attr_name in attrs['names']:
            if attrs['attr_info'][attr_name][0] == 'Backend':
                assert kernel['backend'] is not None, \
                    f"{api} api: When there is a parameter with 'Backend' type in attributes, you must set backend of kernel manually."
                attr_backend_count = attr_backend_count + 1

        # preprocess kernel configures
        kernel_select_code = ""
        if kernel['backend'] is not None:
            if '>' in kernel['backend']:
                vars_list = kernel['backend'].split('>')
                assert len(
                    vars_list
                ) == 2, f"{api} api: The number of params to set backend with '>' only allows 2, but received {len(vars_list)}."
231
                assert (vars_list[0].strip() in attrs['names']) and (attrs['attr_info'][vars_list[0].strip()][0] == 'const Place&'), \
J
Jack Zhou 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
                    f"{api} api: When use '>' to set kernel backend, the first param should be a attribute with Place type."
                kernel_select_code = kernel_select_code + f"""
  kernel_backend = ParseBackendWithInputOrder({vars_list[0].strip()}, {vars_list[1].strip()});
"""

            else:
                args_str = ""
                for ele in kernel['backend'].split(','):
                    args_str = args_str + ele.strip() + ', '
                kernel_select_code = kernel_select_code + f"""
  kernel_backend = ParseBackend({args_str[:-2]});
"""

        kernel_select_args = ""
        for input_name in input_names:
            kernel_select_args = kernel_select_args + input_name + ", "

        if len(kernel_select_args) > 2:
            kernel_select_args = kernel_select_args[:-2]

        kernel_select_code = kernel_key_item_init + kernel_select_code

        if len(input_names) > 0:
            kernel_select_code = kernel_select_code + f"""
  auto kernel_key_set = ParseKernelKeyByInputArgs({kernel_select_args});
  auto kernel_key = kernel_key_set.GetHighestPriorityKernelKey();
  kernel_backend = kernel_key.backend();"""

        return kernel_select_code

    def gene_base_api_code(self, inplace_flag=False):
        api_func_name = self.get_api_func_name()
        return f"""
265
PADDLE_API {self.get_return_type(inplace_flag)} {api_func_name}({self.get_define_args(inplace_flag)}) {{
J
Jack Zhou 已提交
266 267 268 269 270 271 272 273 274 275 276 277
{self.gene_kernel_select()}
{self.gen_string_tensor_kernel_code(inplace_flag)}
}}
"""


def header_include():
    return """
#include <tuple>

#include "paddle/phi/api/include/tensor.h"
#include "paddle/phi/common/scalar.h"
278
#include "paddle/phi/common/int_array.h"
J
Jack Zhou 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
#include "paddle/utils/optional.h"
"""


def source_include(header_file_path):
    return f"""
#include "{header_file_path}"

#include "paddle/phi/api/lib/api_gen_utils.h"
#include "paddle/phi/core/kernel_context.h"
#include "paddle/phi/core/string_tensor.h"
#include "paddle/phi/infermeta/strings/nullary.h"
#include "paddle/phi/infermeta/strings/unary.h"
#include "paddle/phi/api/lib/api_registry.h"
#include "paddle/phi/api/lib/kernel_dispatch.h"
#include "paddle/phi/core/kernel_registry.h"
"""


def api_register():
    return """
PD_REGISTER_API(StringsApi);
"""


def api_namespace():
    return ("""
namespace paddle {
namespace experimental {
namespace strings {

""", """

}  // namespace strings
}  // namespace experimental
}  // namespace paddle
""")


def generate_api(api_yaml_path, header_file_path, source_file_path):

    with open(api_yaml_path, 'r') as f:
        apis = yaml.load(f, Loader=yaml.FullLoader)
    header_file = open(header_file_path, 'w')
    source_file = open(source_file_path, 'w')

    namespace = api_namespace()

    header_file.write("#pragma once\n")
    header_file.write(header_include())
    header_file.write(namespace[0])

    include_header_file = "paddle/phi/api/include/strings_api.h"
    source_file.write(source_include(include_header_file))
    source_file.write(namespace[0])

    for api in apis:
        strings_api = StringsAPI(api)
        header_file.write(strings_api.gene_api_declaration())
        source_file.write(strings_api.gene_api_code())

    header_file.write(namespace[1])
    source_file.write(namespace[1])

    # source_file.write(api_register())

    header_file.close()
    source_file.close()


def main():
    parser = argparse.ArgumentParser(
        description='Generate PaddlePaddle C++ Strings API files')
352 353
    parser.add_argument('--api_yaml_path',
                        help='path to sparse api yaml file',
354
                        default='paddle/phi/api/yaml/strings_api.yaml')
355 356 357 358 359 360 361 362

    parser.add_argument('--api_header_path',
                        help='output of generated api header code file',
                        default='paddle/phi/api/include/strings_api.h')

    parser.add_argument('--api_source_path',
                        help='output of generated api source code file',
                        default='paddle/phi/api/lib/strings_api.cc')
J
Jack Zhou 已提交
363 364 365 366 367 368 369 370 371 372 373 374

    options = parser.parse_args()

    api_yaml_path = options.api_yaml_path
    header_file_path = options.api_header_path
    source_file_path = options.api_source_path

    generate_api(api_yaml_path, header_file_path, source_file_path)


if __name__ == '__main__':
    main()