Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Sum Op #2379

Merged
merged 17 commits into from
Oct 23, 2020
24 changes: 24 additions & 0 deletions Acknowledgements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2954,3 +2954,27 @@ PyTorch Lightning - https://github.com/PyTorchLightning/pytorch-lightning
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.

========================

uSHET Library - CPP Magic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could mention why we are ack-ing this for the future reader.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment to relevant file - static_map.h


Copyright (c) 2014 Thomas Nixon, Jonathan Heathcote

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
8 changes: 8 additions & 0 deletions dali/kernels/reduce/reduce_gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ extern template class SumGPU<float, uint32_t>;
extern template class SumGPU<int64_t, int32_t>;
extern template class SumGPU<float, int32_t>;

extern template class SumGPU<uint8_t, uint8_t>;
extern template class SumGPU<int8_t, int8_t>;
extern template class SumGPU<uint16_t, uint16_t>;
extern template class SumGPU<int16_t, int16_t>;
extern template class SumGPU<uint32_t, uint32_t>;
extern template class SumGPU<int32_t, int32_t>;
extern template class SumGPU<uint64_t, uint64_t>;
extern template class SumGPU<int64_t, int64_t>;
extern template class SumGPU<float, float>;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ template class SumGPU<int64_t, int32_t>;
template class SumGPU<float, int32_t>;
template class SumGPU<uint64_t, uint32_t>;
template class SumGPU<float, uint32_t>;
template class SumGPU<uint8_t, uint8_t>;
template class SumGPU<int8_t, int8_t>;
template class SumGPU<uint16_t, uint16_t>;
template class SumGPU<int16_t, int16_t>;
template class SumGPU<uint32_t, uint32_t>;
template class SumGPU<int32_t, int32_t>;
template class SumGPU<uint64_t, uint64_t>;
template class SumGPU<int64_t, int64_t>;
template class SumGPU<float, float>;

} // namespace kernels
Expand Down
121 changes: 121 additions & 0 deletions dali/kernels/test/static_map_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright (c) 2020, NVIDIA CORPORATION. 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.
// 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.

#include <gtest/gtest.h>
#include <typeinfo>
#include <vector>
#include <unordered_map>

#include "dali/kernels/kernel.h"
#include "dali/core/static_map.h"
#include "dali/kernels/type_tag.h"

#define TEST_TYPES_MAP ( \
((uint8_t), (uint8_t, uint64_t, float)), \
((int8_t), (int64_t)), \
((uint16_t), (uint16_t, uint64_t)), \
((int16_t), (int16_t)), \
((uint32_t), (uint32_t, float)), \
((int32_t), (int32_t)), \
((int64_t), (int64_t)), \
((float), (float)))

namespace {
template <typename T, typename S>
void TypedFunc() {}

using TypeTagMap = std::unordered_map<int, std::vector<int>>;

template <typename T>
struct StaticMap : public testing::Test {
StaticMap() {
type_mapping_[dali::TypeTag<uint8_t>()] = {
dali::TypeTag<uint8_t>(), dali::TypeTag<uint64_t>(), dali::TypeTag<float>() };
type_mapping_[dali::TypeTag<int8_t>()] = { dali::TypeTag<int64_t>() };
type_mapping_[dali::TypeTag<uint16_t>()] = {
dali::TypeTag<uint16_t>(), dali::TypeTag<uint64_t>() };
type_mapping_[dali::TypeTag<int16_t>()] = { dali::TypeTag<int16_t>() };
type_mapping_[dali::TypeTag<uint32_t>()] = {
dali::TypeTag<uint32_t>(), dali::TypeTag<float>() };
type_mapping_[dali::TypeTag<int32_t>()] = { dali::TypeTag<int32_t>() };
type_mapping_[dali::TypeTag<int64_t>()] = { dali::TypeTag<int64_t>() };
type_mapping_[dali::TypeTag<float>()] = { dali::TypeTag<float>() };
}

protected:
template <typename P, typename R>
void TypedMethod(int input_arg_type, int output_arg_type) {
int input_call_type = dali::TypeTag<P>();
int output_call_type = dali::TypeTag<R>();

ASSERT_EQ(input_arg_type, input_call_type);
ASSERT_EQ(output_arg_type, output_call_type);
}
TypeTagMap type_mapping_;
};

} // namespace

typedef testing::Types<
uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, int64_t, float> TypesToTest;

TYPED_TEST_SUITE(StaticMap, TypesToTest);

TYPED_TEST(StaticMap, ValidTypes) {
using TestedType = gtest_TypeParam_;

int input_type = dali::TypeTag<TestedType>();
for (auto &output_type : this->type_mapping_[input_type]) {
TYPE_MAP(
input_type,
output_type,
dali::TypeTag,
InputType,
OutputType,
TEST_TYPES_MAP,
(this->template TypedMethod<InputType, OutputType>(input_type, output_type);),
(FAIL()),
(FAIL()))
}
}

TEST(StaticMapFailure, InvalidInputType) {
int input_type = dali::TypeTag<uint64_t>();
int output_type = dali::TypeTag<int32_t>();
TYPE_MAP(
input_type,
output_type,
dali::TypeTag,
InputType,
OutputType,
TEST_TYPES_MAP,
(TypedFunc<InputType, OutputType>(); FAIL();),
(SUCCEED()),
(FAIL()))
}

TEST(StaticMapFailure, InvalidOutputType) {
int input_type = dali::TypeTag<float>();
int output_type = dali::TypeTag<int32_t>();
TYPE_MAP(
input_type,
output_type,
dali::TypeTag,
InputType,
OutputType,
TEST_TYPES_MAP,
(TypedFunc<InputType, OutputType>(); FAIL();),
(FAIL()),
(SUCCEED()))
}
121 changes: 121 additions & 0 deletions dali/kernels/test/static_map_test.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright (c) 2020, NVIDIA CORPORATION. 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.
// 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.

#include <gtest/gtest.h>
#include <typeinfo>
#include <vector>
#include <unordered_map>

#include "dali/kernels/kernel.h"
#include "dali/core/static_map.h"
#include "dali/kernels/type_tag.h"

#define TEST_TYPES_MAP ( \
((uint8_t), (uint8_t, uint64_t, float)), \
((int8_t), (int64_t)), \
((uint16_t), (uint16_t, uint64_t)), \
((int16_t), (int16_t)), \
((uint32_t), (uint32_t, float)), \
((int32_t), (int32_t)), \
((int64_t), (int64_t)), \
((float), (float)))

namespace {
template <typename T, typename S>
void TypedFunc() {}

using TypeTagMap = std::unordered_map<int, std::vector<int>>;

template <typename T>
struct StaticMapNVCC : public testing::Test {
StaticMapNVCC() {
type_mapping_[dali::TypeTag<uint8_t>()] = {
dali::TypeTag<uint8_t>(), dali::TypeTag<uint64_t>(), dali::TypeTag<float>() };
type_mapping_[dali::TypeTag<int8_t>()] = { dali::TypeTag<int64_t>() };
type_mapping_[dali::TypeTag<uint16_t>()] = {
dali::TypeTag<uint16_t>(), dali::TypeTag<uint64_t>() };
type_mapping_[dali::TypeTag<int16_t>()] = { dali::TypeTag<int16_t>() };
type_mapping_[dali::TypeTag<uint32_t>()] = {
dali::TypeTag<uint32_t>(), dali::TypeTag<float>() };
type_mapping_[dali::TypeTag<int32_t>()] = { dali::TypeTag<int32_t>() };
type_mapping_[dali::TypeTag<int64_t>()] = { dali::TypeTag<int64_t>() };
type_mapping_[dali::TypeTag<float>()] = { dali::TypeTag<float>() };
}

protected:
template <typename P, typename R>
void TypedMethod(int input_arg_type, int output_arg_type) {
int input_call_type = dali::TypeTag<P>();
int output_call_type = dali::TypeTag<R>();

ASSERT_EQ(input_arg_type, input_call_type);
ASSERT_EQ(output_arg_type, output_call_type);
}
TypeTagMap type_mapping_;
};

} // namespace

typedef testing::Types<
uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, int64_t, float> TypesToTest;

TYPED_TEST_SUITE(StaticMapNVCC, TypesToTest);

TYPED_TEST(StaticMapNVCC, ValidTypes) {
using TestedType = gtest_TypeParam_;

int input_type = dali::TypeTag<TestedType>();
for (auto &output_type : this->type_mapping_[input_type]) {
TYPE_MAP(
input_type,
output_type,
dali::TypeTag,
InputType,
OutputType,
TEST_TYPES_MAP,
(this->template TypedMethod<InputType, OutputType>(input_type, output_type);),
(FAIL()),
(FAIL()))
}
}

TEST(StaticMapFailureNVCC, InvalidInputType) {
int input_type = dali::TypeTag<uint64_t>();
int output_type = dali::TypeTag<int32_t>();
TYPE_MAP(
input_type,
output_type,
dali::TypeTag,
InputType,
OutputType,
TEST_TYPES_MAP,
(TypedFunc<InputType, OutputType>(); FAIL();),
(SUCCEED()),
(FAIL()))
}

TEST(StaticMapFailureNVCC, InvalidOutputType) {
int input_type = dali::TypeTag<float>();
int output_type = dali::TypeTag<int32_t>();
TYPE_MAP(
input_type,
output_type,
dali::TypeTag,
InputType,
OutputType,
TEST_TYPES_MAP,
(TypedFunc<InputType, OutputType>(); FAIL();),
(FAIL()),
(SUCCEED()))
}
36 changes: 26 additions & 10 deletions dali/operators/generic/reduce/reduce.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include "dali/operators/generic/reduce/reduce.h"
#include "dali/operators/generic/reduce/sum.h"


namespace dali {
Expand All @@ -29,27 +30,42 @@ Not providing any axis results in reduction of all elements.)code",
"If True, maintains original input dimensions.",
false);

DALI_SCHEMA(Min)
DALI_SCHEMA(reductions__Sum)
.DocStr("Gets sum of elements along provided axes.")
.AddOptionalArg("dtype",
R"code(Output data type. This type is used to accumulate the result.)code",
DALI_NO_TYPE)
.NumInput(1)
.NumOutput(1)
.AddParent("ReduceBase");

DALI_SCHEMA(reductions__Min)
.DocStr("Gets minimal input element along provided axes.")
.NumInput(1)
.NumOutput(1)
.AddParent("ReduceBase");

DALI_SCHEMA(Max)
DALI_SCHEMA(reductions__Max)
.DocStr("Gets maximal input element along provided axes.")
.NumInput(1)
.NumOutput(1)
.AddParent("ReduceBase");

using MinCPU = Reduce<kernels::MinCPU, CPUBackend>;
DALI_REGISTER_OPERATOR(Min, MinCPU, CPU);
using SumCPU = SumOp<kernels::SumCPU, CPUBackend>;
DALI_REGISTER_OPERATOR(reductions__Sum, SumCPU, CPU);

using SumGPU = SumOp<kernels::SumGPU, GPUBackend>;
DALI_REGISTER_OPERATOR(reductions__Sum, SumGPU, GPU);

using MinCPU = ReduceOp<kernels::MinCPU, CPUBackend>;
DALI_REGISTER_OPERATOR(reductions__Min, MinCPU, CPU);

using MinGPU = Reduce<kernels::MinGPU, GPUBackend>;
DALI_REGISTER_OPERATOR(Min, MinGPU, GPU);
using MinGPU = ReduceOp<kernels::MinGPU, GPUBackend>;
DALI_REGISTER_OPERATOR(reductions__Min, MinGPU, GPU);

using MaxCPU = Reduce<kernels::MaxCPU, CPUBackend>;
DALI_REGISTER_OPERATOR(Max, MaxCPU, CPU);
using MaxCPU = ReduceOp<kernels::MaxCPU, CPUBackend>;
DALI_REGISTER_OPERATOR(reductions__Max, MaxCPU, CPU);

using MaxGPU = Reduce<kernels::MaxGPU, GPUBackend>;
DALI_REGISTER_OPERATOR(Max, MaxGPU, GPU);
using MaxGPU = ReduceOp<kernels::MaxGPU, GPUBackend>;
DALI_REGISTER_OPERATOR(reductions__Max, MaxGPU, GPU);
} // namespace dali
Loading