test_block_exact_match.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import os
  2. import hivemind
  3. import torch
  4. from src.bloom.from_pretrained import load_pretrained_block
  5. from src.client.remote_block import RemoteTransformerBlock
  6. from src.dht_utils import get_remote_module
  7. INITIAL_PEERS = os.environ.get("INITIAL_PEERS")
  8. if not INITIAL_PEERS:
  9. raise RuntimeError("Must specify INITIAL_PEERS environment variable with one or more peer ids")
  10. INITIAL_PEERS = INITIAL_PEERS.split()
  11. BLOCK_UID = os.environ.get("BLOCK_UID")
  12. if not BLOCK_UID:
  13. raise RuntimeError("Must specify BLOCK_UID as an index of a transformer block to be tested")
  14. REF_NAME = os.environ.get("REF_NAME", "bigscience/test-bloomd-6b3")
  15. REF_INDEX = int(os.environ.get("REF_INDEX", BLOCK_UID[-1].split(".")[-1]))
  16. def test_remote_block_exact_match(atol_forward=1e-5, atol_inference=1e-3):
  17. dht = hivemind.DHT(initial_peers=INITIAL_PEERS, client_mode=True, start=True)
  18. remote_block = get_remote_module(dht, BLOCK_UID)
  19. assert remote_block is not None, f"Could not find {BLOCK_UID} in DHT"
  20. assert isinstance(remote_block, RemoteTransformerBlock)
  21. inputs = torch.randn(1, 8, 4096)
  22. (outputs_forward,) = remote_block(inputs)
  23. outputs_inference = []
  24. with remote_block.begin_inference_session() as sess:
  25. for i in range(inputs.shape[1]):
  26. outputs_inference.append(sess.step(inputs[:, i : i + 1, :]))
  27. outputs_inference = torch.cat(outputs_inference, dim=1)
  28. ref_block = load_pretrained_block(REF_NAME, REF_INDEX, torch_dtype=torch.float32)
  29. (outputs_local,) = ref_block(inputs)
  30. assert torch.allclose(outputs_local, outputs_forward, rtol=0, atol=atol_forward)
  31. assert torch.allclose(outputs_local, outputs_inference, rtol=0, atol=atol_inference)