Yes, your case is working, but in this line, for example:
top_preds, top_words = output[0].topk(beam_size, 0, True, True)

‘topk()’ first argument must be int, not Tensor, so I still need this option (converatation tensor to a single number).
I try to script it like this:
        from torch import Tensor
        @torch.jit.script
        def get_item(x: Tensor):
            item = x.item()
            return item
        item = get_item(beam_size)

but got this error:
RuntimeError: get_item() Expected a value of type 'Tensor' for argument 'x' but instead found type 'int'.
Position: 0
Value: 1
Declaration: get_item(Tensor x) -> (Scalar)
Cast error details: Unable to cast Python instance of type <class 'int'> to C++ type 'at::Tensor'

What do you think about possible solution for this problems?