Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bert的输出为什么没有把hidden作为输出? #9

Closed
shizhh opened this issue Apr 25, 2020 · 7 comments · Fixed by #10
Closed

bert的输出为什么没有把hidden作为输出? #9

shizhh opened this issue Apr 25, 2020 · 7 comments · Fixed by #10

Comments

@shizhh
Copy link

shizhh commented Apr 25, 2020

hidden_cache = self.encoder(hidden_states=hidden_cache,
attention_mask=extended_attention_masks,
return_type=ReturnType.turbo_transformers,
output=hidden_cache)
self.seq_pool = SequencePool(PoolingMap[pooling_type])
output = self.seq_pool(input_tensor=hidden_cache,
return_type=return_type,
output_tensor=output)
return output

基于工作需求,要对hidden做进一步的处理

@chengduoZH
Copy link
Collaborator

chengduoZH commented Apr 25, 2020

这里只返回了seq_pool之后的结果,确实跟huggingface/transformers里面的返回结果不一样,我们内部讨论一下是否要做出一致的,稍后答复。

一个临时的解决方案是您那边将hidden_cache放到返回值列表中(这里的hidden_cache是encoder的最后一层的state,维度为[batch, seq_len, hidden_size]):

 hidden_cache = self.encoder(hidden_states=hidden_cache, 
                             attention_mask=extended_attention_masks, 
                             return_type=ReturnType.turbo_transformers, 
                             output=hidden_cache) 
  
 self.seq_pool = SequencePool(PoolingMap[pooling_type]) 
 output = self.seq_pool(input_tensor=hidden_cache, 
                        return_type=return_type, 
                        output_tensor=output) 
 return  convert_returns_as_type(hidden_cache, return_type), output 

另外,如果您的代码中同时用到了BertModelWithPooler,在BertModelWithPooler里面也需要改一下:

        encoder_output = self.bertmodel(
            inputs,
            attention_masks,
            token_type_ids,
            position_ids,
            pooling_type,
            hidden_cache,
            output=None,
            return_type=ReturnType.turbo_transformers)
        pooler_output = self.pooler(encoder_output[1], return_type, pooler_output)
        return pooler_output, convert_returns_as_type(encoder_output,
                                                      return_type)

@shizhh
Copy link
Author

shizhh commented Apr 25, 2020

@chengduoZH 赞!测试了下,在我的机器上,cpu环境下速度提升了4倍。另外问一下,会不会提供GPU的版本?

@feifeibear
Copy link
Collaborator

@chengduoZH 赞!测试了下,在我的机器上,cpu环境下速度提升了4倍。另外问一下,会不会提供GPU的版本?

已经有GPU版本了呀,另外你要要hidden_state就给加一下也不麻烦

@shizhh
Copy link
Author

shizhh commented Apr 25, 2020

cpu环境下速度提升了4倍

更正下,是因为线程数设置了4;如果线程数为1,速度提升没那么明显,10%左右吧

@shizhh
Copy link
Author

shizhh commented Apr 25, 2020

已经有GPU版本了呀,另外你要要hidden_state就给加一下也不麻烦

@feifeibear 什么时候提供conda的安装方式呢

@feifeibear feifeibear linked a pull request Apr 25, 2020 that will close this issue
@feifeibear
Copy link
Collaborator

已经有GPU版本了呀,另外你要要hidden_state就给加一下也不麻烦

@feifeibear 什么时候提供conda的安装方式呢

已经加了你的hidden_state返回需求到master,什么情况下必须一个conda包进行部署?gpu的wheel包满足不了需求么?

@feifeibear feifeibear reopened this Apr 25, 2020
@reyoung
Copy link
Collaborator

reyoung commented Apr 28, 2020

Hidden需求已经满足了。

使用 #19 这个issue来跟踪conda 安装包的开发进度。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants