api_base.py 36.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (c) 2021 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 re

17
PREFIX_TENSOR_NAME = 'input_'
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
PREFIX_META_TENSOR_NAME = 'meta_'


class BaseAPI(object):
    def __init__(self, api_item_yaml):
        self.api = self.get_api_name(api_item_yaml)

        # inputs:
        #     names : [], list of input names
        #     input_info : {input_name : type}
        # attrs:
        #     names : [], list of attribute names
        #     attr_info : { attr_name : (type, default_values)}
        # outputs:
        #     names : [], list of output names
        #     types : [], list of output types
        #     return_type : Tensor, vector<Tensor>, ..., the return type of api
        # args_str:
36 37
        #     args_declare : "str" // str of function params with default value. Example: (..., bool flag=false)
        #     args_define : "str" // str of function params without default value. Example: (..., bool flag)
38
        self.inputs, self.attrs, self.outputs, self.args_str, self.optional_vars = self.parse_args(
39 40 41 42 43 44 45
            self.api, api_item_yaml)

        self.is_base_api = True
        if 'invoke' in api_item_yaml:
            self.is_base_api = False
            self.invoke = api_item_yaml['invoke']
        else:
46 47 48
            if 'infer_meta' in api_item_yaml:
                self.infer_meta = self.parse_infer_meta(api_item_yaml[
                    'infer_meta'])
49 50 51 52
            self.kernel = self.parse_kernel(api_item_yaml['kernel'])
            self.support_selected_rows_kernel = False if len(self.kernel[
                'func']) == 1 else True
            self.data_transform = self.parse_data_transform(api_item_yaml)
53
            self.inplace_map = self.parse_inplace(api_item_yaml)
54 55 56 57

    def get_api_name(self, api_item_yaml):
        return api_item_yaml['api']

58 59 60
    def get_api_func_name(self):
        return self.api

61
    def parse_args(self, api_name, api_item_yaml):
62 63 64 65 66
        optional_vars = []
        if 'optional' in api_item_yaml:
            optional_vars = [
                item.strip() for item in api_item_yaml['optional'].split(',')
            ]
67
        inputs, attrs, args_str = self.parse_input_and_attr(
68
            api_name, api_item_yaml['args'], optional_vars)
69 70 71 72 73 74
        output_type_list, output_names, return_type = self.parse_output(
            api_name, api_item_yaml['output'])
        return inputs, attrs, {
            'names': output_names,
            'types': output_type_list,
            'return_type': return_type
75
        }, args_str, optional_vars
76

77
    def parse_input_and_attr(self, api_name, args_config, optional_vars=[]):
78 79 80 81 82 83 84
        inputs = {'names': [], 'input_info': {}}
        attrs = {'names': [], 'attr_info': {}}
        args_str = args_config.strip()
        assert args_str.startswith('(') and args_str.endswith(')'), \
            f"Args declaration should start with '(' and end with ')', please check the args of {api_name} in yaml."
        args_str = args_str[1:-1]
        args_list = args_str.split(',')
Z
zyfncg 已提交
85 86 87 88
        input_types_map = {
            'Tensor': 'const Tensor&',
            'Tensor[]': 'const std::vector<Tensor>&'
        }
89 90 91
        attr_types_map = {
            'ScalarArray': 'const ScalarArray&',
            'Scalar': 'const Scalar&',
92
            'uint8': 'uint8_t',
93
            'int': 'int',
94 95
            'int32': 'int32_t',
            'int64': 'int64_t',
96 97 98 99 100
            'long': 'long',
            'size_t': 'size_t',
            'float': 'float',
            'double': 'double',
            'bool': 'bool',
101
            'str': 'const std::string&',
102 103 104
            'Backend': 'Backend',
            'DataLayout': 'DataLayout',
            'DataType': 'DataType',
105
            'int64[]': 'const std::vector<int64_t>&',
106 107 108 109 110 111 112 113 114
            'int[]': 'const std::vector<int>&',
            'long[]': 'const std::vector<int64_t>&'
        }
        optional_types_trans = {
            'Tensor': 'const paddle::optional<Tensor>&',
            'Tensor[]': 'const paddle::optional<std::vector<Tensor>>&',
            'ScalarArray': 'const paddle::optional<ScalarArray>&',
            'Scalar': 'const paddle::optional<Scalar>&',
            'int': 'paddle::optional<int>',
115 116
            'int32': 'paddle::optional<int32_t>',
            'int64': 'paddle::optional<int64_t>',
117 118 119 120 121 122 123
            'size_t': 'paddle::optional<size_t>',
            'float': 'paddle::optional<float>',
            'double': 'paddle::optional<double>',
            'bool': 'paddle::optional<bool>',
            'Backend': 'paddle::optional<Backend>',
            'DataLayout': 'paddle::optional<DataLayout>',
            'DataType': 'paddle::optional<DataType>',
124
            'int64[]': 'paddle::optional<std::vector<int64_t>>',
125 126 127
            'int[]': 'paddle::optional<std::vector<int>>'
        }

128 129 130 131 132
        args_declare_str = ""
        args_define_str = ""

        for item in args_list:
            item = item.strip()
Z
zyfncg 已提交
133
            type_and_name = item.split(' ')
134 135
            # match the input tensor
            has_input = False
Z
zyfncg 已提交
136 137 138
            for in_type_symbol, in_type in input_types_map.items():
                if type_and_name[0] == in_type_symbol:
                    input_name = type_and_name[1].strip()
139 140 141 142 143
                    assert len(input_name) > 0, \
                        f"The input tensor name should not be empty. Please check the args of {api_name} in yaml."
                    assert len(attrs['names']) == 0, \
                        f"The input Tensor should appear before attributes. please check the position of {api_name}:input({input_name}) in yaml"

144 145 146
                    if input_name in optional_vars:
                        in_type = optional_types_trans[in_type_symbol]

147 148 149 150 151 152 153 154 155 156
                    inputs['names'].append(input_name)
                    inputs['input_info'][input_name] = in_type
                    args_declare_str = args_declare_str + in_type + ' ' + input_name + ', '
                    args_define_str = args_define_str + in_type + ' ' + input_name + ', '
                    has_input = True
                    break
            if has_input:
                continue

            # match the attribute
Z
zyfncg 已提交
157 158 159
            for attr_type_symbol, attr_type in attr_types_map.items():
                if type_and_name[0] == attr_type_symbol:
                    attr_name = item[len(attr_type_symbol):].strip()
160 161 162 163 164 165 166 167
                    assert len(attr_name) > 0, \
                        f"The attribute name should not be empty. Please check the args of {api_name} in yaml."
                    default_value = None
                    if '=' in attr_name:
                        attr_infos = attr_name.split('=')
                        attr_name = attr_infos[0].strip()
                        default_value = attr_infos[1].strip()

168 169 170
                    if attr_name in optional_vars:
                        attr_type = optional_types_trans[attr_type_symbol]

171 172 173 174 175 176 177 178 179 180 181 182 183 184
                    default_value_str = "" if default_value is None else '=' + default_value
                    args_declare_str = args_declare_str + attr_type + ' ' + attr_name + default_value_str + ', '
                    args_define_str = args_define_str + attr_type + ' ' + attr_name + ', '
                    attrs['names'].append(attr_name)
                    attrs['attr_info'][attr_name] = (attr_type, default_value)
                    break

        return inputs, attrs, {
            'args_declare': args_declare_str[:-2],
            'args_define': args_define_str[:-2]
        }

    def parse_output(self, api_name, output_config):
        def parse_output_item(output_item):
Z
zyfncg 已提交
185 186 187 188
            output_type_map = {
                'Tensor': 'Tensor',
                'Tensor[]': 'std::vector<Tensor>'
            }
189
            if re.search(r'\([a-zA-Z0-9_@]*\)', output_item):
190
                result = re.search(
191
                    r"(?P<out_type>[a-zA-Z0-9_[\]]+)\s*\((?P<name>[a-zA-Z0-9_@]+)\)",
192 193
                    output_item)
                out_type = result.group('out_type')
Z
zyfncg 已提交
194 195
                assert out_type in output_type_map, \
                    f"{api_name} : Output type error: the output type only support Tensor and Tensor[], \
196 197 198 199 200
                      but now is {out_type}."

                return out_type, result.group('name')

            else:
Z
zyfncg 已提交
201 202
                if output_item.strip() in output_type_map:
                    return output_type_map[output_item.strip()], 'out'
203 204
                else:
                    raise ValueError(
Z
zyfncg 已提交
205 206
                        "{} : Output type error: the output type only support Tensor and Tensor[], \
                      but now is {}.".format(api_name, output_item.strip()))
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

        temp_list = output_config.split(',')

        if len(temp_list) == 1:
            out_type, out_name = parse_output_item(temp_list[0])
            return [out_type], [out_name], self.get_return_type([out_type])
        else:
            out_type_list = []
            out_name_list = []
            for output_item in temp_list:
                out_type, out_name = parse_output_item(output_item)
                out_type_list.append(out_type)
                out_name_list.append(out_name)

            return out_type_list, out_name_list, self.get_return_type(
                out_type_list)

224 225 226 227 228 229 230 231 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    def parse_infer_meta(self, infer_meta_config):
        infer_meta = infer_meta_config
        if 'param' not in infer_meta_config:
            infer_meta['param'] = None

        return infer_meta

    def parse_kernel(self, kernel_config):
        # kernel :
        #    func : [], Kernel functions (example: scale, scale_sr)
        #    param : [], Input params of kernel
        #    backend : str, the names of param to choose the kernel backend, default is None
        #    layout : str, the names of param to choose the kernel layout, default is None
        #    data_type : str, the names of param to choose the kernel data_type, default is None
        kernel = {
            'func': [],
            'param': None,
            'backend': None,
            'layout': None,
            'data_type': None
        }
        if 'backend' in kernel_config and len(kernel_config['backend']) > 0:
            kernel['backend'] = kernel_config['backend']
        if 'layout' in kernel_config and len(kernel_config['layout']) > 0:
            kernel['layout'] = kernel_config['layout']
        if 'data_type' in kernel_config and len(kernel_config['data_type']) > 0:
            kernel['data_type'] = kernel_config['data_type']
        if 'param' in kernel_config:
            kernel['param'] = kernel_config['param']
        kernel['func'] = [
            kernel_fn.strip() for kernel_fn in kernel_config['func'].split(',')
        ]

        if len(kernel['func']) == 2:
            assert kernel['func'][0] == self.api, \
                    f"{self.api} : Kernel func error: If kernel has two func config, the name of first func should be same with api name({self.api}), \
                      but now is {kernel['func'][0]}."
            assert kernel['func'][1].endswith('_sr'), \
                    f"{self.api} : Kernel func error: If kernel has two func config, the name of second func should be a selected_rows kernel (the func name endwith '_sr'), \
                      but now is {kernel['func'][1]}."

        return kernel

    def parse_data_transform(self, api_item_yaml):
        data_transform = {'skip_transform': [], 'support_trans_dtype': []}
        if 'data_transform' in api_item_yaml:
            if 'skip_transform' in api_item_yaml['data_transform']:
                data_transform['skip_transform'] = api_item_yaml[
                    'data_transform']['skip_transform']
            if 'support_trans_dtype' in api_item_yaml['data_transform']:
                data_transform['support_trans_dtype'] = api_item_yaml[
                    'data_transform']['support_trans_dtype']

        return data_transform

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
    def parse_inplace(self, api_item_yaml):
        if 'inplace' in api_item_yaml:
            inplace_map = {}
            inplace_list = api_item_yaml['inplace'].split(',')
            for item in inplace_list:
                result = re.search(r"(?P<in>\w+)\s*->\s(?P<out>\w+)", item)
                in_val = result.group('in')
                out_val = result.group('out')
                assert in_val in self.inputs['names'], \
                    f"{self.api} : Inplace input error: the input var name('{in_val}') is not found in the input args of {self.api}."
                assert out_val in self.outputs['names'], \
                    f"{self.api} : Inplace output error: the output var name('{out_val}') is not found in the output args of {self.api}."

                inplace_map[out_val] = in_val

            return inplace_map
        else:
            return None

298 299 300 301 302 303
    # Override by child class
    def get_return_type(self, out_type_list):
        return None

    def gene_api_declaration(self):
        api_declaration = f"""
304
PADDLE_API {self.gene_return_type_code()} {self.get_api_func_name()}({self.args_str['args_declare']});
305 306 307 308
"""

        if self.is_base_api and self.inplace_map is not None:
            api_declaration = api_declaration + f"""
309
PADDLE_API {self.gene_return_type_code()} {self.get_api_func_name() + '_'}({self.args_str['args_declare']});
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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
"""

        return api_declaration

    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::UNDEFINED;
  DataType kernel_data_type = DataType::UNDEFINED;
"""
        # 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
            if attrs['attr_info'][attr_name][0] == 'DataLayout':
                assert kernel['layout'] is not None, \
                    f"{api} api: When there is a parameter with 'DataLayout' type in attributes, you must set layout of kernel manually."
                attr_layout_count = attr_layout_count + 1
            if attrs['attr_info'][attr_name][0] == 'DataType':
                assert kernel['data_type'] is not None, \
                    f"{api} api: When there is a parameter with 'DataType' type in attributes, you must set data_type of kernel manually."
                attr_data_type_count = attr_data_type_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)}."
                assert (vars_list[0].strip() in attrs['names']) and (attrs['attr_info'][vars_list[0].strip()][0] == 'Backend'), \
                    f"{api} api: When use '>' to set kernel backend, the first param should be a attribute with Backend 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]});
"""

        if kernel['layout'] is not None:
            if '>' in kernel['layout']:
                vars_list = kernel['layout'].split('>')
                assert len(
                    vars_list
                ) == 2, f"{api} api: The number of params to set layout with '>' only allows 2, but received {len(vars_list)}."
                assert vars_list[0].strip() in attrs['names'] and attrs['attr_info'][vars_list[0].strip()][0] == 'DataLayout', \
                    f"{api} api: When use '>' to set kernel layout, the first param should be a attribute with DataLayout type."
                kernel_select_code = kernel_select_code + f"""
  kernel_layout = ParseLayoutWithInputOrder({vars_list[0].strip()}, {vars_list[1].strip()});
"""

            else:
                vars_list = kernel['layout'].split(',')
                assert len(
                    vars_list
                ) == 1, f"{api} api: The number of params to set layout must be 1, but received {len(vars_list)}."
                kernel_select_code = kernel_select_code + f"""
  kernel_layout = ParseLayout({vars_list[0].strip()});
"""

        if kernel['data_type'] is not None:
            if '>' in kernel['data_type']:
                vars_list = kernel['data_type'].split('>')
                assert len(
                    vars_list
                ) == 2, f"{api} api: The number of params to set data_type with '>' only allows 2, but received {len(vars_list)}."
                assert vars_list[0].strip() in attrs['names'] and attrs['attr_info'][vars_list[0].strip()][0] == 'DataType', \
                    f"{api} api: When use '>' to set kernel data_type, the first param should be a attribute with DataType type."
                kernel_select_code = kernel_select_code + f"""
  kernel_data_type = ParseDataTypeWithInputOrder({vars_list[0].strip()}, {vars_list[1].strip()});
"""

            else:
                vars_list = kernel['data_type'].split(',')
                assert len(
                    vars_list
                ) == 1, f"{api} api: The number of params to set data_type only allows 2, but received {len(vars_list)}."
                kernel_select_code = kernel_select_code + f"""
  kernel_data_type = ParseDataType({vars_list[0].strip()});
"""

        if len(input_names) == 0:
408 409
            assert attr_backend_count > 0 and attr_data_type_count > 0, \
                f"{api} api: When there is no input tensor, the args must have 'Backend' and 'DataType'."
410 411 412 413 414 415 416 417 418 419 420

        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:
421 422
            if self.support_selected_rows_kernel:
                kernel_select_code = kernel_select_code + f"""
423
  KernelType kernel_type = ParseKernelTypeByInputArgs({", ".join(input_names)});
424 425
"""

426 427 428 429 430
            kernel_select_code = kernel_select_code + f"""
  if (kernel_backend == Backend::UNDEFINED
        || kernel_layout == DataLayout::UNDEFINED
        || kernel_data_type == DataType::UNDEFINED ) {{
    auto kernel_key_set = ParseKernelKeyByInputArgs({kernel_select_args});
431
    auto kernel_key = kernel_key_set.GetHighestPriorityKernelKey();
432 433 434 435 436 437 438 439 440 441 442 443 444
    if (kernel_backend == Backend::UNDEFINED) {{
      kernel_backend = kernel_key.backend();
    }}
    if (kernel_layout == DataLayout::UNDEFINED) {{
      kernel_layout = kernel_key.layout();
    }}
    if (kernel_data_type == DataType::UNDEFINED) {{
      kernel_data_type = kernel_key.dtype();
    }}
  }}"""

        return kernel_select_code

445
    def gene_infer_meta(self, kernel_output_names, code_indent) -> str:
446 447 448 449 450 451 452 453 454 455 456 457
        input_names = self.inputs['names']
        attr_names = self.attrs['names']
        infer_meta = self.infer_meta

        infer_meta_params = infer_meta[
            'param'] + kernel_output_names if infer_meta[
                'param'] is not None else input_names + attr_names + kernel_output_names
        # generate meta tensors
        meta_tensor_code = ""
        param_code = ""
        for param in infer_meta_params:
            if param in input_names:
458 459 460 461 462
                if self.inputs['input_info'][param] == "const Tensor&":
                    param_code = param_code + "MakeMetaTensor(*" + PREFIX_TENSOR_NAME + param + "), "
                elif self.inputs['input_info'][
                        param] == "const std::vector<Tensor>&":
                    meta_tensor_code = meta_tensor_code + f"""
463
{code_indent}  auto {param}_meta_vec = MakeMetaTensor({PREFIX_TENSOR_NAME}{param});
464 465 466 467 468 469 470 471
{code_indent}  std::vector<phi::MetaTensor*> {param}_metas({param}_meta_vec.size());
{code_indent}  for (size_t i = 0; i < {param}_meta_vec.size(); ++i) {{
{code_indent}    {param}_metas[i] = &{param}_meta_vec[i];
{code_indent}  }}
"""

                    param_code = param_code + param + "_metas, "
                elif param in self.optional_vars:
472 473 474 475 476 477 478 479 480
                    meta_tensor_code = meta_tensor_code + f"""
{code_indent}  paddle::optional<const phi::MetaTensor&> {PREFIX_TENSOR_NAME}meta_ref_{param}(paddle::none);
{code_indent}  auto {PREFIX_TENSOR_NAME}meta_{param} = MakeMetaTensor({PREFIX_TENSOR_NAME}{param});
{code_indent}  if ({PREFIX_TENSOR_NAME}meta_{param}) {{
{code_indent}    {PREFIX_TENSOR_NAME}meta_ref_{param} = paddle::make_optional<const phi::MetaTensor&>(*{PREFIX_TENSOR_NAME}meta_{param});
{code_indent}  }}"""

                    param_code = param_code + f"{PREFIX_TENSOR_NAME}meta_ref_{param}, "
                else:
481 482 483
                    raise ValueError(
                        f"{self.api} : Param of infer_meta error : {self.inputs['input_info'][param]} type is not supported."
                    )
484
            elif param in kernel_output_names:
485
                meta_tensor_code = meta_tensor_code + code_indent + "  phi::MetaTensor " + param.replace(
486
                    'kernel_', PREFIX_META_TENSOR_NAME) + "(" + param + ");\n"
487
                param_code = param_code + "&" + param.replace(
488
                    'kernel_', PREFIX_META_TENSOR_NAME) + ", "
489 490 491 492 493 494 495 496 497 498 499
            elif param in attr_names:
                param_code = param_code + param + ", "
            elif isinstance(param, str):
                param_code = param_code + "\"" + param + "\", "
            elif isinstance(param, bool):
                param_code = param_code + str(param).lower() + ", "
            else:
                param_code = param_code + str(param) + ", "

        param_code = param_code[:-2]
        return f"""{meta_tensor_code}
500
{code_indent}  phi::{infer_meta['func']}({param_code});
501 502
"""

503
    def get_kernel_args(self, code_indent):
504
        input_trans_map = {
505
            'const Tensor&': 'const phi::DenseTensor&',
506
            'const std::vector<Tensor>&':
507
            'const std::vector<const phi::DenseTensor*>&',
508 509 510 511
            'const paddle::optional<Tensor>&':
            'paddle::optional<const phi::DenseTensor&>',
            'const paddle::optional<std::vector<Tensor>>&':
            'paddle::optional<const std::vector<phi::DenseTensor>&>'
512 513
        }
        out_trans_map = {
514 515
            'Tensor': 'phi::DenseTensor*',
            'std::vector<Tensor>': 'std::vector<phi::DenseTensor*>&'
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
        }
        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 = ""
        for i, input_name in enumerate(input_names):
            # set input code
            if input_name in kernel_param:
                trans_flag = "{}"
                if input_name in self.data_transform['skip_transform']:
                    trans_flag = "{true}"
                elif input_name in self.data_transform['support_trans_dtype']:
                    trans_flag = "{false, true}"
535 536 537 538 539 540 541 542 543
                if input_name in self.optional_vars:
                    input_tensor_code = input_tensor_code + f"""
{code_indent}  {input_trans_map[input_infos[input_name]]} {PREFIX_TENSOR_NAME}{input_name}(paddle::none);
{code_indent}  auto {PREFIX_TENSOR_NAME}{input_name}_ptr = PrepareData({input_name}, kernel.InputAt({i}), {trans_flag});
{code_indent}  if ({PREFIX_TENSOR_NAME}{input_name}_ptr) {{
{code_indent}    {PREFIX_TENSOR_NAME}{input_name} = paddle::make_optional<const phi::DenseTensor&>(*{PREFIX_TENSOR_NAME}{input_name}_ptr);
{code_indent}  }}"""

                else:
544 545
                    if self.inputs['input_info'][input_name] == "const Tensor&":
                        input_tensor_code = input_tensor_code + f"""
546
{code_indent}  auto {PREFIX_TENSOR_NAME}{input_name} = PrepareData({input_name}, kernel.InputAt({i}), {trans_flag});"""
547

548 549 550 551 552 553 554 555 556 557 558 559
                    elif self.inputs['input_info'][
                            input_name] == "const std::vector<Tensor>&":
                        input_tensor_code = input_tensor_code + f"""
{code_indent}  auto {PREFIX_TENSOR_NAME}{input_name}_vec = PrepareData({input_name}, kernel.InputAt({i}), {trans_flag});
{code_indent}  std::vector<const phi::DenseTensor*> {PREFIX_TENSOR_NAME}{input_name}({PREFIX_TENSOR_NAME}{input_name}_vec->size());
{code_indent}  for (size_t i = 0; i < {PREFIX_TENSOR_NAME}{input_name}.size(); ++i) {{
{code_indent}    {PREFIX_TENSOR_NAME}{input_name}[i] = &{PREFIX_TENSOR_NAME}{input_name}_vec->at(i);
{code_indent}  }}"""

                    else:
                        # do nothing
                        pass
560
            else:
561 562 563 564 565 566 567 568 569 570
                if input_name in self.optional_vars:
                    input_tensor_code = input_tensor_code + f"""
{code_indent}  {input_trans_map[input_infos[input_name]]} {PREFIX_TENSOR_NAME}{input_name}(paddle::none);
{code_indent}  auto {PREFIX_TENSOR_NAME}{input_name}_ptr = TensorToDenseTensor({input_name});
{code_indent}  if ({PREFIX_TENSOR_NAME}{input_name}_ptr) {{
{code_indent}    {PREFIX_TENSOR_NAME}{input_name} = paddle::make_optional<const phi::DenseTensor&>(*{PREFIX_TENSOR_NAME}{input_name}_ptr);
{code_indent}  }}"""

                else:
                    input_tensor_code = input_tensor_code + f"""
571 572 573 574 575
{code_indent}  auto {PREFIX_TENSOR_NAME}{input_name} = TensorToDenseTensor({input_name});"""

        kernel_args = "*dev_ctx, "
        for param in kernel_param:
            if param in input_names:
576 577 578
                if param in self.optional_vars:
                    kernel_args = kernel_args + PREFIX_TENSOR_NAME + param + ", "
                else:
579 580 581 582 583 584 585 586
                    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
587 588
                kernel_in_type = input_trans_map[input_infos[param]]
                kernel_args_type_list.append(kernel_in_type)
589 590 591
            elif param in attr_names:
                # set attr for kernel_context
                if 'ScalarArray' in self.attrs['attr_info'][param][0]:
592 593
                    kernel_args_type_list.append('const phi::ScalarArray&')
                    param = 'phi::ScalarArray(' + param + ')'
594
                elif 'Scalar' in self.attrs['attr_info'][param][0]:
595 596
                    kernel_args_type_list.append('const phi::Scalar&')
                    param = 'phi::Scalar(' + param + ')'
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
                else:
                    kernel_args_type_list.append(self.attrs['attr_info'][param][
                        0])
                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])

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

        return input_tensor_code, kernel_args[:-2], kernel_signature

    def get_selected_rows_kernel_args(self, code_indent):
        input_trans_map = {
615
            'const Tensor&': 'const phi::SelectedRows&',
616 617
            'const paddle::optional<Tensor>&':
            'paddle::optional<const phi::SelectedRows&>'
618
        }
619
        out_trans_map = {'Tensor': 'phi::SelectedRows*'}
620 621 622 623 624 625 626 627 628 629 630 631
        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 = ""
        for i, input_name in enumerate(input_names):
            # set input code
632 633 634 635 636 637 638 639 640 641 642
            if input_name in self.optional_vars:
                input_tensor_code = input_tensor_code + f"""

{code_indent}  {input_trans_map[input_infos[input_name]]} {PREFIX_TENSOR_NAME}{input_name}(paddle::none);
{code_indent}  auto {PREFIX_TENSOR_NAME}{input_name}_ptr = TensorToSelectedRows({input_name});
{code_indent}  if ({PREFIX_TENSOR_NAME}{input_name}_ptr) {{
{code_indent}    {PREFIX_TENSOR_NAME}{input_name} = paddle::make_optional<const phi::SelectedRows&>(*{PREFIX_TENSOR_NAME}{input_name}_ptr);
{code_indent}  }}"""

            else:
                input_tensor_code = input_tensor_code + f"""
643
{code_indent}  auto {PREFIX_TENSOR_NAME}{input_name} = TensorToSelectedRows({input_name});"""
644 645 646 647

        kernel_args = "*dev_ctx, "
        for param in kernel_param:
            if param in input_names:
648 649 650 651 652 653
                if param in self.optional_vars:
                    kernel_args = kernel_args + PREFIX_TENSOR_NAME + param + ", "
                else:
                    kernel_args = kernel_args + "*" + PREFIX_TENSOR_NAME + param + ", "
                kernel_in_type = input_trans_map[input_infos[param]]
                kernel_args_type_list.append(kernel_in_type)
654 655 656
            elif param in attr_names:
                # set attr for kernel_context
                if 'ScalarArray' in self.attrs['attr_info'][param][0]:
657 658
                    kernel_args_type_list.append('const phi::ScalarArray&')
                    param = 'phi::ScalarArray(' + param + ')'
659
                elif 'Scalar' in self.attrs['attr_info'][param][0]:
660 661
                    kernel_args_type_list.append('const phi::Scalar&')
                    param = 'phi::Scalar(' + param + ')'
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
                else:
                    kernel_args_type_list.append(self.attrs['attr_info'][param][
                        0])
                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])

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

        return input_tensor_code, kernel_args[:-2], kernel_signature

678 679 680 681 682 683 684 685
    # Override by child class
    def gene_return_type_code(self):
        return self.outputs['return_type']

    # Override by child class
    def gene_return_code(self):
        return "api_output"

686
    # Override by child class
687 688 689 690 691
    def gene_output(self,
                    output_type_list,
                    set_out_func,
                    code_indent,
                    inplace_flag=False):
692 693
        return None, None, None

694
    def gen_dense_tensor_kernel_code(self, code_indent, inplace_flag=False):
695 696 697
        input_tensors, kernel_args, kernel_signature = self.get_kernel_args(
            code_indent)
        outputs_args, kernel_output_names, output_create = self.gene_output(
698
            self.outputs['types'], 'SetKernelOutput', code_indent, inplace_flag)
699
        api_func_name = self.get_api_func_name() + ('_' if inplace_flag else '')
700
        return f"""
701
{code_indent}  const auto& kernel = phi::KernelFactory::Instance().SelectKernelOrThrowError(
702 703 704 705 706 707 708 709 710 711 712
{code_indent}      "{self.kernel['func'][0]}", {{kernel_backend, kernel_layout, kernel_data_type}});
{code_indent}  VLOG(6) << "{self.api} API kernel key: [" << kernel_backend << ", " << kernel_layout << ", "<< kernel_data_type << "]";
{code_indent}  VLOG(6) << "{self.api} API kernel: " << kernel;

{code_indent}  auto* dev_ctx = GetDeviceContextByBackend(kernel_backend);
{input_tensors}
{output_create}
{self.gene_infer_meta(kernel_output_names, code_indent)}

{code_indent}  using kernel_signature = {kernel_signature};
{code_indent}  auto* kernel_fn = kernel.GetVariadicKernelFn<kernel_signature>();
713 714 715 716
{code_indent}  {{
{code_indent}    paddle::platform::RecordEvent kernel_record_event(\"{api_func_name} compute\", paddle::platform::TracerEventType::Operator, 1);
{code_indent}    (*kernel_fn)({kernel_args}, {outputs_args});
{code_indent}  }}
717

718
{code_indent}  return {self.gene_return_code()};"""
719

720
    def gen_selected_rows_kernel_code(self, code_indent, inplace_flag=False):
721 722 723
        input_tensors, kernel_args, kernel_signature = self.get_selected_rows_kernel_args(
            code_indent)
        outputs_args, kernel_output_names, output_create = self.gene_output(
724 725
            self.outputs['types'], 'SetSelectedRowsKernelOutput', code_indent,
            inplace_flag)
726
        api_func_name = self.get_api_func_name() + ('_' if inplace_flag else '')
727
        return f"""
728
{code_indent}  auto kernel = phi::KernelFactory::Instance().SelectKernelOrThrowError(
729 730 731 732 733 734 735 736 737 738 739
{code_indent}      "{self.kernel['func'][1]}", {{kernel_backend, kernel_layout, kernel_data_type}});
{code_indent}  VLOG(6) << "{self.api} API SelectedRows kernel key: [" << kernel_backend << ", " << kernel_layout << ", "<< kernel_data_type << "]";
{code_indent}  VLOG(6) << "{self.api} API SelectedRows kernel: " << kernel;

{code_indent}  auto* dev_ctx = GetDeviceContextByBackend(kernel_backend);
{input_tensors}
{output_create}
{self.gene_infer_meta(kernel_output_names, code_indent)}

{code_indent}  using kernel_signature = {kernel_signature};
{code_indent}  auto* kernel_fn = kernel.GetVariadicKernelFn<kernel_signature>();
740 741 742 743
{code_indent}  {{
{code_indent}    paddle::platform::RecordEvent kernel_record_event(\"{api_func_name} compute\", paddle::platform::TracerEventType::Operator, 1);
{code_indent}    (*kernel_fn)({kernel_args}, {outputs_args});
{code_indent}  }}
744

745
{code_indent}  return {self.gene_return_code()};"""
746

747 748 749
    def gene_base_api_code(self, inplace_flag=False):
        api_func_name = self.get_api_func_name() + ('_' if inplace_flag else '')
        api_code = f"""
750
PADDLE_API {self.gene_return_type_code()} {api_func_name}({self.args_str["args_define"]}) {{
751
{self.gene_kernel_select()}
752
"""
753

754 755 756
        if self.support_selected_rows_kernel:
            code_indent = '  '
            return api_code + f"""
757
  if(kernel_type == KernelType::DENSE_TENSOR_KENREL){{
758
{self.gen_dense_tensor_kernel_code(code_indent, inplace_flag)}
759
  }} else {{
760
{self.gen_selected_rows_kernel_code(code_indent, inplace_flag)}
761
  }}
762
}}
763 764
"""

765 766 767 768
        else:
            code_indent = ''
            return api_code + self.gen_dense_tensor_kernel_code(
                code_indent, inplace_flag) + """
769
}
770 771
"""

772 773 774 775 776 777 778
    def gene_api_code(self):
        if self.is_base_api:
            api_code = self.gene_base_api_code()
            if self.inplace_map is not None:
                api_code = api_code + self.gene_base_api_code(inplace_flag=True)
            return api_code

779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
        else:
            inveke_func_name = self.invoke.split('(')[0].strip()
            if inveke_func_name in self.attrs['names']:
                # Adjust the param whose name is same with api invoked.
                pattern = r'\W' + inveke_func_name + '[^A-Za-z0-9_(]'

                def adjust_name(matched):
                    matched_str = matched.group()
                    return matched_str[0:-1] + '_val' + matched_str[-1]

                invoke_code = re.sub(pattern, adjust_name, self.invoke)
                params_code = re.sub(pattern, adjust_name,
                                     self.args_str["args_define"])
            else:
                invoke_code = self.invoke
                params_code = self.args_str["args_define"]
            return f"""
{self.outputs['return_type']} {self.api}({params_code}) {{
  return {invoke_code};
}}
"""