Repository: pytorch
Issue: Use jit in flask
Labels: oncall: jit

## 🐛 Bug

<!-- A clear and concise description of what the bug is. -->

## To Reproduce

Steps to reproduce the behavior:

1. start flask and import this class
2. error in torch.jit.load

class Conformer:
    def __init__(self,
                beam=5,
                device=None):
                
        # print("In conformer onnx test")
        start = time.time()
        self.model_abs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'resource/model.zip')
        self.logger_initializer = Logger()
        self.logger = self.logger_initializer.get_logger()
        if not os.path.exists(self.model_abs_dir):
            self.logger.error("cant find {}".format(self.model_abs_dir))
            exit(1)
        os.environ['CUDA_VISIBLE_DEVICES'] = ""
        os.environ['PYTORCH_JIT'] = '0'
        os.environ['OMP_NUM_THREADS'] = '1'
        os.environ['OPENBLAS_NUM_THREADS'] = '1'
        os.environ['MKL_NUM_THREADS'] = '1'
        os.environ['LRU_CACHE_CAPACITY'] = '1'
        torch.set_num_threads(1)
        torch.backends.cudnn.enabled = False
        self.model = torch.jit.load(self.model_abs_dir, map_location='cpu').eval()


## Expected behavior

<!-- A clear and concise description of what you expected to happen. -->

## Environment

Please copy and paste the output from our
[environment collection script](https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py)
(or fill out the checklist below manually).

You can get the script and run it with:
```
wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py
```

 - PyTorch Version (e.g., 1.0): 1.8.1+cpu
 - OS (e.g., Linux): ubuntu16.04
 - How you installed PyTorch (`conda`, `pip`, source): pip
 - Build command you used (if compiling from source): no
 - Python version: 3.7.3
 - CUDA/cuDNN version: no
 - GPU models and configuration: no
 - Any other relevant information: no

## Additional context

Traceback (most recent call last):
  File "server.py", line 51, in <module>
    from api.asr import *
  File "/storage03/gzdinghanyu/code/voiceservice/ASR-E2E/Conformer/code/api/asr.py", line 21, in <module>
    asr_decoder = ASRecognizer(int(config['asr']['beam_width']), gpu_id)
  File "/storage03/gzdinghanyu/code/voiceservice/ASR-E2E/Conformer/code/interface/ASRInterface.py", line 15, in __init__
    self.handler = Conformer(beam, self.device)
  File "/storage03/gzdinghanyu/code/voiceservice/ASR-E2E/Conformer/code/plugins/Conformer/Conformer.py", line 43, in __init__
    self.model = torch.jit.load(self.model_abs_dir, map_location='cpu').eval()
  File "/home/gzdinghanyu/miniconda3/envs/torch-1.8.1/lib/python3.7/site-packages/torch/jit/_serialization.py", line 168, in load
    return wrap_cpp_module(cpp_module)
  File "/home/gzdinghanyu/miniconda3/envs/torch-1.8.1/lib/python3.7/site-packages/torch/jit/_recursive.py", line 773, in wrap_cpp_module
    return torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
AttributeError: type object 'RecursiveScriptModule' has no attribute '_construct'



cc @gmagogsfm