jrmccluskey commented on code in PR #24039:
URL: https://github.com/apache/beam/pull/24039#discussion_r1017054307
##########
sdks/python/apache_beam/ml/inference/tensorrt_inference.py:
##########
@@ -164,11 +165,63 @@ def get_engine_attrs(self):
self.stream)
+TensorRTInferenceFn = Callable[
+ [Sequence[np.ndarray], TensorRTEngine, Optional[Dict[str, Any]]],
+ Iterable[PredictionResult]]
+
+
+def _default_tensorRT_inference_fn(
+ batch: Sequence[np.ndarray],
+ engine: TensorRTEngine,
+ inference_args: Optional[Dict[str,
+ Any]] = None) -> Iterable[PredictionResult]:
+ from cuda import cuda
+ (
+ engine,
+ context,
+ context_lock,
+ inputs,
+ outputs,
+ gpu_allocations,
+ cpu_allocations,
+ stream) = engine.get_engine_attrs()
+
+ # Process I/O and execute the network
+ with context_lock:
+ _assign_or_fail(
+ cuda.cuMemcpyHtoDAsync(
+ inputs[0]['allocation'],
+ np.ascontiguousarray(batch),
+ inputs[0]['size'],
+ stream))
+ context.execute_async_v2(gpu_allocations, stream)
+ for output in range(len(cpu_allocations)):
+ _assign_or_fail(
+ cuda.cuMemcpyDtoHAsync(
+ cpu_allocations[output],
+ outputs[output]['allocation'],
+ outputs[output]['size'],
+ stream))
+ _assign_or_fail(cuda.cuStreamSynchronize(stream))
+
+ return [
+ PredictionResult(
+ x, [prediction[idx] for prediction in cpu_allocations]) for idx,
+ x in enumerate(batch)
+ ]
+
+
@experimental(extra_message="No backwards-compatibility guarantees.")
class TensorRTEngineHandlerNumPy(ModelHandler[np.ndarray,
PredictionResult,
TensorRTEngine]):
- def __init__(self, min_batch_size: int, max_batch_size: int, **kwargs):
+ def __init__(
+ self,
+ min_batch_size: int,
+ max_batch_size: int,
+ *,
+ inference_fn: TensorRTInferenceFn = _default_tensorRT_inference_fn,
+ **kwargs):
Review Comment:
This pattern looks a little quirky here, but the keyword-only arg has to
appear after varargs and before kwargs. This shouldn't change anything,
fortunately.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]