mirror of
https://github.com/huggingface/text-generation-inference.git
synced 2025-04-21 06:42:10 +00:00
* feat: add neuron backend * feat(neuron): add server standalone installation * feat(neuron): add server and integration tests * fix(neuron): increase ulimit when building image The base image used to compile the rust components seems to have a low ulimit for opened files, which leads to errors during compilation. * test(neuron): merge integration tests and fixtures * test: add --neuron option * review: do not use latest tag * review: remove ureq pinned version * review: --privileged should be the exception * feat: add neuron case to build ci * fix(neuron): export models from container in test fixtures The neuron tests require models to have been previously exported and cached on the hub. This is done automatically by the neuron.model fixture the first time the tests are ran for a specific version. This fixture used to export the models using optimum-neuron directly, but this package is not necessarily present on the system. Instead, it is now done through the neuron TGI itself, since it contains all the tools required to export the models. Note that since the CI runs docker in docker (dind) it does not seem possible to share a volume between the CI container and the container used to export the model. For that reason, a specific image with a modified entrypoint is built on-the-fly when a model export is required. * refactor: remove sagemaker entry-point The SageMaker image is built differently anyway. * fix(neuron): avoid using Levenshtein * test(neuron): use smaller llama model * feat(neuron): avoid installing CUDA in image * test(neuron): no error anymore when requesting too many tokens * ci: doing a precompilation step (with a different token). * test(neuron): avoid using image sha when exporting models We now manually evaluate the apparent hash of the neuron backend by combining the hash of the neuron backend directory and Dockerfile. This new hash is used to identify exported neuron models instead of the image sha. This has two benefits: - it changes less frequently (only hwen the neuron backend changes), which means less neuron models being pushed to the hub, - it can be evaluated locally, meaning that running the tests once locally will export the models before the CI uses them. * test(neuron): added a small script to prune test models --------- Co-authored-by: drbh <david.richard.holtz@gmail.com> Co-authored-by: Nicolas Patry <patry.nicolas@protonmail.com>
150 lines
6.1 KiB
Python
150 lines
6.1 KiB
Python
from text_generation_server.generator import NeuronGenerator
|
|
from text_generation_server.pb.generate_pb2 import (
|
|
Batch,
|
|
NextTokenChooserParameters,
|
|
Request,
|
|
StoppingCriteriaParameters,
|
|
)
|
|
|
|
|
|
def create_request(
|
|
id: int,
|
|
inputs: str,
|
|
truncate: int = 0,
|
|
max_new_tokens: int = 20,
|
|
do_sample: bool = False,
|
|
top_k: int = 50,
|
|
top_p: float = 0.9,
|
|
temperature: float = 1.0,
|
|
seed: int = 42,
|
|
repetition_penalty: float = 1.0,
|
|
):
|
|
parameters = NextTokenChooserParameters(
|
|
temperature=temperature,
|
|
top_k=top_k,
|
|
top_p=top_p,
|
|
do_sample=do_sample,
|
|
seed=seed,
|
|
repetition_penalty=repetition_penalty,
|
|
)
|
|
stopping_parameters = StoppingCriteriaParameters(max_new_tokens=max_new_tokens)
|
|
return Request(
|
|
id=id, inputs=inputs, truncate=truncate, parameters=parameters, stopping_parameters=stopping_parameters
|
|
)
|
|
|
|
|
|
def check_prefill(input_text, expected_token_id, expected_token_text, do_sample, batch_size, model_path):
|
|
"""Verify that a prefill for a single request generates the expected output."""
|
|
generator = NeuronGenerator.from_pretrained(model_path)
|
|
assert generator.model.batch_size >= batch_size
|
|
requests = []
|
|
max_new_tokens = 20
|
|
for i in range(batch_size):
|
|
requests.append(create_request(id=0, inputs=input_text, do_sample=do_sample, max_new_tokens=max_new_tokens))
|
|
# Let's be pessimistic when estimating max_tokens
|
|
batch_size * (len(input_text) + max_new_tokens)
|
|
max_length = generator.model.max_length
|
|
batch = Batch(id=0, requests=requests, size=batch_size, max_tokens=batch_size * max_length)
|
|
generations, next_batch = generator.prefill(batch)
|
|
assert next_batch.size == batch_size
|
|
# Whatever was passed as max_tokens, the server will correct it
|
|
# because of static batching
|
|
assert next_batch.max_tokens == batch_size * max_length
|
|
assert len(generations) == batch_size
|
|
for g in generations:
|
|
tokens = g.tokens
|
|
assert tokens.ids == [expected_token_id]
|
|
assert tokens.texts == [expected_token_text]
|
|
|
|
|
|
def check_decode_single(input_text, max_new_tokens, generated_text, do_sample, model_path):
|
|
"""Verify that a decoding for a single request generates the expected output."""
|
|
generator = NeuronGenerator.from_pretrained(model_path)
|
|
request = create_request(id=0, inputs=input_text, max_new_tokens=max_new_tokens, do_sample=do_sample)
|
|
max_length = generator.model.max_length
|
|
batch = Batch(id=0, requests=[request], size=1, max_tokens=max_length)
|
|
generations, next_batch = generator.prefill(batch)
|
|
# We already generated one token: call decode max_new_tokens - 1 times
|
|
for _ in range(max_new_tokens - 1):
|
|
assert next_batch.size == 1
|
|
assert next_batch.max_tokens == max_length
|
|
assert len(generations) == 1
|
|
assert len(generations[0].tokens.ids) == 1
|
|
generations, next_batch = generator.decode([next_batch])
|
|
assert next_batch is None
|
|
assert len(generations) == 1
|
|
output = generations[0].generated_text
|
|
assert output.generated_tokens == max_new_tokens
|
|
assert output.finish_reason == 0
|
|
assert output.text == generated_text
|
|
|
|
|
|
def check_decode_multiple(model_path):
|
|
"""Verify that two requests added to the batch at different generation steps
|
|
generate the same outputs (continuous batching).
|
|
"""
|
|
generator = NeuronGenerator.from_pretrained(model_path)
|
|
assert generator.model.batch_size > 1
|
|
input_text = "Once upon a time"
|
|
max_new_tokens = 20
|
|
# Prefill a single request, remembering the generated token
|
|
tokens = {0: [], 1: []}
|
|
request = create_request(id=0, inputs=input_text, max_new_tokens=max_new_tokens)
|
|
max_length = generator.model.max_length
|
|
batch = Batch(id=0, requests=[request], size=1, max_tokens=max_length)
|
|
generations, next_batch = generator.prefill(batch)
|
|
assert next_batch.size == 1
|
|
assert len(generations) == 1
|
|
g = generations[0]
|
|
tokens[g.request_id].append(g.tokens.ids[0])
|
|
assert len(tokens[0]) == 1
|
|
# Decode a few tokens
|
|
gen_tokens = 4
|
|
for _ in range(gen_tokens - 1):
|
|
generations, next_batch = generator.decode([next_batch])
|
|
assert len(generations) == 1
|
|
g = generations[0]
|
|
tokens[g.request_id].append(g.tokens.ids[0])
|
|
assert len(tokens[0]) == gen_tokens
|
|
assert next_batch.size == 1
|
|
# Add a second request
|
|
request = create_request(id=1, inputs=input_text, max_new_tokens=max_new_tokens)
|
|
batch = Batch(id=1, requests=[request], size=1, max_tokens=max_length)
|
|
generations, next_batch_1 = generator.prefill(batch)
|
|
assert next_batch_1.size == 1
|
|
# We should have generated only a single token
|
|
assert len(generations) == 1
|
|
g = generations[0]
|
|
tokens[g.request_id].append(g.tokens.ids[0])
|
|
assert len(tokens[0]) == gen_tokens
|
|
assert len(tokens[1]) == 1
|
|
# Decode more tokens until we reach the maximum for the first request
|
|
batches = [next_batch, next_batch_1]
|
|
for _ in range(max_new_tokens - gen_tokens):
|
|
generations, next_batch = generator.decode(batches)
|
|
for g in generations:
|
|
tokens[g.request_id].append(g.tokens.ids[0])
|
|
batches = [next_batch]
|
|
# Verify we now only have one pending request
|
|
assert next_batch.size == 1
|
|
assert len(tokens[0]) == max_new_tokens
|
|
assert len(tokens[1]) == max_new_tokens - gen_tokens + 1
|
|
# Verify we have the output for the first request
|
|
for g in generations:
|
|
if g.request_id == 0:
|
|
output = g.generated_text
|
|
assert output.text != ""
|
|
assert output.generated_tokens == max_new_tokens
|
|
generated_text = output.text
|
|
# Continue decoding until the end of the second request
|
|
for _ in range(gen_tokens - 1):
|
|
generations, next_batch = generator.decode([next_batch])
|
|
assert len(generations) == 1
|
|
g = generations[0]
|
|
tokens[g.request_id].append(g.tokens.ids[0])
|
|
assert next_batch is None
|
|
output = generations[0].generated_text
|
|
assert output.generated_tokens == max_new_tokens
|
|
assert tokens[0] == tokens[1]
|
|
assert output.text == generated_text
|