nemotron-ocr 2.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nemotron_ocr-2.0.0/.gitignore +9 -0
- nemotron_ocr-2.0.0/PKG-INFO +13 -0
- nemotron_ocr-2.0.0/cpp/.gitattributes +1 -0
- nemotron_ocr-2.0.0/cpp/.gitignore +6 -0
- nemotron_ocr-2.0.0/cpp/.gitmodules +3 -0
- nemotron_ocr-2.0.0/cpp/README.md +15 -0
- nemotron_ocr-2.0.0/cpp/beam_decode/beam_decode.cpp +459 -0
- nemotron_ocr-2.0.0/cpp/beam_decode/beam_decode.h +17 -0
- nemotron_ocr-2.0.0/cpp/beam_decode/kn_lm.cpp +85 -0
- nemotron_ocr-2.0.0/cpp/beam_decode/kn_lm.h +26 -0
- nemotron_ocr-2.0.0/cpp/beam_decode/language_model.cpp +146 -0
- nemotron_ocr-2.0.0/cpp/beam_decode/language_model.h +65 -0
- nemotron_ocr-2.0.0/cpp/beam_decode/log_sum_exp.cpp +6 -0
- nemotron_ocr-2.0.0/cpp/beam_decode/log_sum_exp.h +53 -0
- nemotron_ocr-2.0.0/cpp/beam_decode/ngram_lm_base.cpp +329 -0
- nemotron_ocr-2.0.0/cpp/beam_decode/ngram_lm_base.h +79 -0
- nemotron_ocr-2.0.0/cpp/beam_decode/prefix.cpp +22 -0
- nemotron_ocr-2.0.0/cpp/beam_decode/prefix.h +157 -0
- nemotron_ocr-2.0.0/cpp/beam_decode/sbo_lm.cpp +46 -0
- nemotron_ocr-2.0.0/cpp/beam_decode/sbo_lm.h +20 -0
- nemotron_ocr-2.0.0/cpp/better_grid_sample/cpu_indirect_grid_sample.cpp +93 -0
- nemotron_ocr-2.0.0/cpp/better_grid_sample/gpu_grid_sample_utils.cuh +41 -0
- nemotron_ocr-2.0.0/cpp/better_grid_sample/gpu_indirect_grid_sample.cu +327 -0
- nemotron_ocr-2.0.0/cpp/better_grid_sample/grid_sample.h +66 -0
- nemotron_ocr-2.0.0/cpp/common.cpp +12 -0
- nemotron_ocr-2.0.0/cpp/common.h +57 -0
- nemotron_ocr-2.0.0/cpp/cuda_intellisense.cuh +50 -0
- nemotron_ocr-2.0.0/cpp/geometry.h +1100 -0
- nemotron_ocr-2.0.0/cpp/geometry_api/calc_poly_min_rrect.cpp +164 -0
- nemotron_ocr-2.0.0/cpp/geometry_api/geometry_api.cpp +100 -0
- nemotron_ocr-2.0.0/cpp/geometry_api/geometry_api.h +15 -0
- nemotron_ocr-2.0.0/cpp/geometry_api/geometry_api_common.h +120 -0
- nemotron_ocr-2.0.0/cpp/geometry_api/geometry_api_gpu.cu +141 -0
- nemotron_ocr-2.0.0/cpp/geometry_api/get_rel_continuation_cos.cpp +59 -0
- nemotron_ocr-2.0.0/cpp/geometry_api/matrix2x2.h +92 -0
- nemotron_ocr-2.0.0/cpp/geometry_api/poly_bounds_quad.cpp +60 -0
- nemotron_ocr-2.0.0/cpp/graph_detection/encode_util.cpp +271 -0
- nemotron_ocr-2.0.0/cpp/graph_detection/encode_util.h +183 -0
- nemotron_ocr-2.0.0/cpp/half_ops.cu +4 -0
- nemotron_ocr-2.0.0/cpp/half_ops.cuh +148 -0
- nemotron_ocr-2.0.0/cpp/local_ips/local_ips.h +10 -0
- nemotron_ocr-2.0.0/cpp/local_ips/quad_all_2_all_dist_v2.cu +161 -0
- nemotron_ocr-2.0.0/cpp/module.cpp +124 -0
- nemotron_ocr-2.0.0/cpp/non_maximal_suppression/cpu_non_maximal_suppression.cpp +208 -0
- nemotron_ocr-2.0.0/cpp/non_maximal_suppression/cuda_non_maximal_suppression.cu +1668 -0
- nemotron_ocr-2.0.0/cpp/non_maximal_suppression/nms_common.h +226 -0
- nemotron_ocr-2.0.0/cpp/non_maximal_suppression/nms_kd_tree.h +448 -0
- nemotron_ocr-2.0.0/cpp/non_maximal_suppression/non_maximal_suppression.cpp +389 -0
- nemotron_ocr-2.0.0/cpp/non_maximal_suppression/non_maximal_suppression.h +101 -0
- nemotron_ocr-2.0.0/cpp/non_maximal_suppression/strided_quad.h +52 -0
- nemotron_ocr-2.0.0/cpp/promote.h +37 -0
- nemotron_ocr-2.0.0/cpp/quad_rectify/quad_rectify.h +97 -0
- nemotron_ocr-2.0.0/cpp/quad_rectify/quad_rectify_cpu.cpp +181 -0
- nemotron_ocr-2.0.0/cpp/quad_rectify/quad_rectify_cpu.h +23 -0
- nemotron_ocr-2.0.0/cpp/quad_rectify/quad_rectify_gpu.cu +288 -0
- nemotron_ocr-2.0.0/cpp/quad_rectify/quad_rectify_gpu.h +23 -0
- nemotron_ocr-2.0.0/cpp/quad_rectify/quad_rectify_shared.h +119 -0
- nemotron_ocr-2.0.0/cpp/scope_timer.h +44 -0
- nemotron_ocr-2.0.0/cpp/sparse_select/sparse_select.cpp +57 -0
- nemotron_ocr-2.0.0/cpp/sparse_select/sparse_select.h +10 -0
- nemotron_ocr-2.0.0/cpp/text_region_grouping/dense_relations_to_graph.cpp +252 -0
- nemotron_ocr-2.0.0/cpp/text_region_grouping/text_region_grouping.cpp +367 -0
- nemotron_ocr-2.0.0/cpp/text_region_grouping/text_region_grouping.h +26 -0
- nemotron_ocr-2.0.0/cpp/third_party/clipper/clipper.cpp +4623 -0
- nemotron_ocr-2.0.0/cpp/third_party/clipper/clipper.hpp +404 -0
- nemotron_ocr-2.0.0/cpp/trove/LICENSE +24 -0
- nemotron_ocr-2.0.0/cpp/trove/README.md +138 -0
- nemotron_ocr-2.0.0/cpp/trove/doc/contiguous.png +3 -0
- nemotron_ocr-2.0.0/cpp/trove/doc/random.png +3 -0
- nemotron_ocr-2.0.0/cpp/trove/doc/transpose.png +3 -0
- nemotron_ocr-2.0.0/cpp/trove/tests/Makefile +14 -0
- nemotron_ocr-2.0.0/cpp/trove/tests/benchmark.cu +366 -0
- nemotron_ocr-2.0.0/cpp/trove/tests/block.cu +70 -0
- nemotron_ocr-2.0.0/cpp/trove/tests/timer.h +20 -0
- nemotron_ocr-2.0.0/cpp/trove/trove/aos.h +279 -0
- nemotron_ocr-2.0.0/cpp/trove/trove/array.h +269 -0
- nemotron_ocr-2.0.0/cpp/trove/trove/block.h +67 -0
- nemotron_ocr-2.0.0/cpp/trove/trove/detail/dismember.h +132 -0
- nemotron_ocr-2.0.0/cpp/trove/trove/detail/fallback.h +133 -0
- nemotron_ocr-2.0.0/cpp/trove/trove/memory.h +170 -0
- nemotron_ocr-2.0.0/cpp/trove/trove/print_array.h +48 -0
- nemotron_ocr-2.0.0/cpp/trove/trove/ptr.h +80 -0
- nemotron_ocr-2.0.0/cpp/trove/trove/rotate.h +117 -0
- nemotron_ocr-2.0.0/cpp/trove/trove/shfl.h +106 -0
- nemotron_ocr-2.0.0/cpp/trove/trove/static_gcd.h +77 -0
- nemotron_ocr-2.0.0/cpp/trove/trove/static_mod_inverse.h +54 -0
- nemotron_ocr-2.0.0/cpp/trove/trove/transpose.h +686 -0
- nemotron_ocr-2.0.0/cpp/trove/trove/utility.h +162 -0
- nemotron_ocr-2.0.0/cpp/trove/trove/warp.h +48 -0
- nemotron_ocr-2.0.0/hatch_build.py +86 -0
- nemotron_ocr-2.0.0/pyproject.toml +53 -0
- nemotron_ocr-2.0.0/scripts/build-extension.py +134 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/__init__.py +3 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/encoders/__init__.py +3 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/encoders/base.py +42 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/encoders/recognizer_encoder.py +312 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/encoders/relational_encoder.py +188 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/models/__init__.py +3 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/models/blocks.py +68 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/models/detector/__init__.py +3 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/models/detector/aspp.py +108 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/models/detector/fots_detector.py +322 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/models/detector/regnet.py +797 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/models/recognizer.py +117 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/models/relational.py +480 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/models/utils.py +50 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/pipeline.py +584 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/pipeline_v2.py +914 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/post_processing/__init__.py +3 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/post_processing/data/data_container.py +9 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/post_processing/data/quadrangle.py +254 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/post_processing/data/relation_graph.py +187 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/post_processing/data/text_region.py +335 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/post_processing/indirect_grid_sample.py +49 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/post_processing/quad_rectify.py +86 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/post_processing/research_ops.py +158 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr/inference/pre_processing.py +64 -0
- nemotron_ocr-2.0.0/src/nemotron_ocr_cpp/__init__.py +12 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nemotron-ocr
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Nemoton OCR
|
|
5
|
+
Project-URL: Homepage, https://nvidia.com/
|
|
6
|
+
Author: NVIDIA Nemotron
|
|
7
|
+
Requires-Python: <3.13,>=3.12
|
|
8
|
+
Requires-Dist: huggingface-hub>=0.20.0
|
|
9
|
+
Requires-Dist: numpy>=1.26.0
|
|
10
|
+
Requires-Dist: pillow>=12.0.0
|
|
11
|
+
Requires-Dist: shapely<3,>=2.1.2
|
|
12
|
+
Requires-Dist: torchvision~=0.26.0
|
|
13
|
+
Requires-Dist: torch~=2.11.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
load_png/wuffs-v0.3.c filter=lfs diff=lfs merge=lfs -text
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Optimized Image Operations for PyTorch
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
python setup.py install
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
# It's important that you do this first
|
|
13
|
+
import torch
|
|
14
|
+
from pytorch_image_ops import color_transform, spatial_transform
|
|
15
|
+
```
|
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
#include "beam_decode.h"
|
|
5
|
+
|
|
6
|
+
#include <vector>
|
|
7
|
+
#include <deque>
|
|
8
|
+
#include <limits>
|
|
9
|
+
#include <memory>
|
|
10
|
+
#include <unordered_set>
|
|
11
|
+
#include <set>
|
|
12
|
+
#include <algorithm>
|
|
13
|
+
#include <chrono>
|
|
14
|
+
|
|
15
|
+
#include "../common.h"
|
|
16
|
+
#include "prefix.h"
|
|
17
|
+
#include "log_sum_exp.h"
|
|
18
|
+
#include "sbo_lm.h"
|
|
19
|
+
|
|
20
|
+
using namespace std;
|
|
21
|
+
|
|
22
|
+
template<typename scalar_t>
|
|
23
|
+
using pred_seq_t = torch::TensorAccessor<scalar_t, 2>;
|
|
24
|
+
|
|
25
|
+
struct PrefixScore
|
|
26
|
+
{
|
|
27
|
+
float_t lProbBlank;
|
|
28
|
+
float_t lProbChar;
|
|
29
|
+
// float_t raw_lProbBlank;
|
|
30
|
+
// float_t raw_lProbChar;
|
|
31
|
+
mutable float_t _lProb;
|
|
32
|
+
|
|
33
|
+
PrefixScore(float_t lProbBlank = NEG_INF /* log P(0) */, float_t lProbChar = NEG_INF /* log P(0) */)
|
|
34
|
+
: lProbBlank(lProbBlank), lProbChar(lProbChar), _lProb(NEG_INF)
|
|
35
|
+
// , raw_lProbBlank(lProbBlank), raw_lProbChar(lProbChar)
|
|
36
|
+
{}
|
|
37
|
+
|
|
38
|
+
float_t get_lScore() const {
|
|
39
|
+
if (_lProb == NEG_INF) {
|
|
40
|
+
_lProb = log_sum_exp(lProbBlank, lProbChar);
|
|
41
|
+
}
|
|
42
|
+
return _lProb;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// float_t get_raw_lScore() const {
|
|
46
|
+
// return log_sum_exp(raw_lProbBlank, raw_lProbChar);
|
|
47
|
+
// }
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
typedef std::unordered_map<Prefix*, PrefixScore> PrefixMap;
|
|
51
|
+
typedef std::pair<Prefix*, PrefixScore> BeamItem;
|
|
52
|
+
typedef std::vector<BeamItem> Beam;
|
|
53
|
+
|
|
54
|
+
/*
|
|
55
|
+
Allows us to get an estimate of the vision model confidence, irrespective of how the language
|
|
56
|
+
model guided the decoding. NOTE: This scoring could follow an entirely different path than
|
|
57
|
+
the returned decoded sequence.
|
|
58
|
+
*/
|
|
59
|
+
template<typename scalar_t>
|
|
60
|
+
scalar_t get_vision_confidence(const pred_seq_t<scalar_t> &logProbs, scalar_t minProb)
|
|
61
|
+
{
|
|
62
|
+
const int64_t T = logProbs.size(0);
|
|
63
|
+
const int64_t S = logProbs.size(1);
|
|
64
|
+
|
|
65
|
+
scalar_t ret = 0; // log(1)
|
|
66
|
+
|
|
67
|
+
for (size_t t = 0; t < T; ++t) {
|
|
68
|
+
float_t maxP = logProbs[t][0];
|
|
69
|
+
int64_t maxC = 0;
|
|
70
|
+
for (int64_t c = 1; c < S; ++c) {
|
|
71
|
+
float_t p = logProbs[t][c];
|
|
72
|
+
if (p > maxP) {
|
|
73
|
+
maxP = p;
|
|
74
|
+
maxC = c;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
ret += maxP;
|
|
78
|
+
// Ignore everything past the sequence terminator
|
|
79
|
+
if (maxC == 1) {
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (ret < minProb) {
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return ret;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
template<typename scalar_t>
|
|
93
|
+
pair<vector<token_t>, float_t>
|
|
94
|
+
ctc_beam_decode_impl(const pred_seq_t<scalar_t> &probs, const int64_t beamSize,
|
|
95
|
+
const int64_t blank, scalar_t minProb,
|
|
96
|
+
const LanguageModel &langModel, scalar_t lmWeight)
|
|
97
|
+
{
|
|
98
|
+
if (blank != 0) {
|
|
99
|
+
throw runtime_error("Currently, only ordinal 0 supported for the blank prediction");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const int64_t T = probs.size(0);
|
|
103
|
+
const int64_t S = probs.size(1);
|
|
104
|
+
|
|
105
|
+
// NOTE: In log space, the following is true:
|
|
106
|
+
// 1. Adding two probabilities: log_sum_exp(l_p_a, l_p_b)
|
|
107
|
+
// 2. Multiplying two probabilities: l_p_a + l_p_b
|
|
108
|
+
// 3. log P(0) = -inf
|
|
109
|
+
// 4. log P(1) = 0
|
|
110
|
+
|
|
111
|
+
// Convert to log-space
|
|
112
|
+
if (minProb > 0) {
|
|
113
|
+
minProb = log(minProb);
|
|
114
|
+
} else {
|
|
115
|
+
minProb = NEG_INF;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
auto retScore = get_vision_confidence(probs, minProb);
|
|
119
|
+
|
|
120
|
+
if (retScore < minProb) {
|
|
121
|
+
return { {}, NEG_INF };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
PrefixAllocator prefixAlloc;
|
|
125
|
+
|
|
126
|
+
Beam beam;
|
|
127
|
+
beam.emplace_back(prefixAlloc.GetPrefix(), PrefixScore{0, NEG_INF}); // Add a dummy first node
|
|
128
|
+
|
|
129
|
+
Beam terminated;
|
|
130
|
+
|
|
131
|
+
typedef tuple<Prefix*, token_t> lm_cache_key_t;
|
|
132
|
+
unordered_map<lm_cache_key_t, float_t> lmScoreCache;
|
|
133
|
+
|
|
134
|
+
for (int64_t t = 0; t < T; ++t) {
|
|
135
|
+
PrefixMap nextBeam;
|
|
136
|
+
|
|
137
|
+
// Add all of the completed paths to the next beam.
|
|
138
|
+
// This allows us to accumulate new paths into these,
|
|
139
|
+
// but otherwise not process them
|
|
140
|
+
for (const BeamItem &prevNode : beam) {
|
|
141
|
+
if (prevNode.first->Token == 1) {
|
|
142
|
+
nextBeam.insert(prevNode);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Loop over vocab
|
|
147
|
+
for (int64_t s = 0; s < S; ++s) {
|
|
148
|
+
float_t lpEmit = probs[t][s];
|
|
149
|
+
|
|
150
|
+
if (lpEmit < minProb) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
for (const BeamItem &prevNode : beam) {
|
|
155
|
+
Prefix *prevPrefix = prevNode.first;
|
|
156
|
+
const PrefixScore &prevScore = prevNode.second;
|
|
157
|
+
|
|
158
|
+
// Ignore already completed paths
|
|
159
|
+
if (prevPrefix->Token == 1) {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Ignore impossible paths
|
|
164
|
+
if (prevScore.lProbBlank == NEG_INF && prevScore.lProbChar == NEG_INF) {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// If we propose a blank the prefix doesn't change.
|
|
169
|
+
// Only the probability of ending in blank gets updated.
|
|
170
|
+
if (s == blank) {
|
|
171
|
+
PrefixScore &score = nextBeam[prevPrefix];
|
|
172
|
+
score.lProbBlank = log_sum_exp(score.lProbBlank , prevScore.lProbBlank + lpEmit, prevScore.lProbChar + lpEmit);
|
|
173
|
+
// score.raw_lProbBlank = log_sum_exp(score.raw_lProbBlank, prevScore.raw_lProbBlank + lpEmit, prevScore.raw_lProbChar + lpEmit);
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Extend the prefix by the new character s and add it to the beam.
|
|
178
|
+
// Only the probability of not ending in blank gets updated.
|
|
179
|
+
token_t prevToken = prevPrefix->Token;
|
|
180
|
+
|
|
181
|
+
// NOTE: We always create a new prefix regardless of duplication because the PrefixScore
|
|
182
|
+
// is simultaneously tracking prefixes that do and don't end in a blank. And it's those
|
|
183
|
+
// that end in a blank that would cause the prefix to be extended.
|
|
184
|
+
auto extendPrefix = prefixAlloc.GetPrefix(s, prevPrefix);
|
|
185
|
+
|
|
186
|
+
// Evaluate the language model, but use the cache if we've already considered this string before
|
|
187
|
+
auto lmCacheItem = make_tuple(prevPrefix, s);
|
|
188
|
+
auto lmCacheIter = lmScoreCache.find(lmCacheItem);
|
|
189
|
+
float_t lpLang = 0;
|
|
190
|
+
if (lmCacheIter == lmScoreCache.end()) {
|
|
191
|
+
lpLang = langModel.ScoreTransition(prevPrefix, s);
|
|
192
|
+
lpLang *= lmWeight;
|
|
193
|
+
lmCacheIter = lmScoreCache.emplace(lmCacheItem, lpLang).first;
|
|
194
|
+
}
|
|
195
|
+
lpLang = lmCacheIter->second;
|
|
196
|
+
|
|
197
|
+
PrefixScore &extendScore = nextBeam[extendPrefix];
|
|
198
|
+
// Remember, adding two log probabilities is equivalent to multiplying two probabilities
|
|
199
|
+
if (s != prevToken) {
|
|
200
|
+
extendScore.lProbChar = log_sum_exp(extendScore.lProbChar, prevScore.lProbBlank + lpEmit + lpLang, prevScore.lProbChar + lpEmit + lpLang);
|
|
201
|
+
// extendScore.raw_lProbChar = log_sum_exp(extendScore.raw_lProbChar, prevScore.raw_lProbBlank + lpEmit , prevScore.raw_lProbChar + lpEmit );
|
|
202
|
+
} else {
|
|
203
|
+
// We don't include the previous probability of not ending in blank if s is repeated at the end. The CTC
|
|
204
|
+
// algorithm merges characters not separated by a blank.
|
|
205
|
+
extendScore.lProbChar = log_sum_exp(extendScore.lProbChar , prevScore.lProbBlank + lpEmit + lpLang);
|
|
206
|
+
// extendScore.raw_lProbChar = log_sum_exp(extendScore.raw_lProbChar, prevScore.raw_lProbBlank + lpEmit );
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// If the token is repeated, we also have to deal with the unchanged prefix since repeated characters are collapsed
|
|
210
|
+
if (s == prevToken) {
|
|
211
|
+
PrefixScore &collapseScore = nextBeam[prevPrefix];
|
|
212
|
+
collapseScore.lProbChar = log_sum_exp(collapseScore.lProbChar , prevScore.lProbChar + lpEmit);
|
|
213
|
+
// collapseScore.raw_lProbChar = log_sum_exp(collapseScore.raw_lProbChar, prevScore.raw_lProbChar + lpEmit);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
Beam vecNextBeam(begin(nextBeam), end(nextBeam));
|
|
220
|
+
|
|
221
|
+
if (vecNextBeam.size() > beamSize) {
|
|
222
|
+
partial_sort(begin(vecNextBeam), begin(vecNextBeam) + beamSize, end(vecNextBeam),
|
|
223
|
+
[] (const BeamItem &a, const BeamItem &b) {
|
|
224
|
+
return a.second.get_lScore() > b.second.get_lScore();
|
|
225
|
+
}
|
|
226
|
+
);
|
|
227
|
+
vecNextBeam.resize(beamSize);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
beam = move(vecNextBeam);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Find the best raw score
|
|
234
|
+
const BeamItem *bestItem = nullptr;
|
|
235
|
+
// for (const BeamItem &b : beam) {
|
|
236
|
+
// if (bestItem == nullptr or b.second.get_raw_lScore() > bestItem->second.get_raw_lScore()) {
|
|
237
|
+
// bestItem = &b;
|
|
238
|
+
// }
|
|
239
|
+
// }
|
|
240
|
+
if (! beam.empty()) {
|
|
241
|
+
bestItem = &beam[0];
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (bestItem != nullptr) {
|
|
245
|
+
auto retList = bestItem->first->ToList();
|
|
246
|
+
|
|
247
|
+
return { move(retList), retScore };
|
|
248
|
+
} else {
|
|
249
|
+
return { {}, NEG_INF };
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
typedef std::pair<Prefix*, float_t> RegBeamItem;
|
|
254
|
+
|
|
255
|
+
bool operator<(const RegBeamItem &a, const RegBeamItem &b) {
|
|
256
|
+
return a.second > b.second;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
template<typename scalar_t>
|
|
260
|
+
pair<vector<token_t>, float_t>
|
|
261
|
+
reg_beam_decode_impl(const pred_seq_t<scalar_t> &logProbs, const int64_t beamSize,
|
|
262
|
+
scalar_t minProb,
|
|
263
|
+
const LanguageModel &langModel, scalar_t lmWeight)
|
|
264
|
+
{
|
|
265
|
+
const int64_t T = logProbs.size(0);
|
|
266
|
+
const int64_t S = logProbs.size(1);
|
|
267
|
+
|
|
268
|
+
// NOTE: In log space, the following is true:
|
|
269
|
+
// 1. Adding two probabilities: log_sum_exp(l_p_a, l_p_b)
|
|
270
|
+
// 2. Multiplying two probabilities: l_p_a + l_p_b
|
|
271
|
+
// 3. log P(0) = -inf
|
|
272
|
+
// 4. log P(1) = 0
|
|
273
|
+
|
|
274
|
+
// Convert to log-space
|
|
275
|
+
if (minProb > 0) {
|
|
276
|
+
minProb = log(minProb);
|
|
277
|
+
} else {
|
|
278
|
+
minProb = NEG_INF;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
auto retScore = get_vision_confidence(logProbs, minProb);
|
|
282
|
+
|
|
283
|
+
if (retScore < minProb) {
|
|
284
|
+
return { {}, NEG_INF };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
PrefixAllocator prefixAlloc;
|
|
288
|
+
|
|
289
|
+
vector<RegBeamItem> beam, nextBeam;
|
|
290
|
+
beam.emplace_back(prefixAlloc.GetPrefix(), 0); // log(1) = 0
|
|
291
|
+
|
|
292
|
+
for (int64_t t = 0; t < T && !beam.empty(); ++t) {
|
|
293
|
+
nextBeam.clear();
|
|
294
|
+
|
|
295
|
+
auto addToBeam = [&nextBeam, beamSize] (const RegBeamItem &rbi) {
|
|
296
|
+
nextBeam.push_back(rbi);
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
// Expand each path in the beam
|
|
300
|
+
for (const RegBeamItem &prevNode : beam) {
|
|
301
|
+
if (prevNode.first->Token == 1) {
|
|
302
|
+
// Move completed paths along without processing further
|
|
303
|
+
addToBeam(prevNode);
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
Prefix *prevPrefix = prevNode.first;
|
|
308
|
+
float_t prevScore = prevNode.second;
|
|
309
|
+
|
|
310
|
+
// Loop over vocab
|
|
311
|
+
for (int64_t s = 0; s < S; ++s) {
|
|
312
|
+
float_t lpEmit = logProbs[t][s];
|
|
313
|
+
|
|
314
|
+
if (lpEmit < minProb) {
|
|
315
|
+
// The probability dropped below threshold, so stop processing this path
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
auto extendPrefix = prefixAlloc.GetPrefix(s, prevPrefix);
|
|
320
|
+
|
|
321
|
+
float_t lpLang = langModel.ScoreTransition(prevPrefix, s);
|
|
322
|
+
|
|
323
|
+
float_t lpNext = prevScore + lpLang + lpEmit;
|
|
324
|
+
|
|
325
|
+
addToBeam({extendPrefix, lpNext});
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (nextBeam.size() > beamSize) {
|
|
330
|
+
// Find the top-k items, and then truncate the rest
|
|
331
|
+
partial_sort(begin(nextBeam), begin(nextBeam) + beamSize, end(nextBeam));
|
|
332
|
+
nextBeam.resize(beamSize);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
std::swap(beam, nextBeam);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (!beam.empty()) {
|
|
339
|
+
// The highest probability element will always be in the back
|
|
340
|
+
RegBeamItem rbi{ nullptr, NEG_INF };
|
|
341
|
+
for (auto &rb : beam) {
|
|
342
|
+
if (rbi.first == nullptr || rb.second > rbi.second) {
|
|
343
|
+
rbi = rb;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
auto retList = rbi.first->ToList();
|
|
348
|
+
|
|
349
|
+
return { move(retList), retScore };
|
|
350
|
+
} else {
|
|
351
|
+
return { {}, NEG_INF };
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
template<typename scalar_t>
|
|
358
|
+
void dp_beam_decode_impl(const torch::TensorAccessor<scalar_t, 3> &probsAccess,
|
|
359
|
+
torch::TensorAccessor<int64_t, 2> retAccess,
|
|
360
|
+
torch::TensorAccessor<scalar_t, 1> confAccess,
|
|
361
|
+
int64_t beamSize, int64_t blank,
|
|
362
|
+
scalar_t minProb,
|
|
363
|
+
const LanguageModel *langModel,
|
|
364
|
+
scalar_t lmWeight,
|
|
365
|
+
bool combineDuplicates)
|
|
366
|
+
{
|
|
367
|
+
const int64_t N = probsAccess.size(0);
|
|
368
|
+
|
|
369
|
+
#pragma omp parallel for num_threads(8)
|
|
370
|
+
for (int64_t i = 0; i < N; ++i) {
|
|
371
|
+
vector<token_t> seq;
|
|
372
|
+
float_t lConf;
|
|
373
|
+
if (combineDuplicates) {
|
|
374
|
+
tie(seq, lConf) = ctc_beam_decode_impl(probsAccess[i], beamSize, blank,
|
|
375
|
+
minProb,
|
|
376
|
+
*langModel, lmWeight);
|
|
377
|
+
} else {
|
|
378
|
+
tie(seq, lConf) = reg_beam_decode_impl(probsAccess[i], beamSize,
|
|
379
|
+
minProb,
|
|
380
|
+
*langModel, lmWeight);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
int64_t sz = min<int64_t>(seq.size(), retAccess.size(1));
|
|
384
|
+
|
|
385
|
+
for (int64_t k = 0; k < sz; ++k) {
|
|
386
|
+
retAccess[i][k] = seq[k];
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
confAccess[i] = exp(lConf);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
std::tuple<torch::Tensor, torch::Tensor>
|
|
394
|
+
beam_decode(torch::Tensor probs, int64_t beamSize, int64_t blank,
|
|
395
|
+
float minProb,
|
|
396
|
+
const LanguageModel *langModel,
|
|
397
|
+
float lmWeight,
|
|
398
|
+
bool combineDuplicates)
|
|
399
|
+
{
|
|
400
|
+
if (langModel == nullptr) {
|
|
401
|
+
langModel = &NullLanguageModel;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
auto tStart = chrono::high_resolution_clock::now();
|
|
405
|
+
|
|
406
|
+
probs = probs.contiguous();
|
|
407
|
+
|
|
408
|
+
bool collapse = false;
|
|
409
|
+
if (probs.dim() == 2) {
|
|
410
|
+
// N,T,C
|
|
411
|
+
probs = probs.unsqueeze(0);
|
|
412
|
+
collapse = true;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
probs = probs.log();
|
|
416
|
+
|
|
417
|
+
torch::Tensor ret = torch::ones({ probs.size(0), probs.size(1) }, torch::kInt64);
|
|
418
|
+
torch::Tensor conf = torch::zeros({ probs.size(0) }, probs.options());
|
|
419
|
+
|
|
420
|
+
auto retAccess = ret.accessor<int64_t, 2>();
|
|
421
|
+
|
|
422
|
+
AT_DISPATCH_FLOATING_TYPES(
|
|
423
|
+
probs.scalar_type(),
|
|
424
|
+
"cpu_beam_decode",
|
|
425
|
+
([&] {
|
|
426
|
+
dp_beam_decode_impl(
|
|
427
|
+
probs.accessor<scalar_t, 3>(),
|
|
428
|
+
retAccess,
|
|
429
|
+
conf.accessor<scalar_t, 1>(),
|
|
430
|
+
beamSize, blank,
|
|
431
|
+
static_cast<scalar_t>(minProb),
|
|
432
|
+
langModel,
|
|
433
|
+
static_cast<scalar_t>(lmWeight),
|
|
434
|
+
combineDuplicates
|
|
435
|
+
);
|
|
436
|
+
})
|
|
437
|
+
);
|
|
438
|
+
|
|
439
|
+
if (collapse) {
|
|
440
|
+
ret = ret.squeeze(0);
|
|
441
|
+
conf = conf[0];
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
auto tEnd = chrono::high_resolution_clock::now();
|
|
445
|
+
|
|
446
|
+
typedef chrono::duration<double, std::milli> tp_t;
|
|
447
|
+
tp_t totalElapsed = tEnd - tStart;
|
|
448
|
+
|
|
449
|
+
cout << "Beam Decode " << probs.size(0) << " - "
|
|
450
|
+
<< "Total: " << totalElapsed.count() << "ms"
|
|
451
|
+
<< endl;
|
|
452
|
+
|
|
453
|
+
return { ret, conf };
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
std::unique_ptr<LanguageModel> create_sbo_lm(const std::string &dataFilePath, token_mapping_t tokenMapping, float_t backoffWeight)
|
|
457
|
+
{
|
|
458
|
+
return make_unique<SBO_LanguageModel>(dataFilePath, move(tokenMapping), backoffWeight);
|
|
459
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
#pragma once
|
|
5
|
+
|
|
6
|
+
#include <torch/torch.h>
|
|
7
|
+
|
|
8
|
+
#include "language_model.h"
|
|
9
|
+
|
|
10
|
+
std::tuple<torch::Tensor, torch::Tensor>
|
|
11
|
+
beam_decode(torch::Tensor probs, int64_t beamSize, int64_t blank,
|
|
12
|
+
float minProb,
|
|
13
|
+
const LanguageModel *langModel,
|
|
14
|
+
float lmWeight,
|
|
15
|
+
bool combineDuplicates);
|
|
16
|
+
|
|
17
|
+
std::unique_ptr<LanguageModel> create_sbo_lm(const std::string &dataFilePath, token_mapping_t tokenMapping, float_t backoffWeight);
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
#include "kn_lm.h"
|
|
5
|
+
|
|
6
|
+
using namespace std;
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
KN_LanguageModel::KN_LanguageModel(const string &dataFilePath, token_mapping_t tokenMapping, float_t knDelta)
|
|
10
|
+
: NGramLMBase(dataFilePath, move(tokenMapping)), m_knDelta(knDelta)
|
|
11
|
+
{
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
float KN_LanguageModel::ScoreTransitionImpl(const std::wstring &prefix, const std::wstring &suffix) const
|
|
15
|
+
{
|
|
16
|
+
if (prefix.empty()) {
|
|
17
|
+
return ScoreUnigram(suffix);
|
|
18
|
+
} else {
|
|
19
|
+
return ScoreTransition(prefix, suffix);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
float_t KN_LanguageModel::ScoreUnigram(const std::wstring &uni) const
|
|
24
|
+
{
|
|
25
|
+
auto lIter = m_lookup[1].find(L""s);
|
|
26
|
+
if (lIter == m_lookup[1].end()) {
|
|
27
|
+
throw std::runtime_error("Unigrams not supported by this model!");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
auto uniIter = lIter->second.find(uni);
|
|
31
|
+
float_t ctUni = 1e-8;
|
|
32
|
+
if (uniIter != lIter->second.end()) {
|
|
33
|
+
ctUni = uniIter->second;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
float_t ctSuffixes = GetPrefixSum(L""s);
|
|
37
|
+
|
|
38
|
+
return ctUni / ctSuffixes;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
float_t KN_LanguageModel::ScoreTransition(const std::wstring &prefix, const std::wstring &suffix) const
|
|
42
|
+
{
|
|
43
|
+
if (prefix.empty()) {
|
|
44
|
+
// The number of distinct bigrams that end with this token
|
|
45
|
+
auto rlIter = m_reverseLookup.find(suffix);
|
|
46
|
+
|
|
47
|
+
float_t ctEndingBigrams = 0;
|
|
48
|
+
if (rlIter != m_reverseLookup.end()) {
|
|
49
|
+
ctEndingBigrams = rlIter->second[2].size();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
float_t ctAllBigrams = m_lookup[2].size();
|
|
53
|
+
|
|
54
|
+
return ctEndingBigrams / ctAllBigrams;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
auto lIter = m_lookup[prefix.size() + 1].find(prefix);
|
|
58
|
+
float_t ctUqSuffixes = 0;
|
|
59
|
+
float_t ctSuffixes = 0;
|
|
60
|
+
float_t ctSuffix = 0;
|
|
61
|
+
if (lIter != m_lookup[prefix.size() + 1].end()) {
|
|
62
|
+
ctUqSuffixes = lIter->second.size();
|
|
63
|
+
|
|
64
|
+
ctSuffixes = GetPrefixSum(prefix);
|
|
65
|
+
|
|
66
|
+
auto sIter = lIter->second.find(suffix);
|
|
67
|
+
if (sIter != lIter->second.end()) {
|
|
68
|
+
ctSuffix = sIter->second;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
float_t factor = 0;
|
|
73
|
+
float_t main = 0;
|
|
74
|
+
if (ctSuffixes != 0) {
|
|
75
|
+
factor = m_knDelta * ctUqSuffixes / ctSuffixes;
|
|
76
|
+
// TODO: Figure out how to make this call without copying the string!
|
|
77
|
+
factor *= ScoreTransition({begin(prefix) + 1, end(prefix)}, suffix);
|
|
78
|
+
|
|
79
|
+
main = max<float_t>(ctSuffix - m_knDelta, 0) / ctSuffixes;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
float_t total = main + factor;
|
|
83
|
+
|
|
84
|
+
return total;
|
|
85
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
#pragma once
|
|
5
|
+
|
|
6
|
+
#include <unordered_map>
|
|
7
|
+
#include <vector>
|
|
8
|
+
|
|
9
|
+
#include "ngram_lm_base.h"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class KN_LanguageModel
|
|
13
|
+
: public NGramLMBase
|
|
14
|
+
{
|
|
15
|
+
public:
|
|
16
|
+
KN_LanguageModel(const std::string &dataFilePath, token_mapping_t tokenMapping, float_t knDelta);
|
|
17
|
+
|
|
18
|
+
protected:
|
|
19
|
+
virtual float_t ScoreTransitionImpl(const std::wstring &prefix, const std::wstring &suffix) const override;
|
|
20
|
+
|
|
21
|
+
private:
|
|
22
|
+
float_t ScoreUnigram(const std::wstring &uni) const;
|
|
23
|
+
float_t ScoreTransition(const std::wstring &prefix, const std::wstring &suffix) const;
|
|
24
|
+
|
|
25
|
+
float_t m_knDelta;
|
|
26
|
+
};
|