Esempio n. 1
0
def generate(info, func_name, func_name_snakecase, template):
    io_info = common.function_io(info)
    ctypes = ', '.join(io_info['template_types'])
    templates = ', '.join(io_info['templates'])
    template_defines = ', '.join(['typename {}'.format(t)
                                  for t in io_info['templates']])

    return template.format(func_name=func_name,
                           func_name_snakecase=func_name_snakecase,
                           template_defines=template_defines,
                           templates=templates,
                           ctypes=ctypes)
Esempio n. 2
0
def generate(info, func_name, func_name_snakecase, template):

    arg_info = common.function_arguments(info)
    func_arg_variable_defines = '\n'.join(['protected:'] + [
        '{} {}_;'.format(utils.type_conv.type_from_proto[t]['cpp_var'], n)
        for t, n in zip(arg_info['types'], arg_info['names'])
    ])
    func_arg_variable_types = ', '.join(
        [func_name] +
        [utils.type_conv.type_from_proto[t]['cpp'] for t in arg_info['types']])
    func_arg_initializers = ', '.join(
        ['{0}_({0})'.format(n) for n in arg_info['names']])
    func_arg_variables = ', '.join(
        ['ctx_'] + ['{}_'.format(n) for n in arg_info['names']])
    func_args = ', '.join(['const Context &ctx'] + [
        '{} {}'.format(utils.type_conv.type_from_proto[t]['cpp'], n)
        for t, n in zip(arg_info['types'], arg_info['names'])
    ])

    io_info = common.function_io(info)
    template_defines = ', '.join(
        ['typename {}'.format(t) for t in io_info['templates']])
    in_types = ', '.join(
        ['get_dtype<{}>()'.format(t) for t in io_info['input']['types']])
    out_types = ', '.join(
        ['get_dtype<{}>()'.format(t) for t in io_info['output']['types']])
    min_inputs = io_info['input']['min']
    min_outputs = io_info['output']['min']

    base_function_types = ', '.join(
        [utils.type_conv.type_from_proto[t]['cpp'] for t in arg_info['types']])
    base_function_args = ', '.join(['ctx'] +
                                   ['{}'.format(n) for n in arg_info['names']])

    return template.format(func_name=func_name,
                           func_name_upcase=func_name.upper(),
                           template_defines=template_defines,
                           func_args=func_args,
                           func_arg_variable_defines=func_arg_variable_defines,
                           func_arg_variable_types=func_arg_variable_types,
                           func_arg_variables=func_arg_variables,
                           func_arg_initializers=func_arg_initializers,
                           in_types=in_types,
                           out_types=out_types,
                           min_inputs=min_inputs,
                           min_outputs=min_outputs,
                           base_function_types=base_function_types,
                           base_function_args=base_function_args)
def generate(info, func_name, func_name_snakecase, template):

    arg_info = common.function_arguments(info)
    func_arg_variables = ', '.join(
        ['ctx'] + ['{}'.format(n) for n in arg_info['names']])
    func_args = ', '.join(['const Context &ctx'] + ['{} {}'.format(utils.type_conv.type_from_proto[
                          t]['cpp'], n) for t, n in zip(arg_info['types'], arg_info['names'])])

    io_info = common.function_io(info)
    template_defines = ', '.join(['typename {}'.format(t)
                                  for t in io_info['templates']])
    templates = ', '.join(io_info['templates'])

    return template.format(func_name=func_name,
                           func_name_snakecase=func_name_snakecase,
                           func_name_upcase=func_name.upper(),
                           templates=templates,
                           template_defines=template_defines,
                           func_args=func_args,
                           func_arg_variables=func_arg_variables)
def generate(info, func_name, func_name_snakecase, template):

    arg_info = common.function_arguments(info)
    func_arg_variable_defines = '\n'.join(['protected:'] + ['{} {}_;'.format(utils.type_conv.type_from_proto[
                                          t]['cpp_var'], n) for t, n in zip(arg_info['types'], arg_info['names'])])
    func_arg_variable_types = ', '.join(
        [func_name] + [utils.type_conv.type_from_proto[t]['cpp'] for t in arg_info['types']])
    func_arg_initializers = ', ' + ', '.join(['{0}_({0})'.format(n) for n in arg_info['names']])
    func_arg_variables = ', '.join(
        ['ctx_'] + ['{}_'.format(n) for n in arg_info['names']])
    func_args = ', '.join(['const Context &ctx'] + ['{} {}'.format(utils.type_conv.type_from_proto[
                          t]['cpp'], n) for t, n in zip(arg_info['types'], arg_info['names'])])

    io_info = common.function_io(info)
    template_defines = ', '.join(['typename {}'.format(t)
                                  for t in io_info['templates']])
    in_types = ', '.join(['get_dtype<{}>()'.format(t)
                          for t in io_info['input']['types']])
    out_types = ', '.join(['get_dtype<{}>()'.format(t)
                           for t in io_info['output']['types']])
    min_inputs = io_info['input']['min']
    min_outputs = io_info['output']['min']

    base_function_types = ', '.join([utils.type_conv.type_from_proto[t]['cpp'] for t in arg_info['types']])
    base_function_args = ', '.join(['ctx'] + ['{}'.format(n) for n in arg_info['names']])

    return template.format(func_name=func_name,
                           func_name_upcase=func_name.upper(),
                           template_defines=template_defines,
                           func_args=func_args,
                           func_arg_variable_defines=func_arg_variable_defines,
                           func_arg_variable_types=func_arg_variable_types,
                           func_arg_variables=func_arg_variables,
                           func_arg_initializers=func_arg_initializers,
                           in_types=in_types,
                           out_types=out_types,
                           min_inputs=min_inputs,
                           min_outputs=min_outputs,
                           base_function_types=base_function_types,
                           base_function_args=base_function_args)
Esempio n. 5
0
def generate(info, func_name, func_name_snakecase, template):

    arg_info = common.function_arguments(info)
    func_arg_variables = ', '.join(['ctx'] +
                                   ['{}'.format(n) for n in arg_info['names']])
    func_args = ', '.join(['const Context &ctx'] + [
        '{} {}'.format(utils.type_conv.type_from_proto[t]['cpp'], n)
        for t, n in zip(arg_info['types'], arg_info['names'])
    ])

    io_info = common.function_io(info)
    template_defines = ', '.join(
        ['typename {}'.format(t) for t in io_info['templates']])
    templates = ', '.join(io_info['templates'])

    return template.format(func_name=func_name,
                           func_name_snakecase=func_name_snakecase,
                           func_name_upcase=func_name.upper(),
                           templates=templates,
                           template_defines=template_defines,
                           func_args=func_args,
                           func_arg_variables=func_arg_variables)
Esempio n. 6
0
def generate(info, template):
    apis = []
    for function, function_info in each_function(info):
        io_info = function_io(function_info)
        arg_info = function_arguments(function_info)

        desc = function_info['description']

        variadic = False
        desc.append('Args:')
        for a_n, a_info in function_info['input'].items():
            a_type = '~nnabla.Variable'
            if 'Options' in a_info:
                if 'Variadic' in a_info['Options']:
                    variadic = True
            name = a_n
            if variadic:
                name = '*inputs'
            if 'Options' in a_info:
                desc.append('    {}({}): [{}] {}'.format(
                    name, a_type, a_info['Options'], a_info['Description']))
            else:
                desc.append('    {}({}): {}'.format(name, a_type,
                                                    a_info['Description']))

        if 'argument' in function_info:
            for a_n, a_info in function_info['argument'].items():

                a_type = a_info['Type']
                if a_type == 'Shape':
                    a_type = ':obj:`tuple` of :obj:`int`'
                elif a_type == 'int64':
                    a_type = 'int'
                elif a_type == 'double':
                    a_type = 'float'

                if variadic:
                    desc.append('    **param({}): [name={}] {}'.format(
                        a_type, a_n, a_info['Description']))
                else:
                    desc.append('    {}({}): {}'.format(
                        a_n, a_type, a_info['Description']))

        desc.append('')
        desc.append('Returns:')

        for o_n, o_info in function_info['output'].items():
            desc.append('    ~nnabla.Variable: {}'.format(
                o_info['Description']))

        desc.append('')

        description = '\n    '.join(desc)

        if io_info['input']['repeat']:
            apis.append(
                generate_repeated_inputs(info, function, io_info, arg_info,
                                         description))
            continue

        args1 = []
        args2 = []
        args3 = []
        args3_optional = []
        optional_code = []
        set_default_values = []

        for i, n in enumerate(io_info['input']['names']):
            if i < io_info['input']['min']:
                args1.append(n)
                args3.append(n)
            else:
                args1.append('{} = None'.format(n))
                args3_optional.append(n)

        for n, d in zip(arg_info['names'], arg_info['defaults']):
            args2.append(n)
            if d is None:
                args1.append(n)
            else:
                if d.__class__.__name__ == "DefaultRHS":
                    args1.append('{}=None'.format(n))
                    set_default_values.append('    if {} is None:'.format(n))
                    set_default_values.append('        {} = {}'.format(
                        n, d.rhs))
                else:
                    args1.append('{}={}'.format(n, repr(d)))

        if len(args1) > 0:
            args1 = ', ' + ', '.join(args1)
        else:
            args1 = ''
        if len(args2) > 0:
            args2 = ', ' + ', '.join(args2)
        else:
            args2 = ''
        if len(args3_optional) > 0:
            optional_code.append('    inputs = ({})'.format(', '.join(args3)))
            for a in args3_optional:
                optional_code.append('    if {} is not None:'.format(a))
                optional_code.append('        inputs += ({}, )'.format(a))
            args3 = '*inputs'
        else:
            args3 = ','.join(args3)
        if args3:
            args3 += ', '

        apis.append('')
        apis.append('')
        apis.append('@function_api')
        apis.append('def {}(ctx{}, n_outputs=-1, outputs=None):'.format(
            info['Names'][function], args1))
        apis.append('    r"""{}'.format(description))
        apis.append('    """')
        apis.append('')
        apis += optional_code
        apis += set_default_values
        apis.append(
            '    return F.{}(ctx{})({}n_outputs=n_outputs, auto_forward=get_auto_forward(), outputs=outputs)'
            .format(function, args2, args3))
    return template.format(function_apis='\n'.join(apis))
def generate(info, template):
    apis = []
    for function, function_info in each_function(info):
        io_info = function_io(function_info)
        arg_info = function_arguments(function_info)

        desc = function_info['description']

        variadic = False
        desc.append('Args:')
        for a_n, a_info in function_info['input'].items():
            a_type = '~nnabla.Variable'
            if 'Options' in a_info:
                if 'Variadic' in a_info['Options']:
                    variadic = True
            name = a_n
            if variadic:
                name = '*inputs'
            if 'Options' in a_info:
                desc.append('    {}({}): [{}] {}'.format(
                    name, a_type, a_info['Options'], a_info['Description']))
            else:
                desc.append('    {}({}): {}'.format(
                    name, a_type, a_info['Description']))

        if 'argument' in function_info:
            for a_n, a_info in function_info['argument'].items():

                a_type = a_info['Type']
                if a_type == 'Shape':
                    a_type = ':obj:`tuple` of :obj:`int`'
                elif a_type == 'int64':
                    a_type = 'int'
                elif a_type == 'double':
                    a_type = 'float'

                if variadic:
                    desc.append(
                        '    **param({}): [name={}] {}'.format(a_type, a_n, a_info['Description']))
                else:
                    desc.append('    {}({}): {}'.format(
                        a_n, a_type, a_info['Description']))

        desc.append('')
        desc.append('Returns:')

        for o_n, o_info in function_info['output'].items():
            desc.append('    ~nnabla.Variable: {}'.format(
                o_info['Description']))

        desc.append('')

        description = '\n    '.join(desc)

        if io_info['input']['repeat']:
            apis.append(generate_repeated_inputs(
                info, function, io_info, arg_info, description))
            continue

        args1 = []
        args2 = []
        args3 = []
        args3_optional = []
        optional_code = []
        set_default_values = []

        for i, n in enumerate(io_info['input']['names']):
            if i < io_info['input']['min']:
                args1.append(n)
                args3.append(n)
            else:
                args1.append('{} = None'.format(n))
                args3_optional.append(n)

        for n, d in zip(arg_info['names'], arg_info['defaults']):
            args2.append(n)
            if d is None:
                args1.append(n)
            else:
                if d.__class__.__name__ == "DefaultRHS":
                    args1.append('{}=None'.format(n))
                    set_default_values.append('    if {} is None:'.format(n))
                    set_default_values.append(
                        '        {} = {}'.format(n, d.rhs))
                else:
                    args1.append('{}={}'.format(n, repr(d)))

        if len(args1) > 0:
            args1 = ', ' + ', '.join(args1)
        else:
            args1 = ''
        if len(args2) > 0:
            args2 = ', ' + ', '.join(args2)
        else:
            args2 = ''
        if len(args3_optional) > 0:
            optional_code.append('    inputs = ({})'.format(', '.join(args3)))
            for a in args3_optional:
                optional_code.append('    if {} is not None:'.format(a))
                optional_code.append('        inputs += ({}, )'.format(a))
            args3 = '*inputs'
        else:
            args3 = ','.join(args3)
        if args3:
            args3 += ', '

        apis.append('')
        apis.append('')
        apis.append('@function_api')
        apis.append('def {}(ctx{}, n_outputs=-1, outputs=None):'.format(
            info['Names'][function], args1))
        apis.append('    r"""{}'.format(description))
        apis.append('    """')
        apis.append('')
        apis += optional_code
        apis += set_default_values
        apis.append('    return F.{}(ctx{})({}n_outputs=n_outputs, auto_forward=get_auto_forward(), outputs=outputs)'.format(
            function, args2, args3))
    return template.format(function_apis='\n'.join(apis))