It fails with attribute 'tensor_constant' of type 'Tensor' is not usable in a script method (Tensors must be added to a module as a buffer or parameter):
In other words, in TorchScript how can I use a tensor populated using a different module?
You created a new class ‘S’ which subclassed torch.jit.ScriptModule. Now after creating the class S, you have to call it’s super-constructor i.e. you have to run the __init__() function of the class you are subclassing from (which in your case is init() function of torch.jit.ScriptModule). It is done with this code super().__init__()
When creating torch scripts you have to use register buffers (as JIT compiles the module for you, so it must have info about everything)