test_chained_calls.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. ref_blocks = [
  24. load_pretrained_block(MODEL_NAME, 3, torch_dtype=torch.float32),
  25. load_pretrained_block(MODEL_NAME, 4, torch_dtype=torch.float32),
  26. load_pretrained_block(MODEL_NAME, 5, torch_dtype=torch.float32),
  27. ]
  28. inputs = torch.randn(1, seq_length, config.hidden_size, requires_grad=True)
  29. outputs_rpc = remote_block.forward(inputs)[0]
  30. outputs_rpc.sum().backward()
  31. grads_rpc = inputs.grad
  32. inputs.grad = None
  33. hidden_states = inputs
  34. for ref_block in ref_blocks:
  35. hidden_states = ref_block.forward(hidden_states)[0]
  36. outputs_ref = hidden_states
  37. outputs_ref.sum().backward()
  38. grads_ref = inputs.grad
  39. assert torch.allclose(outputs_ref, outputs_rpc, rtol=0, atol=atol_forward)
  40. assert torch.allclose(grads_ref, grads_rpc, rtol=0, atol=atol_backward)
  41. @pytest.mark.forked
  42. def test_chained_inference_exact_match(atol_inference=1e-4):
  43. dht = hivemind.DHT(initial_peers=INITIAL_PEERS, client_mode=True, start=True)
  44. config = src.DistributedBloomConfig.from_pretrained(MODEL_NAME)
  45. remote_block = get_remote_module(dht, f"{MODEL_NAME}{UID_DELIMITER}0", config)
  46. assert remote_block is not None, f"Could not find {MODEL_NAME}{UID_DELIMITER}0 in DHT"
  47. assert isinstance(remote_block, RemoteTransformerBlock)
  48. inputs = torch.randn(1, 8, config.hidden_size)
  49. outputs_inference = []
  50. with remote_block.inference_session(max_length=inputs.shape[1]) as sess:
  51. for i in range(inputs.shape[1]):
  52. outputs_inference.append(sess.step(inputs[:, i : i + 1, :]))
  53. outputs_inference = torch.cat(outputs_inference, dim=1)
  54. ref_blocks = [
  55. load_pretrained_block(MODEL_NAME, 3, torch_dtype=torch.float32),
  56. load_pretrained_block(MODEL_NAME, 4, torch_dtype=torch.float32),
  57. ]
  58. outputs_ref = []
  59. caches = [None, None]
  60. for i in range(inputs.shape[1]):
  61. new_caches = []
  62. hidden_states = inputs[:, i : i + 1, :]
  63. for ref_block, cache in zip(ref_blocks, caches):
  64. with torch.no_grad():
  65. hidden_states, new_cache = ref_block.forward(hidden_states, use_cache=True, layer_past=cache)
  66. new_caches.append(new_cache)
  67. outputs_ref.append(hidden_states)
  68. caches = new_caches
  69. outputs_ref = torch.cat(outputs_ref, dim=1)
  70. assert torch.allclose(outputs_ref, outputs_inference, rtol=0, atol=atol_inference)