simulate_torch_grad.py 753 B

123456789101112131415161718192021222324252627282930
  1. from evaluator import *
  2. DESCRIPTION = "This test case checks if the model can predict what the gradient of a variable is in PyTorch."
  3. TAGS = ['explain', 'python']
  4. question = """
  5. What will this function print
  6. ```
  7. def diff_round(x, decimals=1):
  8. scale_factor = (10 ** decimals)
  9. x = x * scale_factor
  10. diff = (1 + 1e-2) * x - torch.floor(x)
  11. x = x - diff + (torch.floor(x) + torch.where(diff >= 0.5, 1, 0))
  12. x = x / scale_factor
  13. return x
  14. g = torch.tensor([.99, 1.54, 1.9], dtype=torch.float32, requires_grad=True)
  15. loss = torch.sum(diff_round(g, 1))
  16. loss.backward()
  17. print(g.grad.sum())
  18. ```
  19. """
  20. TestSimTorchGrad = question >> LLMRun() >> SubstringEvaluator("-0.03")
  21. if __name__ == "__main__":
  22. print(run_test(TestSimTorchGrad))