faster_l2_diff.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from evaluator import *
  2. DESCRIPTION = "Test if the model can optimize a given Python program for speed and memory efficiency."
  3. TAGS = ['code', 'performance', 'python']
  4. question = """
  5. I have this program
  6. import numpy as np
  7. def diff(out, templates):
  8. return (out[:, None, :] - templates[None, :, :]).square().sum(2)
  9. Make it faster and more memory efficient without expanding the arrays. Call it `diff` still.
  10. """
  11. tracer = """
  12. import tracemalloc
  13. import numpy as np
  14. if __name__ == "__main__":
  15. tracemalloc.start() # Start tracing
  16. snapshot1 = tracemalloc.take_snapshot() # Before execution
  17. diff(np.arange(10000).reshape((100, 100)), np.arange(10000).reshape((100, 100)))
  18. snapshot2 = tracemalloc.take_snapshot() # After execution
  19. # Calculate the difference in memory usage
  20. current, peak = tracemalloc.get_traced_memory()
  21. tracemalloc.stop() # Stop tracing
  22. if peak < 10**6:
  23. print("Efficient")
  24. """
  25. test_case, answer = make_python_test([("diff(np.arange(100).reshape((10, 10)), np.arange(100).reshape((10, 10))).sum()", "1650000")])
  26. TestFastL2 = question >> LLMRun() >> ExtractCode() >> \
  27. ((PythonRun(test_case) >> SubstringEvaluator(answer)) &
  28. (PythonRun(tracer) >> SubstringEvaluator("Efficient")) &
  29. (LLMRun("Below is a students answer to a question on writing efficient code. They're supposed to be vectorizing a loop. Is the following code vectorized? It is incorrect to use for loops explicitly. Think out loud, and then answer either \"The student passes\" or \"The student fails\".\n<A>", llm=EVAL_LLM) >> SubstringEvaluator("student passes")))
  30. if __name__ == "__main__":
  31. print(run_test(TestFastL2))