test_chained_calls.py 3.7 KB

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