python_parallel_wordcount.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from evaluator import *
  2. DESCRIPTION = "Test if the model can parallelize a python program to perform a wordcount."
  3. TAGS = ['code', 'python', 'performance']
  4. import collections
  5. question = """
  6. Write a self-contained Python program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout.
  7. {"This": 1, "is": 2, "is,":1, "an":1, "example":1}
  8. The program should be run by calling /path/to/python_word_count with no arguments.
  9. Make it run in parallel on 4 cores at the same time, each taking 1/4 of the file.
  10. Print out 4 differnt json dicts, one per line. I'll do the merging.
  11. """
  12. def setup():
  13. txt = "it was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way"
  14. open("/tmp/file.txt", "w").write(txt)
  15. def merge(dicts):
  16. out = {}
  17. for line in dicts.split("\n"):
  18. if line:
  19. if '{' not in line: continue
  20. try:
  21. d = json.loads(line)
  22. except:
  23. continue
  24. for k, v in d.items():
  25. out[k] = out.get(k, 0) + v
  26. return out
  27. TestPythonCountPar = Setup(setup) >> StringNode(question) >> LLMRun() >> ExtractCode(keep_main=True) >> ((PythonRun() >> PyFunc(merge) >> EqualEvaluator(({'the': 11, 'it': 10, 'was': 10, 'of': 10, 'we': 4, 'times,': 2, 'age': 2, 'epoch': 2, 'season': 2, 'had': 2, 'before': 2, 'us,': 2, 'were': 2, 'all': 2, 'going': 2, 'direct': 2, 'best': 1, 'worst': 1, 'wisdom,': 1, 'foolishness,': 1, 'belief,': 1, 'incredulity,': 1, 'Light,': 1, 'Darkness,': 1, 'spring': 1, 'hope,': 1, 'winter': 1, 'despair,': 1, 'everything': 1, 'nothing': 1, 'to': 1, 'Heaven,': 1, 'other': 1, 'way': 1}))) & (LLMRun("Below is some student's code. I asked them to parallelize some code. Do not judge if it is correct, I have test cases for that. Just tell me if they tried to parallelize it.\n\n<A>\n\nAnswer either 'The student passes' or 'The student fails'.", llm=EVAL_LLM) >> Echo() >> SubstringEvaluator("student passes")))
  28. if __name__ == "__main__":
  29. print(run_test(TestPythonCountPar))