使用动态图ERNIE并添加自定义特征时出现问题
Created by: yaweisun
- 运行环境 AI studio高级版 python 3.7 paddlepaddle 1.7.1
- 问题描述 我用动态图的ERNIE做序列标注,想要增加词频特征,于是仿照ErnieModel这个类重写了下面这个类,这里只列出了主要函数的代码。在不传入freq_ids这个参数的时候,没有问题,但是在传入这个参数之后,程序就报错了。报错信息也在后面。
class SciKEBase(D.Layer, PretrainedModel):
def forward(self, src_ids, freq_ids=None, sent_ids=None, pos_ids=None, input_mask=None, attn_bias=None):
assert len(src_ids.shape) == 2, 'expect src_ids.shape = [batch, sequecen], got %s' % (repr(src_ids.shape))
d_batch = L.shape(src_ids)[0]
d_seqlen = L.shape(src_ids)[1]
if pos_ids is None:
pos_ids = L.reshape(L.range(0, d_seqlen, 1, dtype='int32'), [1, -1])
pos_ids = L.cast(pos_ids, 'int64')
if attn_bias is None:
if input_mask is None:
input_mask = L.cast(src_ids != 0, 'float32')
assert len(input_mask.shape) == 2
input_mask = L.unsqueeze(input_mask, axes=[1,3])
attn_bias = (1. - L.matmul(input_mask, input_mask, transpose_y=True)) * -10000.0
attn_bias = L.expand(attn_bias, [1, self.n_head, 1, 1]) # avoid broadcast =_=
attn_bias.stop_gradient = True
if sent_ids is None:
sent_ids = L.zeros_like(src_ids)
src_embedded = self.word_emb(src_ids)
pos_embedded = self.pos_emb(pos_ids)
sent_embedded = self.sent_emb(sent_ids)
if freq_ids:
freq_embedded = self.freq_emb(freq_ids)
# freq_embedded = self.dropout(self.ln(freq_embedded))
embedded = src_embedded + pos_embedded + sent_embedded + freq_embedded
else:
embedded = src_embedded + pos_embedded + sent_embedded
embedded = self.dropout(self.ln(embedded))
encoded = self.encoder_stack(embedded, attn_bias)
pooled = self.pooler(encoded[:, 0, :])
if freq_ids:
freq_embedded = self.freq_emb(freq_ids)
freq_embedded = self.dropout(self.ln(freq_embedded))
return pooled, encoded
- 报错信息
-------------------------------------------------------------EnforceNotMet Traceback (most recent call last) in 4 for j in range(len(train_data) // BATCH): 5 feature, freq, label = get_batch_data(train_data, j) ----> 6 loss, _ = ernie(feature, freq_ids=freq, labels=label) 7 # loss, _ = ernie(feature, labels=label) 8 loss.backward() /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py in call(self, *inputs, **kwargs) 302 self._built = True 303 --> 304 outputs = self.forward(*inputs, **kwargs) 305 return outputs 306 ~/ernie/modeling_ernie.py in forward(self, *args, **kwargs) 758 759 labels = kwargs.pop('labels', None) --> 760 pooled, encoded = super(SciKE, self).forward(*args, **kwargs) 761 hidden = self.dropout(encoded) # maybe not? 762 logits = self.classifier(hidden) ~/ernie/modeling_ernie.py in forward(self, src_ids, freq_ids, sent_ids, pos_ids, input_mask, attn_bias) 720 embedded = self.dropout(self.ln(embedded)) 721 --> 722 encoded = self.encoder_stack(embedded, attn_bias) 723 pooled = self.pooler(encoded[:, 0, :]) 724 /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py in call(self, *inputs, **kwargs) 302 self._built = True 303 --> 304 outputs = self.forward(*inputs, **kwargs) 305 return outputs 306 ~/ernie/modeling_ernie.py in forward(self, inputs, attn_bias) 145 def forward(self, inputs, attn_bias=None): 146 for b in self.block: --> 147 inputs = b(inputs, attn_bias) 148 return inputs 149 /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py in call(self, *inputs, **kwargs) 302 self._built = True 303 --> 304 outputs = self.forward(*inputs, **kwargs) 305 return outputs 306 ~/ernie/modeling_ernie.py in forward(self, inputs, attn_bias) 130 hidden = self.ln1(hidden) # dropout/ add/ norm 131 --> 132 ffn_out = self.ffn(hidden) 133 ffn_out = self.dropout(ffn_out) 134 hidden = ffn_out + hidden /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py in call(self, *inputs, **kwargs) 302 self._built = True 303 --> 304 outputs = self.forward(*inputs, **kwargs) 305 return outputs 306 ~/ernie/modeling_ernie.py in forward(self, inputs) 105 106 def forward(self, inputs): --> 107 hidden = self.i(inputs) 108 hidden = self.dropout(hidden) 109 out = self.o(hidden) /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py in call(self, *inputs, **kwargs) 302 self._built = True 303 --> 304 outputs = self.forward(*inputs, **kwargs) 305 return outputs 306 /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/nn.py in forward(self, input) 930 931 if in_dygraph_mode(): --> 932 outs = core.ops.matmul(inputs, attrs) 933 pre_bias = outs['Out'][0] 934 EnforceNotMet:
C++ Call Stacks (More useful to developers):
0 std::string paddle::platform::GetTraceBackString<char const*>(char const*&&, char const*, int) 1 paddle::platform::EnforceNotMet::EnforceNotMet(std::exception_ptr::exception_ptr, char const*, int) 2 void paddle::operators::math::Blaspaddle::platform::CUDADeviceContext::MatMul(paddle::framework::Tensor const&, paddle::operators::math::MatDescriptor const&, paddle::framework::Tensor const&, paddle::operators::math::MatDescriptor const&, float, paddle::framework::Tensor*, float) const 3 paddle::operators::MatMulKernel<paddle::platform::CUDADeviceContext, float>::Compute(paddle::framework::ExecutionContext const&) const 4 std::Function_handler<void (paddle::framework::ExecutionContext const&), paddle::framework::OpKernelRegistrarFunctor<paddle::platform::CUDAPlace, false, 0ul, paddle::operators::MatMulKernel<paddle::platform::CUDADeviceContext, float>, paddle::operators::MatMulKernel<paddle::platform::CUDADeviceContext, double>, paddle::operators::MatMulKernel<paddle::platform::CUDADeviceContext, paddle::platform::float16> >::operator()(char const*, char const*, int) const::{lambda(paddle::framework::ExecutionContext const&)#1 (closed)}>::M_invoke(std::Any_data const&, paddle::framework::ExecutionContext const&) 5 paddle::imperative::PreparedOp::Run(std::map<std::string, std::vector<std::shared_ptrpaddle::imperative::VarBase, std::allocator<std::shared_ptrpaddle::imperative::VarBase > >, std::lessstd::string, std::allocator<std::pair<std::string const, std::vector<std::shared_ptrpaddle::imperative::VarBase, std::allocator<std::shared_ptrpaddle::imperative::VarBase > > > > > const*, std::map<std::string, std::vector<std::shared_ptrpaddle::imperative::VarBase, std::allocator<std::shared_ptrpaddle::imperative::VarBase > >, std::lessstd::string, std::allocator<std::pair<std::string const, std::vector<std::shared_ptrpaddle::imperative::VarBase, std::allocator<std::shared_ptrpaddle::imperative::VarBase > > > > > const*, std::unordered_map<std::string, boost::variant<boost::blank, int, float, std::string, std::vector<int, std::allocator >, std::vector<float, std::allocator >, std::vector<std::string, std::allocatorstd::string >, bool, std::vector<bool, std::allocator >, paddle::framework::BlockDesc*, long, std::vector<paddle::framework::BlockDesc*, std::allocatorpaddle::framework::BlockDesc* >, std::vector<long, std::allocator >, boost::detail::variant::void, boost::detail::variant::void, boost::detail::variant::void, boost::detail::variant::void, boost::detail::variant::void, boost::detail::variant::void_, boost::detail::variant::void_>, std::hashstd::string, std::equal_tostd::string, std::allocator<std::pair<std::string const, boost::variant<boost::blank, int, float, std::string, std::vector<int, std::allocator >, std::vector<float, std::allocator >, std::vector<std::string, std::allocatorstd::string >, bool, std::vector<bool, std::allocator >, paddle::framework::BlockDesc*, long, std::vector<paddle::framework::BlockDesc*, std::allocatorpaddle::framework::BlockDesc* >, std::vector<long, std::allocator >, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> > > > const*) 6 paddle::imperative::OpBase::Run(std::map<std::string, std::vector<std::shared_ptrpaddle::imperative::VarBase, std::allocator<std::shared_ptrpaddle::imperative::VarBase > >, std::lessstd::string, std::allocator<std::pair<std::string const, std::vector<std::shared_ptrpaddle::imperative::VarBase, std::allocator<std::shared_ptrpaddle::imperative::VarBase > > > > > const&, std::map<std::string, std::vector<std::shared_ptrpaddle::imperative::VarBase, std::allocator<std::shared_ptrpaddle::imperative::VarBase > >, std::lessstd::string, std::allocator<std::pair<std::string const, std::vector<std::shared_ptrpaddle::imperative::VarBase, std::allocator<std::shared_ptrpaddle::imperative::VarBase > > > > > const&) 7 paddle::imperative::Tracer::TraceOp(std::string const&, std::map<std::string, std::vector<std::shared_ptrpaddle::imperative::VarBase, std::allocator<std::shared_ptrpaddle::imperative::VarBase > >, std::lessstd::string, std::allocator<std::pair<std::string const, std::vector<std::shared_ptrpaddle::imperative::VarBase, std::allocator<std::shared_ptrpaddle::imperative::VarBase > > > > > const&, std::map<std::string, std::vector<std::shared_ptrpaddle::imperative::VarBase, std::allocator<std::shared_ptrpaddle::imperative::VarBase > >, std::lessstd::string, std::allocator<std::pair<std::string const, std::vector<std::shared_ptrpaddle::imperative::VarBase, std::allocator<std::shared_ptrpaddle::imperative::VarBase > > > > > const&, std::unordered_map<std::string, boost::variant<boost::blank, int, float, std::string, std::vector<int, std::allocator >, std::vector<float, std::allocator >, std::vector<std::string, std::allocatorstd::string >, bool, std::vector<bool, std::allocator >, paddle::framework::BlockDesc*, long, std::vector<paddle::framework::BlockDesc*, std::allocatorpaddle::framework::BlockDesc* >, std::vector<long, std::allocator >, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_>, std::hashstd::string, std::equal_tostd::string, std::allocator<std::pair<std::string const, boost::variant<boost::blank, int, float, std::string, std::vector<int, std::allocator >, std::vector<float, std::allocator >, std::vector<std::string, std::allocatorstd::string >, bool, std::vector<bool, std::allocator >, paddle::framework::BlockDesc*, long, std::vector<paddle::framework::BlockDesc*, std::allocatorpaddle::framework::BlockDesc* >, std::vector<long, std::allocator >, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> > > >)
Error Message Summary:
Error: An error occurred here. There is no accurate error hint for this error yet. We are continuously in the process of increasing hint for this kind of error check. It would be helpful if you could inform us of how this conversion went by opening a github issue. And we will resolve it with high priority.
- New issue link: https://github.com/PaddlePaddle/Paddle/issues/new
- Recommended issue content: all error stack information [Hint: CUBLAS_STATUS_EXECUTION_FAILED] at (/paddle/paddle/fluid/operators/math/blas_impl.cu.h:51)