Skip to content

Commit

Permalink
Automated sync from github.com/tensorflow/tensorflow (tensorflow#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
TFLM-bot authored Jun 1, 2021
1 parent 159ae9a commit 9ea4331
Show file tree
Hide file tree
Showing 37 changed files with 595 additions and 171 deletions.
45 changes: 45 additions & 0 deletions tensorflow/lite/experimental/microfrontend/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ cc_library(
"fft_util.h",
],
deps = [
":fprintf_shim",
":memory_util",
"@kissfft//:kiss_fftr_16",
],
)
Expand All @@ -37,9 +39,19 @@ cc_library(
deps = [
":bits",
":fft",
":fprintf_shim",
":memory_util",
],
)

cc_library(
name = "fprintf_shim",
hdrs = [
"fprintf_shim.h",
],
defines = ["MICROFRONTEND_USE_FPRINTF"],
)

cc_library(
name = "frontend",
srcs = [
Expand All @@ -54,6 +66,7 @@ cc_library(
":bits",
":fft",
":filterbank",
":fprintf_shim",
":log_scale",
":noise_reduction",
":pcan_gain_control",
Expand All @@ -78,6 +91,16 @@ cc_library(
],
)

cc_library(
name = "memory_util",
srcs = [
"memory_util_stdlib.c",
],
hdrs = [
"memory_util.h",
],
)

cc_library(
name = "noise_reduction",
srcs = [
Expand All @@ -88,6 +111,10 @@ cc_library(
"noise_reduction.h",
"noise_reduction_util.h",
],
deps = [
":fprintf_shim",
":memory_util",
],
)

cc_library(
Expand All @@ -102,6 +129,8 @@ cc_library(
],
deps = [
":bits",
":fprintf_shim",
":memory_util",
],
)

Expand All @@ -115,6 +144,10 @@ cc_library(
"window.h",
"window_util.h",
],
deps = [
"fprintf_shim",
":memory_util",
],
)

cc_test(
Expand Down Expand Up @@ -162,6 +195,18 @@ cc_test(
],
)

cc_test(
name = "memory_util_test",
srcs = ["memory_util_test.cc"],
# Setting copts for experimental code to [], but this code should be fixed
# to build with the default copts (micro_copts())
copts = [],
deps = [
":memory_util",
"//tensorflow/lite/micro/testing:micro_test",
],
)

cc_test(
name = "noise_reduction_test",
srcs = ["noise_reduction_test.cc"],
Expand Down
26 changes: 15 additions & 11 deletions tensorflow/lite/experimental/microfrontend/lib/fft_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ limitations under the License.
#include "tensorflow/lite/experimental/microfrontend/lib/fft_io.h"

void FftWriteMemmapPreamble(FILE* fp, const struct FftState* state) {
fprintf(fp, "static int16_t fft_input[%zu];\n", state->fft_size);
fprintf(fp, "static struct complex_int16_t fft_output[%zu];\n",
state->fft_size / 2 + 1);
fprintf(fp, "static char fft_scratch[%zu];\n", state->scratch_size);
fprintf(fp, "\n");
MICROFRONTEND_FPRINTF(fp, "static int16_t fft_input[%zu];\n",
state->fft_size);
MICROFRONTEND_FPRINTF(fp, "static struct complex_int16_t fft_output[%zu];\n",
state->fft_size / 2 + 1);
MICROFRONTEND_FPRINTF(fp, "static char fft_scratch[%zu];\n",
state->scratch_size);
MICROFRONTEND_FPRINTF(fp, "\n");
}

void FftWriteMemmap(FILE* fp, const struct FftState* state,
const char* variable) {
fprintf(fp, "%s->input = fft_input;\n", variable);
fprintf(fp, "%s->output = fft_output;\n", variable);
fprintf(fp, "%s->fft_size = %zu;\n", variable, state->fft_size);
fprintf(fp, "%s->input_size = %zu;\n", variable, state->input_size);
fprintf(fp, "%s->scratch = fft_scratch;\n", variable);
fprintf(fp, "%s->scratch_size = %zu;\n", variable, state->scratch_size);
MICROFRONTEND_FPRINTF(fp, "%s->input = fft_input;\n", variable);
MICROFRONTEND_FPRINTF(fp, "%s->output = fft_output;\n", variable);
MICROFRONTEND_FPRINTF(fp, "%s->fft_size = %zu;\n", variable, state->fft_size);
MICROFRONTEND_FPRINTF(fp, "%s->input_size = %zu;\n", variable,
state->input_size);
MICROFRONTEND_FPRINTF(fp, "%s->scratch = fft_scratch;\n", variable);
MICROFRONTEND_FPRINTF(fp, "%s->scratch_size = %zu;\n", variable,
state->scratch_size);
}
29 changes: 15 additions & 14 deletions tensorflow/lite/experimental/microfrontend/lib/fft_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ limitations under the License.
==============================================================================*/
#include "tensorflow/lite/experimental/microfrontend/lib/fft_util.h"

#include <stdio.h>

#define FIXED_POINT 16
#include "kiss_fft.h"
#include "tools/kiss_fftr.h"
#include "tensorflow/lite/experimental/microfrontend/lib/fprintf_shim.h"
#include "tensorflow/lite/experimental/microfrontend/lib/memory_util.h"

int FftPopulateState(struct FftState* state, size_t input_size) {
state->input_size = input_size;
Expand All @@ -28,16 +28,16 @@ int FftPopulateState(struct FftState* state, size_t input_size) {
}

state->input = reinterpret_cast<int16_t*>(
malloc(state->fft_size * sizeof(*state->input)));
microfrontend_alloc(state->fft_size * sizeof(*state->input)));
if (state->input == nullptr) {
fprintf(stderr, "Failed to alloc fft input buffer\n");
MICROFRONTEND_FPRINTF(stderr, "Failed to alloc fft input buffer\n");
return 0;
}

state->output = reinterpret_cast<complex_int16_t*>(
malloc((state->fft_size / 2 + 1) * sizeof(*state->output) * 2));
state->output = reinterpret_cast<complex_int16_t*>(microfrontend_alloc(
(state->fft_size / 2 + 1) * sizeof(*state->output) * 2));
if (state->output == nullptr) {
fprintf(stderr, "Failed to alloc fft output buffer\n");
MICROFRONTEND_FPRINTF(stderr, "Failed to alloc fft output buffer\n");
return 0;
}

Expand All @@ -46,27 +46,28 @@ int FftPopulateState(struct FftState* state, size_t input_size) {
kiss_fftr_cfg kfft_cfg = kiss_fftr_alloc(
state->fft_size, 0, nullptr, &scratch_size);
if (kfft_cfg != nullptr) {
fprintf(stderr, "Kiss memory sizing failed.\n");
MICROFRONTEND_FPRINTF(stderr, "Kiss memory sizing failed.\n");
return 0;
}
state->scratch = malloc(scratch_size);
state->scratch = microfrontend_alloc(scratch_size);
if (state->scratch == nullptr) {
fprintf(stderr, "Failed to alloc fft scratch buffer\n");
MICROFRONTEND_FPRINTF(stderr, "Failed to alloc fft scratch buffer\n");
return 0;
}
state->scratch_size = scratch_size;
// Let kissfft configure the scratch space we just allocated
kfft_cfg = kiss_fftr_alloc(state->fft_size, 0,
state->scratch, &scratch_size);
if (kfft_cfg != state->scratch) {
fprintf(stderr, "Kiss memory preallocation strategy failed.\n");
MICROFRONTEND_FPRINTF(stderr,
"Kiss memory preallocation strategy failed.\n");
return 0;
}
return 1;
}

void FftFreeStateContents(struct FftState* state) {
free(state->input);
free(state->output);
free(state->scratch);
microfrontend_free(state->input);
microfrontend_free(state->output);
microfrontend_free(state->scratch);
}
39 changes: 23 additions & 16 deletions tensorflow/lite/experimental/microfrontend/lib/filterbank_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ limitations under the License.

static void PrintArray(FILE* fp, const char* name, const int16_t* values,
size_t size) {
fprintf(fp, "static int16_t filterbank_%s[] = {", name);
MICROFRONTEND_FPRINTF(fp, "static int16_t filterbank_%s[] = {", name);
int i;
for (i = 0; i < size; ++i) {
fprintf(fp, "%d", values[i]);
MICROFRONTEND_FPRINTF(fp, "%d", values[i]);
if (i < size - 1) {
fprintf(fp, ", ");
MICROFRONTEND_FPRINTF(fp, ", ");
}
}
fprintf(fp, "};\n");
MICROFRONTEND_FPRINTF(fp, "};\n");
}

void FilterbankWriteMemmapPreamble(FILE* fp,
Expand All @@ -44,24 +44,31 @@ void FilterbankWriteMemmapPreamble(FILE* fp,
PrintArray(fp, "weights", state->weights, num_weights);
PrintArray(fp, "unweights", state->unweights, num_weights);

fprintf(fp, "static uint64_t filterbank_work[%d];\n", num_channels_plus_1);
fprintf(fp, "\n");
MICROFRONTEND_FPRINTF(fp, "static uint64_t filterbank_work[%d];\n",
num_channels_plus_1);
MICROFRONTEND_FPRINTF(fp, "\n");
}

void FilterbankWriteMemmap(FILE* fp, const struct FilterbankState* state,
const char* variable) {
fprintf(fp, "%s->num_channels = %d;\n", variable, state->num_channels);
fprintf(fp, "%s->start_index = %d;\n", variable, state->start_index);
fprintf(fp, "%s->end_index = %d;\n", variable, state->end_index);
MICROFRONTEND_FPRINTF(fp, "%s->num_channels = %d;\n", variable,
state->num_channels);
MICROFRONTEND_FPRINTF(fp, "%s->start_index = %d;\n", variable,
state->start_index);
MICROFRONTEND_FPRINTF(fp, "%s->end_index = %d;\n", variable,
state->end_index);

fprintf(
MICROFRONTEND_FPRINTF(
fp,
"%s->channel_frequency_starts = filterbank_channel_frequency_starts;\n",
variable);
fprintf(fp, "%s->channel_weight_starts = filterbank_channel_weight_starts;\n",
variable);
fprintf(fp, "%s->channel_widths = filterbank_channel_widths;\n", variable);
fprintf(fp, "%s->weights = filterbank_weights;\n", variable);
fprintf(fp, "%s->unweights = filterbank_unweights;\n", variable);
fprintf(fp, "%s->work = filterbank_work;\n", variable);
MICROFRONTEND_FPRINTF(
fp, "%s->channel_weight_starts = filterbank_channel_weight_starts;\n",
variable);
MICROFRONTEND_FPRINTF(fp, "%s->channel_widths = filterbank_channel_widths;\n",
variable);
MICROFRONTEND_FPRINTF(fp, "%s->weights = filterbank_weights;\n", variable);
MICROFRONTEND_FPRINTF(fp, "%s->unweights = filterbank_unweights;\n",
variable);
MICROFRONTEND_FPRINTF(fp, "%s->work = filterbank_work;\n", variable);
}
68 changes: 38 additions & 30 deletions tensorflow/lite/experimental/microfrontend/lib/filterbank_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ limitations under the License.

#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

#include "tensorflow/lite/experimental/microfrontend/lib/fprintf_shim.h"
#include "tensorflow/lite/experimental/microfrontend/lib/memory_util.h"

#define kFilterbankIndexAlignment 4
#define kFilterbankChannelBlockSize 4
Expand Down Expand Up @@ -65,29 +68,29 @@ int FilterbankPopulateState(const struct FilterbankConfig* config,
? 1
: kFilterbankIndexAlignment / sizeof(int16_t));

state->channel_frequency_starts =
malloc(num_channels_plus_1 * sizeof(*state->channel_frequency_starts));
state->channel_weight_starts =
malloc(num_channels_plus_1 * sizeof(*state->channel_weight_starts));
state->channel_frequency_starts = microfrontend_alloc(
num_channels_plus_1 * sizeof(*state->channel_frequency_starts));
state->channel_weight_starts = microfrontend_alloc(
num_channels_plus_1 * sizeof(*state->channel_weight_starts));
state->channel_widths =
malloc(num_channels_plus_1 * sizeof(*state->channel_widths));
state->work = malloc(num_channels_plus_1 * sizeof(*state->work));
microfrontend_alloc(num_channels_plus_1 * sizeof(*state->channel_widths));
state->work = microfrontend_alloc(num_channels_plus_1 * sizeof(*state->work));

float* center_mel_freqs =
malloc(num_channels_plus_1 * sizeof(*center_mel_freqs));
microfrontend_alloc(num_channels_plus_1 * sizeof(*center_mel_freqs));
int16_t* actual_channel_starts =
malloc(num_channels_plus_1 * sizeof(*actual_channel_starts));
microfrontend_alloc(num_channels_plus_1 * sizeof(*actual_channel_starts));
int16_t* actual_channel_widths =
malloc(num_channels_plus_1 * sizeof(*actual_channel_widths));
microfrontend_alloc(num_channels_plus_1 * sizeof(*actual_channel_widths));

if (state->channel_frequency_starts == NULL ||
state->channel_weight_starts == NULL || state->channel_widths == NULL ||
center_mel_freqs == NULL || actual_channel_starts == NULL ||
actual_channel_widths == NULL) {
free(center_mel_freqs);
free(actual_channel_starts);
free(actual_channel_widths);
fprintf(stderr, "Failed to allocate channel buffers\n");
microfrontend_free(center_mel_freqs);
microfrontend_free(actual_channel_starts);
microfrontend_free(actual_channel_widths);
MICROFRONTEND_FPRINTF(stderr, "Failed to allocate channel buffers\n");
return 0;
}

Expand Down Expand Up @@ -160,15 +163,19 @@ int FilterbankPopulateState(const struct FilterbankConfig* config,
// Allocate the two arrays to store the weights - weight_index_start contains
// the index of what would be the next set of weights that we would need to
// add, so that's how many weights we need to allocate.
state->weights = calloc(weight_index_start, sizeof(*state->weights));
state->unweights = calloc(weight_index_start, sizeof(*state->unweights));
state->weights =
microfrontend_alloc(weight_index_start * sizeof(*state->weights));
memset(state->weights, 0, (weight_index_start * sizeof(*state->weights)));
state->unweights =
microfrontend_alloc(weight_index_start * sizeof(*state->unweights));
memset(state->unweights, 0, (weight_index_start * sizeof(*state->unweights)));

// If the alloc failed, we also need to nuke the arrays.
if (state->weights == NULL || state->unweights == NULL) {
free(center_mel_freqs);
free(actual_channel_starts);
free(actual_channel_widths);
fprintf(stderr, "Failed to allocate weights or unweights\n");
microfrontend_free(center_mel_freqs);
microfrontend_free(actual_channel_starts);
microfrontend_free(actual_channel_widths);
MICROFRONTEND_FPRINTF(stderr, "Failed to allocate weights or unweights\n");
return 0;
}

Expand Down Expand Up @@ -200,21 +207,22 @@ int FilterbankPopulateState(const struct FilterbankConfig* config,
}
}

free(center_mel_freqs);
free(actual_channel_starts);
free(actual_channel_widths);
microfrontend_free(center_mel_freqs);
microfrontend_free(actual_channel_starts);
microfrontend_free(actual_channel_widths);
if (state->end_index >= spectrum_size) {
fprintf(stderr, "Filterbank end_index is above spectrum size.\n");
MICROFRONTEND_FPRINTF(stderr,
"Filterbank end_index is above spectrum size.\n");
return 0;
}
return 1;
}

void FilterbankFreeStateContents(struct FilterbankState* state) {
free(state->channel_frequency_starts);
free(state->channel_weight_starts);
free(state->channel_widths);
free(state->weights);
free(state->unweights);
free(state->work);
microfrontend_free(state->channel_frequency_starts);
microfrontend_free(state->channel_weight_starts);
microfrontend_free(state->channel_widths);
microfrontend_free(state->weights);
microfrontend_free(state->unweights);
microfrontend_free(state->work);
}
Loading

0 comments on commit 9ea4331

Please sign in to comment.