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

Linear Transformation kernel for CPU #1300

Merged
merged 127 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from 123 commits
Commits
Show all changes
127 commits
Select commit Hold shift + click to select a range
1584f61
size_t -> int
szalpal Sep 19, 2019
d584290
review
szalpal Sep 19, 2019
a3f8104
wip
szalpal Aug 20, 2019
dffea7e
working
szalpal Aug 20, 2019
fc4ea7b
wip
szalpal Aug 20, 2019
0bc4c75
working
szalpal Aug 20, 2019
0d90738
wip
szalpal Aug 22, 2019
7c7007b
wip
szalpal Aug 23, 2019
ddeca11
wip
szalpal Aug 23, 2019
969ebea
works
szalpal Sep 16, 2019
99e168a
warnings
szalpal Sep 16, 2019
9383459
remove file
szalpal Sep 16, 2019
7b20891
after merge
szalpal Sep 16, 2019
f5867c4
missing file
szalpal Sep 16, 2019
e8fec71
rename
szalpal Sep 16, 2019
a825181
lint
szalpal Sep 16, 2019
9af5c64
lint and review
szalpal Sep 18, 2019
092b907
roi
szalpal Sep 20, 2019
b3120df
wip
szalpal Aug 20, 2019
3d09334
working
szalpal Aug 20, 2019
5aec1b0
wip
szalpal Aug 20, 2019
7d31a5a
working
szalpal Aug 20, 2019
5696c01
wip
szalpal Aug 22, 2019
ff8ec67
wip
szalpal Aug 23, 2019
5049772
wip
szalpal Aug 23, 2019
4f63345
works
szalpal Sep 16, 2019
a9b9eea
warnings
szalpal Sep 16, 2019
ebb2bfa
remove file
szalpal Sep 16, 2019
ec5116d
after merge
szalpal Sep 16, 2019
d54ada4
missing file
szalpal Sep 16, 2019
3a40a46
rename
szalpal Sep 16, 2019
f3802d0
lint
szalpal Sep 16, 2019
f60bf2e
size_t -> int
szalpal Sep 19, 2019
2f0f1b2
review
szalpal Sep 19, 2019
94f716f
wip
szalpal Aug 20, 2019
dcd3b77
working
szalpal Aug 20, 2019
cdf26fc
wip
szalpal Aug 20, 2019
3cd1139
working
szalpal Aug 20, 2019
14bdb4b
wip
szalpal Aug 22, 2019
8649c98
wip
szalpal Aug 23, 2019
c50c840
wip
szalpal Aug 23, 2019
0dc11c4
warnings
szalpal Sep 16, 2019
a835685
remove file
szalpal Sep 16, 2019
2fc1495
after merge
szalpal Sep 16, 2019
5b2bd4f
rename
szalpal Sep 16, 2019
ecb39a2
lint and review
szalpal Sep 18, 2019
b63e612
roi
szalpal Sep 20, 2019
ce7ceb9
add tvector
szalpal Sep 23, 2019
0abf9d3
Merge branch 'hsv_gpu' of github.com:szalpal/DALI into hsv_gpu
szalpal Sep 24, 2019
7414446
add span
szalpal Sep 24, 2019
45f0b58
change location
szalpal Sep 24, 2019
e32d21a
lint & warnings
szalpal Sep 24, 2019
9f1c5ba
cmake
szalpal Sep 24, 2019
6f84fcc
Merge branch 'master' into hsv_gpu
szalpal Sep 24, 2019
51264e3
size_t -> int
szalpal Sep 19, 2019
ecf7aaa
review
szalpal Sep 19, 2019
3b184d5
wip
szalpal Aug 20, 2019
23cff3d
working
szalpal Aug 20, 2019
c571a11
wip
szalpal Aug 20, 2019
d72338e
working
szalpal Aug 20, 2019
ad0e3d8
wip
szalpal Aug 22, 2019
148f65e
wip
szalpal Aug 23, 2019
a8187ca
wip
szalpal Aug 23, 2019
2af5721
works
szalpal Sep 16, 2019
258084c
warnings
szalpal Sep 16, 2019
a1c9865
remove file
szalpal Sep 16, 2019
fd9ff9c
after merge
szalpal Sep 16, 2019
0744dbd
missing file
szalpal Sep 16, 2019
2d00e34
rename
szalpal Sep 16, 2019
8312d4c
lint
szalpal Sep 16, 2019
0176be4
lint and review
szalpal Sep 18, 2019
2a79823
roi
szalpal Sep 20, 2019
0419cc8
wip
szalpal Aug 20, 2019
3d48025
working
szalpal Aug 20, 2019
a0bd651
wip
szalpal Aug 20, 2019
ddfbd0d
working
szalpal Aug 20, 2019
fd4cec6
wip
szalpal Aug 22, 2019
d39109d
wip
szalpal Aug 23, 2019
8479ef6
wip
szalpal Aug 23, 2019
e522f77
warnings
szalpal Sep 16, 2019
da67362
remove file
szalpal Sep 16, 2019
b86939c
after merge
szalpal Sep 16, 2019
8c4bdff
rename
szalpal Sep 16, 2019
e232dc9
size_t -> int
szalpal Sep 19, 2019
23b035a
review
szalpal Sep 19, 2019
d152501
wip
szalpal Aug 20, 2019
f0b0337
working
szalpal Aug 20, 2019
562ee2e
wip
szalpal Aug 20, 2019
932b069
working
szalpal Aug 20, 2019
c5a7d4a
wip
szalpal Aug 22, 2019
b399ee4
wip
szalpal Aug 23, 2019
4985ea0
wip
szalpal Aug 23, 2019
7d3f2a6
warnings
szalpal Sep 16, 2019
c5691ca
remove file
szalpal Sep 16, 2019
f4c46c0
after merge
szalpal Sep 16, 2019
918823c
rename
szalpal Sep 16, 2019
4891484
add tvector
szalpal Sep 23, 2019
4791ccb
add span
szalpal Sep 24, 2019
91dcb3f
change location
szalpal Sep 24, 2019
fa7d6a8
lint & warnings
szalpal Sep 24, 2019
eafaf9a
cmake
szalpal Sep 24, 2019
00c443e
fix file
szalpal Sep 26, 2019
d2d9a99
Merge branch 'hsv_gpu' of github.com:szalpal/DALI into hsv_gpu
szalpal Sep 27, 2019
3a2e492
review
szalpal Sep 30, 2019
1abed02
review
szalpal Oct 1, 2019
e5ff2b6
review
szalpal Oct 1, 2019
b6d8e54
review
szalpal Oct 1, 2019
7330d1f
more tests
szalpal Oct 1, 2019
62b44c9
review
szalpal Oct 1, 2019
d3a809b
fix memleak
szalpal Oct 1, 2019
15331ba
skeleton
szalpal Sep 24, 2019
4af0d44
works
szalpal Sep 25, 2019
5b5c209
skeleton
szalpal Sep 24, 2019
945f4b9
works
szalpal Sep 25, 2019
13473f8
default values
szalpal Sep 26, 2019
2c8f833
Merge branch 'master' into lintrans_cpu
szalpal Oct 2, 2019
403776b
works
szalpal Oct 2, 2019
0244f94
review
szalpal Oct 2, 2019
036ad6b
wip
szalpal Oct 2, 2019
e4a7062
review
szalpal Oct 3, 2019
eb829bc
static eye
szalpal Oct 4, 2019
2a954c6
lint
szalpal Oct 4, 2019
c2d90f3
review
szalpal Oct 4, 2019
83bd20a
review
szalpal Oct 4, 2019
c484b66
lint
szalpal Oct 4, 2019
495d841
error
szalpal Oct 4, 2019
8a963bd
fix error
szalpal Oct 4, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions dali/core/geom_mat_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace dali {

TEST(Mat, Constructor) {
TEST(MatTest, Constructor) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What else can it be? It looks like renaming John, Bob and Alice to JohnHuman, BobHumand and AliceHuman.

Copy link
Member Author

Choose a reason for hiding this comment

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

Naming test suites in a way, that it ends with Test seems to be quite a common thing. Be aware, that for a test case TEST(Mat, Constructor) gtest's macro-magic generates a class Mat_Constructor. In no case such name implies, that this class is actually a test. I'd say it's a thing we could add to the code style

#1330

Copy link
Contributor

Choose a reason for hiding this comment

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

I strongly oppose making it a requirement. While I may even agree upon this in case of something as short as "Mat", adding "Test" to all test names will just add noise and produce long strings in GTest output.

mat3x4 m = {{ { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }};
for (int i = 0; i < m.rows; i++)
for (int j = 0; j < m.cols; j++)
Expand All @@ -31,7 +31,7 @@ TEST(Mat, Constructor) {
EXPECT_EQ(m31(2, 0), 3);
}

TEST(Mat, Equality) {
TEST(MatTest, Equality) {
mat2x4 a = {{ { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }};
imat2x4 b = {{ { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }};
EXPECT_TRUE((a == b));
Expand All @@ -48,7 +48,7 @@ TEST(Mat, Equality) {
}
}

TEST(Mat, FromScalar) {
TEST(MatTest, FromScalar) {
mat3x4 m = 5;
mat3x4 ref = {{
{ 5, 0, 0, 0 },
Expand All @@ -60,7 +60,7 @@ TEST(Mat, FromScalar) {
EXPECT_EQ(m(i, j), ref(i, j));
}

TEST(Mat, Transpose) {
TEST(MatTest, Transpose) {
mat3x4 m = {{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
Expand All @@ -72,7 +72,7 @@ TEST(Mat, Transpose) {
EXPECT_EQ(transposed(j, i), m(i, j));
}

TEST(Mat, Mul) {
TEST(MatTest, Mul) {
mat3x4 m1 = {{
{ 1, 10, 100, 1000 },
{ 2, 20, 200, 2000 },
Expand All @@ -95,7 +95,7 @@ TEST(Mat, Mul) {
EXPECT_EQ(result(i, j), ref(i, j)) << "@ " << i << ", " << j;
}

TEST(Mat, Mul4x4) {
TEST(MatTest, Mul4x4) {
mat4 m1 = {{
{ 1, 10, 100, 1000 },
{ 2, 20, 200, 2000 },
Expand All @@ -122,7 +122,7 @@ TEST(Mat, Mul4x4) {
}


TEST(Mat, CompoundAssignMatrix) {
TEST(MatTest, CompoundAssignMatrix) {
#define TEST_ASSIGN_OP(op) \
{ \
m1 op##= m2; \
Expand Down Expand Up @@ -184,7 +184,7 @@ TEST(Mat, CompoundAssignMatrix) {
#undef TEST_ASSIGN_OP
}

TEST(Mat, CompoundAssignScalar) {
TEST(MatTest, CompoundAssignScalar) {
#define TEST_ASSIGN_OP(op) \
{ \
m1 op## = s; \
Expand Down Expand Up @@ -243,7 +243,7 @@ void RandomFill(mat<rows, cols, T> &m, RNG &rng, int lo, int hi) {
m(i, j) = rng()%(hi-lo) + lo;
}

TEST(Mat, BinOp) {
TEST(MatTest, BinOp) {
#define TEST_OPERATOR_SIZE(op, mat_type) { \
mat_type m1, m2; \
int lo = 0, hi = 31; \
Expand Down Expand Up @@ -304,7 +304,7 @@ DEVICE_TEST(Dev_Mat, NormalUsage, 1, 1) {
DEV_EXPECT_EQ(v[3], 1);
}

TEST(Mat, VecTransform) {
TEST(MatTest, VecTransform) {
mat2x3 m = {{
{ 2, 3, 100 },
{ 5, 11, 1000 }
Expand All @@ -313,7 +313,7 @@ TEST(Mat, VecTransform) {
EXPECT_EQ(v, vec2(180, 1270));
}

TEST(Mat, SelectCol) {
TEST(MatTest, SelectCol) {
mat3 m = {{
{ 1, 2, 3 },
{ 4, 5, 6 },
Expand All @@ -324,7 +324,7 @@ TEST(Mat, SelectCol) {
EXPECT_EQ(v, vec3(2, 5, 8));
}

TEST(Mat, CatCols) {
TEST(MatTest, CatCols) {
mat3 a = {{
{ 1, 2, 3 },
{ 4, 5, 6 },
Expand All @@ -346,7 +346,7 @@ TEST(Mat, CatCols) {
EXPECT_EQ(result(i, j), ref(i, j)) << "@ " << i << ", " << j;
}

TEST(Mat, CatColsVec) {
TEST(MatTest, CatColsVec) {
mat3 a = {{
{ 1, 2, 3 },
{ 4, 5, 6 },
Expand Down Expand Up @@ -385,7 +385,7 @@ TEST(Mat, CatColsVec) {
EXPECT_EQ(result2(i, j), ref2(i, j)) << "@ " << i << ", " << j;
}

TEST(Mat, CatRows) {
TEST(MatTest, CatRows) {
mat3 a = {{
{ 1, 2, 3 },
{ 4, 5, 6 },
Expand All @@ -408,4 +408,27 @@ TEST(Mat, CatRows) {
EXPECT_EQ(result(i, j), ref(i, j)) << "@ " << i << ", " << j;
}


TEST(MatTest, IdentityMatrix) {
mat3 three_by_three = {{
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
}};
mat3x1 three_by_one = {{
{1},{0},{0} // NOLINT
szalpal marked this conversation as resolved.
Show resolved Hide resolved
}};
mat3x2 three_by_two = {{
{1, 0},
{0, 1},
{0, 0},
}};
auto m1 = imat3::eye();
auto m2 = mat<3, 1, int>::eye();
auto m3 = mat<3, 2, int>::eye();
EXPECT_EQ(m1, three_by_three);
EXPECT_EQ(m2, three_by_one);
EXPECT_EQ(m3, three_by_two);
}

} // namespace dali
82 changes: 82 additions & 0 deletions dali/kernels/imgproc/pointwise/linear_transformation_cpu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) 2019, 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.

#ifndef DALI_KERNELS_IMGPROC_POINTWISE_LINEAR_TRANSFORMATION_CPU_H_
#define DALI_KERNELS_IMGPROC_POINTWISE_LINEAR_TRANSFORMATION_CPU_H_

#include <vector>
#include <utility>
#include "dali/core/format.h"
#include "dali/core/convert.h"
#include "dali/core/geom/box.h"
#include "dali/kernels/common/block_setup.h"
#include "dali/kernels/imgproc/surface.h"
#include "dali/kernels/imgproc/roi.h"

namespace dali {
namespace kernels {

template <typename OutputType, typename InputType, int channels_out, int channels_in, int ndims>
class LinearTransformationCpu {
private:
static_assert(ndims == 3, "This kernel is currently implemented for 3 dims only");
static constexpr int spatial_ndims_ = ndims - 1;
using Mat = ::dali::mat<channels_out, channels_in, float>;
using Vec = ::dali::vec<channels_out, float>;

public:
KernelRequirements Setup(KernelContext &context, const InTensorCPU<InputType, ndims> &in,
Mat tmatrix = Mat::eye(), Vec tvector = {},
const Roi<spatial_ndims_> *roi = nullptr) {
DALI_ENFORCE(in.shape.shape.back() == channels_in,
"Unexpected number of channels. Number of channels in InTensorCPU has to match"
" the number of channels, that the kernel is instantiated with");
DALI_ENFORCE(!roi || all_coords(roi->hi >= roi->lo),
make_string("Invalid ROI: it doesn't follow {lo, hi} convention. ", roi));

auto adjusted_roi = AdjustRoi(roi, in.shape);
KernelRequirements req;
TensorListShape<ndims> output_shape({ShapeFromRoi(adjusted_roi, channels_out)});
req.output_shapes = {std::move(output_shape)};
return req;
}


void Run(KernelContext &context, const OutTensorCPU<OutputType, ndims> &out,
const InTensorCPU<InputType, ndims> &in, Mat tmatrix = Mat::eye(), Vec tvector = {},
const Roi<spatial_ndims_> *roi = nullptr) {
auto adjusted_roi = AdjustRoi(roi, in.shape);
auto ptr = out.data;
auto in_width = in.shape[1];

for (int y = adjusted_roi.lo.y; y < adjusted_roi.hi.y; y++) {
auto *row_ptr = &in.data[y * in_width * channels_in];
for (int x = adjusted_roi.lo.x; x < adjusted_roi.hi.x; x++) {
vec<channels_in, float> v_in;
for (int k = 0; k < channels_in; k++) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can use memcpy here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not really, cause what I silently did here is casting the InputType to float, cause computation is performed in float. I added static_cast, so it's clear that this happens.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove this static cast, as it will silence warnings if something really stupid happens (e.g. someone runs it on a tensor of boolean values).

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

v_in[k] = row_ptr[channels_in * x + k];
}
vec<channels_out> v_out = tmatrix * v_in + tvector;
for (int k = 0; k < channels_out; k++) {
*ptr++ = ConvertSat<OutputType>(v_out[k]);
}
}
}
}
};

} // namespace kernels
} // namespace dali

#endif // DALI_KERNELS_IMGPROC_POINTWISE_LINEAR_TRANSFORMATION_CPU_H_
Loading