Skip to content

Commit

Permalink
Fix score calculation error when intersecting FsaVec and Fsa on cpu (#…
Browse files Browse the repository at this point in the history
…775)

* fix score calculation error when intersecting FsaVec and Fsa on cpu

* add test cace to comfirm the result of intersecting an FsaVec and Fsa is correct

Co-authored-by: pkufool <[email protected]>
  • Loading branch information
pkufool and pkufool authored Jul 7, 2021
1 parent 53cb3c9 commit d0bfa7e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions k2/csrc/fsa_algo.cu
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ bool Intersect(FsaOrVec &a_fsas, int32_t properties_a, FsaOrVec &b_fsas,
if (arc_map_a) {
int32_t arc_offset_a = a_fsas_row_splits12_data[i * stride_a];
for (int32_t i = 0; i < this_num_arcs; i++)
this_arc_map_a[i] += arc_offset_a;
if (this_arc_map_a[i] != -1) this_arc_map_a[i] += arc_offset_a;
}
if (arc_map_b) {
int32_t arc_offset_b = b_fsas_row_splits12_data[i * stride_b];
for (int32_t i = 0; i < this_num_arcs; i++)
this_arc_map_b[i] += arc_offset_b;
if (this_arc_map_b[i] != -1) this_arc_map_b[i] += arc_offset_b;
}
}
*out = creator.GetFsaVec();
Expand Down
31 changes: 31 additions & 0 deletions k2/csrc/fsa_algo_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,37 @@ TEST(FsaAlgo, IntersectFsaVec) {
CheckArrayData(arc_map, std::vector<int32_t>{0, 2, 3});
}

// Intersect an FsaVec with Fsa should be equivalent with intersect with these
// fsa alone one by one
TEST(FsaAlgo, IntersectFsaVecRandom) {
FsaVec fsa_vec = RandomFsaVec();
ArcSort(&fsa_vec);
Fsa fsa = RandomFsa();
ArcSort(&fsa);

FsaVec fsa_vec_out;
Array1<int32_t> arc_map_a;
Array1<int32_t> arc_map_b;
Intersect(fsa_vec, -1, fsa, -1, true, &fsa_vec_out, &arc_map_a,
&arc_map_b);
std::ostringstream oss_vec;
oss_vec << fsa_vec_out;

std::ostringstream oss;
oss << "[ ";
for (int32_t i = 0; i < fsa_vec.Dim0(); ++i) {
FsaVec out_tmp;
Array1<int32_t> arc_map_a_t;
Array1<int32_t> arc_map_b_t;
Fsa fsa_element = GetFsaVecElement(fsa_vec, i);
Intersect(fsa_element, -1, fsa, -1, true, &out_tmp,
&arc_map_a_t, &arc_map_b_t);
oss << GetFsaVecElement(out_tmp, 0) << " ";
}
oss << "]";
EXPECT_EQ(oss.str(), oss_vec.str());
}

TEST(FsaAlgo, AddEpsilonSelfLoopsFsa) {
std::string s1 = R"(0 1 1 0.1
0 2 1 0.2
Expand Down

0 comments on commit d0bfa7e

Please sign in to comment.