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

krakjoe / ort / 16250982398

13 Jul 2025 04:05PM UTC coverage: 92.405% (-0.2%) from 92.643%
16250982398

push

github

krakjoe
softmax migrate

1 of 1 new or added line in 1 file covered. (100.0%)

29 existing lines in 2 files now uncovered.

5134 of 5556 relevant lines covered (92.4%)

74838.17 hits per line

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

87.0
/src/maths.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 "status.h"
21

22
#include "maths.h"
23
#include "maths/api.h"
24
#include "maths/result.h"
25
#include "maths/promotion.h"
26

27
#include "maths/schema/add.h"
28
#include "maths/schema/div.h"
29
#include "maths/schema/dot.h"
30
#include "maths/schema/matmul.h"
31
#include "maths/schema/max.h"
32
#include "maths/schema/mean.h"
33
#include "maths/schema/min.h"
34
#include "maths/schema/mod.h"
35
#include "maths/schema/mul.h"
36
#include "maths/schema/neg.h"
37
#include "maths/schema/pow.h"
38
#include "maths/schema/recip.h"
39
#include "maths/schema/sign.h"
40
#include "maths/schema/sqrt.h"
41
#include "maths/schema/sub.h"
42
#include "maths/schema/sum.h"
43

44
typedef struct _php_ort_math_schema_t {
45
    const ort_math_type_promotion_schema_t* schema;
46
    zend_string*                            symbol;
47
    zend_object std;
48
} php_ort_math_schema_t;
49

50
zend_class_entry *php_ort_math_schema_ce;
51
zend_object_handlers php_ort_math_schema_handlers;
52

53
static zend_always_inline php_ort_math_schema_t* php_ort_math_schema_fetch(zend_object *o) {
54
    return (php_ort_math_schema_t*) (((char*) o) - XtOffsetOf(php_ort_math_schema_t, std));
55
}
56

57
static zend_object* php_ort_math_schema_create(zend_class_entry *ce) {
304✔
58
    php_ort_math_schema_t *ort = 
304✔
59
        zend_object_alloc(sizeof(php_ort_math_schema_t), ce);
304✔
60
        
61
    zend_object_std_init(&ort->std, ce);
304✔
62
    object_properties_init(&ort->std, ce);
304✔
63

64
    ort->std.handlers = &php_ort_math_schema_handlers;
304✔
65
    return &ort->std;
304✔
66
}
67

68
static HashTable* php_ort_math_schema_debug(zend_object *zo, int *temp) {
×
69
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(zo);
×
UNCOV
70
    HashTable *debug;
×
71

72
    ALLOC_HASHTABLE(debug);
×
UNCOV
73
    zend_hash_init(debug, 3, NULL, ZVAL_PTR_DTOR, 0);
×
74

75
    if (ort->symbol) {
×
UNCOV
76
        zval symbol;
×
77

UNCOV
78
        ZVAL_STR_COPY(&symbol, ort->symbol);
×
79

UNCOV
80
        zend_hash_str_update(debug,
×
81
            "symbol", sizeof("symbol") - 1,
82
            &symbol);
83
    }
84

85
__php_ort_tensor_debug_return:
×
UNCOV
86
    *temp = 1;
×
87

UNCOV
88
    return debug;
×
89
}
90

91
void php_ort_math_schema_free(zend_object *zo) {
304✔
92
    php_ort_math_schema_t* ort = php_ort_math_schema_fetch(zo);
304✔
93

94
    if (ort->symbol) {
304✔
95
        zend_string_release(ort->symbol);
304✔
96
    }
97

98
    zend_object_std_dtor(zo);
304✔
99
}
304✔
100

101
#define ORT_MATH_UNARY_FUNCTION_IMPL(fname)                    \
102
    ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(                    \
103
        php_ort_math_##fname##_arginfo, 0, 1, ONNX\\Tensor, 0) \
104
        ZEND_ARG_OBJ_INFO(0, tensor, ONNX\\Tensor, 0)          \
105
    ZEND_END_ARG_INFO()                                        \
106
    PHP_FUNCTION(fname)                                        \
107
    {                                                          \
108
        zval *tensor_zv;                                       \
109
        ZEND_PARSE_PARAMETERS_START(1, 1)                      \
110
            Z_PARAM_OBJECT_OF_CLASS(tensor_zv,                 \
111
                php_ort_tensor_interface_ce)                   \
112
        ZEND_PARSE_PARAMETERS_END();                           \
113
        php_ort_tensor_t* tensor_ort =                         \
114
            php_ort_tensor_fetch(Z_OBJ_P(tensor_zv));          \
115
        ort_tensor_t* result =                                 \
116
            ort_math_result_##fname(tensor_ort->object);       \
117
        object_init_ex(return_value,                           \
118
            php_ort_tensor_transient_ce);                      \
119
        php_ort_tensor_t* rv =                                 \
120
            php_ort_tensor_fetch(Z_OBJ_P(return_value));       \
121
        rv->object = result;                                   \
122
    }
123

124
#define ORT_MATH_BINARY_FUNCTION_IMPL(fname)                    \
125
    ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(                     \
126
        php_ort_math_##fname##_arginfo, 0, 2, ONNX\\Tensor, 0)  \
127
        ZEND_ARG_OBJ_INFO(0, tensor_a, ONNX\\Tensor, 0)         \
128
        ZEND_ARG_INFO(0, tensor_b_or_scalar)                    \
129
    ZEND_END_ARG_INFO()                                         \
130
    PHP_FUNCTION(fname)                                         \
131
    {                                                           \
132
        zval *tensor_a_zv, *tensor_b_or_scalar;                 \
133
        ZEND_PARSE_PARAMETERS_START(2, 2)                       \
134
            Z_PARAM_OBJECT_OF_CLASS(tensor_a_zv,                \
135
                php_ort_tensor_interface_ce)                    \
136
            Z_PARAM_ZVAL(tensor_b_or_scalar)                    \
137
        ZEND_PARSE_PARAMETERS_END();                            \
138
        php_ort_tensor_t* tensor_a_ort =                        \
139
            php_ort_tensor_fetch(Z_OBJ_P(tensor_a_zv));         \
140
        ort_tensor_t* result = NULL;                            \
141
        if (Z_TYPE_P(tensor_b_or_scalar) == IS_OBJECT &&        \
142
            instanceof_function(Z_OBJCE_P(tensor_b_or_scalar),  \
143
                php_ort_tensor_interface_ce)) {                 \
144
            php_ort_tensor_t* tensor_b_ort =                    \
145
                php_ort_tensor_fetch(Z_OBJ_P(tensor_b_or_scalar)); \
146
            result = ort_math_result_##fname(                   \
147
                tensor_a_ort->object, tensor_b_ort->object);    \
148
        } else if (Z_TYPE_P(tensor_b_or_scalar) == IS_LONG ||   \
149
                   Z_TYPE_P(tensor_b_or_scalar) == IS_DOUBLE || \
150
                   (Z_TYPE_P(tensor_b_or_scalar) == IS_TRUE ||  \
151
                    Z_TYPE_P(tensor_b_or_scalar) == IS_FALSE)) { \
152
            result = ort_math_result_##fname##_scalar(          \
153
                tensor_a_ort->object, tensor_b_or_scalar);      \
154
        } else {                                                \
155
            php_ort_status_throw(                               \
156
                php_ort_status_math_invalidtype_ce,             \
157
                #fname ": second argument must be a Tensor or numeric value"); \
158
            return;                                             \
159
        }                                                       \
160
        object_init_ex(return_value,                            \
161
            php_ort_tensor_transient_ce);                       \
162
        php_ort_tensor_t* rv =                                  \
163
            php_ort_tensor_fetch(Z_OBJ_P(return_value));        \
164
        rv->object = result;                                    \
165
    }
166

167

168
#define ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(fname)             \
169
    ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(                        \
170
            php_ort_math_reduce_tensor_##fname##_arginfo,          \
171
                0, 1, ONNX\\Tensor, 0)                             \
172
        ZEND_ARG_OBJ_INFO(0, tensor, ONNX\\Tensor, 0)              \
173
    ZEND_END_ARG_INFO()                                            \
174
    PHP_NAMED_FUNCTION(php_ort_math_reduce_tensor_##fname)         \
175
    {                                                              \
176
        zval *tensor_zv;                                           \
177
        ZEND_PARSE_PARAMETERS_START(1, 1)                          \
178
            Z_PARAM_OBJECT_OF_CLASS(tensor_zv,                     \
179
                php_ort_tensor_interface_ce)                       \
180
        ZEND_PARSE_PARAMETERS_END();                               \
181
        php_ort_tensor_t* tensor_ort =                             \
182
            php_ort_tensor_fetch(Z_OBJ_P(tensor_zv));              \
183
        ort_tensor_t* result =                                     \
184
            ort_math_result_reduce_tensor_##fname(                 \
185
                tensor_ort->object);                               \
186
        object_init_ex(return_value,                               \
187
            php_ort_tensor_transient_ce);                          \
188
        php_ort_tensor_t* rv =                                     \
189
            php_ort_tensor_fetch(Z_OBJ_P(return_value));           \
190
        rv->object = result;                                       \
191
    }
192

193
// Macro for reduction functions (Tensor, axis, keepdims)
194
#define ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(fname)               \
195
    ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(                        \
196
            php_ort_math_reduce_axis_##fname##_arginfo, 0, 2, ONNX\\Tensor, 0) \
197
        ZEND_ARG_OBJ_INFO(0, tensor, ONNX\\Tensor, 0)              \
198
        ZEND_ARG_TYPE_INFO(0, axis, IS_LONG, 0)                    \
199
        ZEND_ARG_TYPE_INFO(0, keepdims, _IS_BOOL, 0)               \
200
    ZEND_END_ARG_INFO()                                            \
201
    PHP_NAMED_FUNCTION(php_ort_math_reduce_axis_##fname)           \
202
    {                                                              \
203
        zval *tensor_zv;                                           \
204
        zend_long axis = 0;                                        \
205
        zend_bool keepdims = 0;                                    \
206
        ZEND_PARSE_PARAMETERS_START(2, 3)                          \
207
            Z_PARAM_OBJECT_OF_CLASS(tensor_zv,                     \
208
                php_ort_tensor_interface_ce)                       \
209
            Z_PARAM_LONG(axis)                                     \
210
            Z_PARAM_OPTIONAL                                       \
211
            Z_PARAM_BOOL(keepdims)                                 \
212
        ZEND_PARSE_PARAMETERS_END();                               \
213
        php_ort_tensor_t* tensor_ort =                             \
214
            php_ort_tensor_fetch(Z_OBJ_P(tensor_zv));              \
215
        ort_tensor_t* result = ort_math_result_reduce_axis_##fname(\
216
            tensor_ort->object, axis, keepdims);                   \
217
        object_init_ex(return_value,                               \
218
            php_ort_tensor_transient_ce);                          \
219
        php_ort_tensor_t* rv =                                     \
220
            php_ort_tensor_fetch(Z_OBJ_P(return_value));           \
221
        rv->object = result;                                       \
222
    }
223
/* Mathematical functions in ONNX\Math namespace */
224

225
ORT_MATH_BINARY_FUNCTION_IMPL(add)
16,896✔
226
ORT_MATH_BINARY_FUNCTION_IMPL(multiply)
4,112✔
227
ORT_MATH_BINARY_FUNCTION_IMPL(subtract)
3,088✔
228
ORT_MATH_BINARY_FUNCTION_IMPL(divide)
3,456✔
229

230
ORT_MATH_BINARY_FUNCTION_IMPL(pow)
2,448✔
231
ORT_MATH_BINARY_FUNCTION_IMPL(mod)
2,496✔
232

233
ORT_MATH_UNARY_FUNCTION_IMPL(sin)
736✔
234
ORT_MATH_UNARY_FUNCTION_IMPL(cos)
736✔
235
ORT_MATH_UNARY_FUNCTION_IMPL(tan)
736✔
236
ORT_MATH_UNARY_FUNCTION_IMPL(asin)
704✔
237
ORT_MATH_UNARY_FUNCTION_IMPL(acos)
704✔
238
ORT_MATH_UNARY_FUNCTION_IMPL(atan)
704✔
239
ORT_MATH_UNARY_FUNCTION_IMPL(sinh)
704✔
240
ORT_MATH_UNARY_FUNCTION_IMPL(cosh)
704✔
241
ORT_MATH_UNARY_FUNCTION_IMPL(tanh)
704✔
242
ORT_MATH_UNARY_FUNCTION_IMPL(exp)
736✔
243
ORT_MATH_UNARY_FUNCTION_IMPL(exp2)
736✔
244
ORT_MATH_UNARY_FUNCTION_IMPL(log)
736✔
245
ORT_MATH_UNARY_FUNCTION_IMPL(log2)
736✔
246
ORT_MATH_UNARY_FUNCTION_IMPL(log10)
736✔
247
ORT_MATH_UNARY_FUNCTION_IMPL(abs)
320✔
248
ORT_MATH_UNARY_FUNCTION_IMPL(recip)
704✔
249
ORT_MATH_UNARY_FUNCTION_IMPL(neg)
608✔
250
ORT_MATH_UNARY_FUNCTION_IMPL(cbrt)
736✔
251
ORT_MATH_UNARY_FUNCTION_IMPL(ceil)
320✔
252
ORT_MATH_UNARY_FUNCTION_IMPL(floor)
320✔
253
ORT_MATH_UNARY_FUNCTION_IMPL(round)
320✔
254
ORT_MATH_UNARY_FUNCTION_IMPL(trunc)
320✔
255
ORT_MATH_UNARY_FUNCTION_IMPL(sign)
416✔
256
ORT_MATH_UNARY_FUNCTION_IMPL(sqrt)
1,504✔
257

258
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(min)
320✔
259
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(min)
2,176✔
260

261
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(max)
320✔
262
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(max)
2,176✔
263

264
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(mean)
320✔
265
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(mean)
2,176✔
266

267
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(sum)
224✔
268
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(sum)
2,304✔
269

270
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(softmax)
832✔
271

272
/* Dot reduction function */
273
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(php_ort_math_dot_arginfo, 0, 1, ONNX\\Tensor, 0)
274
    ZEND_ARG_OBJ_INFO(0, tensor_a, ONNX\\Tensor, 0)
275
    ZEND_ARG_OBJ_INFO(0, tensor_b, ONNX\\Tensor, 0)
276
ZEND_END_ARG_INFO()
277

278
PHP_FUNCTION(dot)
288✔
279
{
280
    zval *tensor_a, *tensor_b;
288✔
281

282
    ZEND_PARSE_PARAMETERS_START(2, 2)
288✔
283
        Z_PARAM_OBJECT_OF_CLASS(tensor_a, php_ort_tensor_interface_ce)
576✔
284
        Z_PARAM_OBJECT_OF_CLASS(tensor_b, php_ort_tensor_interface_ce)
576✔
285
    ZEND_PARSE_PARAMETERS_END();
288✔
286

287
    php_ort_tensor_t* tensor_a_ort = php_ort_tensor_fetch(Z_OBJ_P(tensor_a));
288✔
288
    php_ort_tensor_t* tensor_b_ort = php_ort_tensor_fetch(Z_OBJ_P(tensor_b));
288✔
289
    ort_tensor_t* result = ort_math_result_dot(
288✔
290
        tensor_a_ort->object, tensor_b_ort->object);
291

292
    /* Create PHP tensor object for result */
293
    object_init_ex(return_value,
288✔
294
        php_ort_tensor_transient_ce);
295
    php_ort_tensor_t* rv =
288✔
296
        php_ort_tensor_fetch(Z_OBJ_P(return_value));
288✔
297
    rv->object = result;
288✔
298
}
299

300
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(php_ort_math_matmul_arginfo, 0, 2, ONNX\\Tensor, 0)
301
    ZEND_ARG_OBJ_INFO(0, matrix_a, ONNX\\Tensor, 0)
302
    ZEND_ARG_OBJ_INFO(0, matrix_b, ONNX\\Tensor, 0)
303
ZEND_END_ARG_INFO()
304

305
PHP_FUNCTION(matmul)
672✔
306
{
307
    zval *matrix_a_zv, *matrix_b_zv;
672✔
308

309
    ZEND_PARSE_PARAMETERS_START(2, 2)
672✔
310
        Z_PARAM_OBJECT_OF_CLASS(matrix_a_zv, php_ort_tensor_interface_ce)
1,344✔
311
        Z_PARAM_OBJECT_OF_CLASS(matrix_b_zv, php_ort_tensor_interface_ce)
1,344✔
312
    ZEND_PARSE_PARAMETERS_END();
672✔
313

314
    php_ort_tensor_t* matrix_a_ort = php_ort_tensor_fetch(Z_OBJ_P(matrix_a_zv));
672✔
315
    php_ort_tensor_t* matrix_b_ort = php_ort_tensor_fetch(Z_OBJ_P(matrix_b_zv));
672✔
316
    
317
    ort_tensor_t* result = ort_math_result_matmul(matrix_a_ort->object, matrix_b_ort->object);
672✔
318

319
    object_init_ex(return_value,
672✔
320
        php_ort_tensor_transient_ce);
321
    php_ort_tensor_t* rv =
672✔
322
        php_ort_tensor_fetch(Z_OBJ_P(return_value));
672✔
323
    rv->object = result;
672✔
324
}
325

326
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(php_ort_math_backend_arginfo, 0, 0, MAY_BE_STRING|MAY_BE_FALSE)
327
ZEND_END_ARG_INFO()
328

329
PHP_FUNCTION(backend)
16✔
330
{
331
    ZEND_PARSE_PARAMETERS_NONE();
16✔
332

333
#ifdef ORT_BACKEND_ENABLED
334
    RETURN_STRING(ORT_BACKEND_NAME);
12✔
335
#else
336
    RETURN_FALSE;
4✔
337
#endif
338
}
339

340
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(php_ort_math_cores_arginfo, 0, 0, IS_LONG, 0)
341
ZEND_END_ARG_INFO()
342

343
PHP_FUNCTION(cores)
16✔
344
{
345
    ZEND_PARSE_PARAMETERS_NONE();
16✔
346

347
    RETURN_LONG(
16✔
348
        ort_pool_cores());
349
}
350

351
/* Function table for ONNX\Math namespace */
352
static const zend_function_entry php_ort_math_functions[] = {
353
    ZEND_NS_FE("ONNX\\Math", add, php_ort_math_add_arginfo)
354
    ZEND_NS_FE("ONNX\\Math", multiply, php_ort_math_multiply_arginfo) 
355
    ZEND_NS_FE("ONNX\\Math", subtract, php_ort_math_subtract_arginfo)
356
    ZEND_NS_FE("ONNX\\Math", divide, php_ort_math_divide_arginfo)
357
    ZEND_NS_FE("ONNX\\Math", sqrt, php_ort_math_sqrt_arginfo)
358
    ZEND_NS_FE("ONNX\\Math", sin, php_ort_math_sin_arginfo)
359
    ZEND_NS_FE("ONNX\\Math", cos, php_ort_math_cos_arginfo)
360
    ZEND_NS_FE("ONNX\\Math", exp, php_ort_math_exp_arginfo)
361
    ZEND_NS_FE("ONNX\\Math", log, php_ort_math_log_arginfo)
362
    ZEND_NS_FE("ONNX\\Math", matmul, php_ort_math_matmul_arginfo)
363
    ZEND_NS_FE("ONNX\\Math", asin, php_ort_math_asin_arginfo)
364
    ZEND_NS_FE("ONNX\\Math", acos, php_ort_math_acos_arginfo)
365
    ZEND_NS_FE("ONNX\\Math", atan, php_ort_math_atan_arginfo)
366
    ZEND_NS_FE("ONNX\\Math", sinh, php_ort_math_sinh_arginfo)
367
    ZEND_NS_FE("ONNX\\Math", cosh, php_ort_math_cosh_arginfo)
368
    ZEND_NS_FE("ONNX\\Math", tanh, php_ort_math_tanh_arginfo)
369
    ZEND_NS_FE("ONNX\\Math", exp2, php_ort_math_exp2_arginfo)
370
    ZEND_NS_FE("ONNX\\Math", log2, php_ort_math_log2_arginfo)
371
    ZEND_NS_FE("ONNX\\Math", log10, php_ort_math_log10_arginfo)
372
    ZEND_NS_FE("ONNX\\Math", cbrt, php_ort_math_cbrt_arginfo)
373
    ZEND_NS_FE("ONNX\\Math", ceil, php_ort_math_ceil_arginfo)
374
    ZEND_NS_FE("ONNX\\Math", floor, php_ort_math_floor_arginfo)
375
    ZEND_NS_FE("ONNX\\Math", round, php_ort_math_round_arginfo)
376
    ZEND_NS_FE("ONNX\\Math", sign, php_ort_math_sign_arginfo)
377
    ZEND_NS_FE("ONNX\\Math", tan, php_ort_math_tan_arginfo)
378
    ZEND_NS_FE("ONNX\\Math", abs, php_ort_math_abs_arginfo)
379
    ZEND_NS_FE("ONNX\\Math", pow, php_ort_math_pow_arginfo)
380
    ZEND_NS_FE("ONNX\\Math", mod, php_ort_math_mod_arginfo)
381

382
    ZEND_NS_FE("ONNX\\Math", neg,     php_ort_math_neg_arginfo)
383
    ZEND_NS_FE("ONNX\\Math", recip,   php_ort_math_recip_arginfo)
384
    ZEND_NS_FE("ONNX\\Math", trunc,   php_ort_math_trunc_arginfo)
385

386
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\tensor", min, php_ort_math_reduce_tensor_min, php_ort_math_reduce_tensor_min_arginfo)
387
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\axis",   min, php_ort_math_reduce_axis_min,   php_ort_math_reduce_axis_min_arginfo)
388
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\tensor", max, php_ort_math_reduce_tensor_max, php_ort_math_reduce_tensor_max_arginfo)
389
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\axis",   max, php_ort_math_reduce_axis_max,   php_ort_math_reduce_axis_max_arginfo)
390
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\tensor", mean, php_ort_math_reduce_tensor_mean, php_ort_math_reduce_tensor_mean_arginfo)
391
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\axis",   mean, php_ort_math_reduce_axis_mean,   php_ort_math_reduce_axis_mean_arginfo)
392
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\tensor", sum, php_ort_math_reduce_tensor_sum, php_ort_math_reduce_tensor_sum_arginfo)
393
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\axis",   sum, php_ort_math_reduce_axis_sum,   php_ort_math_reduce_axis_sum_arginfo)
394
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\axis",   softmax, php_ort_math_reduce_axis_softmax,   php_ort_math_reduce_axis_softmax_arginfo)
395

396
    ZEND_NS_FE("ONNX\\Math", dot, php_ort_math_dot_arginfo)
397

398
    ZEND_NS_FE("ONNX\\Math", backend, php_ort_math_backend_arginfo)
399
    ZEND_NS_FE("ONNX\\Math", cores,   php_ort_math_cores_arginfo)
400
    ZEND_FE_END
401
};
402

403
/* {{{ */
404
ZEND_BEGIN_ARG_INFO_EX(php_ort_math_schema___construct_arginfo, 0, 0, 1)
405
    ZEND_ARG_TYPE_INFO(0, symbol, IS_STRING, 0)
406
ZEND_END_ARG_INFO()
407

408
PHP_METHOD(ONNX_Math_Schema, __construct)
304✔
409
{
410
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
304✔
411
    zend_string *symbol;
304✔
412

413
    ZEND_PARSE_PARAMETERS_START(1, 1)
304✔
414
        Z_PARAM_STR(symbol)
608✔
415
    ZEND_PARSE_PARAMETERS_END();
304✔
416

417
    if (zend_string_equals_literal_ci(symbol, "add")) {
304✔
418
        ort->schema = &ort_math_promotion_schema_add;
64✔
419
    } else if (zend_string_equals_literal_ci(symbol, "div")) {
240✔
420
        ort->schema = &ort_math_promotion_schema_div;
16✔
421
    } else if (zend_string_equals_literal_ci(symbol, "dot")) {
224✔
422
        ort->schema = &ort_math_promotion_schema_dot;
16✔
423
    } else if (zend_string_equals_literal_ci(symbol, "matmul")) {
208✔
424
        ort->schema = &ort_math_promotion_schema_matmul;
16✔
425
    } else if (zend_string_equals_literal_ci(symbol, "max")) {
192✔
426
        ort->schema = &ort_math_promotion_schema_max;
16✔
427
    } else if (zend_string_equals_literal_ci(symbol, "mean")) {
176✔
428
        ort->schema = &ort_math_promotion_schema_mean;
16✔
429
    } else if (zend_string_equals_literal_ci(symbol, "min")) {
160✔
430
        ort->schema = &ort_math_promotion_schema_min;
16✔
431
    } else if (zend_string_equals_literal_ci(symbol, "mod")) {
144✔
432
        ort->schema = &ort_math_promotion_schema_mod;
16✔
433
    } else if (zend_string_equals_literal_ci(symbol, "mul")) {
128✔
434
        ort->schema = &ort_math_promotion_schema_mul;
16✔
435
    } else if (zend_string_equals_literal_ci(symbol, "neg")) {
112✔
436
        ort->schema = &ort_math_promotion_schema_neg;
16✔
437
    } else if (zend_string_equals_literal_ci(symbol, "pow")) {
96✔
438
        ort->schema = &ort_math_promotion_schema_pow;
16✔
439
    } else if (zend_string_equals_literal_ci(symbol, "recip")) {
80✔
440
        ort->schema = &ort_math_promotion_schema_recip;
16✔
441
    } else if (zend_string_equals_literal_ci(symbol, "sign")) {
64✔
442
        ort->schema = &ort_math_promotion_schema_sign;
16✔
443
    } else if (zend_string_equals_literal_ci(symbol, "sqrt")) {
48✔
444
        ort->schema = &ort_math_promotion_schema_sqrt;
16✔
445
    } else if (zend_string_equals_literal_ci(symbol, "sub")) {
32✔
446
        ort->schema = &ort_math_promotion_schema_sub;
16✔
447
    } else if (zend_string_equals_literal_ci(symbol, "sum")) {
16✔
448
        ort->schema = &ort_math_promotion_schema_sum;
16✔
449
    } else {
450
        /* throw */
UNCOV
451
        return;
×
452
    }
453

454
    ort->symbol = zend_string_copy(symbol);
560✔
455
}
456

457
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(php_ort_math_schema_getSymbol_arginfo, 0, 0, MAY_BE_STRING|MAY_BE_NULL)
458
ZEND_END_ARG_INFO()
459

460
PHP_METHOD(ONNX_Math_Schema, getSymbol)
16✔
461
{
462
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
16✔
463

464
    if (!ort->symbol) {
16✔
465
        return;
466
    }
467

468
    RETURN_STR_COPY(ort->symbol);
16✔
469
}
470

471
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(php_ort_math_schema_getKind_arginfo, 0, 0, IS_LONG, 0)
472
ZEND_END_ARG_INFO()
473

474
PHP_METHOD(ONNX_Math_Schema, getKind)
16✔
475
{
476
    php_ort_math_schema_t *ort =
16✔
477
        php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
16✔
478

479
    ZEND_PARSE_PARAMETERS_NONE();
16✔
480

481
    RETURN_LONG(ort->schema->kind);
16✔
482
}
483

484
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(php_ort_math_schema_resolve_arginfo, 0, 1, IS_LONG, 0)
485
    ZEND_ARG_VARIADIC_INFO(0, types)
486
ZEND_END_ARG_INFO()
487

488
PHP_METHOD(ONNX_Math_Schema, resolve)
16✔
489
{
490
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
16✔
491
    zval *types;
16✔
492
    size_t count;
16✔
493

494
    ZEND_PARSE_PARAMETERS_START(1, -1)
16✔
495
        Z_PARAM_VARIADIC('+', types, count)
16✔
496
    ZEND_PARSE_PARAMETERS_END();
16✔
497

498
    if (count == 0) {
16✔
499
        /* throw */
UNCOV
500
        RETURN_LONG(-1);
×
501
    }
502

503
    if (ort->schema->kind == ORT_MATH_TYPE_PROMOTION_SCHEMA_BINARY) {
16✔
504
        if (count != 2) {
16✔
505
            /* throw */
UNCOV
506
            RETURN_LONG(-1);
×
507
        }
508

509
        if (Z_TYPE(types[0]) != IS_LONG || Z_TYPE(types[1]) != IS_LONG) {
16✔
510
            /* throw */
UNCOV
511
            RETURN_LONG(-1);
×
512
        }
513

514
        ONNXTensorElementDataType result =
16✔
515
            ort_math_type_promotion_schema_resolve_binary(
32✔
516
                ort->schema,
517
                (ONNXTensorElementDataType) Z_LVAL(types[0]),
16✔
518
                (ONNXTensorElementDataType) Z_LVAL(types[1]));
16✔
519
        RETURN_LONG(result);
16✔
UNCOV
520
    } else if (ort->schema->kind == ORT_MATH_TYPE_PROMOTION_SCHEMA_UNARY) {
×
UNCOV
521
        if (count != 1) {
×
522
            RETURN_LONG(-1);
×
523
        }
524

UNCOV
525
        if (Z_TYPE(types[0]) != IS_LONG) {
×
526
            /* throw */
527
            RETURN_LONG(-1);
×
528
        }
529

530
        ONNXTensorElementDataType result =
×
531
            ort_math_type_promotion_schema_resolve_unary(
×
532
                ort->schema,
UNCOV
533
                (ONNXTensorElementDataType) Z_LVAL(types[0]));
×
534
        RETURN_LONG(result);
×
535
    } else {
536
        /* throw */
UNCOV
537
        RETURN_LONG(-1);
×
538
    }
539
}
540

541
static const zend_function_entry php_ort_math_schema_methods[] = {
542
    PHP_ME(ONNX_Math_Schema, __construct, php_ort_math_schema___construct_arginfo, ZEND_ACC_PUBLIC)
543
    PHP_ME(ONNX_Math_Schema, getSymbol,   php_ort_math_schema_getSymbol_arginfo,   ZEND_ACC_PUBLIC)
544
    PHP_ME(ONNX_Math_Schema, getKind,     php_ort_math_schema_getKind_arginfo,     ZEND_ACC_PUBLIC)
545
    PHP_ME(ONNX_Math_Schema, resolve,     php_ort_math_schema_resolve_arginfo,     ZEND_ACC_PUBLIC)
546
    PHP_FE_END
547
}; /* }}} */
548

549
PHP_MINIT_FUNCTION(ORT_MATH)
3,168✔
550
{
551
    ort_math_startup();
3,168✔
552

553
    zend_class_entry ce;
3,168✔
554

555
    INIT_NS_CLASS_ENTRY(ce, "ONNX\\Math", "Schema", php_ort_math_schema_methods);
3,168✔
556
    php_ort_math_schema_ce = zend_register_internal_class(&ce);
3,168✔
557
    php_ort_math_schema_ce->create_object = php_ort_math_schema_create;
3,168✔
558
    php_ort_math_schema_ce->ce_flags |= ZEND_ACC_FINAL;
3,168✔
559

560
    zend_declare_class_constant_long(
3,168✔
561
        php_ort_math_schema_ce,
562
        ZEND_STRL("BINARY"),
563
        ORT_MATH_TYPE_PROMOTION_SCHEMA_BINARY);
564
    zend_declare_class_constant_long(
3,168✔
565
        php_ort_math_schema_ce,
566
        ZEND_STRL("UNARY"),
567
        ORT_MATH_TYPE_PROMOTION_SCHEMA_UNARY);
568

569
    memcpy(&php_ort_math_schema_handlers,
3,168✔
570
        zend_get_std_object_handlers(), sizeof(zend_object_handlers));
571
    php_ort_math_schema_handlers.offset = XtOffsetOf(php_ort_math_schema_t, std);
3,168✔
572
    php_ort_math_schema_handlers.free_obj = php_ort_math_schema_free;
3,168✔
573
    php_ort_math_schema_handlers.get_debug_info = php_ort_math_schema_debug;
3,168✔
574
    php_ort_math_schema_handlers.clone_obj = NULL; // No cloning support
3,168✔
575

576
    zend_register_functions(NULL,
3,168✔
577
        php_ort_math_functions, NULL, MODULE_PERSISTENT);
578

579
    return SUCCESS;
3,168✔
580
}
581

582
PHP_RINIT_FUNCTION(ORT_MATH)
3,168✔
583
{
584
    ort_math_activate();
3,168✔
585

586
    return SUCCESS;
3,168✔
587
}
588

589
PHP_RSHUTDOWN_FUNCTION(ORT_MATH)
3,168✔
590
{
591
    ort_math_deactivate();
3,168✔
592

593
    return SUCCESS;
3,168✔
594
}
595

596
PHP_MSHUTDOWN_FUNCTION(ORT_MATH)
3,168✔
597
{
598
    ort_math_shutdown();
3,168✔
599

600
    return SUCCESS;
3,168✔
601
}
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