Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

AttributeError: 'bytes' object has no attribute 'encode' #26

Open
cy69855522 opened this issue Mar 12, 2019 · 4 comments
Open

AttributeError: 'bytes' object has no attribute 'encode' #26

cy69855522 opened this issue Mar 12, 2019 · 4 comments

Comments

@cy69855522
Copy link

AttributeError Traceback (most recent call last)

in
49 model.train()
50 optimizer.zero_grad()
---> 51 classes = model(data)
52 loss = focal_loss(classes, focal_label) + margin_loss(classes, margin_label)
53 loss.backward()

~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
--> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)

in forward(self, batch)
63 # x = torch.nn.utils.rnn.pack_padded_sequence(x, lengths)
64 # self.rnn.flatten_parameters()
---> 65 x, _ = self.rnn(x)
66 # x, _ = torch.nn.utils.rnn.pad_packed_sequence(x)
67 x = self.dropout(x)

~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
--> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)

~/.local/lib/python3.6/site-packages/torchqrnn/qrnn.py in forward(self, input, hidden)
162
163 for i, layer in enumerate(self.layers):
--> 164 input, hn = layer(input, None if hidden is None else hidden[i])
165 next_hidden.append(hn)
166

~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
--> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)

~/.local/lib/python3.6/site-packages/torchqrnn/qrnn.py in forward(self, X, hidden)
97 # Forget Mult
98 # For testing QRNN without ForgetMult CUDA kernel, C = Z * F may be useful
---> 99 C = ForgetMult()(F, Z, hidden, use_cuda=self.use_cuda)
100
101 # Apply (potentially optional) output gate

~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
--> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)

~/.local/lib/python3.6/site-packages/torchqrnn/forget_mult.py in forward(self, f, x, hidden_init, use_cuda)
176 ###
177 # Avoiding 'RuntimeError: expected a Variable argument, but got NoneType' when hidden_init is None
--> 178 if hidden_init is None: return GPUForgetMult()(f, x) if use_cuda else CPUForgetMult()(f, x)
179 return GPUForgetMult()(f, x, hidden_init) if use_cuda else CPUForgetMult()(f, x, hidden_init)
180

~/.local/lib/python3.6/site-packages/torchqrnn/forget_mult.py in forward(self, f, x, hidden_init)
118
119 def forward(self, f, x, hidden_init=None):
--> 120 self.compile()
121 seq_size, batch_size, hidden_size = f.size()
122 result = f.new(seq_size + 1, batch_size, hidden_size)

~/.local/lib/python3.6/site-packages/torchqrnn/forget_mult.py in compile(self)
100 def compile(self):
101 if self.ptx is None:
--> 102 program = Program(kernel.encode(), 'recurrent_forget_mult.cu'.encode())
103 GPUForgetMult.ptx = program.compile()
104

~/.local/lib/python3.6/site-packages/pynvrtc/compiler.py in init(self, src, name, headers, include_names, lib_name)
50 self._program = self._interface.nvrtcCreateProgram(src, name,
51 headers,
---> 52 include_names)
53
54 def del(self):

~/.local/lib/python3.6/site-packages/pynvrtc/interface.py in nvrtcCreateProgram(self, src, name, headers, include_names)
198 include_names_array[:] = encode_str_list(include_names)
199 code = self._lib.nvrtcCreateProgram(byref(res),
--> 200 c_char_p(encode_str(src)), c_char_p(encode_str(name)),
201 len(headers),
202 headers_array, include_names_array)

~/.local/lib/python3.6/site-packages/pynvrtc/interface.py in encode_str(s)
52 if is_python2:
53 return s
---> 54 return s.encode("utf-8")
55
56

AttributeError: 'bytes' object has no attribute 'encode'

@mizunt1
Copy link

mizunt1 commented Mar 20, 2019

this is a result of this issue with pynvrtc:
https://github.com/taolei87/sru/issues/62
to fix this problem, go to forget_mult.py and change L102 to
program = Program(kernel, 'recurrent_forget_mult.cu')

n2cholas added a commit to n2cholas/pytorch-qrnn that referenced this issue Jan 5, 2020
The new version of pynvrtc expects bytes instead of a string, so it's not possible to call `.encode()` for the `Program` inputs. based on  salesforce#26
@delip
Copy link

delip commented Mar 20, 2020

I did that change and now I get a different error:


/opt/conda/lib/python3.6/site-packages/torchqrnn/forget_mult.py in forward(self, f, x, hidden_init)
    118 
    119     def forward(self, f, x, hidden_init=None):
--> 120         self.compile()
    121         seq_size, batch_size, hidden_size = f.size()
    122         result = f.new(seq_size + 1, batch_size, hidden_size)

/opt/conda/lib/python3.6/site-packages/torchqrnn/forget_mult.py in compile(self)
    100     def compile(self):
    101         if self.ptx is None:
--> 102             program = Program(kernel.encode(), 'recurrent_forget_mult.cu')
    103             GPUForgetMult.ptx = program.compile()
    104 

/opt/conda/lib/python3.6/site-packages/pynvrtc/compiler.py in __init__(self, src, name, headers, include_names, lib_name)
     50         self._program = self._interface.nvrtcCreateProgram(src, name,
     51                                                            headers,
---> 52                                                            include_names)
     53 
     54     def __del__(self):

/opt/conda/lib/python3.6/site-packages/pynvrtc/interface.py in nvrtcCreateProgram(self, src, name, headers, include_names)
    198         include_names_array[:] = encode_str_list(include_names)
    199         code = self._lib.nvrtcCreateProgram(byref(res),
--> 200                                             c_char_p(encode_str(src)), c_char_p(encode_str(name)),
    201                                             len(headers),
    202                                             headers_array, include_names_array)

/opt/conda/lib/python3.6/site-packages/pynvrtc/interface.py in encode_str(s)
     52     if is_python2:
     53         return s
---> 54     return s.encode("utf-8")
     55 
     56 

AttributeError: 'bytes' object has no attribute 'encode'

I am installing the latest master of qrnn via python setup.py install and my pynvrtc version is 9.2

Python 3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 19:07:31) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.8.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import pynvrtc                                                                                                              

In [2]: pynvrtc.__version__                                                                                                         
Out[2]: '9.2'

@mohitsharma29
Copy link

Hey @delip , I got it working by also removing kernel.encode in line 102.

@bwang482
Copy link

Yeah I removed both .encode() in line 102, though no clue the consequence of the removal.

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

No branches or pull requests

5 participants