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

self.random_sample和 self.beam_search返回值tensor未统一转化numpy,导致两函数同时调用报错 #165

Open
ocyisheng opened this issue Mar 2, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@ocyisheng
Copy link

提问时请尽可能提供如下信息:

基本信息

  • 你使用的操作系统: win0
  • 你使用的Python版本: 3.10
  • 你使用的Pytorch版本: 2.1
  • 你使用的bert4torch版本:
  • 你加载的预训练模型:

核心代码

# 请在此处贴上你的核心代码

def generate(self, passage, topk=1, topp=0.95):
token_ids, segment_ids = tokenizer.encode(passage, maxlen=max_p_len)
a_ids = self.random_sample([token_ids, segment_ids], n=1, topp=topp)[0] # 基于随机采样
token_ids += list(a_ids)
segment_ids += [1] * len(a_ids)
q_ids = self.beam_search([token_ids, segment_ids], topk=topk)[0] # 基于beam search
return (tokenizer.decode(q_ids.cpu().numpy()), tokenizer.decode(a_ids.cpu().numpy()))

输出信息

# 请在此处贴上你的调试输出

Traceback (most recent call last):
File "F:\project\code\python\bert4torch\examples\seq2seq\task_question_answer_generation_by_seq2seq.py", line 191, in
valid(valid_data)
File "F:\project\code\python\bert4torch\examples\seq2seq\task_question_answer_generation_by_seq2seq.py", line 165, in valid
q,a = qag.generate(d[0])
File "F:\project\code\python\bert4torch\examples\seq2seq\task_question_answer_generation_by_seq2seq.py", line 145, in generate
q_ids = self.beam_search([token_ids, segment_ids], topk=topk)[0] # 基于beam search
File "F:\project\code\python\bert4torch\bert4torch\generation.py", line 375, in beam_search
inputs = self._trans2tensors(inputs)
File "F:\project\code\python\bert4torch\bert4torch\generation.py", line 197, in trans2tensors
input_new = torch.tensor(sequence_padding(input
, value=self.pad_token_id, mode=self.pad_mode), dtype=torch.long, device=self.device)
File "F:\project\code\python\bert4torch\bert4torch\snippets\data_process.py", line 196, in sequence_padding
length = np.max([np.shape(x)[:seq_dims] for x in inputs], axis=0)
File "F:\project\code\python\bert4torch\bert4torch\snippets\data_process.py", line 196, in
length = np.max([np.shape(x)[:seq_dims] for x in inputs], axis=0)
File "D:\Develop\anaconda\envs\NovelSpeech\lib\site-packages\numpy\core\fromnumeric.py", line 2024, in shape
result = asarray(a).shape
File "D:\Develop\anaconda\envs\NovelSpeech\lib\site-packages\torch_tensor.py", line 1030, in array
return self.numpy()
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

自我尝试

此处请贴上你的自我尝试过程
def generate(self, passage, topk=1, topp=0.95):
token_ids, segment_ids = tokenizer.encode(passage, maxlen=max_p_len)
a_ids = self.random_sample([token_ids, segment_ids], n=1, topp=topp)[0] # 基于随机采样
# return tokenizer.decode(a_ids.cpu().numpy())
a_ids = a_ids.cpu().numpy() #这里需要转化成numpy
token_ids += list(a_ids)
segment_ids += [1] * len(a_ids)
q_ids = self.beam_search([token_ids, segment_ids], topk=topk)[0] # 基于beam search
# return tokenizer.decode(q_ids.cpu().numpy())
return (tokenizer.decode(q_ids.cpu().numpy()), tokenizer.decode(a_ids))#

@ocyisheng
Copy link
Author

你使用的bert4torch版本:git仓库直接拉取

@Tongjilibo
Copy link
Owner

这样操作应该是可以的,已经修改了

@Tongjilibo Tongjilibo added the bug Something isn't working label Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants