dataloader.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2021-2022, NVIDIA CORPORATION. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import glob
  16. import tensorflow as tf
  17. from triton.tf_dataloader import eval_input_fn
  18. def get_dataloader_fn(
  19. *,
  20. data_pattern: str,
  21. batch_size: int,
  22. ):
  23. files_path = (glob.glob(data_pattern))
  24. assert len(files_path), "Expected at least 1 parquet file, found 0"
  25. with tf.device('/cpu:0'):
  26. input_fn = eval_input_fn(
  27. files_path=files_path,
  28. records_batch_size=batch_size,
  29. )
  30. def _get_dataloader():
  31. for x, y, ids in input_fn:
  32. ids = ids.numpy()
  33. x = {name: tensor.numpy() for name, tensor in x.items()}
  34. y = {'wide_deep_model': y.numpy()}
  35. yield ids, x, y
  36. return _get_dataloader
  37. def main():
  38. import argparse
  39. parser = argparse.ArgumentParser(description="short_description")
  40. parser.add_argument("--data_pattern", required=True)
  41. parser.add_argument("--batch_size", type=int, required=True)
  42. args = parser.parse_args()
  43. dataloader_fn = get_dataloader_fn(data_pattern=args.data_pattern,
  44. batch_size=args.batch_size)
  45. for i, (ids, x, y) in enumerate(dataloader_fn()):
  46. print(x, y)
  47. if __name__ == "__main__":
  48. main()