tensor array 赋值错误
Created by: Angus07
我用fluid.layers.create_array(dtype='float32')建立了一个array 在 with while_op.block() 循环中赋值: layers.array_write(weights, i=step_idx, array=weights_array) 并且这时候确保是有数据的(打印出来看过了) 但是在循环结束后,我尝试读取或操作这个array,比如 step_idx = layers.fill_constant( shape=[1], dtype=tgt_ids.dtype, value=0, force_cpu=True) weight_debug = layers.array_read(weights_array, i=step_idx) layers.Print(weight_debug, message="weights_debug", summarize=100) 就报错了,报错信息如下,极大概率是整个array 都是空的:
Error Message Summary:
PaddleCheckError: holder_ should not be null Tensor holds no memory. Call Tensor::mutable_data first. at [/paddle/paddle/fluid/framework/tensor.cc:23] [operator < read_from_array > error]
详情代码如下: weights_array = fluid.layers.create_array(dtype='float32') global_src_id = src_ids with while_op.block(): pre_ids = layers.array_read(array=ids, i=step_idx) pre_ids = layers.reshape(pre_ids, (-1, 1, 1), inplace=True) pre_scores = layers.array_read(array=scores, i=step_idx)
tmp_tgt_input_mask = layers.array_read(tgt_masks, i=step_idx)
tmp_tgt_input_mask_encoder = layers.array_read(tgt_masks_encoder, i=step_idx)
append_mask = layers.fill_constant_batch_size_like(
input=tmp_tgt_input_mask,
value=1.0,
shape=[-1, 1, 1],
dtype=tmp_tgt_input_mask.dtype)
append_mask_encoder = layers.fill_constant_batch_size_like(
input=tmp_tgt_input_mask,
value=0.0,
shape=[-1, 1, 1],
dtype=tmp_tgt_input_mask.dtype)
tmp_tgt_input_mask = layers.concat([tmp_tgt_input_mask, append_mask], axis=2)
tmp_tgt_input_mask_encoder = layers.concat([tmp_tgt_input_mask_encoder, append_mask_encoder], axis=2)
pre_mask = layers.gather(input=tmp_tgt_input_mask, index=parent_idx)
pre_mask_encoder = layers.gather(input=tmp_tgt_input_mask_encoder, index=parent_idx)
pre_pos = layers.elementwise_mul(
x=layers.fill_constant_batch_size_like(
input=pre_mask,
value=1,
shape=[-1, 1, 1],
dtype=pre_ids.dtype), y=step_idx, axis=0)
type_ids = layers.fill_constant_batch_size_like(
input=pre_mask,
value=args.tgt_type_id,
shape=[-1, 1, 1],
dtype=pre_ids.dtype)
dec_out, weights, gens = ernie.encode(pre_ids, pre_pos, type_ids, pre_mask, pre_mask_encoder, parent_idx)
tmp_id = layers.gather(input=global_src_id, index=parent_idx)
tmp_id = layers.concat([tmp_id, pre_ids], axis=1)
layers.assign(tmp_id, global_src_id)
fc_out = cal_logit(tmp_id, dec_out, None, args, ernie_config, weights, gens)
topk_scores, topk_indices = layers.topk(
input=fc_out, k=args.beam_size)
accu_scores = layers.elementwise_add(
x=layers.log(topk_scores), y=pre_scores, axis=0)
topk_indices = layers.lod_reset(topk_indices, pre_ids)
accu_scores = layers.lod_reset(accu_scores, pre_ids)
selected_ids, selected_scores, gather_idx = layers.beam_search(
pre_ids=pre_ids,
pre_scores=pre_scores,
ids=topk_indices,
scores=accu_scores,
beam_size=args.beam_size,
end_id=args.eos_idx,
return_parent_idx=True)
layers.increment(x=step_idx, value=1.0, in_place=True)
layers.array_write(selected_ids, i=step_idx, array=ids)
layers.array_write(selected_scores, i=step_idx, array=scores)
layers.Print(weights, message="weights_here", summarize=100)
layers.array_write(weights, i=step_idx, array=weights_array)
weight_debug = layers.array_read(weights_array, i=step_idx)
layers.Print(weight_debug, message="weights_debug", summarize=100)
layers.array_write(pre_mask, i=step_idx, array=tgt_masks)
layers.array_write(pre_mask_encoder, i=step_idx, array=tgt_masks_encoder)
layers.assign(gather_idx, parent_idx)
length_cond = layers.less_than(x=step_idx, y=max_len)
finish_cond = layers.logical_not(layers.is_empty(x=selected_ids))
layers.logical_and(x=length_cond, y=finish_cond, out=cond)
finished_ids, finished_scores = layers.beam_search_decode(
ids, scores, beam_size=args.beam_size, end_id=args.eos_idx)
step_idx = layers.fill_constant(
shape=[1], dtype=tgt_ids.dtype, value=0, force_cpu=True)
weight_debug = layers.array_read(weights_array, i=step_idx)
layers.Print(weight_debug, message="weights_debug", summarize=100)
weights_output, _ = fluid.layers.tensor_array_to_tensor(input=weights_array, axis=0 , use_stack=True)