You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by wwwwcu via TVM Discuss <no...@discuss.tvm.ai> on 2020/05/13 06:26:05 UTC

[TVM Discuss] [Questions] Problems when import BERT model from PyTorch Relay


Hi,
    I was trying to import [bert-base-uncased](https://pypi.org/project/pytorch-pretrained-bert/) by PyTorch relay. It said that "NotImplementedError: The following operators are not implemented: ['prim::ImplicitTensorToNum']"
    I can't find any useful information about 'ImplicitTensorToNum'.
    How can I fix this?





---
[Visit Topic](https://discuss.tvm.ai/t/problems-when-import-bert-model-from-pytorch-relay/6659/1) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/e65439e754c93eea88fb619e645f49522dd9a42a5894755d4581f23e7434ac77).

[TVM Discuss] [Questions] Problems when import BERT model from PyTorch Relay

Posted by wwwwcu via TVM Discuss <no...@discuss.tvm.ai>.

Sorry I didn't make it clear.
The code pasted in the first issue works well after adopting your solution.
Then I tested 'gpt2' model, it reported an error:
```
TypeError: int() argument must be a string, a bytes-like object or a number, not ‘Call’
```

The test code is shown below:
```
from tvm import relay
import torch
from pytorch_transformers import GPT2Tokenizer, GPT2LMHeadModel

tokenizer = GPT2Tokenizer.from_pretrained('gpt2')

text = "What is the fastest car in the"
indexed_tokens = tokenizer.encode(text)
tokens_tensor = torch.tensor([indexed_tokens])

model = GPT2LMHeadModel.from_pretrained('gpt2')
model.eval()

with torch.no_grad():
    outputs = model(tokens_tensor)
    predictions = outputs[0]

predicted_index = torch.argmax(predictions[0, -1, :]).item()
predicted_text = tokenizer.decode(indexed_tokens + [predicted_index])
print(predicted_text)

scripted_model = torch.jit.trace(model, tokens_tensor).eval()

input_name = 'input_ids'
input_shapes = [(input_name, [1, 7])]
mod, params = relay.frontend.from_pytorch(scripted_model, input_shapes)

```





---
[Visit Topic](https://discuss.tvm.ai/t/problems-when-import-bert-model-from-pytorch-relay/6659/6) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/5b0eab7fbacdc2444a4b437e5dde67b2a86a03c5004e1aad07f85a26918b8dad).

[TVM Discuss] [Questions] Problems when import BERT model from PyTorch Relay

Posted by Siju via TVM Discuss <no...@discuss.tvm.ai>.

> from tvm import relay
> import torch
> from pytorch_pretrained_bert import BertTokenizer, BertModel, BertForMaskedLM
> import logging
> 
> logging.basicConfig(level=logging.INFO)


I used the code you pasted in the first issue and its able to generate complete relay function. Im not able to get error you mentioned. How to reproduce the below issue
> TypeError: int() argument must be a string, a bytes-like object or a number, not 'Call'





---
[Visit Topic](https://discuss.tvm.ai/t/problems-when-import-bert-model-from-pytorch-relay/6659/5) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/572b4f8c688f4300b570e8c0e356c59766203e4c59e81f0a654f0fcdf4dd5dd5).

[TVM Discuss] [Questions] Problems when import BERT model from PyTorch Relay

Posted by wwwwcu via TVM Discuss <no...@discuss.tvm.ai>.

Thanks! 

I try to fix it by
```
def _tensortonum():
    def _impl(inputs, input_types):
        return inputs[0]
    return _impl
```

Another error accurs:
```
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Call'
```

```
'58', %58 : Scalar = prim::ImplicitTensorToNum(%57),
......
'position_ids.1', %position_ids.1 : Long(7) = aten::arange(%59, %58, %60, %61, %62, %63, %64)
```
'aten::arange' will _create_typed_const(inputs[1], dtype), but 'inputs[1]' is 'Call', that's the reason of ‘TypeError’ mentioned above.

So I use the following implementation.
```
def _tensortonum():
    def _impl(inputs, input_types):
        tmp = _infer_value(inputs[0], None)
        return np.array(tmp).astype(np.str)
    return _impl
```
It works, but I'm not sure if it is correct because when it goes to 'aten::view', the following error accurs:
```
TVMError: Check failed: ObjectTypeChecker<TObjectRef>: :Check(ptr): Expect List[IntImm] but get Array
```
It seems like something is wrong when it goes to:
_op.transform.reshape(data, new_shape)





---
[Visit Topic](https://discuss.tvm.ai/t/problems-when-import-bert-model-from-pytorch-relay/6659/4) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/5e97c539f2e2123932bb180be030e977e3249af9f63d609f9dbb1b91cd97244c).

[TVM Discuss] [Questions] Problems when import BERT model from PyTorch Relay

Posted by Siju via TVM Discuss <no...@discuss.tvm.ai>.

[PR 5603](https://github.com/apache/incubator-tvm/pull/5603) will help to solve the issue of `prim::ImplicitTensorToNum`

If you come across issue with matmul, you can merge [PR 5604](https://github.com/apache/incubator-tvm/pull/5604) as well.





---
[Visit Topic](https://discuss.tvm.ai/t/problems-when-import-bert-model-from-pytorch-relay/6659/3) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/6fff6ffb792a0aba1e4d7e4f5674edd266bf6cb878f0a9d21330b3fbd729a0aa).