text icon indicating copy to clipboard operation
text copied to clipboard

Starting removing to_ivalue() and implementing jit __prepare_scriptable__ interface.

Open dongreenberg opened this issue 4 years ago • 2 comments

Begin trying to use the new interface in https://github.com/pytorch/pytorch/pull/45645

dongreenberg avatar Oct 01 '20 16:10 dongreenberg

Run a quick test again the two PRs

import torch
import torchtext
from torchtext.experimental.transforms import PRETRAINED_SP_MODEL
from torchtext.experimental.transforms import sentencepiece_tokenizer
sp_model_path = torchtext.utils.download_from_url(PRETRAINED_SP_MODEL['text_unigram_25000'])
spm_tokenizer = sentencepiece_tokenizer(sp_model_path)
jit_spm_tokenizer = torch.jit.script(spm_tokenizer)

And get the follow error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-3-7fa6028c3f9b> in <module>
----> 1 jit_spm_tokenizer = torch.jit.script(spm_tokenizer)

~/tmp/PyTorch/pytorch/torch/jit/_script.py in script(obj, optimize, _frames_up, _rcb)
    896     if isinstance(obj, torch.nn.Module):
    897         return torch.jit._recursive.create_script_module(
--> 898             obj, torch.jit._recursive.infer_methods_to_compile
    899         )
    900

~/tmp/PyTorch/pytorch/torch/jit/_recursive.py in create_script_module(nn_module, stubs_fn, share_types)
    350     check_module_initialized(nn_module)
    351     concrete_type = get_module_concrete_type(nn_module, share_types)
--> 352     return create_script_module_impl(nn_module, concrete_type, stubs_fn)
    353
    354 def create_script_module_impl(nn_module, concrete_type, stubs_fn):

~/tmp/PyTorch/pytorch/torch/jit/_recursive.py in create_script_module_impl(nn_module, concrete_type, stubs_fn)
    409     # Compile methods if necessary
    410     if concrete_type not in concrete_type_store.methods_compiled:
--> 411         create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
    412         torch._C._run_emit_module_hook(cpp_module)
    413         concrete_type_store.methods_compiled.add(concrete_type)

~/tmp/PyTorch/pytorch/torch/jit/_recursive.py in create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
    302     property_rcbs = [p.resolution_callback for p in property_stubs]
    303
--> 304     concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
    305
    306

RuntimeError:
Module 'SentencePieceTokenizer' has no attribute 'sp_model' (This attribute exists on the Python module, but we failed to convert Python type: 'torchtext._torchtext.SentencePiece' to a TorchScript type.):
  File "/private/home/zhangguanheng/anaconda3/lib/python3.7/site-packages/torchtext-0.8.0a0+b733bb1-py3.7-linux-x86_64.egg/torchtext/experimental/transforms.py", line 280
                Note: SentencePiece treats the input text just as a sequence of Unicode characters. Whitespace is also handled as a normal symbol. To handle the whitespace as a basic token explicitly, SentencePiece first escapes the whitespace with a meta symbol "▁" (U+2581) as follows.
                """
        return self.sp_model.EncodeAsPieces(line)
               ~~~~~~~~~~~~~ <--- HERE

I'm wondering if __prepare_scriptable__ is placed in the right place of the _recursive.py file. Will explore this myself later.

zhangguanheng66 avatar Oct 02 '20 01:10 zhangguanheng66

It seems that calling 'prepare_scriptable' in torch.jit._script.script() function works for the building blocks in torchtext (a.k.a. calling prepare_scriptable function on L897 in torch/jit/_script.py file).

zhangguanheng66 avatar Oct 02 '20 01:10 zhangguanheng66

Hi @dongreenberg!

Thank you for your pull request.

We require contributors to sign our Contributor License Agreement, and yours needs attention.

You currently have a record in our system, but the CLA is no longer valid, and will need to be resubmitted.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

facebook-github-bot avatar Sep 17 '22 07:09 facebook-github-bot

Closing as this PR seems stale

Nayef211 avatar Sep 19 '22 18:09 Nayef211