test_chained_calls.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 hivemind
  6. import pytest
  7. import torch
  8. import transformers
  9. from hivemind.moe.expert_uid import UID_DELIMITER, ExpertInfo
  10. from test_utils import *
  11. import src
  12. from src.bloom.from_pretrained import load_pretrained_block
  13. from src.client.remote_sequential import RemoteTransformerBlock
  14. from src.data_structures import UID_DELIMITER
  15. from src.dht_utils import get_remote_module
  16. @pytest.mark.forked
  17. def test_forward_backward_exact_match(atol_forward=1e-4, atol_backward=1e-4, seq_length=1):
  18. dht = hivemind.DHT(initial_peers=INITIAL_PEERS, client_mode=True, start=True)
  19. config = src.DistributedBloomConfig.from_pretrained(MODEL_NAME)
  20. remote_block = get_remote_module(dht, f"{MODEL_NAME}{UID_DELIMITER}0", config)
  21. assert remote_block is not None, f"Could not find {MODEL_NAME}{UID_DELIMITER}0 in DHT"
  22. assert isinstance(remote_block, RemoteTransformerBlock)
  23. _ = remote_block.info # lazy-init info now, because otherwise we will _break_ info init by chaning _info
  24. remote_block._info = ExpertInfo(f"{MODEL_NAME}.3 {MODEL_NAME}.4 {MODEL_NAME}.5", remote_block._info.peer_id)
  25. ref_blocks = [
  26. load_pretrained_block(MODEL_NAME, 3, torch_dtype=torch.float32),
  27. load_pretrained_block(MODEL_NAME, 4, torch_dtype=torch.float32),
  28. load_pretrained_block(MODEL_NAME, 5, torch_dtype=torch.float32),
  29. ]
  30. inputs = torch.randn(1, seq_length, config.hidden_size, requires_grad=True)
  31. outputs_rpc = remote_block.forward(inputs)[0]
  32. outputs_rpc.sum().backward()
  33. grads_rpc = inputs.grad
  34. inputs.grad = None
  35. hidden_states = inputs
  36. for ref_block in ref_blocks:
  37. hidden_states = ref_block.forward(hidden_states)[0]
  38. outputs_ref = hidden_states
  39. outputs_ref.sum().backward()
  40. grads_ref = inputs.grad
  41. assert torch.allclose(outputs_ref, outputs_rpc, rtol=0, atol=atol_forward)
  42. assert torch.allclose(grads_ref, grads_rpc, rtol=0, atol=atol_backward)
  43. @pytest.mark.forked
  44. def test_chained_inference_exact_match(atol_inference=1e-4):
  45. dht = hivemind.DHT(initial_peers=INITIAL_PEERS, client_mode=True, start=True)
  46. config = src.DistributedBloomConfig.from_pretrained(MODEL_NAME)
  47. remote_block = get_remote_module(dht, f"{MODEL_NAME}{UID_DELIMITER}0", config)
  48. assert remote_block is not None, f"Could not find {MODEL_NAME}{UID_DELIMITER}0 in DHT"
  49. assert isinstance(remote_block, RemoteTransformerBlock)
  50. _ = remote_block.info # lazy-init info now, because otherwise we will _break_ info init by chaning _info
  51. remote_block._info = ExpertInfo(f"{MODEL_NAME}.3 {MODEL_NAME}.4", remote_block._info.peer_id)
  52. inputs = torch.randn(1, 8, config.hidden_size)
  53. outputs_inference = []
  54. with remote_block.inference_session(max_length=inputs.shape[1]) as sess:
  55. for i in range(inputs.shape[1]):
  56. outputs_inference.append(sess.step(inputs[:, i : i + 1, :]))
  57. outputs_inference = torch.cat(outputs_inference, dim=1)
  58. ref_blocks = [
  59. load_pretrained_block(MODEL_NAME, 3, torch_dtype=torch.float32),
  60. load_pretrained_block(MODEL_NAME, 4, torch_dtype=torch.float32),
  61. ]
  62. outputs_ref = []
  63. caches = [None, None]
  64. for i in range(inputs.shape[1]):
  65. new_caches = []
  66. hidden_states = inputs[:, i : i + 1, :]
  67. for ref_block, cache in zip(ref_blocks, caches):
  68. with torch.no_grad():
  69. hidden_states, new_cache = ref_block.forward(hidden_states, use_cache=True, layer_past=cache)
  70. new_caches.append(new_cache)
  71. outputs_ref.append(hidden_states)
  72. caches = new_caches
  73. outputs_ref = torch.cat(outputs_ref, dim=1)
  74. assert torch.allclose(outputs_ref, outputs_inference, rtol=0, atol=atol_inference)