code: def test_grad_scaler():
      import torch
  
      device_id = torch.cuda.current_device()
  
      scaler = PyTorchGradScaler(enabled=True)
      scaler.to_(device_id)
  
      #  Test that scaling works as expected.
      t = torch.tensor([1.0], device=device_id)
      assert scaler.scale([torch.tensor([1.0], device=device_id)]) == [
          torch.tensor([2.0**16], device=device_id)
      ]
      assert scaler.scale(
          torch.tensor([1.0], device=device_id)
      ) == torch.tensor([2.0**16], device=device_id)
      with pytest.raises(ValueError):
          scaler.scale("bogus")
      with pytest.raises(ValueError):
          scaler.scale(42)
  
      # Test infinity detection.
      g = [
          torch.tensor([2.0**16], device=device_id),
          torch.tensor([float("Inf")], device=device_id),
      ]
  
      # Check that infinity was found.
      assert scaler.unscale(g)
  
      # Check whether unscale was successful.
      assert g[0] == torch.tensor([1.0]).cuda()
  
      scaler.update()
  
      # Since infinity was found, the scale should be halved from 2**16
      # to 2**15 for the next step.
      assert scaler.scale([torch.tensor([1.0], device=device_id)]) == [
          torch.tensor([2.0**15], device=device_id)
      ]
  
file_path: thinc/thinc/tests/shims/test_pytorch_grad_scaler.py
imports: import pytest
  from hypothesis import given, settings
  from hypothesis.strategies import lists, one_of, tuples
  from thinc.api import PyTorchGradScaler
  from thinc.compat import has_torch, has_torch_amp, has_torch_cuda_gpu, torch
  from thinc.util import is_torch_array
  from ..strategies import ndarrays
label_desc: Use image node_id 3 for calling a global function with example usage: test_grad_scaler() without return types