def scatter_nd_add_fix_bug(ref, index, updates, name=None): """fix bug of paddle.fluid.layers.scatter_nd_add Args: ref (TYPE): NULL index (TYPE): NULL updates (TYPE): NULL name (TYPE): Default is None Returns: TODO Raises: NULL """ if ref.dtype != updates.dtype: raise ValueError("ref and updates must have same data type.") helper = LayerHelper('scatter_nd_add', **locals()) dtype = helper.input_dtype(input_param_name='ref') if name is None: output = helper.create_variable_for_type_inference(dtype) else: output = helper.create_variable( name=name, dtype=dtype, persistable=False) helper.append_op( type="scatter_nd_add", inputs={"X": ref, "Index": index, "Updates": updates}, outputs={"Out": output}) return output
def rrpn_box_coder(prior_box, prior_box_var, target_box, name=None): """ Args: prior_box(Variable): Box list prior_box is a 2-D Tensor with shape [M, 5] holds M boxes and data type is float32 or float64. Each box is represented as [x, y, w, h, angle], [x, y] is the center coordinate of the anchor box, [w, h] is the width and height of the anchor box, angle is rotated angle of prior_box. prior_box_var(List|Variable|None): "prior_box_var is a 2-D Tensor with shape [M, 5] holds M group of variance." target_box(Variable): This input can be a 2-D LoDTensor with shape [M, 5]. Each box is represented as [x, y, w, h, angle]. The data type is float32 or float64. name(str): Name of this layer. None by default. Returns: Variable: output_box(Variable): The output tensor of rrpn_box_coder_op with shape [N, 5] representing the result of N target boxes encoded with N Prior boxes and variances. N represents the number of box and 5 represents [x, y, w, h ,angle]. Examples: .. code-block:: python import paddle.fluid as fluid prior_box_decode = fluid.data(name='prior_box_decode', shape=[512, 5], dtype='float32') target_box_decode = fluid.data(name='target_box_decode', shape=[512, 5], dtype='float32') output_decode = rrpn_box_coder(prior_box=prior_box_decode, prior_box_var=[10, 10, 5, 5, 1], target_box=target_box_decode) """ helper = LayerHelper("rrpn_box_coder", **locals()) if name is None: output_box = helper.create_variable_for_type_inference( dtype=prior_box.dtype) else: output_box = helper.create_variable( name=name, dtype=prior_box.dtype, persistable=False) inputs = {"PriorBox": prior_box, "TargetBox": target_box} attrs = {} if isinstance(prior_box_var, Variable): inputs['PriorBoxVar'] = prior_box_var elif isinstance(prior_box_var, list): attrs['variance'] = prior_box_var else: raise TypeError( "Input variance of rrpn_box_coder must be Variable or list") helper.append_op( type="rrpn_box_coder", inputs=inputs, attrs=attrs, outputs={"OutputBox": output_box}) return output_box
def relu3(x, name=None): helper = LayerHelper("relu3", **locals()) out = helper.create_variable(type=x.type, name=name, dtype=x.dtype, persistable=False) helper.append_op(type="relu3", inputs={"X": x}, outputs={"Y": out}) return out
def test_dygraph(self): with fluid.dygraph.guard(): np_x = np.random.uniform(0.1, 1, [123]).astype(np.float32) x = fluid.dygraph.to_variable(np_x) self.assertTrue( np.allclose( paddle.logsumexp(x).numpy(), np.log(np.sum(np.exp(np_x))))) np_x = np.random.uniform(0.1, 1, [2, 3, 4]).astype(np.float32) x = fluid.dygraph.to_variable(np_x) self.assertTrue( np.allclose( paddle.logsumexp(x, dim=[1, 2]).numpy(), np.log(np.sum(np.exp(np_x), axis=(1, 2))))) np_x = np.random.uniform(0.1, 1, [2, 3, 4]).astype(np.float32) x = fluid.dygraph.to_variable(np_x) self.assertTrue( np.allclose( paddle.logsumexp(x, dim=[2]).numpy(), np.log(np.sum(np.exp(np_x), axis=(2))))) np_x = np.random.uniform(0.1, 1, [2, 3, 4]).astype(np.float32) x = fluid.dygraph.to_variable(np_x) self.assertTrue( np.allclose( paddle.logsumexp(x, keepdim=True).numpy(), np.log(np.sum(np.exp(np_x), keepdims=True)))) np_x = np.random.uniform(0.1, 1, [2, 3, 4]).astype(np.float32) x = fluid.dygraph.to_variable(np_x) helper = LayerHelper("test_logsumexp") out = helper.create_variable(type=x.type, name='out', dtype=x.dtype, persistable=False) paddle.logsumexp(x, out=out) self.assertTrue( np.allclose(out.numpy(), np.log(np.sum(np.exp(np_x)))))
def run_momentum_op(params, grads, velocitys, master_params, learning_rate, place, multi_precision, mu=0.9, rescale_grad=0.01, use_merged=False): assert len(params) == len(grads) assert len(params) == len(velocitys) if multi_precision: assert len(params) == len(master_params) op_type = 'merged_momentum' if use_merged else 'momentum' main = paddle.static.Program() startup = paddle.static.Program() with paddle.static.program_guard(main, startup): helper = LayerHelper(op_type, **locals()) attrs = { 'mu': mu, 'multi_precision': multi_precision, 'rescale_grad': rescale_grad, } param_vars = [ helper.create_variable(persistable=True, shape=p.shape, dtype=p.dtype) for p in params ] grad_vars = [ helper.create_variable(shape=g.shape, dtype=g.dtype) for g in grads ] velocity_vars = [ helper.create_variable(persistable=True, shape=v.shape, dtype=v.dtype) for v in velocitys ] lr_var = helper.create_variable(persistable=True, shape=learning_rate.shape, dtype=learning_rate.dtype) feed_dict = OrderedDict() feed_dict.update( OrderedDict([(p_var.name, p_val) for p_var, p_val in zip(param_vars, params)])) feed_dict.update( OrderedDict([(v_var.name, v_val) for v_var, v_val in zip(velocity_vars, velocitys)])) fetch_list = list(feed_dict.keys()) feed_dict.update( OrderedDict([(g_var.name, g_val) for g_var, g_val in zip(grad_vars, grads)])) feed_dict.update({lr_var.name: learning_rate}) if multi_precision: master_param_vars = [ helper.create_variable(persistable=True, shape=p.shape, dtype=p.dtype) for p in master_params ] feed_dict.update( OrderedDict([ (mp_var.name, mp_val) for mp_var, mp_val in zip(master_param_vars, master_params) ])) # CPUPlace does not use MasterParam if isinstance(place, paddle.CUDAPlace): fetch_list = fetch_list + [ mp_var.name for mp_var in master_param_vars ] else: master_param_vars = None if not use_merged: for i, (p, g, v) in enumerate(zip(param_vars, grad_vars, velocity_vars)): inputs = { 'Param': p, 'Grad': g, 'Velocity': v, 'LearningRate': lr_var, } outputs = {'ParamOut': p, 'VelocityOut': v} if multi_precision: inputs['MasterParam'] = master_param_vars[i] outputs['MasterParamOut'] = master_param_vars[i] helper.append_op(type=op_type, inputs=inputs, outputs=outputs, attrs=attrs) else: inputs = { 'Param': param_vars, 'Grad': grad_vars, 'Velocity': velocity_vars, 'LearningRate': lr_var, } outputs = {'ParamOut': param_vars, 'VelocityOut': velocity_vars} if multi_precision: inputs['MasterParam'] = master_param_vars outputs['MasterParamOut'] = master_param_vars helper.append_op(type=op_type, inputs=inputs, outputs=outputs, attrs=attrs) exe = paddle.static.Executor(place) with paddle.static.scope_guard(paddle.static.Scope()): exe.run(startup) return exe.run(main, feed=feed_dict, fetch_list=fetch_list)
def shuffle_batch(x, seed=None): """ This layer shuffle input tensor :attr:`x` . Normally, :attr:`x` is 2-D LoDTensor. :attr:`x` is a LoDTensor to be shuffled with shape :math:`[N_1, N_2, ..., N_k, D]` . Note that the last dim of input will not be shuffled. :math:`N_1 * N_2 * ... * N_k` numbers of elements with length :math:`D` will be shuffled randomly. For Example: .. code-block:: text Input: x.data = [[1, 2], [3, 4], [5, 6], [7, 8]] x.dims = [4, 2] Attrs: seed = 2019 Output: Out.data =[[7, 8], [1, 2], [3, 4], [5, 6]] Out.dims = [4, 2] Args: x (Variable): The input variable. The input variable is a N-D LoDTensor with type int, float32 or float64. seed (None|int|Variable): The start up seed. If set, seed will be set as the start up seed of shuffle engine. If not set(Default), start up seed of shuffle engine will be generated randomly. Returns: Variables: The shuffled LoDTensor with the same shape and lod as input. Examples: .. code-block:: python import paddle.fluid as fluid x = fluid.layers.data(name="x", shape=[-1, 4]) out = fluid.contrib.layers.shuffle_batch(x) """ helper = LayerHelper('shuffle_batch', **locals()) out = helper.create_variable_for_type_inference(dtype=x.dtype) shuffle_idx = helper.create_variable_for_type_inference(dtype=np.int64) if seed is None and helper.main_program.random_seed != 0: seed = helper.main_program.random_seed if seed is None: seed = np.random.randint(-65536, 65535) op_attrs = {} if isinstance(seed, int): op_attrs["startup_seed"] = seed seed = helper.create_variable( name=unique_name.generate("shuffle_batch_seed"), dtype="int64", persistable=True) helper.append_op(type='shuffle_batch', inputs={ 'X': x, 'Seed': seed }, outputs={ 'Out': out, 'ShuffleIdx': shuffle_idx, 'SeedOut': seed }, attrs=op_attrs) return out
def infer_transformer_encoder( input, q_weight, q_bias, k_weight, k_bias, v_weight, v_bias, attn_out_weight, attn_out_bias, attn_mask, norm1_weight, norm1_bias, norm2_weight, norm2_bias, ffn_inter_weight, ffn_inter_bias, ffn_out_weight, ffn_out_bias, # sequence_id_offset, # trt_seqlen_offset, # amax_list, n_head, size_per_head, n_layer=12, use_gelu=True, remove_padding=False, int8_mode=0, layer_idx=0, allow_gemm_test=False, use_trt_kernel=False, normalize_before=False): """ Fusion Encoder API intergrating Encoder inference in FasterTransformer. It accepts the weight and bias of TransformerEncoder and some other parameters for inference. """ helper = LayerHelper('fusion_encoder', **locals()) inputs = { 'Input': input, 'SelfQueryWeight': q_weight, 'SelfQueryBias': q_bias, 'SelfKeyWeight': k_weight, 'SelfKeyBias': k_bias, 'SelfValueWeight': v_weight, 'SelfValueBias': v_bias, 'SelfAttnOutputWeight': attn_out_weight, 'SelfAttnOutputBias': attn_out_bias, "SelfAttnMask": attn_mask, 'SelfAttnOutputLayernormWeight': norm1_weight, 'SelfAttnOutputLayernormBias': norm1_bias, 'OutputLayernormWeight': norm2_weight, 'OutputLayernormBias': norm2_bias, 'FFNInterWeight': ffn_inter_weight, 'FFNInterBias': ffn_inter_bias, 'FFNOutputWeight': ffn_out_weight, 'FFNOutputBias': ffn_out_bias, # "SequenceIdOffset": sequence_id_offset, # "TRTSeqLenOffset": trt_seqlen_offset, # 'AmaxList': amax_list } attrs = { 'head_num': n_head, 'size_per_head': size_per_head, 'use_gelu': use_gelu, "remove_padding": remove_padding, 'int8_mode': int8_mode, 'num_layer': n_layer, 'layer_idx': layer_idx, 'allow_gemm_test': allow_gemm_test, 'use_trt_kernel': use_trt_kernel, 'normalize_before': normalize_before } encoder_out = helper.create_variable(dtype=input.dtype) outputs = {"EncoderOut": encoder_out} helper.append_op(type='fusion_encoder', inputs=inputs, outputs=outputs, attrs=attrs) return encoder_out
def infer_transformer_decoder( from_tensor, memory_tensor, mem_seq_len, self_ln_weight, self_ln_bias, self_q_weight, self_q_bias, self_k_weight, self_k_bias, self_v_weight, self_v_bias, self_out_weight, self_out_bias, cross_ln_weight, cross_ln_bias, cross_q_weight, cross_q_bias, cross_k_weight, cross_k_bias, cross_v_weight, cross_v_bias, cross_out_weight, cross_out_bias, ffn_ln_weight, ffn_ln_bias, ffn_inter_weight, ffn_inter_bias, ffn_out_weight, ffn_out_bias, old_self_cache, old_mem_cache, n_head, size_per_head): helper = LayerHelper('fusion_decoder', **locals()) inputs = { "FromTensor": from_tensor, "MemoryTensor": memory_tensor, "MemSeqLen": mem_seq_len, "SelfLayernormWeight": self_ln_weight, "SelfLayernormBias": self_ln_bias, "SelfQueryWeight": self_q_weight, "SelfQueryBias": self_q_bias, "SelfKeyWeight": self_k_weight, "SelfKeyBias": self_k_bias, "SelfValueWeight": self_v_weight, "SelfValueBias": self_v_bias, "SelfOutWeight": self_out_weight, "SelfOutBias": self_out_bias, "CrossLayernormWeight": cross_ln_weight, "CrossLayernormBias": cross_ln_bias, "CrossQueryWeight": cross_q_weight, "CrossQueryBias": cross_q_bias, "CrossKeyWeight": cross_k_weight, "CrossKeyBias": cross_k_bias, "CrossValueWeight": cross_v_weight, "CrossValueBias": cross_v_bias, "CrossOutWeight": cross_out_weight, "CrossOutBias": cross_out_bias, "FFNLayernormWeight": ffn_ln_weight, "FFNLayernormBias": ffn_ln_bias, "FFNInterWeight": ffn_inter_weight, "FFNInterBias": ffn_inter_bias, "FFNOutWeight": ffn_out_weight, "FFNOutBias": ffn_out_bias, "OldSelfCache": old_self_cache, "OldMemCache": old_mem_cache } attrs = {'n_head': n_head, 'size_per_head': size_per_head} decoder_output = helper.create_variable(dtype=memory_tensor.dtype) new_self_cache = helper.create_variable(dtype=memory_tensor.dtype) new_mem_cache = helper.create_variable(dtype=memory_tensor.dtype) outputs = { 'DecoderOutput': decoder_output, 'NewSelfCache': new_self_cache, 'NewMemCache': new_mem_cache } helper.append_op(type='fusion_decoder', inputs=inputs, outputs=outputs, attrs=attrs) return decoder_output, new_self_cache, new_mem_cache
def dot(x, y, name=None): """ This operator calculates inner product for vectors. .. note:: Support 1-d and 2-d Tensor. When it is 2d, the first dimension of this matrix is the batch dimension, which means that the vectors of multiple batches are dotted. Parameters: x(Tensor): 1-D or 2-D ``Tensor``. Its dtype should be ``float32``, ``float64``, ``int32``, ``int32`` y(Tensor): 1-D or 2-D ``Tensor``. Its dtype soulde be ``float32``, ``float64``, ``int32``, ``int32`` name(str, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name` Returns: Variable: the calculated result Tensor. Examples: .. code-block:: python import paddle import numpy as np paddle.disable_static() x_data = np.random.uniform(0.1, 1, [10]).astype(np.float32) y_data = np.random.uniform(1, 3, [10]).astype(np.float32) x = paddle.to_tensor(x_data) y = paddle.to_tensor(y_data) z = paddle.dot(x, y) print(z.numpy()) """ op_type = 'dot' # skip var type check in dygraph mode to improve efficiency if in_dygraph_mode(): op = getattr(core.ops, op_type) return op(x, y) assert x is not None, 'x cannot be None in {}'.format(op_type) assert y is not None, 'y cannot be None in {}'.format(op_type) check_variable_and_dtype(x, 'x', ['float32', 'float64', 'int32', 'int32'], op_type) check_variable_and_dtype(y, 'y', ['float32', 'float64', 'int32', 'int32'], op_type) helper = LayerHelper(op_type, **locals()) if name is None: out = helper.create_variable_for_type_inference(dtype=x.dtype) else: out = helper.create_variable(name=name, dtype=x.dtype, persistable=False) helper.append_op(type="dot", inputs={ 'X': x, 'Y': y }, attrs={}, outputs={"Out": out}) return out
def infer_transformer_decoder( enc_output, memory_seq_lens, word_emb, slf_ln_weight, slf_ln_bias, slf_q_weight, slf_q_bias, slf_k_weight, slf_k_bias, slf_v_weight, slf_v_bias, slf_out_weight, slf_out_bias, cross_ln_weight, cross_ln_bias, cross_q_weight, cross_q_bias, cross_k_weight, cross_k_bias, cross_v_weight, cross_v_bias, cross_out_weight, cross_out_bias, ffn_ln_weight, ffn_ln_bias, ffn_inter_weight, ffn_inter_bias, ffn_out_weight, ffn_out_bias, decoder_ln_weight, decoder_ln_bias, linear_weight, linear_bias, pos_emb, _decoding_strategy, _beam_size, _topk, _topp, _n_head, _size_per_head, _n_layer, _bos_id, _eos_id, _max_out_len, _beam_search_diversity_rate): helper = LayerHelper('fusion_decoding', **locals()) inputs = { 'Input': enc_output, 'MemSeqLen': memory_seq_lens, 'WordEmbedding': word_emb, 'SelfLayernormWeight@VECTOR': slf_ln_weight, 'SelfLayernormBias@VECTOR': slf_ln_bias, 'SelfQueryWeight@VECTOR': slf_q_weight, 'SelfQueryBias@VECTOR': slf_q_bias, 'SelfKeyWeight@VECTOR': slf_k_weight, 'SelfKeyBias@VECTOR': slf_k_bias, 'SelfValueWeight@VECTOR': slf_v_weight, 'SelfValueBias@VECTOR': slf_v_bias, 'SelfOutWeight@VECTOR': slf_out_weight, 'SelfOutBias@VECTOR': slf_out_bias, 'CrossLayernormWeight@VECTOR': cross_ln_weight, 'CrossLayernormBias@VECTOR': cross_ln_bias, 'CrossQueryWeight@VECTOR': cross_q_weight, 'CrossQueryBias@VECTOR': cross_q_bias, 'CrossKeyWeight@VECTOR': cross_k_weight, 'CrossKeyBias@VECTOR': cross_k_bias, 'CrossValueWeight@VECTOR': cross_v_weight, 'CrossValueBias@VECTOR': cross_v_bias, 'CrossOutWeight@VECTOR': cross_out_weight, 'CrossOutBias@VECTOR': cross_out_bias, 'FFNLayernormWeight@VECTOR': ffn_ln_weight, 'FFNLayernormBias@VECTOR': ffn_ln_bias, 'FFNInterWeight@VECTOR': ffn_inter_weight, 'FFNInterBias@VECTOR': ffn_inter_bias, 'FFNOutWeight@VECTOR': ffn_out_weight, 'FFNOutBias@VECTOR': ffn_out_bias, 'DecoderLayernormWeight': decoder_ln_weight, 'DecoderLayernormBias': decoder_ln_bias, 'EmbWeight': linear_weight, 'EmbBias': linear_bias, 'PositionEncEmb': pos_emb } attrs = { 'decoding_strategy': _decoding_strategy, 'beam_size': _beam_size, 'topk': _topk, 'topp': _topp, 'n_head': _n_head, 'size_per_head': _size_per_head, 'num_layer': _n_layer, 'bos_id': _bos_id, 'eos_id': _eos_id, 'max_len': _max_out_len, 'beam_search_diversity_rate': _beam_search_diversity_rate } output_ids = helper.create_variable(dtype="int32") parent_ids = helper.create_variable(dtype="int32") sequence_length = helper.create_variable(dtype="int32") outputs = { 'OutputIds': output_ids, 'ParentIds': parent_ids, 'SequenceLength': sequence_length } helper.append_op(type='fusion_decoding', inputs=inputs, outputs=outputs, attrs=attrs) return output_ids, parent_ids, sequence_length