Run this model with HF Jobs

Run OpenTensorLab/OCRia-1-W4A16-AutoRound on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.

Paste one of these prompts into your coding agent:

Fine-tune on my dataset

Use HF Jobs and TRL to fine-tune https://huggingface.co/OpenTensorLab/OCRia-1-W4A16-AutoRound. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.

Run batch inference

Use HF Jobs to run batch inference with https://huggingface.co/OpenTensorLab/OCRia-1-W4A16-AutoRound on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.

Evaluate on a benchmark

Use HF Jobs to evaluate https://huggingface.co/OpenTensorLab/OCRia-1-W4A16-AutoRound. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
File size: 8,375 Bytes
90d9faa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Copyright (c) 2026 LlamaIndex, Inc. All rights reserved.
// Licensed under the OCRia-1 Model and Code License (LICENSE at the root of this repository).
// This is not open-source software: see the license for permitted uses.
mod density_kernels;
mod geometry_kernels;
use std::slice;
unsafe fn peak_features(
    n: usize,
    width: i64,
    idx: *const i64,
    val: *const f64,
    order: *const i64,
    context: *const f64,
    peak: *const f64,
    logs: *const f64,
    retained: *mut i64,
    out: *mut f64,
) -> usize {
    let ix = slice::from_raw_parts(idx, n);
    let v = slice::from_raw_parts(val, n);
    let order = slice::from_raw_parts(order, n);
    let ctx = slice::from_raw_parts(context, 53);
    let peak = slice::from_raw_parts(peak, 2);
    let logs = slice::from_raw_parts(logs, 5);
    let retained = slice::from_raw_parts_mut(retained, 4);
    let out = slice::from_raw_parts_mut(out, 88);
    let xy: Vec<(f64, f64)> = ix
        .iter()
        .map(|i| ((i % width) as f64 + 0.5, (i / width) as f64 + 0.5))
        .collect();
    let mut count = 0;
    for &j in order {
        let j = j as usize;
        if (0..count).any(|k| {
            let p = xy[retained[k] as usize];
            (xy[j].0 - p.0).powi(2) + (xy[j].1 - p.1).powi(2) < 2.25
        }) {
            continue;
        }
        retained[count] = j as i64;
        count += 1;
        if count == 4 {
            break;
        }
    }
    let total = v.iter().sum::<f64>();
    let maximum = v.iter().copied().fold(f64::NEG_INFINITY, f64::max);
    for k in 0..count {
        let j = retained[k] as usize;
        let p = xy[j];
        let dx = p.0 - peak[0];
        let dy = p.1 - peak[1];
        let nearby_mass: f64 = (0..n)
            .filter(|&i| (xy[i].0 - p.0).abs() <= 1.0 && (xy[i].1 - p.1).abs() <= 1.0)
            .map(|i| v[i])
            .sum();
        let r = &mut out[k * 22..(k + 1) * 22];
        r[0] = k as f64;
        r[1] = v[j] / maximum;
        r[2] = nearby_mass / total;
        r[3] = logs[0];
        r[4] = dx;
        r[5] = dy;
        for h in 0..4 {
            let c = 34 + h * 4;
            let t = 6 + h * 4;
            r[t] = if ctx[c + 3] != 0.0 {
                (ctx[c] - dx).clamp(-30.0, 30.0)
            } else {
                0.0
            };
            r[t + 1] = if ctx[c + 3] != 0.0 {
                (ctx[c + 1] - dy).clamp(-30.0, 30.0)
            } else {
                0.0
            };
            r[t + 2] = logs[h + 1];
            r[t + 3] = ctx[c + 3];
        }
    }
    count
}
// Python validates nonempty matching sparse arrays and contiguous buffers.
// Offsets index flattened cells; contexts have 53 columns, outputs four x 22.
// Caller owns all buffers for the duration of this synchronous call.
#[no_mangle]
pub unsafe extern "C" fn page_peak_features(
    words: usize,
    width: i64,
    offsets: *const i64,
    idx: *const i64,
    val: *const f64,
    order: *const i64,
    context: *const f64,
    peaks: *const f64,
    logs: *const f64,
    retained: *mut i64,
    out: *mut f64,
    counts: *mut i64,
) {
    let offsets = slice::from_raw_parts(offsets, words + 1);
    for w in 0..words {
        let start = offsets[w] as usize;
        let n = (offsets[w + 1] - offsets[w]) as usize;
        *counts.add(w) = peak_features(
            n,
            width,
            idx.add(start),
            val.add(start),
            order.add(start),
            context.add(w * 53),
            peaks.add(w * 2),
            logs.add(w * 5),
            retained.add(w * 4),
            out.add(w * 88),
        ) as i64;
    }
}

// Caller validates sparse lengths and supplies matching NumPy sort orders.
// Output contains ten quantile offsets and twelve threshold edge offsets per word.
#[no_mangle]
pub unsafe extern "C" fn context_bounds(
    words: usize,
    width: i64,
    offsets: *const i64,
    indices: *const i64,
    attention: *const f64,
    mass: *const f64,
    orders_x: *const i64,
    orders_y: *const i64,
    peaks: *const f64,
    output: *mut f64,
) {
    let offsets = slice::from_raw_parts(offsets, words + 1);
    for w in 0..words {
        let start = offsets[w] as usize;
        let n = (offsets[w + 1] - offsets[w]) as usize;
        let ix = slice::from_raw_parts(indices.add(start), n);
        let a = slice::from_raw_parts(attention.add(start), n);
        let m = slice::from_raw_parts(mass.add(start), n);
        let p = slice::from_raw_parts(peaks.add(w * 2), 2);
        let out = slice::from_raw_parts_mut(output.add(w * 22), 22);
        for axis in 0..2 {
            let order = slice::from_raw_parts(
                if axis == 0 {
                    orders_x.add(start)
                } else {
                    orders_y.add(start)
                },
                n,
            );
            let mut cdf = Vec::with_capacity(n);
            let mut total = 0.0;
            for &j in order {
                total += m[j as usize];
                cdf.push(total);
            }
            for (q, &v) in [0.05, 0.25, 0.5, 0.75, 0.95].iter().enumerate() {
                let k = cdf.partition_point(|&x| x < v).min(n - 1);
                let cell = ix[order[k] as usize];
                let coordinate = if axis == 0 {
                    cell % width
                } else {
                    cell / width
                };
                out[axis * 5 + q] = coordinate as f64 + 0.5 - p[axis];
            }
        }
        let maximum = a.iter().copied().fold(f64::NEG_INFINITY, f64::max);
        for (t, &threshold) in [0.1, 0.3, 0.6].iter().enumerate() {
            let mut minx = i64::MAX;
            let mut miny = i64::MAX;
            let mut maxx = i64::MIN;
            let mut maxy = i64::MIN;
            for j in 0..n {
                if a[j] >= threshold * maximum {
                    let x = ix[j] % width;
                    let y = ix[j] / width;
                    minx = minx.min(x);
                    miny = miny.min(y);
                    maxx = maxx.max(x);
                    maxy = maxy.max(y);
                }
            }
            out[10 + t * 4] = minx as f64 - p[0];
            out[11 + t * 4] = miny as f64 - p[1];
            out[12 + t * 4] = maxx as f64 + 1.0 - p[0];
            out[13 + t * 4] = maxy as f64 + 1.0 - p[1];
        }
    }
}

// Python supplies nonempty sparse rows and optional anchor positions (-1 means argmax).
// Each output row has 34 features and a separate two-coordinate peak center.
#[no_mangle]
pub unsafe extern "C" fn local_features_unsorted(
    words: usize,
    height: i64,
    width: i64,
    offsets: *const i64,
    indices: *const i64,
    values: *const f64,
    lengths: *const i64,
    anchors: *const i64,
    output: *mut f64,
    peaks: *mut f64,
) {
    let offsets = slice::from_raw_parts(offsets, words + 1);
    for w in 0..words {
        let start = offsets[w] as usize;
        let n = (offsets[w + 1] - offsets[w]) as usize;
        let ix = slice::from_raw_parts(indices.add(start), n);
        let v = slice::from_raw_parts(values.add(start), n);
        let out = slice::from_raw_parts_mut(output.add(w * 34), 34);
        out.fill(0.0);
        let maximum = v.iter().copied().fold(f64::NEG_INFINITY, f64::max);
        let total = v.iter().sum::<f64>();
        let anchor = *anchors.add(w);
        let peak = if anchor >= 0 {
            anchor as usize
        } else {
            v.iter().position(|&value| value == maximum).unwrap()
        };
        let x = ix[peak] % width;
        let y = ix[peak] / width;
        out[0] = *lengths.add(w) as f64;
        out[1] = height as f64;
        out[2] = width as f64;
        out[3] = maximum;
        out[4] = total;
        *peaks.add(w * 2) = x as f64 + 0.5;
        *peaks.add(w * 2 + 1) = y as f64 + 0.5;
        for j in 0..n {
            let dx = ix[j] % width - x;
            let dy = ix[j] / width - y;
            if dx.abs() <= 2 && dy.abs() <= 2 {
                out[9 + ((dy + 2) * 5 + dx + 2) as usize] = v[j] / maximum;
            }
            let mass = v[j] / total;
            let cx = dx.clamp(-5, 5) as f64;
            let cy = dy.clamp(-5, 5) as f64;
            out[5] += mass * cx;
            out[6] += mass * cy;
            out[7] += mass * (cx * cx);
            out[8] += mass * (cy * cy);
        }
    }
}