debug_broken_code_parcount.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from evaluator import *
  2. DESCRIPTION = "Test if a model can explain a bug in a parallelized wordcount function."
  3. TAGS = ['explain', 'python', 'fix']
  4. import collections
  5. question = """
  6. What is the bug in this code that makes it not count right. (I want to print out 4 arrays un-merged. I'll do the merging later. This is intentional.)
  7. #!/usr/bin/env python3
  8. import json
  9. import multiprocessing
  10. def count_words_in_chunk(start, end, queue):
  11. '''Count how often each word appears in the given chunk of the file.'''
  12. word_counts = {}
  13. with open('/tmp/file.txt', 'r') as f:
  14. f.seek(start)
  15. text = f.read(end - start)
  16. # Avoid cutting words in half at the beginning
  17. if start != 0:
  18. text = text.split(' ', 1)[-1]
  19. # Avoid cutting words in half at the end
  20. f.seek(end)
  21. remainder = f.readline()
  22. text += remainder
  23. # Tokenize and count words
  24. for word in text.split():
  25. word = word.strip().lower() # Lowercasing for consistent counting
  26. if word:
  27. word_counts[word] = word_counts.get(word, 0) + 1
  28. # Put result to the queue
  29. queue.put(word_counts)
  30. def main():
  31. # Get the size of the file
  32. file_size = 0
  33. with open('/tmp/file.txt', 'r') as f:
  34. f.seek(0, 2)
  35. file_size = f.tell()
  36. # Calculate chunk sizes for 4 cores
  37. chunk_size = file_size // 4
  38. offsets = [(i * chunk_size, (i + 1) * chunk_size) for i in range(4)]
  39. # Last chunk should go until the end of the file
  40. offsets[-1] = (offsets[-1][0], file_size)
  41. # Queue for inter-process communication
  42. queue = multiprocessing.Queue()
  43. # Start 4 worker processes
  44. processes = []
  45. for start, end in offsets:
  46. p = multiprocessing.Process(target=count_words_in_chunk, args=(start, end, queue))
  47. processes.append(p)
  48. p.start()
  49. # Collect results
  50. for p in processes:
  51. p.join()
  52. # Print results as JSON objects one-per-process
  53. while not queue.empty():
  54. word_count = queue.get()
  55. print(json.dumps(word_count))
  56. if __name__ == "__main__":
  57. main()
  58. List exactly one reason.
  59. """
  60. TestWhyBuggyPythonCountPar = question >> LLMRun() >> LLMRun("Below is a student's explanation for why some parallel wordcounting code is not working correctly. You don't need to see the full code, just tell me whether or not the student says that the main reason the code is broken is because 'the chunks could contain the same data or be overlapping' or something like this. Saying 'a word might be split in half' is wrong. Saying 'you might miss a word' is also wrong. Here is their answer:\n\n<A>\n\nTell me either 'The student passes' or 'The student fails'.", llm=EVAL_LLM) >> Echo() >> SubstringEvaluator("student passes")
  61. if __name__ == "__main__":
  62. print(run_test(TestWhyBuggyPythonCountPar))