comparison mupdf-source/thirdparty/tesseract/src/lstm/parallel.h @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 ///////////////////////////////////////////////////////////////////////
2 // File: parallel.h
3 // Description: Runs networks in parallel on the same input.
4 // Author: Ray Smith
5 //
6 // (C) Copyright 2013, Google Inc.
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 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 ///////////////////////////////////////////////////////////////////////
17
18 #ifndef TESSERACT_LSTM_PARALLEL_H_
19 #define TESSERACT_LSTM_PARALLEL_H_
20
21 #include "plumbing.h"
22
23 namespace tesseract {
24
25 // Runs multiple networks in parallel, interlacing their outputs.
26 class Parallel : public Plumbing {
27 public:
28 // ni_ and no_ will be set by AddToStack.
29 TESS_API
30 Parallel(const std::string &name, NetworkType type);
31
32 // Returns the shape output from the network given an input shape (which may
33 // be partially unknown ie zero).
34 StaticShape OutputShape(const StaticShape &input_shape) const override;
35
36 std::string spec() const override {
37 std::string spec;
38 if (type_ == NT_PAR_2D_LSTM) {
39 // We have 4 LSTMs operating in parallel here, so the size of each is
40 // the number of outputs/4.
41 spec += "L2xy" + std::to_string(no_ / 4);
42 } else if (type_ == NT_PAR_RL_LSTM) {
43 // We have 2 LSTMs operating in parallel here, so the size of each is
44 // the number of outputs/2.
45 if (stack_[0]->type() == NT_LSTM_SUMMARY) {
46 spec += "Lbxs" + std::to_string(no_ / 2);
47 } else {
48 spec += "Lbx" + std::to_string(no_ / 2);
49 }
50 } else {
51 if (type_ == NT_REPLICATED) {
52 spec += "R" + std::to_string(stack_.size()) + "(" + stack_[0]->spec();
53 } else {
54 for (auto &it : stack_) {
55 spec += it->spec();
56 }
57 }
58 spec += ")";
59 }
60 return spec;
61 }
62
63 // Runs forward propagation of activations on the input line.
64 // See Network for a detailed discussion of the arguments.
65 void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose,
66 NetworkScratch *scratch, NetworkIO *output) override;
67
68 // Runs backward propagation of errors on the deltas line.
69 // See Network for a detailed discussion of the arguments.
70 bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch,
71 NetworkIO *back_deltas) override;
72
73 private:
74 // If *this is a NT_REPLICATED, then it feeds a replicated network with
75 // identical inputs, and it would be extremely wasteful for them to each
76 // calculate and store the same transpose of the inputs, so Parallel does it
77 // and passes a pointer to the replicated network, allowing it to use the
78 // transpose on the next call to Backward.
79 TransposedArray transposed_input_;
80 };
81
82 } // namespace tesseract.
83
84 #endif // TESSERACT_LSTM_PARALLEL_H_