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

Add DCT 1D CPU kernel #1569

Merged
merged 7 commits into from Dec 18, 2019
Merged

Conversation

jantonguirao
Copy link
Contributor

@jantonguirao jantonguirao commented Dec 12, 2019

Why we need this PR?

  • It adds new DCT kernel, needed for the MFCC operator

What happened in this PR?

  • Explain solution of the problem, new feature added.
    Created a DCT 1D CPU kernel implementation. The kernel takes an arbitrarily sized tensor and performs a DCT transformation over the selected axis.
  • What was changed, added, removed?
    Dct1DCpu kernel, tests
  • What is most important part that reviewers should focus on?
    The kernel implementation
  • Was this PR tested? How?
    Unit tests
  • Were docs and examples updated, if necessary?
    Doxygen

JIRA TASK: [DALI-1185]

@jantonguirao jantonguirao requested a review from a team December 12, 2019 15:40
@jantonguirao jantonguirao changed the title Add DCT 1D CPU kernel [WIP] Add DCT 1D CPU kernel Dec 12, 2019
@jantonguirao jantonguirao changed the title [WIP] Add DCT 1D CPU kernel Add DCT 1D CPU kernel Dec 13, 2019
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
@jantonguirao
Copy link
Contributor Author

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1032164]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1032164]: BUILD PASSED

* @brief DCT kernel arguments
*/
struct DctArgs {
/// @brief DCT type. Supported types are 1, 2, 3, 4
Copy link
Contributor

Choose a reason for hiding this comment

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

What this value maps to?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

So this reference should be put here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's in the operator documentation, but ok, I'll add it here as well

make_string("Axis is out of bounds: ", args.axis));
int64_t n = in.shape[args.axis];

if (args.dct_type == 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We have that check repeated in FillCosineTableTypeI. Do we need to have that in two places?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's ok - this is a public API, so we throw - in FillCosineTable we assert (it should not be reachable with such parameter, regardless of what external caller does)..


template <typename T>
void ReferenceDctTypeI(span<T> out, span<const T> in, bool normalize) {
int64_t in_length = in.size();
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we put similar assert as in the implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do

for (int64_t k = 0; k < ndct; k++) {
T norm_factor = (k == 0) ? factor_k_0 : factor_k_i;
for (int64_t n = 0; n < input_length; n++) {
table[idx++] = norm_factor * std::cos(phase_mul * (n + T(0.5)) * k);
Copy link
Contributor

Choose a reason for hiding this comment

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

If it's a one-off calculation, then the code below give more precision:

Suggested change
table[idx++] = norm_factor * std::cos(phase_mul * (n + T(0.5)) * k);
table[idx++] = T(norm_factor * std::cos(phase_mul * (n + 0.5) * k);


template <typename T>
void FillCosineTableTypeII(T *table, int64_t input_length, int64_t ndct, bool normalize) {
T phase_mul = M_PI / input_length;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
T phase_mul = M_PI / input_length;
double phase_mul = M_PI / input_length;

T factor_k_0 = 1, factor_k_i = 1;
if (normalize) {
factor_k_i = std::sqrt(2 / T(input_length));
factor_k_0 = factor_k_i / std::sqrt(T(2));
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't it just:

Suggested change
factor_k_0 = factor_k_i / std::sqrt(T(2));
factor_k_0 = std::sqrt(input_length);

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

1 / std::sqrt(input_length) actually. Changed it

void FillCosineTableTypeI(T *table, int64_t input_length, int64_t ndct, bool normalize) {
assert(input_length > 1);
assert(!normalize);
T phase_mul = M_PI / (input_length - 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
T phase_mul = M_PI / (input_length - 1);
double phase_mul = M_PI / (input_length - 1);

Comment on lines 68 to 80
T phase_mul = M_PI / input_length;
T factor_n_0 = 0.5, factor_n_i = 1;
if (normalize) {
factor_n_i = std::sqrt(T(2) / input_length);
factor_n_0 = factor_n_i / std::sqrt(T(2));
}
int64_t idx = 0;
for (int64_t k = 0; k < ndct; k++) {
table[idx++] = factor_n_0; // n = 0
for (int64_t n = 1; n < input_length; n++) {
table[idx++] = factor_n_i * std::cos(phase_mul * n * (k + T(0.5)));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

As above.

Comment on lines 86 to 93
T phase_mul = M_PI / input_length;
T factor = normalize ? std::sqrt(T(2)/input_length) : T(1);
int64_t idx = 0;
for (int64_t k = 0; k < ndct; k++) {
for (int64_t n = 0; n < input_length; n++) {
table[idx++] = factor * std::cos(phase_mul * (n + T(0.5)) * (k + T(0.5)));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

...and again.

Signed-off-by: Joaquin Anton <janton@nvidia.com>
@jantonguirao
Copy link
Contributor Author

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1038528]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [1038528]: BUILD PASSED

@jantonguirao jantonguirao merged commit a23606c into NVIDIA:master Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants