llama3_distributed.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # In this example, a user is running a home cluster with 3 shards.
  2. # They are prompting the cluster to generate a response to a question.
  3. # The cluster is given the question, and the user is given the response.
  4. from exo.inference.mlx.sharded_utils import get_model_path, load_tokenizer
  5. from exo.inference.shard import Shard
  6. from exo.networking.peer_handle import PeerHandle
  7. from exo.networking.grpc.grpc_peer_handle import GRPCPeerHandle
  8. from exo.topology.device_capabilities import DeviceCapabilities, DeviceFlops
  9. from typing import List
  10. import asyncio
  11. import argparse
  12. import uuid
  13. models = {
  14. "mlx-community/Meta-Llama-3-8B-Instruct-4bit": Shard(model_id="mlx-community/Meta-Llama-3-8B-Instruct-4bit", start_layer=0, end_layer=0, n_layers=32),
  15. "mlx-community/Meta-Llama-3-70B-Instruct-4bit": Shard(model_id="mlx-community/Meta-Llama-3-70B-Instruct-4bit", start_layer=0, end_layer=0, n_layers=80)
  16. }
  17. path_or_hf_repo = "mlx-community/Meta-Llama-3-8B-Instruct-4bit"
  18. model_path = get_model_path(path_or_hf_repo)
  19. tokenizer_config = {}
  20. tokenizer = load_tokenizer(model_path, tokenizer_config)
  21. # we intentionally leave out peer1 to demonstrate equality of nodes in exo.
  22. # there is no "master" node in exo, all nodes are equal and can take on any role.
  23. # peer1 = GRPCPeerHandle(
  24. # "node1",
  25. # "localhost:8080",
  26. # DeviceCapabilities(model="placeholder", chip="placeholder", memory=0)
  27. # )
  28. peer2 = GRPCPeerHandle(
  29. "node2",
  30. "localhost:8081",
  31. DeviceCapabilities(model="placeholder", chip="placeholder", memory=0, flops=DeviceFlops(fp32=0, fp16=0, int8=0))
  32. )
  33. shard = models[path_or_hf_repo]
  34. request_id = str(uuid.uuid4())
  35. async def run_prompt(prompt: str):
  36. if tokenizer.chat_template is None:
  37. tokenizer.chat_template = tokenizer.default_chat_template
  38. if (
  39. hasattr(tokenizer, "apply_chat_template")
  40. and tokenizer.chat_template is not None
  41. ):
  42. messages = [{"role": "user", "content": prompt}]
  43. prompt = tokenizer.apply_chat_template(
  44. messages, tokenize=False, add_generation_prompt=True
  45. )
  46. await peer2.connect()
  47. try:
  48. await peer2.send_prompt(shard, prompt, request_id)
  49. except Exception as e:
  50. print(e)
  51. import time
  52. # poll 10 times per second for result (even though generation is faster, any more than this it's not nice for the user)
  53. previous_length = 0
  54. n_tokens = 0
  55. start_time = time.perf_counter()
  56. while True:
  57. try:
  58. result, is_finished = await peer2.get_inference_result(request_id)
  59. except Exception as e:
  60. continue
  61. await asyncio.sleep(0.1)
  62. # Print the updated string in place
  63. updated_string = tokenizer.decode(result)
  64. n_tokens = len(result)
  65. print(updated_string[previous_length:], end='', flush=True)
  66. previous_length = len(updated_string)
  67. if is_finished:
  68. print("\nDone")
  69. break
  70. end_time = time.perf_counter()
  71. print(f"\nDone. Processed {n_tokens} tokens in {end_time - start_time:.2f} seconds ({n_tokens / (end_time - start_time):.2f} tokens/second)")
  72. if __name__ == "__main__":
  73. parser = argparse.ArgumentParser(description="Run prompt")
  74. parser.add_argument("--prompt", type=str, help="The prompt to run")
  75. args = parser.parse_args()
  76. asyncio.run(run_prompt(args.prompt))