|
| 1 | +// pybind/matrix/compressed_matrix_pybind.cc |
| 2 | + |
| 3 | +// Copyright 2019 Mobvoi AI Lab, Beijing, China (author: Fangjun Kuang) |
| 4 | + |
| 5 | +// See ../../../COPYING for clarification regarding multiple authors |
| 6 | +// |
| 7 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +// you may not use this file except in compliance with the License. |
| 9 | +// You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED |
| 15 | +// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, |
| 16 | +// MERCHANTABLITY OR NON-INFRINGEMENT. |
| 17 | +// See the Apache 2 License for the specific language governing permissions and |
| 18 | +// limitations under the License. |
| 19 | + |
| 20 | +#include "matrix/compressed_matrix_pybind.h" |
| 21 | + |
| 22 | +#include "matrix/compressed-matrix.h" |
| 23 | + |
| 24 | +using namespace kaldi; |
| 25 | + |
| 26 | +void pybind_compressed_matrix(py::module& m) { |
| 27 | + py::enum_<CompressionMethod>( |
| 28 | + m, "CompressionMethod", py::arithmetic(), |
| 29 | + "The enum CompressionMethod is used when creating a CompressedMatrix (a " |
| 30 | + "lossily compressed matrix) from a regular Matrix. It dictates how we " |
| 31 | + "choose the compressed format and how we choose the ranges of floats " |
| 32 | + "that are represented by particular integers.") |
| 33 | + .value( |
| 34 | + "kAutomaticMethod", kAutomaticMethod, |
| 35 | + "This is the default when you don't specify the compression method. " |
| 36 | + " It is a shorthand for using kSpeechFeature if the num-rows is " |
| 37 | + "more than 8, and kTwoByteAuto otherwise.") |
| 38 | + .value( |
| 39 | + "kSpeechFeature", kSpeechFeature, |
| 40 | + "This is the most complicated of the compression methods, and was " |
| 41 | + "designed for speech features which have a roughly Gaussian " |
| 42 | + "distribution with different ranges for each dimension. Each " |
| 43 | + "element is stored in one byte, but there is an 8-byte header per " |
| 44 | + "column; the spacing of the integer values is not uniform but is in " |
| 45 | + "3 ranges.") |
| 46 | + .value("kTwoByteAuto", kTwoByteAuto, |
| 47 | + "Each element is stored in two bytes as a uint16, with the " |
| 48 | + "representable range of values chosen automatically with the " |
| 49 | + "minimum and maximum elements of the matrix as its edges.") |
| 50 | + .value("kTwoByteSignedInteger", kTwoByteSignedInteger, |
| 51 | + "Each element is stored in two bytes as a uint16, with the " |
| 52 | + "representable range of value chosen to coincide with what you'd " |
| 53 | + "get if you stored signed integers, i.e. [-32768.0, 32767.0]. " |
| 54 | + "Suitable for waveform data that was previously stored as 16-bit " |
| 55 | + "PCM.") |
| 56 | + .value("kOneByteAuto", kOneByteAuto, |
| 57 | + "Each element is stored in one byte as a uint8, with the " |
| 58 | + "representable range of values chosen automatically with the " |
| 59 | + "minimum and maximum elements of the matrix as its edges.") |
| 60 | + .value("kOneByteUnsignedInteger", kOneByteUnsignedInteger, |
| 61 | + "Each element is stored in one byte as a uint8, with the " |
| 62 | + "representable range of values equal to [0.0, 255.0].") |
| 63 | + .value("kOneByteZeroOne", kOneByteZeroOne, |
| 64 | + "Each element is stored in one byte as a uint8, with the " |
| 65 | + "representable range of values equal to [0.0, 1.0]. Suitable for " |
| 66 | + "image data that has previously been compressed as int8.") |
| 67 | + .export_values(); |
| 68 | + { |
| 69 | + using PyClass = CompressedMatrix; |
| 70 | + py::class_<PyClass>(m, "CompressedMatrix") |
| 71 | + .def(py::init<>()) |
| 72 | + .def(py::init<const MatrixBase<float>&, CompressionMethod>(), |
| 73 | + py::arg("mat"), py::arg("method") = kAutomaticMethod) |
| 74 | + .def("NumRows", &PyClass::NumRows, |
| 75 | + "Returns number of rows (or zero for emtpy matrix).") |
| 76 | + .def("NumCols", &PyClass::NumCols, |
| 77 | + "Returns number of columns (or zero for emtpy matrix)."); |
| 78 | + } |
| 79 | +} |
0 commit comments