test_chained_calls.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. from test_utils import *
  9. from petals.bloom.from_pretrained import load_pretrained_block
  10. from petals.client import DistributedBloomConfig
  11. from petals.client.remote_sequential import RemoteSequential
  12. from petals.dht_utils import get_remote_sequence
  13. @pytest.mark.forked
  14. def test_forward_backward_exact_match(atol_forward=1e-4, atol_backward=1e-4, seq_length=1):
  15. dht = hivemind.DHT(initial_peers=INITIAL_PEERS, client_mode=True, start=True)
  16. config = DistributedBloomConfig.from_pretrained(MODEL_NAME)
  17. remote_blocks = get_remote_sequence(dht, 3, 6, config)
  18. assert isinstance(remote_blocks, RemoteSequential)
  19. ref_blocks = [
  20. load_pretrained_block(MODEL_NAME, 3, torch_dtype=torch.float32),
  21. load_pretrained_block(MODEL_NAME, 4, torch_dtype=torch.float32),
  22. load_pretrained_block(MODEL_NAME, 5, torch_dtype=torch.float32),
  23. ]
  24. inputs = torch.randn(1, seq_length, config.hidden_size, requires_grad=True)
  25. attention_mask = torch.ones((1, seq_length))
  26. outputs_rpc = remote_blocks.forward(inputs, attention_mask)
  27. outputs_rpc.sum().backward()
  28. grads_rpc = inputs.grad
  29. inputs.grad = None
  30. hidden_states = inputs
  31. for ref_block in ref_blocks:
  32. hidden_states = ref_block.forward(hidden_states, attention_mask)[0]
  33. outputs_ref = hidden_states
  34. outputs_ref.sum().backward()
  35. grads_ref = inputs.grad
  36. assert torch.allclose(outputs_ref, outputs_rpc, rtol=0, atol=atol_forward)
  37. assert torch.allclose(grads_ref, grads_rpc, rtol=0, atol=atol_backward)
  38. @pytest.mark.forked
  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 = DistributedBloomConfig.from_pretrained(MODEL_NAME)
  42. remote_blocks = get_remote_sequence(dht, 3, 5, config)
  43. assert isinstance(remote_blocks, RemoteSequential)
  44. inputs = torch.randn(1, 8, config.hidden_size)
  45. attention_masks = torch.ones((1, 8))
  46. outputs_inference = []
  47. with remote_blocks.inference_session(max_length=inputs.shape[1]) as sess:
  48. for i in range(inputs.shape[1]):
  49. outputs_inference.append(sess.step(inputs[:, i : i + 1, :]))
  50. outputs_inference = torch.cat(outputs_inference, dim=1)
  51. ref_blocks = [
  52. load_pretrained_block(MODEL_NAME, 3, torch_dtype=torch.float32),
  53. load_pretrained_block(MODEL_NAME, 4, torch_dtype=torch.float32),
  54. ]
  55. outputs_ref = []
  56. caches = [None, None]
  57. for i in range(inputs.shape[1]):
  58. new_caches = []
  59. hidden_states = inputs[:, i : i + 1, :]
  60. for ref_block, cache in zip(ref_blocks, caches):
  61. with torch.no_grad():
  62. hidden_states, new_cache = ref_block.forward(
  63. hidden_states, attention_masks[:, : i + 1], use_cache=True, layer_past=cache
  64. )
  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)