test_chained_calls.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. from src.bloom.from_pretrained import load_pretrained_block
  12. from src.client.remote_block import RemoteTransformerBlock
  13. from src.dht_utils import get_remote_module
  14. @pytest.mark.forked
  15. def test_forward_backward_exact_match(atol_forward=1e-4, atol_backward=1e-4, seq_length=1):
  16. dht = hivemind.DHT(initial_peers=INITIAL_PEERS, client_mode=True, start=True)
  17. config = transformers.AutoConfig.from_pretrained(MODEL_NAME)
  18. remote_block = get_remote_module(dht, f"{MODEL_NAME}{UID_DELIMITER}0")
  19. assert remote_block is not None, f"Could not find {MODEL_NAME}{UID_DELIMITER}0 in DHT"
  20. assert isinstance(remote_block, RemoteTransformerBlock)
  21. _ = remote_block.info # lazy-init info now, because otherwise we will _break_ info init by chaning _info
  22. remote_block._info = ExpertInfo(f"{MODEL_NAME}.3 {MODEL_NAME}.4 {MODEL_NAME}.5", remote_block._info.peer_id)
  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 = transformers.AutoConfig.from_pretrained(MODEL_NAME)
  45. remote_block = get_remote_module(dht, f"{MODEL_NAME}{UID_DELIMITER}0")
  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. _ = remote_block.info # lazy-init info now, because otherwise we will _break_ info init by chaning _info
  49. remote_block._info = ExpertInfo(f"{MODEL_NAME}.3 {MODEL_NAME}.4", remote_block._info.peer_id)
  50. inputs = torch.randn(1, 8, config.hidden_size)
  51. outputs_inference = []
  52. with remote_block.inference_session(max_length=inputs.shape[1]) as sess:
  53. for i in range(inputs.shape[1]):
  54. outputs_inference.append(sess.step(inputs[:, i : i + 1, :]))
  55. outputs_inference = torch.cat(outputs_inference, dim=1)
  56. ref_blocks = [
  57. load_pretrained_block(MODEL_NAME, 3, torch_dtype=torch.float32),
  58. load_pretrained_block(MODEL_NAME, 4, torch_dtype=torch.float32),
  59. ]
  60. outputs_ref = []
  61. caches = [None, None]
  62. for i in range(inputs.shape[1]):
  63. new_caches = []
  64. hidden_states = inputs[:, i : i + 1, :]
  65. for ref_block, cache in zip(ref_blocks, caches):
  66. with torch.no_grad():
  67. hidden_states, new_cache = ref_block.forward(hidden_states, use_cache=True, layer_past=cache)
  68. new_caches.append(new_cache)
  69. outputs_ref.append(hidden_states)
  70. caches = new_caches
  71. outputs_ref = torch.cat(outputs_ref, dim=1)
  72. assert torch.allclose(outputs_ref, outputs_inference, rtol=0, atol=atol_inference)