5
0

test_chained_calls.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ######
  2. # Warning:torch this test is a work in progress. It will be modified soon.
  3. # - if you want more stable tests, see test_block_exact_match
  4. # - if you want to figure out chained inference, ask yozh
  5. import os
  6. import hivemind
  7. import torch
  8. import transformers
  9. from hivemind.moe.expert_uid import UID_DELIMITER, ExpertInfo
  10. from src.bloom.from_pretrained import load_pretrained_block
  11. from src.client.remote_block import RemoteTransformerBlock
  12. from src.dht_utils import get_remote_module
  13. INITIAL_PEERS = os.environ.get("INITIAL_PEERS")
  14. if not INITIAL_PEERS:
  15. raise RuntimeError("Must specify INITIAL_PEERS environment variable with one or more peer ids")
  16. INITIAL_PEERS = INITIAL_PEERS.split()
  17. MODEL_NAME = os.environ.get("MODEL_NAME")
  18. if not MODEL_NAME:
  19. raise RuntimeError("Must specify MODEL_NAME as a name of a model to be tested")
  20. REF_NAME = os.environ.get("REF_NAME", "bigscience/test-bloomd-6b3")
  21. def test_forward_backward_exact_match(atol_forward=1e-4, atol_backward=1e-4, seq_length=1):
  22. dht = hivemind.DHT(initial_peers=INITIAL_PEERS, client_mode=True, start=True)
  23. config = transformers.AutoConfig.from_pretrained(MODEL_NAME)
  24. remote_block = get_remote_module(dht, f"{MODEL_NAME}{UID_DELIMITER}0")
  25. assert remote_block is not None, f"Could not find {MODEL_NAME}{UID_DELIMITER}0 in DHT"
  26. assert isinstance(remote_block, RemoteTransformerBlock)
  27. _ = remote_block.info # lazy-init info now, because otherwise we will _break_ info init by chaning _info
  28. remote_block._info = ExpertInfo(f"{MODEL_NAME}.3 {MODEL_NAME}.4 {MODEL_NAME}.5", remote_block._info.peer_id)
  29. ref_blocks = [
  30. load_pretrained_block(REF_NAME, 3, torch_dtype=torch.float32),
  31. load_pretrained_block(REF_NAME, 4, torch_dtype=torch.float32),
  32. load_pretrained_block(REF_NAME, 5, torch_dtype=torch.float32),
  33. ]
  34. inputs = torch.randn(1, seq_length, config.hidden_size, requires_grad=True)
  35. outputs_rpc = remote_block.forward(inputs)[0]
  36. outputs_rpc.sum().backward()
  37. grads_rpc = inputs.grad
  38. inputs.grad = None
  39. hidden_states = inputs
  40. for ref_block in ref_blocks:
  41. hidden_states = ref_block.forward(hidden_states)[0]
  42. outputs_ref = hidden_states
  43. outputs_ref.sum().backward()
  44. grads_ref = inputs.grad
  45. assert torch.allclose(outputs_ref, outputs_rpc, rtol=0, atol=atol_forward)
  46. assert torch.allclose(grads_ref, grads_rpc, rtol=0, atol=atol_backward)
  47. def test_chained_inference_exact_match(atol_inference=1e-4):
  48. dht = hivemind.DHT(initial_peers=INITIAL_PEERS, client_mode=True, start=True)
  49. config = transformers.AutoConfig.from_pretrained(MODEL_NAME)
  50. remote_block = get_remote_module(dht, f"{MODEL_NAME}{UID_DELIMITER}0")
  51. assert remote_block is not None, f"Could not find {MODEL_NAME}{UID_DELIMITER}0 in DHT"
  52. assert isinstance(remote_block, RemoteTransformerBlock)
  53. _ = remote_block.info # lazy-init info now, because otherwise we will _break_ info init by chaning _info
  54. remote_block._info = ExpertInfo(f"{MODEL_NAME}.3 {MODEL_NAME}.4", remote_block._info.peer_id)
  55. inputs = torch.randn(1, 8, config.hidden_size)
  56. outputs_inference = []
  57. with remote_block.inference_session() as sess:
  58. for i in range(inputs.shape[1]):
  59. outputs_inference.append(sess.step(inputs[:, i : i + 1, :]))
  60. outputs_inference = torch.cat(outputs_inference, dim=1)
  61. ref_blocks = [
  62. load_pretrained_block(REF_NAME, 3, torch_dtype=torch.float32),
  63. load_pretrained_block(REF_NAME, 4, torch_dtype=torch.float32),
  64. ]
  65. outputs_ref = []
  66. caches = [None, None]
  67. for i in range(inputs.shape[1]):
  68. new_caches = []
  69. hidden_states = inputs[:, i : i + 1, :]
  70. for ref_block, cache in zip(ref_blocks, caches):
  71. with torch.no_grad():
  72. hidden_states, new_cache = ref_block.forward(hidden_states, use_cache=True, layer_past=cache)
  73. new_caches.append(new_cache)
  74. outputs_ref.append(hidden_states)
  75. caches = new_caches
  76. outputs_ref = torch.cat(outputs_ref, dim=1)
  77. assert torch.allclose(outputs_ref, outputs_inference, rtol=0, atol=atol_inference)