1
0

llama3_distributed.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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("node2", "localhost:8081", DeviceCapabilities(model="placeholder", chip="placeholder", memory=0, flops=DeviceFlops(fp32=0, fp16=0, int8=0)))
  29. shard = models[path_or_hf_repo]
  30. request_id = str(uuid.uuid4())
  31. async def run_prompt(prompt: str):
  32. if tokenizer.chat_template is None:
  33. tokenizer.chat_template = tokenizer.default_chat_template
  34. if (hasattr(tokenizer, "apply_chat_template") and tokenizer.chat_template is not None):
  35. messages = [{"role": "user", "content": prompt}]
  36. prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
  37. await peer2.connect()
  38. try:
  39. await peer2.send_prompt(shard, prompt, request_id)
  40. except Exception as e:
  41. print(e)
  42. import time
  43. # poll 10 times per second for result (even though generation is faster, any more than this it's not nice for the user)
  44. previous_length = 0
  45. n_tokens = 0
  46. start_time = time.perf_counter()
  47. while True:
  48. try:
  49. result, is_finished = await peer2.get_inference_result(request_id)
  50. except Exception as e:
  51. continue
  52. await asyncio.sleep(0.1)
  53. # Print the updated string in place
  54. updated_string = tokenizer.decode(result)
  55. n_tokens = len(result)
  56. print(updated_string[previous_length:], end='', flush=True)
  57. previous_length = len(updated_string)
  58. if is_finished:
  59. print("\nDone")
  60. break
  61. end_time = time.perf_counter()
  62. print(f"\nDone. Processed {n_tokens} tokens in {end_time - start_time:.2f} seconds ({n_tokens / (end_time - start_time):.2f} tokens/second)")
  63. if __name__ == "__main__":
  64. parser = argparse.ArgumentParser(description="Run prompt")
  65. parser.add_argument("--prompt", type=str, help="The prompt to run")
  66. args = parser.parse_args()
  67. asyncio.run(run_prompt(args.prompt))