test_chained_calls.py 3.0 KB

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