Building Kaldi with custom Matrix implementations

moto · November 29, 2020

Once the Vector / Matrix classes are implemented, then we can bring in the implementation of higher level functions. We need to make some modifications to header files, though, so that the implementations missing and not used, like sp-matrix.h, are not included. This can be done in matrix-lib.h.

Another modification required is Kaldi’s numeric type definitions. Kaldi’s numeric type definisions, like int32, are from OpenFST, and there is a place to chage this.

With only these two changes, we can bring in the rest of the code for ComputeKaldiPitch and they compile fine.

Finally, to make it accessible from Python code, we need a wrapper around ComputeKaldiPitch. The code looks like the following [src]. It just creates kaldi::VectorBase object from an existing Tensor and extract the result from kaldi::Matrix as a Tensor.

torch::Tensor ComputeKaldiPitch(
      const torch::Tensor &wave,
      double sample_frequency,
      double frame_length,

      # The rest of the options go here
) {
    kaldi::VectorBase<kaldi::BaseFloat> input(wave);
    kaldi::PitchExtractionOptions opts;
    opts.samp_freq = static_cast<BaseFloat>(sample_frequency);
    opts.frame_length_ms = static_cast<BaseFloat>(frame_length);

    # The rest of the options go...
    ...

    kaldi::Matrix<kaldi::BaseFloat> output;
    kaldi::ComputeKaldiPitch(opts, input, &output);
    return output.tensor_;
}

With the proper implementation of Vector/Matrix classes, calling this function produces the exact same result as the original compute-kaldi-pitch-feats command. Test code can be found here and the result can be seen here.