api_base.py 55.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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.

15
import collections
16
import re
17

18
PREFIX_TENSOR_NAME = 'input_'
19 20 21
PREFIX_META_TENSOR_NAME = 'meta_'


22
class BaseAPI:
23 24 25 26 27 28 29 30 31 32 33 34
    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
35
        #     out_size_expr : [], expression for getting size of vector<Tensor>
36 37 38 39 40 41
        (
            self.inputs,
            self.attrs,
            self.outputs,
            self.optional_vars,
        ) = self.parse_args(self.api, api_item_yaml)
42 43 44 45 46 47

        self.is_base_api = True
        if 'invoke' in api_item_yaml:
            self.is_base_api = False
            self.invoke = api_item_yaml['invoke']
        else:
48
            if 'infer_meta' in api_item_yaml:
49
                self.infer_meta = self.parse_infer_meta(
50 51
                    api_item_yaml['infer_meta']
                )
52 53
            self.kernel = self.parse_kernel(api_item_yaml['kernel'])
            self.data_transform = self.parse_data_transform(api_item_yaml)
54
            self.inplace_map, self.view_map = {}, {}
55

Y
YuanRisheng 已提交
56 57 58
        self.gene_input_func = {
            "const Tensor&": {
                "dense": self.gene_dense_input,
59
                "selected_rows": self.gene_selected_rows_input,
Y
YuanRisheng 已提交
60 61 62
            },
            "const paddle::optional<Tensor>&": {
                "dense": self.gene_dense_input,
63
                "selected_rows": self.gene_selected_rows_input,
Y
YuanRisheng 已提交
64
            },
65
            "const std::vector<Tensor>&": {"dense": self.gene_vec_dense_input},
Y
YuanRisheng 已提交
66 67
            "const paddle::optional<std::vector<Tensor>>&": {
                "dense": self.gene_optional_vec_dense_input
68
            },
Y
YuanRisheng 已提交
69 70
        }

71
    def get_api_name(self, api_item_yaml):
72
        return api_item_yaml['op']
73

74 75 76
    def get_api_func_name(self):
        return self.api

77 78 79
    def get_input_tensor_args(self, inplace_flag=False):
        input_args = []
        inplace_type_map = {
80 81 82 83
            "const Tensor&": "Tensor&",
            "const paddle::optional<Tensor>&": "paddle::optional<Tensor>&",
            "const std::vector<Tensor>&": "std::vector<Tensor>&",
            "const paddle::optional<std::vector<Tensor>>&": "paddle::optional<std::vector<Tensor>>&",
84 85 86 87
        }
        for name in self.inputs['names']:
            name = name.split('@')[0]
            if inplace_flag and name in self.inplace_map.values():
88
                input_args.append(
89 90 91 92
                    inplace_type_map[self.inputs['input_info'][name]]
                    + ' '
                    + name
                )
93 94 95 96 97 98 99 100 101 102
            else:
                input_args.append(self.inputs['input_info'][name] + ' ' + name)
        return input_args

    def get_declare_args(self, inplace_flag=False):
        declare_args = self.get_input_tensor_args(inplace_flag)
        for name in self.attrs['names']:
            default_value = ''
            if self.attrs['attr_info'][name][1] is not None:
                default_value = ' = ' + self.attrs['attr_info'][name][1]
103 104 105
            declare_args.append(
                self.attrs['attr_info'][name][0] + ' ' + name + default_value
            )
106

107 108 109 110 111 112 113 114
        return ", ".join(declare_args)

    def get_define_args(self, inplace_flag=False):
        define_args = self.get_input_tensor_args(inplace_flag)
        for name in self.attrs['names']:
            define_args.append(self.attrs['attr_info'][name][0] + ' ' + name)

        return ", ".join(define_args)
115

116
    def parse_args(self, api_name, api_item_yaml):
117 118 119 120 121
        optional_vars = []
        if 'optional' in api_item_yaml:
            optional_vars = [
                item.strip() for item in api_item_yaml['optional'].split(',')
            ]
122 123 124
        inputs, attrs = self.parse_input_and_attr(
            api_name, api_item_yaml['args'], optional_vars
        )
125
        output_type_list, output_names, out_size_expr = self.parse_output(
126 127 128 129 130 131 132 133 134 135 136 137
            api_name, api_item_yaml['output']
        )
        return (
            inputs,
            attrs,
            {
                'names': output_names,
                'types': output_type_list,
                'out_size_expr': out_size_expr,
            },
            optional_vars,
        )
138

139
    def parse_input_and_attr(self, api_name, args_config, optional_vars=[]):
140 141 142
        inputs = {'names': [], 'input_info': {}}
        attrs = {'names': [], 'attr_info': {}}
        args_str = args_config.strip()
143 144 145
        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."
146 147
        args_str = args_str[1:-1]
        args_list = args_str.split(',')
Z
zyfncg 已提交
148 149
        input_types_map = {
            'Tensor': 'const Tensor&',
150
            'Tensor[]': 'const std::vector<Tensor>&',
Z
zyfncg 已提交
151
        }
152
        attr_types_map = {
153
            'IntArray': 'const IntArray&',
154
            'Scalar': 'const Scalar&',
155 156 157 158
            'Scalar(int)': 'const Scalar&',
            'Scalar(int64_t)': 'const Scalar&',
            'Scalar(float)': 'const Scalar&',
            'Scalar(dobule)': 'const Scalar&',
159
            'Scalar[]': 'const std::vector<phi::Scalar>&',
160
            'int': 'int',
161 162
            'int32_t': 'int32_t',
            'int64_t': 'int64_t',
163 164 165
            'long': 'long',
            'size_t': 'size_t',
            'float': 'float',
166
            'float[]': 'const std::vector<float>&',
167 168
            'double': 'double',
            'bool': 'bool',
169
            'bool[]': 'const std::vector<bool>&',
170
            'str': 'const std::string&',
171
            'str[]': 'const std::vector<std::string>&',
172
            'Place': 'const Place&',
173 174
            'DataLayout': 'DataLayout',
            'DataType': 'DataType',
175
            'int64_t[]': 'const std::vector<int64_t>&',
Z
zhiboniu 已提交
176
            'int[]': 'const std::vector<int>&',
177 178
        }
        optional_types_trans = {
179
            'Tensor': 'const paddle::optional<Tensor>&',
180 181
            'Tensor[]': 'const paddle::optional<std::vector<Tensor>>&',
            'int': 'paddle::optional<int>',
182 183
            'int32_t': 'paddle::optional<int32_t>',
            'int64_t': 'paddle::optional<int64_t>',
184 185 186
            'float': 'paddle::optional<float>',
            'double': 'paddle::optional<double>',
            'bool': 'paddle::optional<bool>',
187
            'Place': 'paddle::optional<const Place&>',
188
            'DataLayout': 'paddle::optional<DataLayout>',
189
            'DataType': 'paddle::optional<DataType>',
190 191
        }

192 193
        for item in args_list:
            item = item.strip()
Z
zyfncg 已提交
194
            type_and_name = item.split(' ')
195 196
            # match the input tensor
            has_input = False
Z
zyfncg 已提交
197 198 199
            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()
200 201 202 203 204 205
                    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"
206

207 208 209
                    if input_name in optional_vars:
                        in_type = optional_types_trans[in_type_symbol]

210 211 212 213 214 215 216 217
                    inputs['names'].append(input_name)
                    inputs['input_info'][input_name] = in_type
                    has_input = True
                    break
            if has_input:
                continue

            # match the attribute
Z
zyfncg 已提交
218 219
            for attr_type_symbol, attr_type in attr_types_map.items():
                if type_and_name[0] == attr_type_symbol:
220 221 222 223
                    attr_name = item[len(attr_type_symbol) :].strip()
                    assert (
                        len(attr_name) > 0
                    ), f"The attribute name should not be empty. Please check the args of {api_name} in yaml."
224 225 226 227 228 229
                    default_value = None
                    if '=' in attr_name:
                        attr_infos = attr_name.split('=')
                        attr_name = attr_infos[0].strip()
                        default_value = attr_infos[1].strip()

230 231 232
                    if attr_name in optional_vars:
                        attr_type = optional_types_trans[attr_type_symbol]

233 234 235
                    default_value_str = (
                        "" if default_value is None else '=' + default_value
                    )
236 237 238 239
                    attrs['names'].append(attr_name)
                    attrs['attr_info'][attr_name] = (attr_type, default_value)
                    break

240
        return inputs, attrs
241 242 243

    def parse_output(self, api_name, output_config):
        def parse_output_item(output_item):
Z
zyfncg 已提交
244 245
            output_type_map = {
                'Tensor': 'Tensor',
246
                'Tensor[]': 'std::vector<Tensor>',
Z
zyfncg 已提交
247
            }
248 249
            result = re.search(
                r"(?P<out_type>[a-zA-Z0-9_[\]]+)\s*(?P<name>\([a-zA-Z0-9_@]+\))?\s*(?P<expr>\{[^\}]+\})?",
250 251 252 253 254
                output_item,
            )
            assert (
                result is not None
            ), f"{api_name} : the output config parse error."
255
            out_type = result.group('out_type')
256 257 258
            assert (
                out_type in output_type_map
            ), f"{api_name} : Output type error: the output type only support Tensor and Tensor[], \
259 260
                  but now is {out_type}."

261 262 263 264 265 266 267 268 269 270
            out_name = (
                'out'
                if result.group('name') is None
                else result.group('name')[1:-1]
            )
            out_size_expr = (
                None
                if result.group('expr') is None
                else result.group('expr')[1:-1]
            )
271
            return output_type_map[out_type], out_name, out_size_expr
272 273 274 275

        temp_list = output_config.split(',')

        if len(temp_list) == 1:
276
            out_type, out_name, size_expr = parse_output_item(temp_list[0])
277
            return [out_type], [out_name], [size_expr]
278 279 280
        else:
            out_type_list = []
            out_name_list = []
281
            out_size_expr_list = []
282
            for output_item in temp_list:
283
                out_type, out_name, size_expr = parse_output_item(output_item)
284 285
                out_type_list.append(out_type)
                out_name_list.append(out_name)
286
                out_size_expr_list.append(size_expr)
287

288
            return out_type_list, out_name_list, out_size_expr_list
289

290 291 292 293 294 295 296 297 298 299 300 301 302 303
    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
304
        #    dispatch : {}, the key is kernel_func, the value is type of inputs and outputs for kernel (example: {kernel_name : (['dense','sparse_coo']#input,['sparse_coo']#output)})
305 306 307 308 309
        kernel = {
            'func': [],
            'param': None,
            'backend': None,
            'layout': None,
Z
zyfncg 已提交
310
            'data_type': None,
311
            'dispatch': {},
312 313 314 315 316 317 318 319 320
        }
        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']
321
        kernel_funcs = re.compile(r'([a-zA-Z0-9_]+)\s*({[^}]+})?').findall(
322 323
            kernel_config['func']
        )
324 325 326 327 328 329 330

        def parse_kernel_in_out_type(in_out_str):
            if len(in_out_str) == 0:
                return None
            tmp_in_out_list = in_out_str[1:-1].split('->')
            inputs = [item.strip() for item in tmp_in_out_list[0].split(',')]
            outputs = [item.strip() for item in tmp_in_out_list[1].split(',')]
331 332 333 334

            # check the tensor type
            for item in inputs:
                assert item in [
335 336 337 338
                    'dense',
                    'selected_rows',
                    'sparse_coo',
                    'sparse_csr',
339 340 341
                ], f"{self.api} : Invalid input tensor type ('{item}'), here we only support 'dense', 'selected_rows', 'sparse_coo' and 'sparse_csr'."
            for item in outputs:
                assert item in [
342 343 344 345
                    'dense',
                    'selected_rows',
                    'sparse_coo',
                    'sparse_csr',
346 347
                ], f"{self.api} : Invalid output tensor type ('{item}'), here we only support 'dense', 'selected_rows', 'sparse_coo' and 'sparse_csr'."

348 349 350 351 352
            return (inputs, outputs)

        for func_item in kernel_funcs:
            kernel['func'].append(func_item[0])
            kernel['dispatch'][func_item[0]] = parse_kernel_in_out_type(
353 354
                func_item[1]
            )
355 356 357 358 359 360 361 362

        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[
363 364
                    'data_transform'
                ]['skip_transform']
365 366
            if 'support_trans_dtype' in api_item_yaml['data_transform']:
                data_transform['support_trans_dtype'] = api_item_yaml[
367 368
                    'data_transform'
                ]['support_trans_dtype']
369 370 371

        return data_transform

372
    # Override by child class
373
    def get_return_type(self, inplace_flag=False):
374 375 376
        return None

    def gene_api_declaration(self):
377 378 379 380 381
        api_declaration = ""
        api_func_name = self.get_api_func_name()
        if api_func_name[-1] != '_':
            api_declaration = f"""
PADDLE_API {self.get_return_type()} {api_func_name}({self.get_declare_args()});
382 383
"""

384 385 386
        if self.is_base_api and len(self.inplace_map) > 0:
            if api_func_name[-1] != '_':
                api_func_name += '_'
387 388 389
            api_declaration = (
                api_declaration
                + f"""
390
PADDLE_API {self.get_return_type(inplace_flag=True)} {api_func_name}({self.get_declare_args(inplace_flag=True)});
391
"""
392
            )
393 394 395

        return api_declaration

396 397 398 399 400 401
    # Backward API Override this method
    def gene_kernel_backend_select(self):
        backend_select_code = ""
        if self.kernel['backend'] is not None:
            if '>' in self.kernel['backend']:
                vars_list = self.kernel['backend'].split('>')
402 403 404 405 406 407 408
                assert (
                    len(vars_list) == 2
                ), f"{self.api} api: The number of params to set backend with '>' only allows 2, but received {len(vars_list)}."
                assert (vars_list[0].strip() in self.attrs['names']) and (
                    self.attrs['attr_info'][vars_list[0].strip()][0]
                    == 'const Place&'
                ), f"{self.api} api: When use '>' to set kernel backend, the first param should be a attribute with Place type."
409 410 411 412 413 414 415 416 417 418 419 420 421 422
                backend_select_code = f"""
  kernel_backend = ParseBackendWithInputOrder({vars_list[0].strip()}, {vars_list[1].strip()});
"""

            else:
                backend_args = [
                    ele.strip() for ele in self.kernel['backend'].split(',')
                ]
                backend_select_code = f"""
  kernel_backend = ParseBackend({", ".join(backend_args)});
"""

        return backend_select_code

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    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']:
439
            if attrs['attr_info'][attr_name][0] == 'const Place&':
440 441 442
                assert (
                    kernel['backend'] is not None
                ), f"{api} api: When there is a parameter with 'Place' type in attributes, you must set backend of kernel manually."
443 444
                attr_backend_count = attr_backend_count + 1
            if attrs['attr_info'][attr_name][0] == 'DataLayout':
445 446 447
                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."
448 449
                attr_layout_count = attr_layout_count + 1
            if attrs['attr_info'][attr_name][0] == 'DataType':
450 451 452
                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."
453 454 455
                attr_data_type_count = attr_data_type_count + 1

        # preprocess kernel configures
456
        kernel_select_code = self.gene_kernel_backend_select()
457 458 459 460

        if kernel['layout'] is not None:
            if '>' in kernel['layout']:
                vars_list = kernel['layout'].split('>')
461 462 463 464 465 466 467 468 469 470 471
                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"""
472 473
  kernel_layout = ParseLayoutWithInputOrder({vars_list[0].strip()}, {vars_list[1].strip()});
"""
474
                )
475 476 477

            else:
                vars_list = kernel['layout'].split(',')
478 479 480 481 482 483
                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"""
484 485
  kernel_layout = ParseLayout({vars_list[0].strip()});
"""
486
                )
487 488

        if kernel['data_type'] is not None:
489 490 491 492 493 494 495 496 497 498 499

            def process_data_type_args(args_item):
                args_item = args_item.strip()
                complex_match_result = re.match(
                    r"complex\((?P<param_name>\w+)\)", args_item
                )
                if complex_match_result:
                    return f"phi::dtype::ToComplex(ParseDataType({complex_match_result.group('param_name')}))"
                else:
                    return f"ParseDataType({args_item})"

500 501
            if '>' in kernel['data_type']:
                vars_list = kernel['data_type'].split('>')
502 503 504 505 506 507 508 509 510 511 512
                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"""
513 514
  kernel_data_type = ParseDataTypeWithInputOrder({vars_list[0].strip()}, {vars_list[1].strip()});
"""
515
                )
516 517 518

            else:
                vars_list = kernel['data_type'].split(',')
519 520 521 522 523 524
                assert (
                    len(vars_list) == 1
                ), f"{api} api: The number of params to set data_type only allows 1, but received {len(vars_list)}."
                kernel_select_code = (
                    kernel_select_code
                    + f"""
525
  kernel_data_type = {process_data_type_args(vars_list[0])};
526
"""
527
                )
528 529

        if len(input_names) == 0:
530 531 532
            assert (
                attr_backend_count > 0 and attr_data_type_count > 0
            ), f"{api} api: When there is no input tensor, the args must have 'Place' and 'DataType'."
533 534 535 536 537 538 539 540 541 542 543

        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:
544 545 546
            kernel_select_code = (
                kernel_select_code
                + f"""
547 548 549 550
  if (kernel_backend == Backend::UNDEFINED
        || kernel_layout == DataLayout::UNDEFINED
        || kernel_data_type == DataType::UNDEFINED ) {{
    auto kernel_key_set = ParseKernelKeyByInputArgs({kernel_select_args});
551
    auto kernel_key = kernel_key_set.GetHighestPriorityKernelKey();
552 553 554 555 556 557 558 559 560 561
    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();
    }}
  }}"""
562
            )
563 564 565

        return kernel_select_code

566
    def gene_infer_meta(self, kernel_output_names, code_indent) -> str:
567 568 569 570
        input_names = self.inputs['names']
        attr_names = self.attrs['names']
        infer_meta = self.infer_meta

571 572 573 574 575
        infer_meta_params = (
            infer_meta['param']
            if infer_meta['param'] is not None
            else input_names + attr_names
        )
576 577 578 579 580
        # generate meta tensors
        meta_tensor_code = ""
        param_code = ""
        for param in infer_meta_params:
            if param in input_names:
581
                if self.inputs['input_info'][param] == "const Tensor&":
582 583 584 585 586 587 588 589 590 591 592 593 594 595
                    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"""
596
{code_indent}  auto {param}_meta_vec = MakeMetaTensor({PREFIX_TENSOR_NAME}{param});
597
{code_indent}  std::vector<const phi::MetaTensor*> {param}_metas({param}_meta_vec.size());
598 599 600 601
{code_indent}  for (size_t i = 0; i < {param}_meta_vec.size(); ++i) {{
{code_indent}    {param}_metas[i] = &{param}_meta_vec[i];
{code_indent}  }}
"""
602
                    )
603
                    param_code = param_code + param + "_metas, "
604 605 606 607 608 609 610
                elif (
                    self.inputs['input_info'][param]
                    == "const paddle::optional<std::vector<Tensor>>&"
                ):
                    meta_tensor_code = (
                        meta_tensor_code
                        + f"""
611 612 613 614 615 616
{code_indent}  auto {param}_meta_vec = MakeMetaTensor({PREFIX_TENSOR_NAME}{param});
{code_indent}  paddle::optional<std::vector<const 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->at(i) = &{param}_meta_vec[i];
{code_indent}  }}
"""
617
                    )
618 619
                    param_code = param_code + param + "_metas, "
                elif param in self.optional_vars:
620 621 622 623 624 625 626
                    param_code = (
                        param_code
                        + "MakeMetaTensor("
                        + PREFIX_TENSOR_NAME
                        + param
                        + "), "
                    )
627
                else:
628 629 630
                    raise ValueError(
                        f"{self.api} : Param of infer_meta error : {self.inputs['input_info'][param]} type is not supported."
                    )
631 632 633 634 635 636 637 638 639
            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) + ", "

640 641
        for i, out_name in enumerate(kernel_output_names):
            if self.outputs['types'][i] == 'std::vector<Tensor>':
642 643 644
                meta_tensor_code = (
                    meta_tensor_code
                    + f"""
645 646 647
{code_indent}  auto {out_name}_{PREFIX_META_TENSOR_NAME}vec = MakeMetaTensor({out_name});
{code_indent}  std::vector<phi::MetaTensor*> {out_name}_metas({out_name}_{PREFIX_META_TENSOR_NAME}vec.size());
{code_indent}  for (size_t i = 0; i < {out_name}_{PREFIX_META_TENSOR_NAME}vec.size(); ++i) {{
648
{code_indent}    {out_name}_metas[i] = {out_name}[i] ? &{out_name}_{PREFIX_META_TENSOR_NAME}vec[i] : nullptr;
649
{code_indent}  }}"""
650
                )
651 652 653

                param_code = param_code + out_name + '_metas, '
            else:
654 655 656 657 658 659 660 661 662
                meta_tensor_code = (
                    meta_tensor_code
                    + code_indent
                    + "  phi::MetaTensor "
                    + out_name.replace('kernel_', PREFIX_META_TENSOR_NAME)
                    + "("
                    + out_name
                    + ");\n"
                )
663
                if len(kernel_output_names) == 1:
664 665 666 667
                    param_code = (
                        param_code
                        + f"&{out_name.replace('kernel_', PREFIX_META_TENSOR_NAME)}, "
                    )
668
                else:
669 670 671 672
                    param_code = (
                        param_code
                        + f"{out_name} ? &{out_name.replace('kernel_', PREFIX_META_TENSOR_NAME)} : nullptr, "
                    )
673

674 675
        param_code = param_code[:-2]
        return f"""{meta_tensor_code}
676
{code_indent}  phi::{infer_meta['func']}({param_code});
677 678
"""

Y
YuanRisheng 已提交
679 680 681 682 683 684 685 686
    def gene_trans_flag(self, input_name):
        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}"
        return trans_flag

687 688 689
    def gene_dense_input(
        self, input_name, input_name_tensor_map, code_indent=''
    ):
Y
YuanRisheng 已提交
690 691
        input_tensor_code = ""
        trans_flag = self.gene_trans_flag(input_name)
692
        input_names = self.inputs['names']
Y
YuanRisheng 已提交
693 694 695 696 697 698
        attr_names = self.attrs['names']
        kernel_param = self.kernel['param']
        if kernel_param is None:
            kernel_param = input_names + attr_names

        input_name_tensor_map[input_name].append(
699 700 701 702 703
            (f"{PREFIX_TENSOR_NAME}{input_name}", False)
        )
        input_tensor_code = (
            input_tensor_code
            + f"""
Y
YuanRisheng 已提交
704
{code_indent}  auto {PREFIX_TENSOR_NAME}{input_name} = PrepareData({input_name}, kernel.InputAt({kernel_param.index(input_name)}), {trans_flag});"""
705
        )
Y
YuanRisheng 已提交
706
        return input_tensor_code
707

708 709 710
    def gene_selected_rows_input(
        self, input_name, input_name_tensor_map, code_indent=''
    ):
Y
YuanRisheng 已提交
711 712 713
        input_tensor_code = ""
        trans_flag = self.gene_trans_flag(input_name)
        input_names = self.inputs['names']
714 715 716 717 718
        attr_names = self.attrs['names']
        kernel_param = self.kernel['param']
        if kernel_param is None:
            kernel_param = input_names + attr_names

Y
YuanRisheng 已提交
719
        input_name_tensor_map[input_name].append(
720 721 722 723 724
            (f"{PREFIX_TENSOR_NAME}{input_name}", False)
        )
        input_tensor_code = (
            input_tensor_code
            + f"""
725
{code_indent}  auto {PREFIX_TENSOR_NAME}{input_name} = PrepareDataForSelectedRows({input_name}, kernel.InputAt({kernel_param.index(input_name)}), {trans_flag});
Y
YuanRisheng 已提交
726
"""
727
        )
Y
YuanRisheng 已提交
728 729
        return input_tensor_code

730 731 732
    def gene_optional_vec_dense_input(
        self, input_name, input_name_tensor_map, code_indent=''
    ):
733
        input_tensor_code = ""
Y
YuanRisheng 已提交
734 735 736 737 738 739 740 741
        trans_flag = self.gene_trans_flag(input_name)
        input_names = self.inputs['names']
        attr_names = self.attrs['names']
        kernel_param = self.kernel['param']
        if kernel_param is None:
            kernel_param = input_names + attr_names
        if input_name in self.inplace_map.values():
            input_name_tensor_map[input_name].append(
742 743 744 745 746
                (f"{PREFIX_TENSOR_NAME}{input_name}", True)
            )
            input_tensor_code = (
                input_tensor_code
                + f"""
747
{code_indent}  paddle::optional<std::vector<const phi::DenseTensor*>> {PREFIX_TENSOR_NAME}{input_name} = TensorToConstDenseTensorPtr({input_name});"""
748
            )
Y
YuanRisheng 已提交
749 750
        else:
            input_name_tensor_map[input_name].append(
751 752 753 754 755
                (f"{PREFIX_TENSOR_NAME}{input_name}_vec", True)
            )
            input_tensor_code = (
                input_tensor_code
                + f"""
756 757 758 759 760 761 762 763
{code_indent}  auto {PREFIX_TENSOR_NAME}{input_name}_vec = PrepareData({input_name}, kernel.InputAt({kernel_param.index(input_name)}), {trans_flag});
{code_indent}  paddle::optional<std::vector<const phi::DenseTensor*>> {PREFIX_TENSOR_NAME}{input_name};
{code_indent}  if ({PREFIX_TENSOR_NAME}{input_name}_vec){{
{code_indent}    {PREFIX_TENSOR_NAME}{input_name} = paddle::optional<std::vector<const phi::DenseTensor*>>({PREFIX_TENSOR_NAME}{input_name}_vec->size());
{code_indent}    for (size_t i = 0; i < {PREFIX_TENSOR_NAME}{input_name}_vec->size(); ++i) {{
{code_indent}      {PREFIX_TENSOR_NAME}{input_name}->at(i) = &{PREFIX_TENSOR_NAME}{input_name}_vec->at(i);
{code_indent}    }}
{code_indent}  }}"""
764
            )
Y
YuanRisheng 已提交
765
        return input_tensor_code
766

767 768 769
    def gene_vec_dense_input(
        self, input_name, input_name_tensor_map, code_indent=''
    ):
Y
YuanRisheng 已提交
770 771 772 773 774 775 776
        input_tensor_code = ""
        trans_flag = self.gene_trans_flag(input_name)
        input_names = self.inputs['names']
        attr_names = self.attrs['names']
        kernel_param = self.kernel['param']
        if kernel_param is None:
            kernel_param = input_names + attr_names
777

Y
YuanRisheng 已提交
778 779
        if input_name in self.inplace_map.values():
            input_name_tensor_map[input_name].append(
780 781 782 783 784
                (f"{PREFIX_TENSOR_NAME}{input_name}", True)
            )
            input_tensor_code = (
                input_tensor_code
                + f"""
785
{code_indent}  std::vector<const phi::DenseTensor*> {PREFIX_TENSOR_NAME}{input_name} = TensorToConstDenseTensorPtr({input_name});"""
786
            )
Y
YuanRisheng 已提交
787 788
        else:
            input_name_tensor_map[input_name].append(
789 790 791 792 793
                (f"{PREFIX_TENSOR_NAME}{input_name}_vec", True)
            )
            input_tensor_code = (
                input_tensor_code
                + f"""
794
{code_indent}  auto {PREFIX_TENSOR_NAME}{input_name}_vec = PrepareData({input_name}, kernel.InputAt({kernel_param.index(input_name)}), {trans_flag});
795 796 797 798
{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}  }}"""
799
            )
Y
YuanRisheng 已提交
800
        return input_tensor_code
801

Y
YuanRisheng 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814
    def gene_input(self, kernel_tensor_type=None, code_indent=''):
        input_names = self.inputs['names']
        attr_names = self.attrs['names']
        kernel_param = self.kernel['param']
        if kernel_param is None:
            kernel_param = input_names + attr_names
        input_name_tensor_map = collections.defaultdict(list)
        input_tensor_code = ""
        for i, input_name in enumerate(input_names):
            # set input code
            if input_name in kernel_param:
                # input is dense tensor
                api_tensor_type = self.inputs['input_info'][input_name]
815 816 817 818 819
                phi_tensor_type = (
                    'dense'
                    if kernel_tensor_type is None
                    else kernel_tensor_type[0][kernel_param.index(input_name)]
                )
Y
YuanRisheng 已提交
820 821
                if api_tensor_type in self.gene_input_func.keys():
                    input_tensor_code += self.gene_input_func[api_tensor_type][
822 823
                        phi_tensor_type
                    ](input_name, input_name_tensor_map, code_indent)
Y
YuanRisheng 已提交
824 825 826
                else:
                    # do nothing
                    pass
827 828 829
            else:
                if input_name in self.infer_meta['param']:
                    if input_name in self.optional_vars:
830 831 832
                        input_tensor_code = (
                            input_tensor_code
                            + f"""
833
{code_indent}  paddle::optional<phi::TensorBase> {PREFIX_TENSOR_NAME}{input_name} = {input_name} ? paddle::optional<phi::TensorBase>(*{input_name}->impl()) : paddle::none;"""
834
                        )
835

836
                    else:
837 838 839 840 841 842 843
                        if (
                            self.inputs['input_info'][input_name]
                            == "const std::vector<Tensor>&"
                        ):
                            input_tensor_code = (
                                input_tensor_code
                                + f"""
844 845
{code_indent}  auto {PREFIX_TENSOR_NAME}{input_name}_uq_ptr = TensorToDenseTensor({input_name});
{code_indent}  const auto& {PREFIX_TENSOR_NAME}{input_name} = *{PREFIX_TENSOR_NAME}{input_name}_uq_ptr;"""
846
                            )
847
                        else:
848 849 850
                            input_tensor_code = (
                                input_tensor_code
                                + f"""
851
{code_indent}  auto {PREFIX_TENSOR_NAME}{input_name} = {input_name}.impl();"""
852
                            )
Y
YuanRisheng 已提交
853 854 855 856 857

        return input_name_tensor_map, input_tensor_code

    def get_kernel_args(self, kernel_tensor_type=None, code_indent=''):
        dense_input_trans_map = {
858 859 860 861 862
            'const Tensor&': 'const phi::DenseTensor&',
            'const std::vector<Tensor>&': 'const std::vector<const phi::DenseTensor*>&',
            'const paddle::optional<Tensor&>': 'paddle::optional<const phi::DenseTensor&>',
            'const paddle::optional<Tensor>&': 'const paddle::optional<phi::DenseTensor>&',
            'const paddle::optional<std::vector<Tensor>>&': 'const paddle::optional<std::vector<const phi::DenseTensor*>>&',
Y
YuanRisheng 已提交
863 864 865
        }
        dense_out_trans_map = {
            'Tensor': 'phi::DenseTensor*',
866
            'std::vector<Tensor>': 'std::vector<phi::DenseTensor*>&',
Y
YuanRisheng 已提交
867 868
        }
        sr_input_trans_map = {
869 870
            'const Tensor&': 'const phi::SelectedRows&',
            'const paddle::optional<Tensor>&': 'const paddle::optional<phi::SelectedRows>&',
Y
YuanRisheng 已提交
871 872 873 874 875 876 877 878 879 880 881 882
        }
        sr_out_trans_map = {'Tensor': 'phi::SelectedRows*'}
        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_name_tensor_map, input_tensor_code = self.gene_input(
883 884
            kernel_tensor_type, code_indent
        )
Y
YuanRisheng 已提交
885

886 887 888
        input_tensor_code = (
            input_tensor_code
            + f"""
889
{code_indent}  if(platform::RecordOpInfoSupplement::IsEnabled()){{"""
890
        )
891 892 893 894 895 896 897 898 899 900 901 902
        single_tensor_names = []
        list_tensor_names = []
        for input_name, input_tensors in input_name_tensor_map.items():
            has_vector_tensor = False
            for input_tensor, is_vector in input_tensors:
                if is_vector is True:
                    has_vector_tensor = True
            if has_vector_tensor is False:
                single_tensor_names.append(input_name)
            else:
                list_tensor_names.append(input_name)
        if not single_tensor_names:
903 904 905
            input_tensor_code = (
                input_tensor_code
                + f"""
906
{code_indent}     std::vector<std::pair<const char*, std::vector<phi::DDim>>> input_shapes;"""
907
            )
908
        else:
909 910 911
            for input_name in single_tensor_names:
                if input_name in self.optional_vars:
                    input_tensors = input_name_tensor_map[input_name]
912 913 914
                    input_tensor_code = (
                        input_tensor_code
                        + f"""
915
{code_indent}     std::vector<phi::DDim> {input_name}_record_shapes;"""
916
                    )
917
                    for input_tensor, _ in input_tensors:
918 919 920
                        input_tensor_code = (
                            input_tensor_code
                            + f"""
921 922 923
{code_indent}     if({input_tensor}){{
{code_indent}       {input_name}_record_shapes.push_back((*{input_tensor}).dims());
{code_indent}     }}"""
924
                        )
925

926 927 928
            input_tensor_code = (
                input_tensor_code
                + f"""
929
{code_indent}     std::vector<std::pair<const char*, std::vector<phi::DDim>>> input_shapes{{"""
930
            )
931
            for input_name in single_tensor_names[:-1]:
932
                if input_name in self.optional_vars:
933 934 935
                    input_tensor_code = (
                        input_tensor_code
                        + f"""
936
{code_indent}     {{"{input_name}", {input_name}_record_shapes}},"""
937
                    )
938
                else:
939 940 941
                    input_tensor_code = (
                        input_tensor_code
                        + f"""
942
{code_indent}     {{"{input_name}", {{"""
943
                    )
944 945
                    input_tensors = input_name_tensor_map[input_name]
                    for input_tensor, _ in input_tensors[:-1]:
946 947 948
                        input_tensor_code = (
                            input_tensor_code
                            + f"""
949
{code_indent}     (*{input_tensor}).dims(),"""
950 951 952 953
                        )
                    input_tensor_code = (
                        input_tensor_code
                        + f"""
954
{code_indent}     (*{input_tensors[-1][0]}).dims()}}}},"""
955
                    )
956
            if single_tensor_names[-1] in self.optional_vars:
957 958 959
                input_tensor_code = (
                    input_tensor_code
                    + f"""
960
{code_indent}     {{"{single_tensor_names[-1]}",
961
{code_indent}     {single_tensor_names[-1]}_record_shapes}}}};"""
962
                )
963
            else:
964 965 966
                input_tensor_code = (
                    input_tensor_code
                    + f"""
967
{code_indent}     {{"{single_tensor_names[-1]}", {{"""
968
                )
969 970
                input_tensors = input_name_tensor_map[single_tensor_names[-1]]
                for input_tensor, _ in input_tensors[:-1]:
971 972 973
                    input_tensor_code = (
                        input_tensor_code
                        + f"""
974
{code_indent}     (*{input_tensor}).dims(),"""
975 976 977 978
                    )
                input_tensor_code = (
                    input_tensor_code
                    + f"""
979
{code_indent}     (*{input_tensors[-1][0]}).dims()}}}}}};"""
980
                )
981
        if list_tensor_names:
982 983 984
            input_tensor_code = (
                input_tensor_code
                + f"""
985
{code_indent}     std::vector<phi::DDim> ddims_vec;"""
986
            )
987
        for input_name in list_tensor_names:
988 989 990
            input_tensor_code = (
                input_tensor_code
                + f"""
991
{code_indent}     ddims_vec.clear();"""
992
            )
993 994
            for input_tensor, is_vector in input_name_tensor_map[input_name]:
                if is_vector:
995 996 997 998
                    input_tensor_truncate = input_tensor[:-4]
                    if input_name in self.inplace_map.values():
                        input_tensor_truncate = input_tensor

999
                    if input_name in self.optional_vars:
1000 1001 1002
                        input_tensor_code = (
                            input_tensor_code
                            + f"""
1003 1004 1005 1006
{code_indent}     if ({input_tensor_truncate}){{
{code_indent}       ddims_vec.reserve({input_tensor_truncate}->size());
{code_indent}       for (size_t i = 0; i < {input_tensor_truncate}->size(); ++i) {{
{code_indent}         ddims_vec.emplace_back((*{input_tensor_truncate}->at(i)).dims());
1007 1008
{code_indent}       }}
{code_indent}     }}"""
1009
                        )
1010
                    else:
1011 1012 1013
                        input_tensor_code = (
                            input_tensor_code
                            + f"""
1014 1015 1016
{code_indent}     ddims_vec.reserve({input_tensor_truncate}.size());
{code_indent}     for (size_t i = 0; i < {input_tensor_truncate}.size(); ++i) {{
{code_indent}       ddims_vec.emplace_back((*{input_tensor_truncate}[i]).dims());
1017
{code_indent}     }}"""
1018
                        )
1019
                else:
1020 1021 1022
                    input_tensor_code = (
                        input_tensor_code
                        + f"""
1023 1024
                  ddims_vec.emplace_back((*{input_tensor}).dims());
{code_indent}     """
1025 1026 1027 1028
                    )
            input_tensor_code = (
                input_tensor_code
                + f"""
1029
{code_indent}     input_shapes.emplace_back("{input_name}", ddims_vec);"""
1030
            )
1031

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
        input_tensor_code += f"""
{code_indent}     framework::AttributeMap attrs;"""

        for attr_name in self.attrs['names']:
            if 'IntArray' in self.attrs['attr_info'][attr_name][0]:
                input_tensor_code += f"""
{code_indent}     attrs["{attr_name}"] = {attr_name}.GetData();"""
            elif 'vector<phi::Scalar>' in self.attrs['attr_info'][attr_name][0]:
                input_tensor_code += f"""
{code_indent}     attrs["{attr_name}"] = "";"""  # TODO(kuizhiqing)
            elif 'Scalar' in self.attrs['attr_info'][attr_name][0]:
                input_tensor_code += f"""
{code_indent}    switch ({attr_name}.dtype()) {{
{code_indent}      case DataType::FLOAT32:
{code_indent}          attrs["{attr_name}"] = static_cast<float>({attr_name}.to<float>());
{code_indent}          break;
{code_indent}      case DataType::FLOAT64:
{code_indent}          attrs["{attr_name}"] = static_cast<double>({attr_name}.to<double>());
{code_indent}          break;
{code_indent}      case DataType::FLOAT16:
{code_indent}          attrs["{attr_name}"] = static_cast<float>({attr_name}.to<float16>());
{code_indent}          break;
{code_indent}      case DataType::BFLOAT16:
{code_indent}          attrs["{attr_name}"] = static_cast<float>({attr_name}.to<bfloat16>());
{code_indent}          break;
{code_indent}      case DataType::INT32:
{code_indent}          attrs["{attr_name}"] = static_cast<int32_t>({attr_name}.to<int32_t>());
{code_indent}          break;
{code_indent}      case DataType::INT64:
{code_indent}          attrs["{attr_name}"] = static_cast<int64_t>({attr_name}.to<int64_t>());
{code_indent}          break;
{code_indent}      case DataType::INT16:
{code_indent}          attrs["{attr_name}"] = static_cast<int16_t>({attr_name}.to<int16_t>());
{code_indent}          break;
{code_indent}      case DataType::INT8:
{code_indent}          attrs["{attr_name}"] = static_cast<int8_t>({attr_name}.to<int8_t>());
{code_indent}          break;
{code_indent}      case DataType::UINT16:
{code_indent}          attrs["{attr_name}"] = static_cast<uint16_t>({attr_name}.to<uint16_t>());
{code_indent}          break;
{code_indent}      case DataType::UINT8:
{code_indent}          attrs["{attr_name}"] = static_cast<uint8_t>({attr_name}.to<uint8_t>());
{code_indent}          break;
{code_indent}      case DataType::BOOL:
{code_indent}          attrs["{attr_name}"] = static_cast<bool>({attr_name}.to<bool>());
{code_indent}          break;
{code_indent}      case DataType::COMPLEX64:
{code_indent}          attrs["{attr_name}"] = static_cast<float>({attr_name}.to<complex64>());
{code_indent}          break;
{code_indent}      case DataType::COMPLEX128:
{code_indent}          attrs["{attr_name}"] = static_cast<double>({attr_name}.to<complex128>());
{code_indent}          break;
{code_indent}      default:
{code_indent}          attrs["{attr_name}"] = "";
{code_indent}          break;
{code_indent}    }}"""
            elif 'DataType' in self.attrs['attr_info'][attr_name][0]:
                pass  # no need
            elif 'Place' in self.attrs['attr_info'][attr_name][0]:
                pass  # no need
            else:
                input_tensor_code += f"""
{code_indent}     attrs["{attr_name}"] = {attr_name};"""

1096 1097 1098
        input_tensor_code = (
            input_tensor_code
            + f"""
1099
{code_indent}     platform::RecordOpInfoSupplement("{self.api}", input_shapes, attrs);
1100
{code_indent}  }}"""
1101
        )
1102
        kernel_args = ["*dev_ctx"]
1103 1104
        for param in kernel_param:
            if param in input_names:
1105
                if param in self.optional_vars:
1106
                    kernel_args.append(PREFIX_TENSOR_NAME + param)
1107
                else:
1108
                    if self.inputs['input_info'][param] == "const Tensor&":
1109
                        kernel_args.append("*" + PREFIX_TENSOR_NAME + param)
1110 1111 1112 1113
                    elif (
                        self.inputs['input_info'][param]
                        == "const std::vector<Tensor>&"
                    ):
1114
                        kernel_args.append(PREFIX_TENSOR_NAME + param)
1115 1116 1117
                    else:
                        # do nothing
                        pass
1118
                # input is dense tensor
1119 1120 1121 1122 1123
                if (
                    kernel_tensor_type is None
                    or kernel_tensor_type[0][kernel_param.index(param)]
                    == 'dense'
                ):
1124
                    kernel_args_type_list.append(
1125 1126
                        dense_input_trans_map[input_infos[param]]
                    )
1127 1128
                else:  # input is selected_rows
                    kernel_args_type_list.append(
1129 1130
                        sr_input_trans_map[input_infos[param]]
                    )
1131 1132
            elif param in attr_names:
                # set attr for kernel_context
1133 1134 1135
                if 'IntArray' in self.attrs['attr_info'][param][0]:
                    kernel_args_type_list.append('const phi::IntArray&')
                    param = 'phi::IntArray(' + param + ')'
1136 1137
                elif 'vector<phi::Scalar>' in self.attrs['attr_info'][param][0]:
                    kernel_args_type_list.append(
1138 1139
                        'const std::vector<phi::Scalar>&'
                    )
1140
                    param = param
1141
                elif 'Scalar' in self.attrs['attr_info'][param][0]:
1142 1143
                    kernel_args_type_list.append('const phi::Scalar&')
                    param = 'phi::Scalar(' + param + ')'
1144
                else:
1145
                    kernel_args_type_list.append(
1146 1147
                        self.attrs['attr_info'][param][0]
                    )
1148
                kernel_args.append(param)
1149
            elif isinstance(param, bool):
1150
                kernel_args.append(str(param).lower())
1151
            else:
1152
                kernel_args.append(str(param))
1153

1154 1155
        for i, out_type in enumerate(self.outputs['types']):
            # output is dense tensor
1156 1157 1158 1159
            if (
                kernel_tensor_type is None
                or kernel_tensor_type[1][i] == 'dense'
            ):
1160 1161 1162
                kernel_args_type_list.append(dense_out_trans_map[out_type])
            else:  # output is selected_rows
                kernel_args_type_list.append(sr_out_trans_map[out_type])
1163 1164 1165

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

1166
        return input_tensor_code, ", ".join(kernel_args), kernel_signature
1167

1168 1169
    # Override by child class
    def gene_return_code(self):
1170
        return "return api_output;"
1171

1172
    # Override by child class
1173 1174 1175 1176 1177 1178 1179
    def gene_output(
        self,
        out_dtype_list,
        out_tensor_type_list=None,
        code_indent='',
        inplace_flag=False,
    ):
1180 1181
        return None, None, None

1182 1183
    def gen_kernel_code(self, kernel_name, code_indent, inplace_flag=False):
        kernel_dispatch = self.kernel['dispatch'][kernel_name]
1184
        input_tensors, kernel_args, kernel_signature = self.get_kernel_args(
1185 1186
            kernel_dispatch, code_indent
        )
1187
        out_tensor_type_list = kernel_dispatch[1] if kernel_dispatch else None
1188
        outputs_args, kernel_output_names, output_create = self.gene_output(
1189 1190 1191 1192 1193
            self.outputs['types'],
            out_tensor_type_list,
            code_indent,
            inplace_flag,
        )
1194 1195
        fallback_kernel_output_trans = ""
        for kernel_out in outputs_args:
1196
            fallback_kernel_output_trans += f"""
1197
{code_indent}    TransDataBackend({kernel_out}, kernel_backend, {kernel_out});"""
1198
        return f"""
F
From00 已提交
1199
{code_indent}  VLOG(6) << "{self.api} API kernel key: [" << kernel_backend << ", " << kernel_layout << ", "<< kernel_data_type << "]";
1200
{code_indent}  auto kernel_result = phi::KernelFactory::Instance().SelectKernelOrThrowError(
1201
{code_indent}      "{kernel_name}", {{kernel_backend, kernel_layout, kernel_data_type}});
1202
{code_indent}  const auto& kernel = kernel_result.kernel;
1203
{code_indent}  VLOG(6) << "{kernel_name} kernel: " << kernel;
1204
{code_indent}  auto* dev_ctx = GetDeviceContextByBackend(kernel_result.has_fallback_cpu ? Backend::CPU : kernel_backend);
1205 1206
{input_tensors}
{output_create}
1207 1208 1209 1210
{code_indent}  paddle::platform::RecordEvent *infer_shape_record_event = nullptr;
{code_indent}  if(paddle::platform::RecordEvent::IsEnabled()){{
{code_indent}    infer_shape_record_event = new paddle::platform::RecordEvent(\"{self.api} infer_meta\", paddle::platform::TracerEventType::OperatorInner, 1);
{code_indent}  }}
1211
{self.gene_infer_meta(kernel_output_names, code_indent)}
1212 1213 1214
{code_indent}  if(infer_shape_record_event != nullptr){{
{code_indent}    delete infer_shape_record_event;
{code_indent}  }}
1215 1216
{code_indent}  using kernel_signature = {kernel_signature};
{code_indent}  auto* kernel_fn = kernel.GetVariadicKernelFn<kernel_signature>();
1217 1218 1219 1220
{code_indent}  paddle::platform::RecordEvent* kernel_record_event = nullptr;
{code_indent}  if(paddle::platform::RecordEvent::IsEnabled()){{
{code_indent}    kernel_record_event = new paddle::platform::RecordEvent(\"{self.api} compute\", paddle::platform::TracerEventType::OperatorInner, 1);
{code_indent}  }}
1221
{code_indent}    (*kernel_fn)({kernel_args}, {", ".join(outputs_args)});
1222 1223
{code_indent}  if(kernel_record_event != nullptr){{
{code_indent}    delete kernel_record_event;
1224 1225 1226
{code_indent}  }}
{code_indent}  if (kernel_result.has_fallback_cpu) {{
{fallback_kernel_output_trans}
1227
{code_indent}  }}
1228
{code_indent}  {self.gene_return_code()}"""
1229

1230
    def get_condition_code(self, kernel_name):
1231 1232 1233
        assert self.kernel['dispatch'][
            kernel_name
        ], f"{self.api} api: the tensor type of inputs and outputs for kernel isn't set, see also 'kernel:func' of 'scale' in ops.yaml."
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
        input_types = self.kernel['dispatch'][kernel_name][0]
        condition_list = []
        for i, in_type in enumerate(input_types):
            if in_type == "dense":
                if self.inputs['names'][i] in self.optional_vars:
                    condition_list.append(
                        f"(!{self.inputs['names'][i]} || {self.inputs['names'][i]}->is_dense_tensor())"
                    )
                else:
                    condition_list.append(
1244 1245
                        f"{self.inputs['names'][i]}.is_dense_tensor()"
                    )
1246 1247 1248 1249 1250 1251 1252
            else:
                if self.inputs['names'][i] in self.optional_vars:
                    condition_list.append(
                        f"(!{self.inputs['names'][i]} || {self.inputs['names'][i]}->is_selected_rows())"
                    )
                else:
                    condition_list.append(
1253 1254
                        f"{self.inputs['names'][i]}.is_selected_rows()"
                    )
1255
        return " && ".join(condition_list)
1256

1257 1258 1259 1260 1261 1262
    def gene_dispatch_code(self, kernel_name, inplace_flag=False):
        return f"""
  if ({self.get_condition_code(kernel_name)}) {{
{self.gen_kernel_code(kernel_name, '  ', inplace_flag)}
  }}
"""
1263

1264
    def gene_base_api_code(self, inplace_flag=False):
1265 1266 1267
        api_func_name = self.get_api_func_name()
        if inplace_flag and api_func_name[-1] != '_':
            api_func_name += '_'
1268
        api_code = f"""
1269
PADDLE_API {self.get_return_type(inplace_flag)} {api_func_name}({self.get_define_args(inplace_flag)}) {{
1270
{self.gene_kernel_select()}
1271
"""
1272

1273 1274 1275 1276
        if len(self.kernel['func']) > 1:
            kernel_dispatch_code = ''
            for kernel_name in self.kernel['func']:
                kernel_dispatch_code += self.gene_dispatch_code(
1277 1278 1279 1280 1281
                    kernel_name, inplace_flag
                )
            return (
                api_code
                + f"""
1282 1283 1284
{kernel_dispatch_code}
  PADDLE_THROW(phi::errors::Unimplemented(
          "The kernel of ({self.api}) for input tensors is unimplemented, please check the type of input tensors."));
1285
}}
1286
"""
1287
            )
1288
        else:
1289 1290 1291 1292
            return (
                api_code
                + self.gen_kernel_code(self.kernel['func'][0], '', inplace_flag)
                + """
1293
}
1294
"""
1295
            )
1296

1297 1298
    def gene_invoke_code(self, invoke_code, params_code):
        return f"""
1299
PADDLE_API {self.get_return_type()} {self.api}({params_code}) {{
1300 1301 1302
  return {invoke_code};
}}"""

1303 1304 1305
    def gene_api_code(self):
        if self.is_base_api:
            api_code = self.gene_base_api_code()
1306
            if len(self.inplace_map) > 0:
Z
zyfncg 已提交
1307 1308
                if self.api[-1] == '_':
                    api_code = ""
1309 1310 1311
                api_code = api_code + self.gene_base_api_code(inplace_flag=True)
            return api_code

1312
        else:
1313 1314
            invoke_func_name = self.invoke.split('(')[0].strip()
            if invoke_func_name in self.attrs['names']:
1315
                # Adjust the param whose name is same with api invoked.
1316
                pattern = r'\W' + invoke_func_name + '[^A-Za-z0-9_(]'
1317 1318 1319 1320 1321 1322

                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)
1323 1324 1325
                params_code = re.sub(
                    pattern, adjust_name, self.get_define_args()
                )
1326 1327
            else:
                invoke_code = self.invoke
1328 1329
                params_code = self.get_define_args()
            return self.gene_invoke_code(invoke_code, params_code)