From 81ed61f75564f4eed0f76b384d160d10012eb28c Mon Sep 17 00:00:00 2001 From: Krzysztof Lecki Date: Wed, 22 Feb 2023 13:21:45 +0100 Subject: [PATCH 1/2] Add support for fp16 in arithmetic and mathematical ops Signed-off-by: Krzysztof Lecki --- dali/operators/math/expressions/arithmetic_meta.h | 9 ++++----- dali/test/python/operator_1/test_arithmetic_ops.py | 5 ++--- docs/math.rst | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/dali/operators/math/expressions/arithmetic_meta.h b/dali/operators/math/expressions/arithmetic_meta.h index 3b147697abe..1499b18d481 100644 --- a/dali/operators/math/expressions/arithmetic_meta.h +++ b/dali/operators/math/expressions/arithmetic_meta.h @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// Copyright (c) 2019-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -261,10 +261,9 @@ DALI_HOST_DEV constexpr bool IsComparison(ArithmeticOp op) { } } - -// TODO(klecki): float16 -#define ARITHMETIC_ALLOWED_TYPES \ - (bool, uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t, float, double) +#define ARITHMETIC_ALLOWED_TYPES \ + (bool, uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t, float16, float, \ + double) /** * @brief Type promotion rules diff --git a/dali/test/python/operator_1/test_arithmetic_ops.py b/dali/test/python/operator_1/test_arithmetic_ops.py index 43ca426c69d..cc23a224ac4 100644 --- a/dali/test/python/operator_1/test_arithmetic_ops.py +++ b/dali/test/python/operator_1/test_arithmetic_ops.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# Copyright (c) 2019-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -72,8 +72,7 @@ def shape_small(arg_idx): np.bool_, np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64 ] -# float16 is marked as TODO in backend for gpu -float_types = [np.float32, np.float64] +float_types = [np.float16, np.float32, np.float64] input_types = integer_types + float_types diff --git a/docs/math.rst b/docs/math.rst index d0c59bf4bd0..ee7f1a86a1b 100644 --- a/docs/math.rst +++ b/docs/math.rst @@ -65,7 +65,7 @@ The resulting type is calculated in accordance to the table below. ``T`` stands for any one of the supported numerical types: ``bool``, ``int8``, ``int16``, ``int32``, ``int64``, ``uint8``, ``uint16``, -``uint32``, ``uint64``, ``float32``, and ``float64``. +``uint32``, ``uint64``, ``float16``, ``float32``, and ``float64``. ``bool`` type is considered the smallest unsigned integer type and is treated as ``uint1`` with respect to the table above. From 81df9235f509d799de0b6e68932c191c5d1cce9b Mon Sep 17 00:00:00 2001 From: Krzysztof Lecki Date: Thu, 23 Feb 2023 14:30:37 +0100 Subject: [PATCH 2/2] Debugging wip Signed-off-by: Krzysztof Lecki --- dali/test/python/operator_1/test_arithmetic_ops.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dali/test/python/operator_1/test_arithmetic_ops.py b/dali/test/python/operator_1/test_arithmetic_ops.py index cc23a224ac4..bb1ace566ea 100644 --- a/dali/test/python/operator_1/test_arithmetic_ops.py +++ b/dali/test/python/operator_1/test_arithmetic_ops.py @@ -617,8 +617,10 @@ def check_comparsion_op(kinds, types, op, shape, _): device_id=0) pipe.build() pipe_out = pipe.run() + np.set_printoptions(formatter={'float':lambda x:float(x).hex()}) for sample in range(batch_size): l_np, r_np, out = extract_data(pipe_out, sample, kinds, None) + print(f"L {l_np.dtype}={l_np},\n\nR {r_np.dtype}={r_np},\n\nOut {out.dtype}={out},\n\nnp_out={op(l_np, r_np)},\n\nOP:{_}") assert_equals(out.dtype, np.bool_) np.testing.assert_array_equal(out, op(l_np, r_np), err_msg=f"{l_np} op\n{r_np} =\n{out}")