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

Support for both OpenFST 1.7.3 and 1.8.2 #4927

Merged
merged 6 commits into from
Sep 8, 2024

Conversation

jtrmal
Copy link
Contributor

@jtrmal jtrmal commented Jul 25, 2024

I added a thin compatibility layer (and a few ifdefs)
By default the build infrastructure uses OpenFST-1.7.2 and there should be no change compared to current pipelines.
It is however switch the openFst version while making /tools like so:

make clean && make -j 18 OPENFST_VERSION=1.8.3

and after that the version should be picked up by configure (also to be re-run) and flags will be appropriately defined. Will be grateful for reviews/testing --so far I tested only on Apple M3

For 1.8.3, not all tests will compile. There are two outstanding tests failing to compile with this error:

In file included from trivial-factor-weight-test.cc:23:
../fstext/trivial-factor-weight.h:393:14: error: no viable overloaded '='
  data->base = new StateIterator< TrivialFactorWeightFst<A, F> >(*this);
  ~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
trivial-factor-weight-test.cc:139:46: note: in instantiation of member function 'fst::TrivialFactorWeightFst<fst::GallicArc<fst::ArcTpl<fst::TropicalWeightTpl<float>>>, fst::GallicFactor<int, fst::TropicalWeightTpl<float>>>::InitStateIterator' requested here
        typename Arc::Weight, GALLIC_LEFT> > fwfst(gallic_fst);
                                             ^
trivial-factor-weight-test.cc:213:10: note: in instantiation of function template specialization 'fst::TestFactor<fst::ArcTpl<fst::TropicalWeightTpl<float>>>' requested here
    fst::TestFactor<fst::StdArc>();
         ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/unique_ptr.h:233:71: note: candidate function not viable: no known conversion from 'StateIterator<TrivialFactorWeightFst<GallicArc<ArcTpl<TropicalWeightTpl<float>, int, int>, fst::GALLIC_LEFT>, GallicFactor<int, TropicalWeightTpl<float>, fst::GALLIC_LEFT>>> *' to 'unique_ptr<StateIteratorBase<GallicArc<ArcTpl<TropicalWeightTpl<float>>>>>' for 1st argument
  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
                                                                      ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/unique_ptr.h:243:71: note: candidate template ignored: could not match 'unique_ptr<_Up, _Ep>' against 'StateIterator<TrivialFactorWeightFst<GallicArc<ArcTpl<TropicalWeightTpl<float>, int, int>, fst::GALLIC_LEFT>, GallicFactor<int, TropicalWeightTpl<float>, fst::GALLIC_LEFT>>> *'
  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
                                                                      ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/unique_ptr.h:268:71: note: candidate function not viable: no known conversion from 'StateIterator<TrivialFactorWeightFst<GallicArc<ArcTpl<TropicalWeightTpl<float>, int, int>, fst::GALLIC_LEFT>, GallicFactor<int, TropicalWeightTpl<float>, fst::GALLIC_LEFT>>> *' to 'nullptr_t' (aka 'std::nullptr_t') for 1st argument
  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
                                                                      ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/unique_ptr.h:127:59: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'StateIterator<TrivialFactorWeightFst<GallicArc<ArcTpl<TropicalWeightTpl<float>, int, int>, fst::GALLIC_LEFT>, GallicFactor<int, TropicalWeightTpl<float>, fst::GALLIC_LEFT>>> *' to 'const std::unique_ptr<fst::StateIteratorBase<fst::GallicArc<fst::ArcTpl<fst::TropicalWeightTpl<float>>>>>' for 1st argument
class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
                                                          ^
1 error generated.
make[1]: *** [trivial-factor-weight-test.o] Error 1

if anyone can help me figure out the issue, I will be grateful

cc: @georgthegreat @mmcauliffe

@jtrmal
Copy link
Contributor Author

jtrmal commented Jul 25, 2024

So, on linux the tests fail to compile with the same error. So I guess it's not just apple/clang weirdness.

@csukuangfj
Copy link
Contributor

csukuangfj commented Jul 25, 2024

Let me help you.

You can change

../fstext/trivial-factor-weight.h:393:14

from

data->base = new StateIterator< TrivialFactorWeightFst<A, F> >(*this);

to

data->base.reset(new StateIterator< TrivialFactorWeightFst<A, F> >(*this));

By the way, if you need to support multiple openfst versions, then you can do

#if OPEN_FST_VERSION_XXX
data->base = new StateIterator< TrivialFactorWeightFst<A, F> >(*this);
#elif OPEN_FST_VERSION_YYY
data->base.reset(new StateIterator< TrivialFactorWeightFst<A, F> >(*this));
#endif

@csukuangfj
Copy link
Contributor

HINT:

data->base was a raw pointer, but it is changed to a std::unique_ptr in openfst 1.8.3

@jtrmal
Copy link
Contributor Author

jtrmal commented Jul 25, 2024

The test is failing because of line src/fstextra/lattice-utils-test.cc:109
I modified it thusly

        // since semiring is idempotent, this should succeed too.
        std::cout << "Distance: " << ShortestDistance(cfst) << " x " << ShortestDistance(nbest_fst_1b) << std::endl;
        assert(ApproxEqual(ShortestDistance(cfst),
                           ShortestDistance(nbest_fst_1b)));

The output is:

Distance: 0,1.25,1_2 x 0,1.25,

lattice-utils-test: lattice-utils-test.cc:109: void fst::TestShortestPath() [with Weight = fst::LatticeWeightTpl<double>; Int = int]: Assertion `ApproxEqual(ShortestDistance(cfst), ShortestDistance(nbest_fst_1b))' failed.

NB: I was able to repro it on another machine/distro.

@jtrmal
Copy link
Contributor Author

jtrmal commented Jul 25, 2024

Full log from the test
lattice-utils-test.testlog.txt

@csukuangfj
Copy link
Contributor

Full log from the test lattice-utils-test.testlog.txt

Have you fixed it?

@jtrmal
Copy link
Contributor Author

jtrmal commented Jul 31, 2024 via email

@@ -172,7 +172,8 @@ int main(int argc, char *argv[]) {
if (g_kaldi_verbose_level >= 2) {
KALDI_LOG << "phn2word FST is below:";
fst::FstPrinter<StdArc> fstprinter(phn2word, NULL, NULL, NULL, false, true, "\t");
fstprinter.Print(&std::cerr, "standard error");
printer_print(std::cerr, fstprinter, "standard error");
//fstprinter.Print(&std::cerr, "standard error");
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this?

Comment on lines +574 to +575
fst::StdCompactAcceptorFst cfst(fst);
cfst.Write(os, write_options);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this pre-1.8 compatible? Should we go flat out 1.8+? We've had the version of OpenFST fixed pretty much forever.

Comment on lines +1028 to +1032
if [ $OPENFST_VER_NUM -lt 10800 ]; then
echo "CXXLANGVERSION = c++14"
else
echo "CXXLANGVERSION = c++17"
fi >> kaldi.mk
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you think of a case when we would run into a pre-C++14 compiler? Maybe it's time to go C++17 unconditionally?

Comment on lines 22 to +26
#include "fstext/fst-test-utils.h"
#include "base/kaldi-math.h"

#include "fstext/openfst_compat.h"

Copy link
Contributor

Choose a reason for hiding this comment

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

Sort #includes alphabetically. Or, if openfst_compat.h requires being included the last, then add a comment to that effect.

Comment on lines +378 to +380
for (SymbolTable::iterator iter = symtab.begin();
iter != symtab.end();
++iter) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, that would have been for (SymbolTable::iterator iter : symtab) in C++17 ;-)

Copy link
Contributor

Choose a reason for hiding this comment

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

better to use auto with range based for here.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can argue with @danpovey about this.🙂

In this particular case, I agree with you. My own rule of thumb is, if the real or conceptual type is obvious, use either auto; otherwise, the type must be explicit. In this case, the conceptual type, namely the iterator, is obvious. But Dan wants explicit types, with an exception for extreme ugliness of their explicit spelling, I believe.

#ifndef KALDI_FSTEXT_OPENFST_COMPAT_H
#define KALDI_FSTEXT_OPENFST_COMPAT_H


Copy link
Contributor

Choose a reason for hiding this comment

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

#include <fst/flags.h> is for the one below, only if you like it better.

<utility> is for std::forward. std::string is used in printer_print.

Suggested change
#include <fst/arc-map.h>
#include <fst/flags.h>
#include <string>
#include <utility>



#if OPENFST_VER < 10800
#define FST_FLAGS_fst_weight_separator FLAGS_fst_weight_separator
Copy link
Contributor

Choose a reason for hiding this comment

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

This may be a bit C++ish than the macros (https://godbolt.org/z/n5roPYzET):

Suggested change
#define FST_FLAGS_fst_weight_separator FLAGS_fst_weight_separator
DECLARE_string(fst_weight_separator);
inline constexpr std::string& FST_FLAGS_fst_weight_separator =
FLAGS_fst_weight_separator;

To your liking, anyway. Either is fine.

return ArcMap(std::forward<Args>(args)...);
}

using MapFstOptions=ArcMapFstOptions;
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
using MapFstOptions=ArcMapFstOptions;
using MapFstOptions = ArcMapFstOptions;

Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest that clang-format should be used to format the file automagically.

Copy link
Contributor

Choose a reason for hiding this comment

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

@csukuangfj, I suggest the same… I mean, feel free… 😁

Copy link
Contributor

Choose a reason for hiding this comment

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

Guys, the focus here is on getting something committed that won't break things, we're in maintenance mode so it's not the time to worry too much about how it looks.

@@ -235,8 +235,13 @@ inline bool HasBannedPrefixPlusDigits(SymbolTable *symTable, std::string prefix,
assert(symTable != NULL);
const char *prefix_ptr = prefix.c_str();
size_t prefix_len = strlen(prefix_ptr); // allowed to be zero but not encouraged.
#if OPENFST_VER >= 10800
for (SymbolTable::iterator siter = symTable->begin(); siter != symTable->end(); ++siter) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Past 80.

Suggested change
for (SymbolTable::iterator siter = symTable->begin(); siter != symTable->end(); ++siter) {
for (SymbolTable::iterator siter = symTable->begin();
siter != symTable->end(); ++siter) {

@@ -24,7 +24,7 @@

namespace kaldi {

typedef kaldi::int32 int32;
typedef int32 int32;
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks weird. Is it still required? IIRC, this was a workaround for a bug in MS compiler 10 years ago. I may be totally wrong, tho.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd say it's likely not required, probably this is from a sed replace operation.

@kkm000
Copy link
Contributor

kkm000 commented Aug 28, 2024

I can take a stab at it tomorrow… To be more precise, it's like 6 in the morning here, and it's still "today", so tomorrow can be any time within 24h from now...

@danpovey
Copy link
Contributor

danpovey commented Sep 5, 2024

I had my wife check that it compiles for her and it was OK. I want to keep this moving or people will become dispirited from the slow progress, so I'll merge this and if there are problems we'll fix them later.

@danpovey
Copy link
Contributor

danpovey commented Sep 5, 2024

Wait, I see there's a test failing, let me see if I can fix it.
Edit: can't reproduce right now as we're having problems connecting to the outside from our servers.

@danpovey
Copy link
Contributor

danpovey commented Sep 5, 2024

Does anyone have time to run the tests locally and see what the output of the failing test (lattice-utils-test I think) is?
Our systems have some issues right now that make this difficult.

@danpovey
Copy link
Contributor

danpovey commented Sep 7, 2024

The issue does not seem to happen with OpenFST 1.7.2. Trying with 1.8.3 which is what the failed test ran with.

@danpovey
Copy link
Contributor

danpovey commented Sep 8, 2024

OK, I have not been able to reproduce the issue with openfst-1.8.3 because of this:
/star-dan/kaldi/tools/openfst-1.8.3/missing: line 81: aclocal-1.16: command not found
but let me just merge anyway because it looks like it doesn't hurt with earlier versions of OpenFST so nothing is broken that was working before. We can fix this later.

@danpovey danpovey merged commit 13441d0 into kaldi-asr:master Sep 8, 2024
1 of 2 checks passed
@jtrmal
Copy link
Contributor Author

jtrmal commented Sep 9, 2024 via email

@danpovey
Copy link
Contributor

danpovey commented Sep 9, 2024 via email

@kkm000
Copy link
Contributor

kkm000 commented Sep 9, 2024

I'll do my best to look at it today. As soon as I crawl out of the comms closet. Looks like I got me my Internet back...

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.

5 participants