Blame view

tools/openfst-1.6.7/src/include/fst/randgen.h 25.1 KB
8dcb6dfcb   Yannick Estève   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
  // See www.openfst.org for extensive documentation on this weighted
  // finite-state transducer library.
  //
  // Classes and functions to generate random paths through an FST.
  
  #ifndef FST_RANDGEN_H_
  #define FST_RANDGEN_H_
  
  #include <math.h>
  #include <stddef.h>
  #include <limits>
  #include <map>
  #include <memory>
  #include <random>
  #include <utility>
  #include <vector>
  
  #include <fst/log.h>
  
  #include <fst/accumulator.h>
  #include <fst/cache.h>
  #include <fst/dfs-visit.h>
  #include <fst/float-weight.h>
  #include <fst/fst-decl.h>
  #include <fst/fst.h>
  #include <fst/mutable-fst.h>
  #include <fst/properties.h>
  #include <fst/util.h>
  #include <fst/weight.h>
  
  namespace fst {
  
  // The RandGenFst class is roughly similar to ArcMapFst in that it takes two
  // template parameters denoting the input and output arc types. However, it also
  // takes an additional template parameter which specifies a sampler object which
  // samples (with replacement) arcs from an FST state. The sampler in turn takes
  // a template parameter for a selector object which actually chooses the arc.
  //
  // Arc selector functors are used to select a random transition given an FST
  // state s, returning a number N such that 0 <= N <= NumArcs(s). If N is
  // NumArcs(s), then the final weight is selected; otherwise the N-th arc is
  // selected. It is assumed these are not applied to any state which is neither
  // final nor has any arcs leaving it.
  
  // Randomly selects a transition using the uniform distribution. This class is
  // not thread-safe.
  template <class Arc>
  class UniformArcSelector {
   public:
    using StateId = typename Arc::StateId;
    using Weight = typename Arc::Weight;
  
    // Constructs a selector with a non-deterministic seed.
    UniformArcSelector() : rand_(std::random_device()()) {}
    // Constructs a selector with a given seed.
    explicit UniformArcSelector(uint64 seed) : rand_(seed) {}
  
    size_t operator()(const Fst<Arc> &fst, StateId s) const {
      const auto n = fst.NumArcs(s) + (fst.Final(s) != Weight::Zero());
      return static_cast<size_t>(
          std::uniform_int_distribution<>(0, n - 1)(rand_));
    }
  
   private:
    mutable std::mt19937_64 rand_;
  };
  
  // Randomly selects a transition w.r.t. the weights treated as negative log
  // probabilities after normalizing for the total weight leaving the state. Zero
  // transitions are disregarded. It assumed that Arc::Weight::Value() accesses
  // the floating point representation of the weight. This class is not
  // thread-safe.
  template <class Arc>
  class LogProbArcSelector {
   public:
    using StateId = typename Arc::StateId;
    using Weight = typename Arc::Weight;
  
    // Constructs a selector with a non-deterministic seed.
    LogProbArcSelector() : rand_(std::random_device()()) {}
    // Constructs a selector with a given seed.
    explicit LogProbArcSelector(uint64 seed) : rand_(seed) {}
  
    size_t operator()(const Fst<Arc> &fst, StateId s) const {
      // Finds total weight leaving state.
      auto sum = Log64Weight::Zero();
      ArcIterator<Fst<Arc>> aiter(fst, s);
      for (; !aiter.Done(); aiter.Next()) {
        const auto &arc = aiter.Value();
        sum = Plus(sum, to_log_weight_(arc.weight));
      }
      sum = Plus(sum, to_log_weight_(fst.Final(s)));
      const double threshold =
          std::uniform_real_distribution<>(0, exp(-sum.Value()))(rand_);
      auto p = Log64Weight::Zero();
      size_t n = 0;
      for (aiter.Reset(); !aiter.Done(); aiter.Next(), ++n) {
        p = Plus(p, to_log_weight_(aiter.Value().weight));
        if (exp(-p.Value()) > threshold) return n;
      }
      return n;
    }
  
   private:
    mutable std::mt19937_64 rand_;
    WeightConvert<Weight, Log64Weight> to_log_weight_;
  };
  
  // Useful alias when using StdArc.
  using StdArcSelector = LogProbArcSelector<StdArc>;
  
  // Same as LogProbArcSelector but use CacheLogAccumulator to cache the weight
  // accumulation computations. This class is not thread-safe.
  template <class Arc>
  class FastLogProbArcSelector : public LogProbArcSelector<Arc> {
   public:
    using StateId = typename Arc::StateId;
    using Weight = typename Arc::Weight;
  
    using LogProbArcSelector<Arc>::operator();
  
    // Constructs a selector with a non-deterministic seed.
    FastLogProbArcSelector() : seed_(std::random_device()()), rand_(seed_) {}
    // Constructs a selector with a given seed.
    explicit FastLogProbArcSelector(uint64 seed) : seed_(seed), rand_(seed_) {}
  
    size_t operator()(const Fst<Arc> &fst, StateId s,
                      CacheLogAccumulator<Arc> *accumulator) const {
      accumulator->SetState(s);
      ArcIterator<Fst<Arc>> aiter(fst, s);
      // Finds total weight leaving state.
      const double sum = to_log_weight_(accumulator->Sum(fst.Final(s), &aiter, 0,
                                                         fst.NumArcs(s)))
                             .Value();
      const double r = -log(std::uniform_real_distribution<>(0, 1)(rand_));
      Weight w = from_log_weight_(r + sum);
      aiter.Reset();
      return accumulator->LowerBound(w, &aiter);
    }
  
    uint64 Seed() const { return seed_; }
  
   private:
    const uint64 seed_;
    mutable std::mt19937_64 rand_;
    WeightConvert<Weight, Log64Weight> to_log_weight_;
    WeightConvert<Log64Weight, Weight> from_log_weight_;
  };
  
  // Random path state info maintained by RandGenFst and passed to samplers.
  template <typename Arc>
  struct RandState {
    using StateId = typename Arc::StateId;
  
    StateId state_id;  // Current input FST state.
    size_t nsamples;   // Number of samples to be sampled at this state.
    size_t length;     // Length of path to this random state.
    size_t select;     // Previous sample arc selection.
    const RandState<Arc> *parent;  // Previous random state on this path.
  
    explicit RandState(StateId state_id, size_t nsamples = 0, size_t length = 0,
                       size_t select = 0, const RandState<Arc> *parent = nullptr)
        : state_id(state_id),
          nsamples(nsamples),
          length(length),
          select(select),
          parent(parent) {}
  
    RandState() : RandState(kNoStateId) {}
  };
  
  // This class, given an arc selector, samples, with replacement, multiple random
  // transitions from an FST's state. This is a generic version with a
  // straightforward use of the arc selector. Specializations may be defined for
  // arc selectors for greater efficiency or special behavior.
  template <class Arc, class Selector>
  class ArcSampler {
   public:
    using StateId = typename Arc::StateId;
    using Weight = typename Arc::Weight;
  
    // The max_length argument may be interpreted (or ignored) by a selector as
    // it chooses. This generic version interprets this literally.
    ArcSampler(const Fst<Arc> &fst, const Selector &selector,
               int32 max_length = std::numeric_limits<int32>::max())
        : fst_(fst), selector_(selector), max_length_(max_length) {}
  
    // Allow updating FST argument; pass only if changed.
    ArcSampler(const ArcSampler<Arc, Selector> &sampler,
               const Fst<Arc> *fst = nullptr)
        : fst_(fst ? *fst : sampler.fst_),
          selector_(sampler.selector_),
          max_length_(sampler.max_length_) {
      Reset();
    }
  
    // Samples a fixed number of samples from the given state. The length argument
    // specifies the length of the path to the state. Returns true if the samples
    // were collected. No samples may be collected if either there are no
    // transitions leaving the state and the state is non-final, or if the path
    // length has been exceeded. Iterator members are provided to read the samples
    // in the order in which they were collected.
    bool Sample(const RandState<Arc> &rstate) {
      sample_map_.clear();
      if ((fst_.NumArcs(rstate.state_id) == 0 &&
           fst_.Final(rstate.state_id) == Weight::Zero()) ||
          rstate.length == max_length_) {
        Reset();
        return false;
      }
      for (size_t i = 0; i < rstate.nsamples; ++i) {
        ++sample_map_[selector_(fst_, rstate.state_id)];
      }
      Reset();
      return true;
    }
  
    // More samples?
    bool Done() const { return sample_iter_ == sample_map_.end(); }
  
    // Gets the next sample.
    void Next() { ++sample_iter_; }
  
    std::pair<size_t, size_t> Value() const { return *sample_iter_; }
  
    void Reset() { sample_iter_ = sample_map_.begin(); }
  
    bool Error() const { return false; }
  
   private:
    const Fst<Arc> &fst_;
    const Selector &selector_;
    const int32 max_length_;
  
    // Stores (N, K) as described for Value().
    std::map<size_t, size_t> sample_map_;
    std::map<size_t, size_t>::const_iterator sample_iter_;
  
    ArcSampler<Arc, Selector> &operator=(const ArcSampler &) = delete;
  };
  
  // Samples one sample of num_to_sample dimensions from a multinomial
  // distribution parameterized by a vector of probabilities. The result
  // container should be pre-initialized (e.g., an empty map or a zeroed vector
  // sized the same as the vector of probabilities.
  // probs.size()).
  template <class Result, class RNG>
  void OneMultinomialSample(const std::vector<double> &probs,
                            size_t num_to_sample, Result *result, RNG *rng) {
    // Left-over probability mass.
    double norm = 0;
    for (double p : probs) norm += p;
    // Left-over number of samples needed.
    for (size_t i = 0; i < probs.size(); ++i) {
      size_t num_sampled = 0;
      if (probs[i] > 0) {
        std::binomial_distribution<> d(num_to_sample, probs[i] / norm);
        num_sampled = d(*rng);
      }
      if (num_sampled != 0) (*result)[i] = num_sampled;
      norm -= probs[i];
      num_to_sample -= num_sampled;
    }
  }
  
  // Specialization for FastLogProbArcSelector.
  template <class Arc>
  class ArcSampler<Arc, FastLogProbArcSelector<Arc>> {
   public:
    using StateId = typename Arc::StateId;
    using Weight = typename Arc::Weight;
  
    using Accumulator = CacheLogAccumulator<Arc>;
    using Selector = FastLogProbArcSelector<Arc>;
  
    ArcSampler(const Fst<Arc> &fst, const Selector &selector,
               int32 max_length = std::numeric_limits<int32>::max())
        : fst_(fst),
          selector_(selector),
          max_length_(max_length),
          accumulator_(new Accumulator()) {
      accumulator_->Init(fst);
      rng_.seed(selector_.Seed());
    }
  
    ArcSampler(const ArcSampler<Arc, Selector> &sampler,
               const Fst<Arc> *fst = nullptr)
        : fst_(fst ? *fst : sampler.fst_),
          selector_(sampler.selector_),
          max_length_(sampler.max_length_) {
      if (fst) {
        accumulator_.reset(new Accumulator());
        accumulator_->Init(*fst);
      } else {  // Shallow copy.
        accumulator_.reset(new Accumulator(*sampler.accumulator_));
      }
    }
  
    bool Sample(const RandState<Arc> &rstate) {
      sample_map_.clear();
      if ((fst_.NumArcs(rstate.state_id) == 0 &&
           fst_.Final(rstate.state_id) == Weight::Zero()) ||
          rstate.length == max_length_) {
        Reset();
        return false;
      }
      if (fst_.NumArcs(rstate.state_id) + 1 < rstate.nsamples) {
        MultinomialSample(rstate);
        Reset();
        return true;
      }
      for (size_t i = 0; i < rstate.nsamples; ++i) {
        ++sample_map_[selector_(fst_, rstate.state_id, accumulator_.get())];
      }
      Reset();
      return true;
    }
  
    bool Done() const { return sample_iter_ == sample_map_.end(); }
  
    void Next() { ++sample_iter_; }
  
    std::pair<size_t, size_t> Value() const { return *sample_iter_; }
  
    void Reset() { sample_iter_ = sample_map_.begin(); }
  
    bool Error() const { return accumulator_->Error(); }
  
   private:
    using RNG = std::mt19937;
  
    // Sample according to the multinomial distribution of rstate.nsamples draws
    // from p_.
    void MultinomialSample(const RandState<Arc> &rstate) {
      p_.clear();
      for (ArcIterator<Fst<Arc>> aiter(fst_, rstate.state_id); !aiter.Done();
           aiter.Next()) {
        p_.push_back(exp(-to_log_weight_(aiter.Value().weight).Value()));
      }
      if (fst_.Final(rstate.state_id) != Weight::Zero()) {
        p_.push_back(exp(-to_log_weight_(fst_.Final(rstate.state_id)).Value()));
      }
      if (rstate.nsamples < std::numeric_limits<RNG::result_type>::max()) {
        OneMultinomialSample(p_, rstate.nsamples, &sample_map_, &rng_);
      } else {
        for (size_t i = 0; i < p_.size(); ++i) {
          sample_map_[i] = ceil(p_[i] * rstate.nsamples);
        }
      }
    }
  
    const Fst<Arc> &fst_;
    const Selector &selector_;
    const int32 max_length_;
  
    // Stores (N, K) for Value().
    std::map<size_t, size_t> sample_map_;
    std::map<size_t, size_t>::const_iterator sample_iter_;
  
    std::unique_ptr<Accumulator> accumulator_;
    RNG rng_;                // Random number generator.
    std::vector<double> p_;  // Multinomial parameters.
    WeightConvert<Weight, Log64Weight> to_log_weight_;
  };
  
  // Options for random path generation with RandGenFst. The template argument is
  // a sampler, typically the class ArcSampler. Ownership of the sampler is taken
  // by RandGenFst.
  template <class Sampler>
  struct RandGenFstOptions : public CacheOptions {
    Sampler *sampler;          // How to sample transitions at a state.
    int32 npath;               // Number of paths to generate.
    bool weighted;             // Is the output tree weighted by path count, or
                               // is it just an unweighted DAG?
    bool remove_total_weight;  // Remove total weight when output is weighted.
  
    RandGenFstOptions(const CacheOptions &opts, Sampler *sampler, int32 npath = 1,
                      bool weighted = true, bool remove_total_weight = false)
        : CacheOptions(opts),
          sampler(sampler),
          npath(npath),
          weighted(weighted),
          remove_total_weight(remove_total_weight) {}
  };
  
  namespace internal {
  
  // Implementation of RandGenFst.
  template <class FromArc, class ToArc, class Sampler>
  class RandGenFstImpl : public CacheImpl<ToArc> {
   public:
    using FstImpl<ToArc>::SetType;
    using FstImpl<ToArc>::SetProperties;
    using FstImpl<ToArc>::SetInputSymbols;
    using FstImpl<ToArc>::SetOutputSymbols;
  
    using CacheBaseImpl<CacheState<ToArc>>::PushArc;
    using CacheBaseImpl<CacheState<ToArc>>::HasArcs;
    using CacheBaseImpl<CacheState<ToArc>>::HasFinal;
    using CacheBaseImpl<CacheState<ToArc>>::HasStart;
    using CacheBaseImpl<CacheState<ToArc>>::SetArcs;
    using CacheBaseImpl<CacheState<ToArc>>::SetFinal;
    using CacheBaseImpl<CacheState<ToArc>>::SetStart;
  
    using Label = typename FromArc::Label;
    using StateId = typename FromArc::StateId;
    using FromWeight = typename FromArc::Weight;
  
    using ToWeight = typename ToArc::Weight;
  
    RandGenFstImpl(const Fst<FromArc> &fst,
                   const RandGenFstOptions<Sampler> &opts)
        : CacheImpl<ToArc>(opts),
          fst_(fst.Copy()),
          sampler_(opts.sampler),
          npath_(opts.npath),
          weighted_(opts.weighted),
          remove_total_weight_(opts.remove_total_weight),
          superfinal_(kNoLabel) {
      SetType("randgen");
      SetProperties(
          RandGenProperties(fst.Properties(kFstProperties, false), weighted_),
          kCopyProperties);
      SetInputSymbols(fst.InputSymbols());
      SetOutputSymbols(fst.OutputSymbols());
    }
  
    RandGenFstImpl(const RandGenFstImpl &impl)
        : CacheImpl<ToArc>(impl),
          fst_(impl.fst_->Copy(true)),
          sampler_(new Sampler(*impl.sampler_, fst_.get())),
          npath_(impl.npath_),
          weighted_(impl.weighted_),
          superfinal_(kNoLabel) {
      SetType("randgen");
      SetProperties(impl.Properties(), kCopyProperties);
      SetInputSymbols(impl.InputSymbols());
      SetOutputSymbols(impl.OutputSymbols());
    }
  
    StateId Start() {
      if (!HasStart()) {
        const auto s = fst_->Start();
        if (s == kNoStateId) return kNoStateId;
        SetStart(state_table_.size());
        state_table_.emplace_back(
            new RandState<FromArc>(s, npath_, 0, 0, nullptr));
      }
      return CacheImpl<ToArc>::Start();
    }
  
    ToWeight Final(StateId s) {
      if (!HasFinal(s)) Expand(s);
      return CacheImpl<ToArc>::Final(s);
    }
  
    size_t NumArcs(StateId s) {
      if (!HasArcs(s)) Expand(s);
      return CacheImpl<ToArc>::NumArcs(s);
    }
  
    size_t NumInputEpsilons(StateId s) {
      if (!HasArcs(s)) Expand(s);
      return CacheImpl<ToArc>::NumInputEpsilons(s);
    }
  
    size_t NumOutputEpsilons(StateId s) {
      if (!HasArcs(s)) Expand(s);
      return CacheImpl<ToArc>::NumOutputEpsilons(s);
    }
  
    uint64 Properties() const override { return Properties(kFstProperties); }
  
    // Sets error if found, and returns other FST impl properties.
    uint64 Properties(uint64 mask) const override {
      if ((mask & kError) &&
          (fst_->Properties(kError, false) || sampler_->Error())) {
        SetProperties(kError, kError);
      }
      return FstImpl<ToArc>::Properties(mask);
    }
  
    void InitArcIterator(StateId s, ArcIteratorData<ToArc> *data) {
      if (!HasArcs(s)) Expand(s);
      CacheImpl<ToArc>::InitArcIterator(s, data);
    }
  
    // Computes the outgoing transitions from a state, creating new destination
    // states as needed.
    void Expand(StateId s) {
      if (s == superfinal_) {
        SetFinal(s, ToWeight::One());
        SetArcs(s);
        return;
      }
      SetFinal(s, ToWeight::Zero());
      const auto &rstate = *state_table_[s];
      sampler_->Sample(rstate);
      ArcIterator<Fst<FromArc>> aiter(*fst_, rstate.state_id);
      const auto narcs = fst_->NumArcs(rstate.state_id);
      for (; !sampler_->Done(); sampler_->Next()) {
        const auto &sample_pair = sampler_->Value();
        const auto pos = sample_pair.first;
        const auto count = sample_pair.second;
        double prob = static_cast<double>(count) / rstate.nsamples;
        if (pos < narcs) {  // Regular transition.
          aiter.Seek(sample_pair.first);
          const auto &aarc = aiter.Value();
          const auto weight =
              weighted_ ? to_weight_(Log64Weight(-log(prob))) : ToWeight::One();
          const ToArc barc(aarc.ilabel, aarc.olabel, weight, state_table_.size());
          PushArc(s, barc);
          auto *nrstate = new RandState<FromArc>(aarc.nextstate, count,
                                                 rstate.length + 1, pos, &rstate);
          state_table_.emplace_back(nrstate);
        } else {  // Super-final transition.
          if (weighted_) {
            const auto weight =
                remove_total_weight_
                    ? to_weight_(Log64Weight(-log(prob)))
                    : to_weight_(Log64Weight(-log(prob * npath_)));
            SetFinal(s, weight);
          } else {
            if (superfinal_ == kNoLabel) {
              superfinal_ = state_table_.size();
              state_table_.emplace_back(
                  new RandState<FromArc>(kNoStateId, 0, 0, 0, nullptr));
            }
            for (size_t n = 0; n < count; ++n) {
              const ToArc barc(0, 0, ToWeight::One(), superfinal_);
              PushArc(s, barc);
            }
          }
        }
      }
      SetArcs(s);
    }
  
   private:
    const std::unique_ptr<Fst<FromArc>> fst_;
    std::unique_ptr<Sampler> sampler_;
    const int32 npath_;
    std::vector<std::unique_ptr<RandState<FromArc>>> state_table_;
    const bool weighted_;
    bool remove_total_weight_;
    StateId superfinal_;
    WeightConvert<Log64Weight, ToWeight> to_weight_;
  };
  
  }  // namespace internal
  
  // FST class to randomly generate paths through an FST, with details controlled
  // by RandGenOptionsFst. Output format is a tree weighted by the path count.
  template <class FromArc, class ToArc, class Sampler>
  class RandGenFst
      : public ImplToFst<internal::RandGenFstImpl<FromArc, ToArc, Sampler>> {
   public:
    using Label = typename FromArc::Label;
    using StateId = typename FromArc::StateId;
    using Weight = typename FromArc::Weight;
  
    using Store = DefaultCacheStore<FromArc>;
    using State = typename Store::State;
  
    using Impl = internal::RandGenFstImpl<FromArc, ToArc, Sampler>;
  
    friend class ArcIterator<RandGenFst<FromArc, ToArc, Sampler>>;
    friend class StateIterator<RandGenFst<FromArc, ToArc, Sampler>>;
  
    RandGenFst(const Fst<FromArc> &fst, const RandGenFstOptions<Sampler> &opts)
        : ImplToFst<Impl>(std::make_shared<Impl>(fst, opts)) {}
  
    // See Fst<>::Copy() for doc.
    RandGenFst(const RandGenFst<FromArc, ToArc, Sampler> &fst, bool safe = false)
        : ImplToFst<Impl>(fst, safe) {}
  
    // Get a copy of this RandGenFst. See Fst<>::Copy() for further doc.
    RandGenFst<FromArc, ToArc, Sampler> *Copy(bool safe = false) const override {
      return new RandGenFst<FromArc, ToArc, Sampler>(*this, safe);
    }
  
    inline void InitStateIterator(StateIteratorData<ToArc> *data) const override;
  
    void InitArcIterator(StateId s, ArcIteratorData<ToArc> *data) const override {
      GetMutableImpl()->InitArcIterator(s, data);
    }
  
   private:
    using ImplToFst<Impl>::GetImpl;
    using ImplToFst<Impl>::GetMutableImpl;
  
    RandGenFst &operator=(const RandGenFst &) = delete;
  };
  
  // Specialization for RandGenFst.
  template <class FromArc, class ToArc, class Sampler>
  class StateIterator<RandGenFst<FromArc, ToArc, Sampler>>
      : public CacheStateIterator<RandGenFst<FromArc, ToArc, Sampler>> {
   public:
    explicit StateIterator(const RandGenFst<FromArc, ToArc, Sampler> &fst)
        : CacheStateIterator<RandGenFst<FromArc, ToArc, Sampler>>(
              fst, fst.GetMutableImpl()) {}
  };
  
  // Specialization for RandGenFst.
  template <class FromArc, class ToArc, class Sampler>
  class ArcIterator<RandGenFst<FromArc, ToArc, Sampler>>
      : public CacheArcIterator<RandGenFst<FromArc, ToArc, Sampler>> {
   public:
    using StateId = typename FromArc::StateId;
  
    ArcIterator(const RandGenFst<FromArc, ToArc, Sampler> &fst, StateId s)
        : CacheArcIterator<RandGenFst<FromArc, ToArc, Sampler>>(
              fst.GetMutableImpl(), s) {
      if (!fst.GetImpl()->HasArcs(s)) fst.GetMutableImpl()->Expand(s);
    }
  };
  
  template <class FromArc, class ToArc, class Sampler>
  inline void RandGenFst<FromArc, ToArc, Sampler>::InitStateIterator(
      StateIteratorData<ToArc> *data) const {
    data->base = new StateIterator<RandGenFst<FromArc, ToArc, Sampler>>(*this);
  }
  
  // Options for random path generation.
  template <class Selector>
  struct RandGenOptions {
    const Selector &selector;  // How an arc is selected at a state.
    int32 max_length;          // Maximum path length.
    int32 npath;               // Number of paths to generate.
    bool weighted;             // Is the output tree weighted by path count, or
                               // is it just an unweighted DAG?
    bool remove_total_weight;  // Remove total weight when output is weighted?
  
    explicit RandGenOptions(const Selector &selector,
                            int32 max_length = std::numeric_limits<int32>::max(),
                            int32 npath = 1, bool weighted = false,
                            bool remove_total_weight = false)
        : selector(selector),
          max_length(max_length),
          npath(npath),
          weighted(weighted),
          remove_total_weight(remove_total_weight) {}
  };
  
  namespace internal {
  
  template <class FromArc, class ToArc>
  class RandGenVisitor {
   public:
    using StateId = typename FromArc::StateId;
    using Weight = typename FromArc::Weight;
  
    explicit RandGenVisitor(MutableFst<ToArc> *ofst) : ofst_(ofst) {}
  
    void InitVisit(const Fst<FromArc> &ifst) {
      ifst_ = &ifst;
      ofst_->DeleteStates();
      ofst_->SetInputSymbols(ifst.InputSymbols());
      ofst_->SetOutputSymbols(ifst.OutputSymbols());
      if (ifst.Properties(kError, false)) ofst_->SetProperties(kError, kError);
      path_.clear();
    }
  
    constexpr bool InitState(StateId, StateId) const { return true; }
  
    bool TreeArc(StateId, const ToArc &arc) {
      if (ifst_->Final(arc.nextstate) == Weight::Zero()) {
        path_.push_back(arc);
      } else {
        OutputPath();
      }
      return true;
    }
  
    bool BackArc(StateId, const FromArc &) {
      FSTERROR() << "RandGenVisitor: cyclic input";
      ofst_->SetProperties(kError, kError);
      return false;
    }
  
    bool ForwardOrCrossArc(StateId, const FromArc &) {
      OutputPath();
      return true;
    }
  
    void FinishState(StateId s, StateId p, const FromArc *) {
      if (p != kNoStateId && ifst_->Final(s) == Weight::Zero()) path_.pop_back();
    }
  
    void FinishVisit() {}
  
   private:
    void OutputPath() {
      if (ofst_->Start() == kNoStateId) {
        const auto start = ofst_->AddState();
        ofst_->SetStart(start);
      }
      auto src = ofst_->Start();
      for (size_t i = 0; i < path_.size(); ++i) {
        const auto dest = ofst_->AddState();
        const ToArc arc(path_[i].ilabel, path_[i].olabel, Weight::One(), dest);
        ofst_->AddArc(src, arc);
        src = dest;
      }
      ofst_->SetFinal(src, Weight::One());
    }
  
    const Fst<FromArc> *ifst_;
    MutableFst<ToArc> *ofst_;
    std::vector<ToArc> path_;
  
    RandGenVisitor(const RandGenVisitor &) = delete;
    RandGenVisitor &operator=(const RandGenVisitor &) = delete;
  };
  
  }  // namespace internal
  
  // Randomly generate paths through an FST; details controlled by
  // RandGenOptions.
  template <class FromArc, class ToArc, class Selector>
  void RandGen(const Fst<FromArc> &ifst, MutableFst<ToArc> *ofst,
               const RandGenOptions<Selector> &opts) {
    using State = typename ToArc::StateId;
    using Weight = typename ToArc::Weight;
    using Sampler = ArcSampler<FromArc, Selector>;
    auto *sampler = new Sampler(ifst, opts.selector, opts.max_length);
    RandGenFstOptions<Sampler> fopts(CacheOptions(true, 0), sampler, opts.npath,
                                     opts.weighted, opts.remove_total_weight);
    RandGenFst<FromArc, ToArc, Sampler> rfst(ifst, fopts);
    if (opts.weighted) {
      *ofst = rfst;
    } else {
      internal::RandGenVisitor<FromArc, ToArc> rand_visitor(ofst);
      DfsVisit(rfst, &rand_visitor);
    }
  }
  
  // Randomly generate a path through an FST with the uniform distribution
  // over the transitions.
  template <class FromArc, class ToArc>
  void RandGen(const Fst<FromArc> &ifst, MutableFst<ToArc> *ofst) {
    const UniformArcSelector<FromArc> uniform_selector;
    RandGenOptions<UniformArcSelector<ToArc>> opts(uniform_selector);
    RandGen(ifst, ofst, opts);
  }
  
  }  // namespace fst
  
  #endif  // FST_RANDGEN_H_