dygraph.py 6.4 KB
Newer Older
1
import os
W
whs 已提交
2 3 4 5
import paddle
import collections
import logging
import numpy as np
6 7
from paddle.fluid.framework import _dygraph_tracer, dygraph_only, _dygraph_guard, program_guard
from paddle.fluid.dygraph.base import program_desc_tracing_guard, _switch_declarative_mode_guard_
W
whs 已提交
8 9 10 11 12 13 14 15 16
from paddle.fluid.dygraph.layers import Layer
from paddle.fluid.framework import Block, ParamBase, Program, Variable
from ..common import get_logger

__all__ = ["dygraph2program"]

_logger = get_logger(__name__, level=logging.INFO)


17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
class NameGenerator:
    def __init__(self):
        self.ids = collections.defaultdict(int)

    def name(self, prefix):
        assert isinstance(prefix, str)

        name = "{}_{}".format(prefix, self.ids[prefix])
        self.ids[prefix] += 1

        return name


NG = NameGenerator()


W
whs 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
def _is_shape(values):
    if not isinstance(values, (list, tuple)):
        return False
    for v in values:
        if not isinstance(v, int):
            return False
    return True


def _is_shapes(values):
    if not isinstance(values, (list, tuple)):
        return False
    for v in values:
        if not _is_shape(v):
            return False
    return True


51
def _create_tensors(shapes, dtypes=None, is_static=False):
W
whs 已提交
52 53 54 55 56 57 58 59 60
    if dtypes is not None:
        assert len(shapes) == len(
            dtypes
        ), "Length of shapes and dtypes must be same. But get len(shapes): {}; len(dtypes): {}; shapes: {}; dtypes: {}".format(
            len(shapes), len(dtypes), shapes, dtypes)
    else:
        dtypes = len(shapes) * ['float32']
    tensors = []
    for shape, dtype in zip(shapes, dtypes):
61 62 63 64 65 66 67
        if is_static:
            tensors.append(
                paddle.static.data(
                    shape=shape, dtype=dtype, name=NG.name("feed")))
        else:
            data = np.ones(tuple(shape)).astype(dtype)
            tensors.append(paddle.to_tensor(data))
W
whs 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    return tensors


def extract_vars(inputs):
    """
    Extract a list of variables from inputs.
    Args:
        inputs(Variable | list<Object> | dict): 
    """
    vars = []
    if isinstance(inputs, Variable):
        vars = [inputs]
    elif isinstance(inputs, dict):
        for _key, _value in inputs.items():
            if isinstance(_value, Variable):
                vars.append(_value)
            else:
                _logger.warn(
                    f"Variable is excepted, but get an element with type({type(_value)}) from inputs whose type is dict. And the key of element is {_key}."
                )
    elif isinstance(inputs, (tuple, list)):
C
Chang Xu 已提交
89

W
whs 已提交
90 91 92 93 94 95 96
        for _value in inputs:
            vars.extend(extract_vars(_value))
    if len(vars) == 0:
        _logger.warn(f"Extract none variables from inputs.")
    return vars


97 98 99 100 101 102 103 104 105 106 107
def _to_var(x):
    """
    Convert Variable or np.array into Placeholder.
    """
    shape = x.shape
    dtype = x.dtype
    name = getattr(x, "name", None) or NG.name("feed")
    return paddle.static.data(shape=shape, dtype=dtype, name=name)


def to_variables(inputs, is_static=False):
W
whs 已提交
108 109 110
    """
    Find and rename variables. Find np.ndarray and convert it to variable.
    """
111 112 113 114 115 116
    if isinstance(inputs,
                  (Variable, paddle.Tensor)) or isinstance(inputs, np.ndarray):
        if is_static:
            return _to_var(inputs)
        else:
            return paddle.fluid.dygraph.to_variable(inputs)
W
whs 已提交
117 118 119
    elif isinstance(inputs, dict):
        ret = {}
        for _key in inputs:
120
            ret[_key] = to_variables(inputs[_key], is_static)
W
whs 已提交
121 122 123 124
        return inputs
    elif isinstance(inputs, list):
        ret = []
        for _value in inputs:
125
            ret.append(to_variables(_value, is_static))
W
whs 已提交
126 127 128 129 130 131 132 133 134 135 136 137
        return ret


@dygraph_only
def dygraph2program(layer,
                    inputs,
                    feed_prefix='feed_',
                    fetch_prefix='fetch_',
                    tmp_prefix='t_',
                    extract_inputs_fn=None,
                    extract_outputs_fn=None,
                    dtypes=None):
138
    print(type(layer))
W
whs 已提交
139 140 141
    assert isinstance(layer, Layer)
    extract_inputs_fn = extract_inputs_fn if extract_inputs_fn is not None else extract_vars
    extract_outputs_fn = extract_outputs_fn if extract_outputs_fn is not None else extract_vars
142 143 144 145 146

    if os.environ.get("FLAGS_enable_eager_mode") == "1":
        return _dy2prog(layer, inputs, feed_prefix, fetch_prefix, tmp_prefix,
                        extract_inputs_fn, extract_outputs_fn, dtypes)

W
whs 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160
    tracer = _dygraph_tracer()._get_program_desc_tracer()

    with program_desc_tracing_guard(True):

        if _is_shape(inputs):
            shapes = [inputs]
            inputs = _create_tensors(shapes, dtypes=dtypes)
            input_var_list = inputs
        elif _is_shapes(inputs):
            inputs = _create_tensors(inputs, dtypes=dtypes)
            input_var_list = inputs
        else:
            inputs = to_variables(inputs)
            input_var_list = extract_inputs_fn(inputs)
C
Chang Xu 已提交
161

W
whs 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175
        original_outputs = layer(*inputs)
        # 'original_outputs' may be dict, so we should convert it to list of varibles.
        # And should not create new varibles in 'extract_vars'.
        out_var_list = extract_outputs_fn(original_outputs)
        program_desc, feed_names, fetch_names, parameters = tracer.create_program_desc(
            input_var_list, feed_prefix, out_var_list, fetch_prefix, tmp_prefix)
        tracer.reset()

    with _dygraph_guard(None):
        program = Program()
        program.desc = program_desc
        program.blocks = [Block(program, 0)]
        program._sync_with_cpp()
    return program
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206


def _dy2prog(layer,
             inputs,
             feed_prefix='feed_',
             fetch_prefix='fetch_',
             tmp_prefix='t_',
             extract_inputs_fn=None,
             extract_outputs_fn=None,
             dtypes=None):
    """
    Tracing program in Eager Mode.
    """
    paddle.enable_static()

    program = Program()
    # convert ParamBase into Parameter automatically by _switch_declarative_mode_guard_
    with program_guard(program), _switch_declarative_mode_guard_(True):
        if _is_shape(inputs):
            shapes = [inputs]
            inputs = _create_tensors(shapes, dtypes=dtypes, is_static=True)
        elif _is_shapes(inputs):
            inputs = _create_tensors(inputs, dtypes=dtypes, is_static=True)
        else:
            inputs = to_variables(inputs, is_static=True)
            inputs = extract_inputs_fn(inputs)
        outputs = layer(*inputs)

    paddle.disable_static()

    return program