• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

krakjoe / ort / 18880448672

28 Oct 2025 03:38PM UTC coverage: 92.092% (-0.6%) from 92.707%
18880448672

push

github

krakjoe
[ci skip] clean checkouts

5159 of 5602 relevant lines covered (92.09%)

153022.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.99
/src/runtime.c
1
/*
2
 +----------------------------------------------------------------------+
3
 | ort                                                                  |
4
 +----------------------------------------------------------------------+
5
 | Copyright (c) Joe Watkins 2025                                       |
6
 +----------------------------------------------------------------------+
7
 | This source file is subject to version 3.01 of the PHP license,      |
8
 | that is bundled with this package in the file LICENSE, and is        |
9
 | available through the world-wide-web at the following url:           |
10
 | http://www.php.net/license/3_01.txt                                  |
11
 | If you did not receive a copy of the PHP license and are unable to   |
12
 | obtain it through the world-wide-web, please send a note to          |
13
 | license@php.net so we can mail you a copy immediately.               |
14
 +----------------------------------------------------------------------+
15
 | Author: krakjoe                                                      |
16
 +----------------------------------------------------------------------+
17
*/
18

19
#include "ort.h"
20
#include "env.h"
21
#include "tensor.h"
22
#include "status.h"
23
#include "runtime.h"
24
#include "options.h"
25

26
zend_class_entry* php_ort_runtime_ce;
27
zend_object_handlers php_ort_runtime_handlers;
28

29
static zend_object* php_ort_runtime_create(zend_class_entry *type) {
88✔
30
    php_ort_runtime_t* ort = ecalloc(1, 
88✔
31
        sizeof(php_ort_runtime_t) + zend_object_properties_size(type));
32

33
    zend_object_std_init(
88✔
34
        &ort->std, php_ort_runtime_ce);
35

36
    ort->std.handlers = &php_ort_runtime_handlers;
88✔
37

38
    return &ort->std;
88✔
39
}
40

41
static void php_ort_runtime_destroy(zend_object* zo) {
88✔
42
    php_ort_runtime_t* ort =
88✔
43
        php_ort_runtime_fetch(zo);
88✔
44

45
    if (ort->session) {
88✔
46
        api->ReleaseSession(ort->session);
88✔
47
    }
48

49
    if (ort->model) {
88✔
50
        php_ort_model_release(ort->model);
88✔
51
    }
52

53
    if (ort->options) {
88✔
54
        php_ort_options_release(ort->options);
88✔
55
    }
56

57
    zend_object_std_dtor(zo);
88✔
58
}
88✔
59

60
ZEND_BEGIN_ARG_INFO_EX(php_ort_runtime_construct_arginfo, 0, 0, 1)
61
    ZEND_ARG_OBJ_INFO(0, model,   \\ORT\\Model,   0)
62
    ZEND_ARG_OBJ_INFO(0, options, \\ORT\\Options, 1)
63
ZEND_END_ARG_INFO()
64

65
static void php_ort_runtime_from_file(php_ort_runtime_t *ort) {
88✔
66
    php_ort_string_temp_t pass =
88✔
67
        php_ort_string_pass(
88✔
68
            ZSTR_VAL(ort->model->source.file));
88✔
69

70
    OrtStatus* status =
88✔
71
        api->CreateSession(
144✔
72
            php_ort_environment(),
88✔
73
            pass,
74
            ort->options->options,
88✔
75
            &ort->session);
76

77
    php_ort_status_flow(
88✔
78
        (status),
79
        {
80
            php_ort_string_free(pass);  
81
            return;
82
        },
83
        php_ort_status_error_ce,
84
        "could not start a runtime session for Model %s: %s",
85
        ZSTR_VAL(ort->model->name),
86
        api->GetErrorMessage(status));
87

88
    php_ort_string_free(pass);
89
}
90

91
static void php_ort_runtime_from_array(php_ort_runtime_t *ort) {
×
92
    OrtStatus* status =
×
93
        api->CreateSessionFromArray(
×
94
            php_ort_environment(),
×
95
            ZSTR_VAL(ort->model->source.memory),
×
96
            ZSTR_LEN(ort->model->source.memory),
97
            ort->options->options, &ort->session);
×
98

99
    php_ort_status_flow(
×
100
        (status),
101
        {
102
            api->ReleaseStatus(status);
103
        },
104
        php_ort_status_model_invalidmemory_ce,
105
        "could not start a runtime session for Model %s: %s",
106
        ZSTR_VAL(ort->model->name),
107
        api->GetErrorMessage(status));
108
}
×
109

110
PHP_METHOD(ONNX_Runtime, __construct)
88✔
111
{
112
    php_ort_runtime_t* ort = php_ort_runtime_fetch(Z_OBJ(EX(This)));
88✔
113
    zend_object *zm, *zo = NULL;
88✔
114

115
    ZEND_PARSE_PARAMETERS_START(1, 2)
88✔
116
        Z_PARAM_OBJ_OF_CLASS_EX(zm, php_ort_model_ce, 1, 0)
88✔
117
        Z_PARAM_OPTIONAL
88✔
118
        Z_PARAM_OBJ_OF_CLASS_EX(zo, php_ort_options_ce, 0, 0)
88✔
119
    ZEND_PARSE_PARAMETERS_END();
22✔
120

121
    php_ort_model_t* model =
88✔
122
        php_ort_model_fetch(zm);
88✔
123

124
    ort->model =
88✔
125
        model->object;
88✔
126

127
    php_ort_atomic_addref(
88✔
128
        &ort->model->refcount);
129

130
    if (!zo) {
88✔
131
        ort->options = php_ort_options_default();
66✔
132

133
        php_ort_atomic_addref(
66✔
134
            &ort->options->refcount);
135

136
        goto __php_ort_runtime_construct;
66✔
137
    }
138

139
    php_ort_options_t* options =
22✔
140
        php_ort_options_fetch(zo);
22✔
141

142
    ort->options =
22✔
143
        options->object;
22✔
144

145
    php_ort_atomic_addref(
22✔
146
        &ort->options->refcount);
147

148
__php_ort_runtime_construct:
88✔
149
    if (ort->model->kind == ORT_MODEL_SOURCE_FILE) {
88✔
150
        php_ort_runtime_from_file(ort);
88✔
151
    } else {
152
        php_ort_runtime_from_array(ort);
×
153
    }
154
}
155

156
ZEND_BEGIN_ARG_INFO_EX(php_ort_runtime_run_arginfo, 0, 0, 1)
157
     ZEND_ARG_TYPE_INFO(0, input, IS_ARRAY, 0)
158
ZEND_END_ARG_INFO()
159

160
PHP_METHOD(ONNX_Runtime, run)
22✔
161
{
162
    php_ort_runtime_t* ort = php_ort_runtime_fetch(Z_OBJ(EX(This)));
22✔
163
    zval *input;
22✔
164

165
    ZEND_PARSE_PARAMETERS_START(1, 1);
22✔
166
        Z_PARAM_ARRAY(input)
22✔
167
    ZEND_PARSE_PARAMETERS_END();
×
168

169
    size_t length = zend_hash_num_elements(Z_ARRVAL_P(input));
22✔
170
    char** names = pecalloc(length, sizeof(char*), 0);
22✔
171
    OrtValue** values = pecalloc(length, sizeof(OrtValue*), 0);
22✔
172

173
    uint32_t idx = 0;
22✔
174
    zend_string* name;
22✔
175
    zval* next;
22✔
176

177
    ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(input), name, next){
66✔
178
        php_ort_status_flow(
44✔
179
            ((!name) || (
180
                Z_TYPE_P(next) != IS_OBJECT || 
181
                !instanceof_function(Z_OBJCE_P(next), php_ort_tensor_interface_ce))),
182
            {
183
                pefree(names, 0);
184
                pefree(values, 0);
185
                return;
186
            },
187
            php_ort_status_model_invalidinput_ce,
188
            "input is required to be an associative array of name => Tensor");
189

190
        names[idx] = (char*) ZSTR_VAL(name);
44✔
191
        values[idx] = php_ort_tensor_value(
44✔
192
            php_ort_tensor_fetch(Z_OBJ_P(next)));
193

194
        idx++;
44✔
195
    } ZEND_HASH_FOREACH_END();
196

197
    OrtStatus* status;
22✔
198
    OrtValue** outputs = pecalloc(
22✔
199
        ort->model->counters.output, sizeof(OrtValue*), 0);
200

201
    php_ort_status_flow(
22✔
202
        (status = api->Run(
203
            ort->session, NULL,
204
            (const char* const*)
205
                names, 
206
            (const OrtValue* const*)
207
                values, 
208
            idx,
209
            (const char* const*)
210
                ort->model->names.output,
211
            ort->model->counters.output,
212
            outputs)),
213
        {
214
            for (idx = 0; idx <
215
                    zend_hash_num_elements(Z_ARRVAL_P(input)); idx++) {
216
                api->ReleaseValue(values[idx]);
217
            }
218

219
            pefree(outputs, 0);
220
            pefree(values, 0);
221
            pefree(names, 0);
222

223
            api->ReleaseStatus(status);
224
            return;
225
        },
226
        php_ort_status_model_runtimeexception_ce,
227
        "failed to run inference on %s: %s",
228
            ZSTR_VAL(ort->model->name),
229
            api->GetErrorMessage(status));
230

231
    for (idx = 0; idx < 
66✔
232
            zend_hash_num_elements(Z_ARRVAL_P(input)); idx++) {
66✔
233
        api->ReleaseValue(values[idx]);
44✔
234
    }
235

236
    array_init_size(return_value, ort->model->counters.output);
22✔
237
    for (idx = 0; idx < ort->model->counters.output; idx++) {
44✔
238
        zval zv;
22✔
239

240
        object_init_ex(&zv, php_ort_tensor_transient_ce);
22✔
241

242
        add_assoc_zval(return_value,
44✔
243
            ort->model->names.output[idx], &zv);
22✔
244

245
        php_ort_tensor_t *value = 
22✔
246
            php_ort_tensor_fetch(Z_OBJ(zv));
22✔
247
        value->object = 
22✔
248
            php_ort_tensor_object(outputs[idx]);
22✔
249
    }
250

251
    pefree(outputs, 0);
22✔
252
    pefree(values, 0);
22✔
253
    pefree(names, 0);
22✔
254
}
255

256
zend_function_entry php_ort_runtime_methods[] = {
257
    PHP_ME(ONNX_Runtime, __construct,       php_ort_runtime_construct_arginfo, ZEND_ACC_PUBLIC)
258
    PHP_ME(ONNX_Runtime, run,               php_ort_runtime_run_arginfo,       ZEND_ACC_PUBLIC)
259
    PHP_FE_END
260
};
261

262
PHP_MINIT_FUNCTION(ORT_RUNTIME)
4,620✔
263
{
264
    zend_class_entry ce;
4,620✔
265

266
    INIT_NS_CLASS_ENTRY(ce, "ORT", "Runtime", php_ort_runtime_methods);
4,620✔
267

268
    php_ort_runtime_ce = zend_register_internal_class(&ce);
4,620✔
269
    php_ort_runtime_ce->create_object = php_ort_runtime_create;
4,620✔
270

271
    memcpy(&php_ort_runtime_handlers,
4,620✔
272
        zend_get_std_object_handlers(), sizeof(zend_object_handlers));
273
    php_ort_runtime_handlers.offset    = XtOffsetOf(php_ort_runtime_t, std);
4,620✔
274
    php_ort_runtime_handlers.free_obj  = php_ort_runtime_destroy;
4,620✔
275
    php_ort_runtime_handlers.clone_obj = NULL;
4,620✔
276

277
    return SUCCESS;
4,620✔
278
}
279

280
PHP_MSHUTDOWN_FUNCTION(ORT_RUNTIME)
4,620✔
281
{
282
    return SUCCESS;
4,620✔
283
}
284

285
PHP_RINIT_FUNCTION(ORT_RUNTIME)
4,620✔
286
{
287
    return SUCCESS;
4,620✔
288
}
289

290
PHP_RSHUTDOWN_FUNCTION(ORT_RUNTIME)
4,620✔
291
{
292
    return SUCCESS;
4,620✔
293
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc