2022-07-19 01:28:04 +00:00
|
|
|
import random
|
2022-06-20 13:50:12 +00:00
|
|
|
|
2022-07-19 01:28:04 +00:00
|
|
|
import pytest
|
2022-06-20 13:50:12 +00:00
|
|
|
import torch
|
|
|
|
|
2023-08-08 15:10:27 +00:00
|
|
|
from petals import AutoDistributedConfig, RemoteSequential
|
2023-08-11 05:24:33 +00:00
|
|
|
from petals.server.block_functions import MAX_SHORT_INFERENCE_TOKENS
|
Add LLaMA support (#323)
This PR:
1. **Abolishes the model conversion procedure.** Now, models are downloaded directly from original repositories like https://huggingface.co/bigscience/bloom. Servers download only shards with blocks to be hosted, and clients download only shards with input/output embeddings and layernorms.
- BLOOM is loaded from `bigscience/bloom`, but we use the DHT prefix `bigscience/bloom-petals` for backward compatibility. Same with smaller BLOOMs and BLOOMZ.
- LLaMA can be loaded from any repo like `username/llama-65b-hf`, but we use the DHT prefix `llama-65b-hf` (without the username) to accomodate blocks from different repos (there're a few of them with minor differences, such as `Llama` vs. `LLaMA` in the class name).
2. **Refactors the client to generalize it for multiple models.** Now, we have `petals.models` packages that contain model-specific code (e.g. `petals.models.bloom`, `petals.models.llama`). General code (e.g. CPU-efficient LM head, p-tuning) is kept in `petals.client`.
3. **Introduces** `WrappedLlamaBlock`, `DistributedLlamaConfig`, `DistributedLlamaForCausalLM`, `DistributedLlamaForSequenceClassification`, and `DistributedLlamaModel` compatible with Petals functionality (p-tuning, adapters, etc.).
4. **Introduces** `AutoDistributedConfig` that automatically chooses the correct config class (`DistributedLlamaConfig` or `DistributedBloomConfig`). The refactored configs contain all model-specific info for both clients and servers.
Upgrade instructions:
- Remove disk caches for blocks in old (converted) format to save disk space. That is, remove `~/.cache/petals/model--bigscience--bloom-petals` and `~/.cache/petals/model--bigscience--bloomz-petals` directories (if present).
2023-06-23 11:46:10 +00:00
|
|
|
from petals.server.from_pretrained import load_pretrained_block
|
2023-03-12 21:49:04 +00:00
|
|
|
from test_utils import *
|
2022-06-20 13:50:12 +00:00
|
|
|
|
|
|
|
|
2022-07-19 01:28:04 +00:00
|
|
|
@pytest.mark.forked
|
2023-01-03 15:35:51 +00:00
|
|
|
def test_remote_block_exact_match(atol_forward=1e-4, atol_inference=1e-3):
|
2023-08-08 15:10:27 +00:00
|
|
|
config = AutoDistributedConfig.from_pretrained(MODEL_NAME, initial_peers=INITIAL_PEERS)
|
Refactor RemoteSequenceManager (#309)
This PR:
1. **Extracts `SequenceManagerConfig` and `SequenceManagerState` subclasses.**
The config is provided by caller and never changed from inside `RemoteSequenceManager`. The state is a part of the `RemoteSequenceManager`'s state shared between the main manager and its slices. We fix some slicing bugs along the way.
2. **Removes `dht_prefix` and `p2p` arguments, makes `dht` argument optional.**
`dht_prefix` can always be overridden using `config.dht_prefix`. `p2p` actually needed only under the hood of `RemoteSequenceManager`, so it can extract it by itself without exposing this low-level class to callers. If strictly necessary, a caller can provide `p2p` as a part of `SequenceManagerState`. `dht` is also needed only by `RemoteSequenceManager`, so we can make it optional in the parent classes and create it automatically when it's not provided.
3. **Simplifies retry logic.**
Previously, we could have "nested" retry loops: one in `._update()`, another in inference/forward/backward steps. The loop in `._update()` could introduce issues to concurrent inference/forward/backward calls, since it blocks the entire class if its delay period becomes too high. Now this logic is simplified: `._update()` performs only one attempt to fetch the DHT info, any retries are triggered by the inference/forward/backward steps.
4. **Removes deprecated `RemoteTransformerBlock`.**
`RemoteTransformerBlock` was deprecated a long time ago, before Petals 1.0.0. Its removal is long due.
5. **Removes `dht_utils.get_remote_module()`, `dht_utils.get_remote_sequence()`.**
This functions duplicate the functionality of the `RemoteSequential` constructor.
6. (minor) **Removes `RemoteSequential.is_subsequence` flag.**
This flag worked incorrectly and was never used. I am removing it for the sake of simplicity.
2023-05-07 09:41:13 +00:00
|
|
|
remote_sequential = RemoteSequential(config)
|
2022-07-15 22:59:23 +00:00
|
|
|
|
2023-08-11 05:24:33 +00:00
|
|
|
block_index = random.randint(0, config.num_hidden_layers - 1)
|
|
|
|
remote_block = remote_sequential[block_index]
|
2022-06-20 13:50:12 +00:00
|
|
|
|
2023-08-11 05:24:33 +00:00
|
|
|
inputs = torch.randn(1, MAX_SHORT_INFERENCE_TOKENS + 8, config.hidden_size)
|
|
|
|
outputs_forward = remote_block(inputs)
|
2022-06-20 13:50:12 +00:00
|
|
|
|
2023-08-11 05:24:33 +00:00
|
|
|
outputs_inference = []
|
|
|
|
with torch.inference_mode():
|
|
|
|
with remote_block.inference_session(max_length=inputs.shape[1]) as sess:
|
|
|
|
# Test long inference (unmerged inference pools)
|
|
|
|
outputs_inference.append(sess.step(inputs[:, : MAX_SHORT_INFERENCE_TOKENS + 1, :]))
|
Add LLaMA support (#323)
This PR:
1. **Abolishes the model conversion procedure.** Now, models are downloaded directly from original repositories like https://huggingface.co/bigscience/bloom. Servers download only shards with blocks to be hosted, and clients download only shards with input/output embeddings and layernorms.
- BLOOM is loaded from `bigscience/bloom`, but we use the DHT prefix `bigscience/bloom-petals` for backward compatibility. Same with smaller BLOOMs and BLOOMZ.
- LLaMA can be loaded from any repo like `username/llama-65b-hf`, but we use the DHT prefix `llama-65b-hf` (without the username) to accomodate blocks from different repos (there're a few of them with minor differences, such as `Llama` vs. `LLaMA` in the class name).
2. **Refactors the client to generalize it for multiple models.** Now, we have `petals.models` packages that contain model-specific code (e.g. `petals.models.bloom`, `petals.models.llama`). General code (e.g. CPU-efficient LM head, p-tuning) is kept in `petals.client`.
3. **Introduces** `WrappedLlamaBlock`, `DistributedLlamaConfig`, `DistributedLlamaForCausalLM`, `DistributedLlamaForSequenceClassification`, and `DistributedLlamaModel` compatible with Petals functionality (p-tuning, adapters, etc.).
4. **Introduces** `AutoDistributedConfig` that automatically chooses the correct config class (`DistributedLlamaConfig` or `DistributedBloomConfig`). The refactored configs contain all model-specific info for both clients and servers.
Upgrade instructions:
- Remove disk caches for blocks in old (converted) format to save disk space. That is, remove `~/.cache/petals/model--bigscience--bloom-petals` and `~/.cache/petals/model--bigscience--bloomz-petals` directories (if present).
2023-06-23 11:46:10 +00:00
|
|
|
|
2023-08-11 05:24:33 +00:00
|
|
|
# Test short inference (merged inference pools)
|
|
|
|
for i in range(MAX_SHORT_INFERENCE_TOKENS + 1, inputs.shape[1]):
|
|
|
|
outputs_inference.append(sess.step(inputs[:, i : i + 1, :]))
|
2022-06-20 13:50:12 +00:00
|
|
|
|
2023-08-11 05:24:33 +00:00
|
|
|
# test that max length is respected
|
|
|
|
with pytest.raises(ValueError, match=r"Maximum length exceeded") as exc_info:
|
|
|
|
sess.step(inputs[:, -1:, :])
|
|
|
|
assert "Maximum length exceeded" in repr(exc_info.value)
|
|
|
|
outputs_inference = torch.cat(outputs_inference, dim=1)
|
2022-06-20 13:50:12 +00:00
|
|
|
|
2023-08-11 05:24:33 +00:00
|
|
|
ref_block = load_pretrained_block(MODEL_NAME, block_index, torch_dtype=torch.float32)
|
|
|
|
(outputs_local,) = ref_block(inputs)
|
|
|
|
|
|
|
|
assert torch.allclose(outputs_local, outputs_forward, rtol=0, atol=atol_forward)
|
|
|
|
assert torch.allclose(outputs_local, outputs_inference, rtol=0, atol=atol_inference)
|