akshayjadiyanv commented on code in PR #39922:
URL: https://github.com/apache/beam/pull/39922#discussion_r3906264451


##########
sdks/python/test-suites/containers/tensorrt_runinference/build_test_engines.py:
##########
@@ -0,0 +1,255 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""Rebuilds the TensorRT engines that the TensorRT tests load from GCS.
+
+A serialized TensorRT engine can only be deserialized by the TensorRT major
+version and GPU architecture that built it. The engines the tests load are
+therefore not portable, and have to be rebuilt whenever the TensorRT version
+in the test container changes or the tests move to a different GPU.
+
+This script rebuilds them from the ONNX sources already staged alongside them,
+and verifies each result by loading it back through the model handler the
+tests use. It cannot run as part of the test suite because it needs a GPU.
+
+Run it in the same container and on the same GPU type the tests use. See
+README.md in this directory for the exact commands.
+"""
+
+# pytype: skip-file
+
+import argparse
+import io
+import logging
+import os
+import sys
+import tempfile
+
+import numpy as np
+import tensorrt as trt
+
+TRT_LOGGER = trt.Logger(trt.Logger.INFO)
+TRT_MAJOR = int(trt.__version__.split('.')[0])
+
+SOURCE = 'gs://apache-beam-ml/models'
+COCO_IMAGES = [
+    'gs://apache-beam-ml/datasets/coco/raw-data/val2017/000000289594.jpg',
+    'gs://apache-beam-ml/datasets/coco/raw-data/val2017/000000000139.jpg',
+]
+
+
+def _copy(src, dst):
+  """Copies between any two paths Beam's FileSystems understands.
+
+  The TensorRT container has no gcloud CLI, but apache-beam[gcp] is installed
+  for the verification step anyway, so reuse it rather than shelling out.
+  """
+  from apache_beam.io.filesystems import FileSystems
+  logging.info('copy %s -> %s', src, dst)
+  with FileSystems.open(src) as fin, FileSystems.create(dst) as fout:
+    while True:
+      chunk = fin.read(8 << 20)
+      if not chunk:
+        break
+      fout.write(chunk)
+
+
+def _network_creation_flags():
+  """Explicit batch is only a flag on TensorRT 8.x; it is the default after."""
+  explicit_batch = getattr(
+      trt.NetworkDefinitionCreationFlag, 'EXPLICIT_BATCH', None)
+  if explicit_batch is None or TRT_MAJOR >= 10:

Review Comment:
   No good point. I have dropped the branch and it now calls create_network() 
directly. 



-- 
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]

Reply via email to