ryanthompson591 commented on code in PR #24039:
URL: https://github.com/apache/beam/pull/24039#discussion_r1028101807
##########
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,
+ *,
Review Comment:
what is this * parameter for?
##########
sdks/python/apache_beam/ml/inference/tensorrt_inference.py:
##########
@@ -241,40 +297,7 @@ def run_inference(
Returns:
An Iterable of type 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)
- ]
+ return self.inference_fn(batch, engine, inference_args)
Review Comment:
is there any way to add a unit test for using a custom function?
--
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]