Selective batching for inference system for transformer-based generation tasks
Abstract
An inference system applies a machine-learning transformer model to a batch of requests with variable input length or variable target length or variable internal sate length by selectively batching a subset of operations in the transformer model but processing requests in the batch individually for a subset of operations in the transformer model. In one embodiment, the operation to be processed individually is an attention operation of an encoder or a decoder of the transformer model. By selective batching, the inference system can allow batching operations to be performed for a batch of requests with variable input or target length or internal state length to utilize the parallel computation capabilities of hardware accelerators while preventing unnecessary computations that occur for workarounds that restrain the data of a batch of requests to a same length.
Claims
exact text as granted — not AI-modifiedWhat is claimed is:
1. A method, comprising:
receiving a batch of two or more token sequences, wherein a length of a first token sequence in the batch is different from a length of a second token sequence in the batch;
accessing a transformer model;
generating one or more output representations, the generating further comprising:
generating one or more queries, one or more keys, and one or more values for the batch by applying a QKV weight tensor to one or more input representations, the one or more queries, the one or more keys, and the one or more values generated by a batch operation,
splitting a first query for the first token sequence from the one or more queries, a first key from the one or more keys, and a first value from the one or more values, and splitting a second query for the second token sequence from the one or more queries, a second key from the one or more keys, and a second value from the one or more values,
generating a first attention output by at least combining the first query, the first key, and the first value,
separately generating a second attention output by at least combining the second query, the second key, and the second value, wherein the second attention output is generated at a different execution engine, a different hardware accelerator, a different graphics processor unit (GPU) kernel, or a same GPU kernel than the first attention output,
concatenating at least the first attention output and the second attention output into a concatenated tensor, and
generating one or more output representations by at least applying one or more weight tensors to the concatenated tensor, the one or more output representations generated by a batch operation.
2. The method of claim 1 , further comprising:
before generating the one or more output representations for the batch, concatenating tokens included in the one or more token sequences for the batch along a single dimension to generate a concatenated input tensor.
3. The method of claim 1 , wherein concatenating at least the first attention output and the second attention output into the concatenated tensor further comprises concatenating elements of the first attention output and the second attention output along a single dimension to generate the concatenated tensor.
4. The method of claim 1 , further comprising updating parameters of the transformer model based at least on the one or more output representations.
5. The method of claim 1 ,
wherein generating the first attention output comprises:
multiplying the first query with the first key to generate a first multiplied tensor, and multiplying the first multiplied tensor with the first value, and
wherein generating the second attention output comprises:
multiplying the second query with the second key to generate a second multiplied tensor, and multiplying the second multiplied tensor with the second value.
6. The method of claim 1 , wherein the first attention output is generated by executing a set of operations on at least the first query, the first key, and the first value, and wherein the second attention output is generated by executing a second set of operations on at least the second query, the second key, and the second value in a separate step from the first set of operations.
7. The method of claim 6 , wherein the first attention output is generated by executing the set of operations on a first kernel of an execution engine, and wherein the second attention output is generated by executing the second set of operations on a second kernel of the execution engine or on a second execution engine.
8. The method of claim 6 , wherein the first attention output is generated by executing the set of operations on a kernel of an execution engine, and wherein the second attention output is generated by executing the second set of operations on the same kernel of the execution engine.
9. The method of claim 1 , wherein applying the set of decoders of the transformer model to the one or more inputs of the batch comprises executing operations of the transformer model on an execution engine including a graphics processing unit (GPU) or a tensor processing unit (TPU).
10. The method of claim 1 , wherein each token in an input token sequence represents a text unit.
11. A non-transitory computer-readable storage medium storing computer program instructions executable to perform operations, the operations comprising:
receiving a batch of two or more token sequences, wherein a length of a first token sequence in the batch is different from a length of a second token sequence in the batch;
accessing the transformer model;
generating one or more output representations, the generating further comprising:
generating one or more queries, one or more keys, and one or more values for the batch by applying a QKV weight tensor to one or more input representations, the one or more queries, the one or more keys, and the one or more values generated by a batch operation,
splitting a first query for the first token sequence from the one or more queries, a first key from the one or more keys, and a first value from the one or more values, and splitting a second query for the second token sequence from the one or more queries, a second key from the one or more keys, and a second value from the one or more values,
generating a first attention output by at least combining the first query, the first key, and the first value,
separately generating a second attention output by at least combining the second query, the second key, and the second value, wherein the second attention output is generated at a different execution engine, a different hardware accelerator, a different graphics processor unit (GPU) kernel, or a same GPU kernel than the first attention output,
concatenating at least the first attention output and the second attention output into a concatenated tensor, and
generating one or more output representations by at least applying one or more weight tensors to the concatenated tensor, the one or more output representations generated by a batch operation.
12. The non-transitory computer-readable storage medium of claim 11 , the operations further comprising:
before generating the one or more output representations for the batch, concatenating tokens included in the one or more token sequences for the batch along a single dimension to generate a concatenated input tensor.
13. The non-transitory computer-readable storage medium of claim 11 , wherein concatenating at least the first attention output and the second attention output into the concatenated tensor further comprises concatenating elements of the first attention output and the second attention output along a single dimension to generate the concatenated tensor.
14. The non-transitory computer-readable storage medium of claim 11 , further comprising updating parameters of the transformer model based at least on the one or more output representations.
15. The non-transitory computer-readable storage medium of claim 11 ,
wherein generating the first attention output comprises:
multiplying the first query with the first key to generate a first multiplied tensor, and multiplying the first multiplied tensor with the first value, and
wherein generating the second attention output comprises:
multiplying the second query with the second key to generate a second multiplied tensor, and multiplying the second multiplied tensor with the second value.
16. The non-transitory computer-readable storage medium of claim 11 , wherein the first attention output is generated by executing a set of operations on at least the first query, the first key, and the first value, and wherein the second attention output is generated by executing a second set of operations on at least the second query, the second key, and the second value in a separate step from the first set of operations.
17. The non-transitory computer-readable storage medium of claim 16 , wherein the first attention output is generated by executing the set of operations on a first kernel of an execution engine, and wherein the second attention output is generated by executing the second set of operations on a second kernel of the execution engine or on a second execution engine.
18. The non-transitory computer-readable storage medium of claim 16 , wherein the first attention output is generated by executing the set of operations on a kernel of an execution engine, and wherein the second attention output is generated by executing the second set of operations on the same kernel of the execution engine.
19. The non-transitory computer-readable storage medium of claim 11 , wherein applying the set of decoders of the transformer model to the one or more inputs of the batch comprises executing operations of the transformer model on an execution engine including a graphics processing unit (GPU) or a tensor processing unit (TPU).
20. The non-transitory computer-readable storage medium of claim 11 , wherein each token in an input token sequence represents a text unit.
21. The method of claim 4 , further comprising determining a loss function from the one or more output representations, and backpropagating a gradient obtained from the loss function to update parameters of the transformer model.
22. The non-transitory computer-readable storage medium of claim 14 , further comprising determining a loss function from the one or more output representations, and backpropagating a gradient obtained from the loss function to update parameters of the transformer model.Cited by (0)
No later patents cite this yet.
References (0)
No backward citations on record.