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

krakjoe / ort / 16252097619

13 Jul 2025 06:19PM UTC coverage: 92.44% (+0.008%) from 92.432%
16252097619

push

github

krakjoe
log schema

3 of 3 new or added lines in 2 files covered. (100.0%)

15 existing lines in 1 file now uncovered.

5160 of 5582 relevant lines covered (92.44%)

68944.37 hits per line

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

88.5
/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/acos.h"
28
#include "maths/schema/atan.h"
29
#include "maths/schema/add.h"
30
#include "maths/schema/asin.h"
31
#include "maths/schema/cos.h"
32
#include "maths/schema/cosh.h"
33
#include "maths/schema/div.h"
34
#include "maths/schema/dot.h"
35
#include "maths/schema/exp.h"
36
#include "maths/schema/exp2.h"
37
#include "maths/schema/log.h"
38
#include "maths/schema/matmul.h"
39
#include "maths/schema/max.h"
40
#include "maths/schema/mean.h"
41
#include "maths/schema/min.h"
42
#include "maths/schema/mod.h"
43
#include "maths/schema/mul.h"
44
#include "maths/schema/neg.h"
45
#include "maths/schema/pow.h"
46
#include "maths/schema/recip.h"
47
#include "maths/schema/sign.h"
48
#include "maths/schema/sin.h"
49
#include "maths/schema/sinh.h"
50
#include "maths/schema/softmax.h"
51
#include "maths/schema/sqrt.h"
52
#include "maths/schema/sub.h"
53
#include "maths/schema/sum.h"
54
#include "maths/schema/tan.h"
55
#include "maths/schema/tanh.h"
56

57
typedef struct _php_ort_math_schema_t {
58
    const ort_math_type_promotion_schema_t* schema;
59
    zend_string*                            symbol;
60
    zend_object std;
61
} php_ort_math_schema_t;
62

63
zend_class_entry *php_ort_math_schema_ce;
64
zend_object_handlers php_ort_math_schema_handlers;
65

66
static zend_always_inline php_ort_math_schema_t* php_ort_math_schema_fetch(zend_object *o) {
67
    return (php_ort_math_schema_t*) (((char*) o) - XtOffsetOf(php_ort_math_schema_t, std));
68
}
69

70
static zend_object* php_ort_math_schema_create(zend_class_entry *ce) {
512✔
71
    php_ort_math_schema_t *ort = 
512✔
72
        zend_object_alloc(sizeof(php_ort_math_schema_t), ce);
512✔
73
        
74
    zend_object_std_init(&ort->std, ce);
512✔
75
    object_properties_init(&ort->std, ce);
512✔
76

77
    ort->std.handlers = &php_ort_math_schema_handlers;
512✔
78
    return &ort->std;
512✔
79
}
80

81
static HashTable* php_ort_math_schema_debug(zend_object *zo, int *temp) {
×
UNCOV
82
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(zo);
×
83
    HashTable *debug;
×
84

UNCOV
85
    ALLOC_HASHTABLE(debug);
×
86
    zend_hash_init(debug, 3, NULL, ZVAL_PTR_DTOR, 0);
×
87

UNCOV
88
    if (ort->symbol) {
×
89
        zval symbol;
×
90

91
        ZVAL_STR_COPY(&symbol, ort->symbol);
×
92

UNCOV
93
        zend_hash_str_update(debug,
×
94
            "symbol", sizeof("symbol") - 1,
95
            &symbol);
96
    }
97

UNCOV
98
__php_ort_tensor_debug_return:
×
99
    *temp = 1;
×
100

UNCOV
101
    return debug;
×
102
}
103

104
void php_ort_math_schema_free(zend_object *zo) {
512✔
105
    php_ort_math_schema_t* ort = php_ort_math_schema_fetch(zo);
512✔
106

107
    if (ort->symbol) {
512✔
108
        zend_string_release(ort->symbol);
512✔
109
    }
110

111
    zend_object_std_dtor(zo);
512✔
112
}
512✔
113

114
#define ORT_MATH_UNARY_FUNCTION_IMPL(fname)                    \
115
    ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(                    \
116
        php_ort_math_##fname##_arginfo, 0, 1, ONNX\\Tensor, 0) \
117
        ZEND_ARG_OBJ_INFO(0, tensor, ONNX\\Tensor, 0)          \
118
    ZEND_END_ARG_INFO()                                        \
119
    PHP_FUNCTION(fname)                                        \
120
    {                                                          \
121
        zval *tensor_zv;                                       \
122
        ZEND_PARSE_PARAMETERS_START(1, 1)                      \
123
            Z_PARAM_OBJECT_OF_CLASS(tensor_zv,                 \
124
                php_ort_tensor_interface_ce)                   \
125
        ZEND_PARSE_PARAMETERS_END();                           \
126
        php_ort_tensor_t* tensor_ort =                         \
127
            php_ort_tensor_fetch(Z_OBJ_P(tensor_zv));          \
128
        ort_tensor_t* result =                                 \
129
            ort_math_result_##fname(tensor_ort->object);       \
130
        object_init_ex(return_value,                           \
131
            php_ort_tensor_transient_ce);                      \
132
        php_ort_tensor_t* rv =                                 \
133
            php_ort_tensor_fetch(Z_OBJ_P(return_value));       \
134
        rv->object = result;                                   \
135
    }
136

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

180

181
#define ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(fname)             \
182
    ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(                        \
183
            php_ort_math_reduce_tensor_##fname##_arginfo,          \
184
                0, 1, ONNX\\Tensor, 0)                             \
185
        ZEND_ARG_OBJ_INFO(0, tensor, ONNX\\Tensor, 0)              \
186
    ZEND_END_ARG_INFO()                                            \
187
    PHP_NAMED_FUNCTION(php_ort_math_reduce_tensor_##fname)         \
188
    {                                                              \
189
        zval *tensor_zv;                                           \
190
        ZEND_PARSE_PARAMETERS_START(1, 1)                          \
191
            Z_PARAM_OBJECT_OF_CLASS(tensor_zv,                     \
192
                php_ort_tensor_interface_ce)                       \
193
        ZEND_PARSE_PARAMETERS_END();                               \
194
        php_ort_tensor_t* tensor_ort =                             \
195
            php_ort_tensor_fetch(Z_OBJ_P(tensor_zv));              \
196
        ort_tensor_t* result =                                     \
197
            ort_math_result_reduce_tensor_##fname(                 \
198
                tensor_ort->object);                               \
199
        object_init_ex(return_value,                               \
200
            php_ort_tensor_transient_ce);                          \
201
        php_ort_tensor_t* rv =                                     \
202
            php_ort_tensor_fetch(Z_OBJ_P(return_value));           \
203
        rv->object = result;                                       \
204
    }
205

206
// Macro for reduction functions (Tensor, axis, keepdims)
207
#define ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(fname)               \
208
    ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(                        \
209
            php_ort_math_reduce_axis_##fname##_arginfo, 0, 2, ONNX\\Tensor, 0) \
210
        ZEND_ARG_OBJ_INFO(0, tensor, ONNX\\Tensor, 0)              \
211
        ZEND_ARG_TYPE_INFO(0, axis, IS_LONG, 0)                    \
212
        ZEND_ARG_TYPE_INFO(0, keepdims, _IS_BOOL, 0)               \
213
    ZEND_END_ARG_INFO()                                            \
214
    PHP_NAMED_FUNCTION(php_ort_math_reduce_axis_##fname)           \
215
    {                                                              \
216
        zval *tensor_zv;                                           \
217
        zend_long axis = 0;                                        \
218
        zend_bool keepdims = 0;                                    \
219
        ZEND_PARSE_PARAMETERS_START(2, 3)                          \
220
            Z_PARAM_OBJECT_OF_CLASS(tensor_zv,                     \
221
                php_ort_tensor_interface_ce)                       \
222
            Z_PARAM_LONG(axis)                                     \
223
            Z_PARAM_OPTIONAL                                       \
224
            Z_PARAM_BOOL(keepdims)                                 \
225
        ZEND_PARSE_PARAMETERS_END();                               \
226
        php_ort_tensor_t* tensor_ort =                             \
227
            php_ort_tensor_fetch(Z_OBJ_P(tensor_zv));              \
228
        ort_tensor_t* result = ort_math_result_reduce_axis_##fname(\
229
            tensor_ort->object, axis, keepdims);                   \
230
        object_init_ex(return_value,                               \
231
            php_ort_tensor_transient_ce);                          \
232
        php_ort_tensor_t* rv =                                     \
233
            php_ort_tensor_fetch(Z_OBJ_P(return_value));           \
234
        rv->object = result;                                       \
235
    }
236
/* Mathematical functions in ONNX\Math namespace */
237

238
ORT_MATH_BINARY_FUNCTION_IMPL(add)
16,896✔
239
ORT_MATH_BINARY_FUNCTION_IMPL(multiply)
4,112✔
240
ORT_MATH_BINARY_FUNCTION_IMPL(subtract)
1,936✔
241
ORT_MATH_BINARY_FUNCTION_IMPL(divide)
3,456✔
242

243
ORT_MATH_BINARY_FUNCTION_IMPL(pow)
2,448✔
244
ORT_MATH_BINARY_FUNCTION_IMPL(mod)
2,496✔
245

246
ORT_MATH_UNARY_FUNCTION_IMPL(sin)
736✔
247
ORT_MATH_UNARY_FUNCTION_IMPL(cos)
736✔
248
ORT_MATH_UNARY_FUNCTION_IMPL(tan)
736✔
249
ORT_MATH_UNARY_FUNCTION_IMPL(asin)
704✔
250
ORT_MATH_UNARY_FUNCTION_IMPL(acos)
704✔
251
ORT_MATH_UNARY_FUNCTION_IMPL(atan)
704✔
252
ORT_MATH_UNARY_FUNCTION_IMPL(sinh)
704✔
253
ORT_MATH_UNARY_FUNCTION_IMPL(cosh)
704✔
254
ORT_MATH_UNARY_FUNCTION_IMPL(tanh)
704✔
255
ORT_MATH_UNARY_FUNCTION_IMPL(exp)
736✔
256
ORT_MATH_UNARY_FUNCTION_IMPL(exp2)
736✔
257
ORT_MATH_UNARY_FUNCTION_IMPL(log)
736✔
258
ORT_MATH_UNARY_FUNCTION_IMPL(log2)
736✔
259
ORT_MATH_UNARY_FUNCTION_IMPL(log10)
736✔
260
ORT_MATH_UNARY_FUNCTION_IMPL(abs)
320✔
261
ORT_MATH_UNARY_FUNCTION_IMPL(recip)
704✔
262
ORT_MATH_UNARY_FUNCTION_IMPL(neg)
608✔
263
ORT_MATH_UNARY_FUNCTION_IMPL(cbrt)
736✔
264
ORT_MATH_UNARY_FUNCTION_IMPL(ceil)
320✔
265
ORT_MATH_UNARY_FUNCTION_IMPL(floor)
320✔
266
ORT_MATH_UNARY_FUNCTION_IMPL(round)
320✔
267
ORT_MATH_UNARY_FUNCTION_IMPL(trunc)
320✔
268
ORT_MATH_UNARY_FUNCTION_IMPL(sign)
416✔
269
ORT_MATH_UNARY_FUNCTION_IMPL(sqrt)
1,504✔
270

271
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(min)
320✔
272
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(min)
2,176✔
273

274
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(max)
320✔
275
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(max)
2,176✔
276

277
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(mean)
320✔
278
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(mean)
2,176✔
279

280
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(sum)
224✔
281
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(sum)
2,304✔
282

283
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(softmax)
832✔
284

285
/* Dot reduction function */
286
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(php_ort_math_dot_arginfo, 0, 1, ONNX\\Tensor, 0)
287
    ZEND_ARG_OBJ_INFO(0, tensor_a, ONNX\\Tensor, 0)
288
    ZEND_ARG_OBJ_INFO(0, tensor_b, ONNX\\Tensor, 0)
289
ZEND_END_ARG_INFO()
290

291
PHP_FUNCTION(dot)
288✔
292
{
293
    zval *tensor_a, *tensor_b;
288✔
294

295
    ZEND_PARSE_PARAMETERS_START(2, 2)
288✔
296
        Z_PARAM_OBJECT_OF_CLASS(tensor_a, php_ort_tensor_interface_ce)
576✔
297
        Z_PARAM_OBJECT_OF_CLASS(tensor_b, php_ort_tensor_interface_ce)
576✔
298
    ZEND_PARSE_PARAMETERS_END();
288✔
299

300
    php_ort_tensor_t* tensor_a_ort = php_ort_tensor_fetch(Z_OBJ_P(tensor_a));
288✔
301
    php_ort_tensor_t* tensor_b_ort = php_ort_tensor_fetch(Z_OBJ_P(tensor_b));
288✔
302
    ort_tensor_t* result = ort_math_result_dot(
288✔
303
        tensor_a_ort->object, tensor_b_ort->object);
304

305
    /* Create PHP tensor object for result */
306
    object_init_ex(return_value,
288✔
307
        php_ort_tensor_transient_ce);
308
    php_ort_tensor_t* rv =
288✔
309
        php_ort_tensor_fetch(Z_OBJ_P(return_value));
288✔
310
    rv->object = result;
288✔
311
}
312

313
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(php_ort_math_matmul_arginfo, 0, 2, ONNX\\Tensor, 0)
314
    ZEND_ARG_OBJ_INFO(0, matrix_a, ONNX\\Tensor, 0)
315
    ZEND_ARG_OBJ_INFO(0, matrix_b, ONNX\\Tensor, 0)
316
ZEND_END_ARG_INFO()
317

318
PHP_FUNCTION(matmul)
672✔
319
{
320
    zval *matrix_a_zv, *matrix_b_zv;
672✔
321

322
    ZEND_PARSE_PARAMETERS_START(2, 2)
672✔
323
        Z_PARAM_OBJECT_OF_CLASS(matrix_a_zv, php_ort_tensor_interface_ce)
1,344✔
324
        Z_PARAM_OBJECT_OF_CLASS(matrix_b_zv, php_ort_tensor_interface_ce)
1,344✔
325
    ZEND_PARSE_PARAMETERS_END();
672✔
326

327
    php_ort_tensor_t* matrix_a_ort = php_ort_tensor_fetch(Z_OBJ_P(matrix_a_zv));
672✔
328
    php_ort_tensor_t* matrix_b_ort = php_ort_tensor_fetch(Z_OBJ_P(matrix_b_zv));
672✔
329
    
330
    ort_tensor_t* result = ort_math_result_matmul(matrix_a_ort->object, matrix_b_ort->object);
672✔
331

332
    object_init_ex(return_value,
672✔
333
        php_ort_tensor_transient_ce);
334
    php_ort_tensor_t* rv =
672✔
335
        php_ort_tensor_fetch(Z_OBJ_P(return_value));
672✔
336
    rv->object = result;
672✔
337
}
338

339
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(php_ort_math_backend_arginfo, 0, 0, MAY_BE_STRING|MAY_BE_FALSE)
340
ZEND_END_ARG_INFO()
341

342
PHP_FUNCTION(backend)
16✔
343
{
344
    ZEND_PARSE_PARAMETERS_NONE();
16✔
345

346
#ifdef ORT_BACKEND_ENABLED
347
    RETURN_STRING(ORT_BACKEND_NAME);
12✔
348
#else
349
    RETURN_FALSE;
4✔
350
#endif
351
}
352

353
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(php_ort_math_cores_arginfo, 0, 0, IS_LONG, 0)
354
ZEND_END_ARG_INFO()
355

356
PHP_FUNCTION(cores)
16✔
357
{
358
    ZEND_PARSE_PARAMETERS_NONE();
16✔
359

360
    RETURN_LONG(
16✔
361
        ort_pool_cores());
362
}
363

364
/* Function table for ONNX\Math namespace */
365
static const zend_function_entry php_ort_math_functions[] = {
366
    ZEND_NS_FE("ONNX\\Math", add, php_ort_math_add_arginfo)
367
    ZEND_NS_FE("ONNX\\Math", multiply, php_ort_math_multiply_arginfo) 
368
    ZEND_NS_FE("ONNX\\Math", subtract, php_ort_math_subtract_arginfo)
369
    ZEND_NS_FE("ONNX\\Math", divide, php_ort_math_divide_arginfo)
370
    ZEND_NS_FE("ONNX\\Math", sqrt, php_ort_math_sqrt_arginfo)
371
    ZEND_NS_FE("ONNX\\Math", sin, php_ort_math_sin_arginfo)
372
    ZEND_NS_FE("ONNX\\Math", cos, php_ort_math_cos_arginfo)
373
    ZEND_NS_FE("ONNX\\Math", exp, php_ort_math_exp_arginfo)
374
    ZEND_NS_FE("ONNX\\Math", log, php_ort_math_log_arginfo)
375
    ZEND_NS_FE("ONNX\\Math", matmul, php_ort_math_matmul_arginfo)
376
    ZEND_NS_FE("ONNX\\Math", asin, php_ort_math_asin_arginfo)
377
    ZEND_NS_FE("ONNX\\Math", acos, php_ort_math_acos_arginfo)
378
    ZEND_NS_FE("ONNX\\Math", atan, php_ort_math_atan_arginfo)
379
    ZEND_NS_FE("ONNX\\Math", sinh, php_ort_math_sinh_arginfo)
380
    ZEND_NS_FE("ONNX\\Math", cosh, php_ort_math_cosh_arginfo)
381
    ZEND_NS_FE("ONNX\\Math", tanh, php_ort_math_tanh_arginfo)
382
    ZEND_NS_FE("ONNX\\Math", exp2, php_ort_math_exp2_arginfo)
383
    ZEND_NS_FE("ONNX\\Math", log2, php_ort_math_log2_arginfo)
384
    ZEND_NS_FE("ONNX\\Math", log10, php_ort_math_log10_arginfo)
385
    ZEND_NS_FE("ONNX\\Math", cbrt, php_ort_math_cbrt_arginfo)
386
    ZEND_NS_FE("ONNX\\Math", ceil, php_ort_math_ceil_arginfo)
387
    ZEND_NS_FE("ONNX\\Math", floor, php_ort_math_floor_arginfo)
388
    ZEND_NS_FE("ONNX\\Math", round, php_ort_math_round_arginfo)
389
    ZEND_NS_FE("ONNX\\Math", sign, php_ort_math_sign_arginfo)
390
    ZEND_NS_FE("ONNX\\Math", tan, php_ort_math_tan_arginfo)
391
    ZEND_NS_FE("ONNX\\Math", abs, php_ort_math_abs_arginfo)
392
    ZEND_NS_FE("ONNX\\Math", pow, php_ort_math_pow_arginfo)
393
    ZEND_NS_FE("ONNX\\Math", mod, php_ort_math_mod_arginfo)
394

395
    ZEND_NS_FE("ONNX\\Math", neg,     php_ort_math_neg_arginfo)
396
    ZEND_NS_FE("ONNX\\Math", recip,   php_ort_math_recip_arginfo)
397
    ZEND_NS_FE("ONNX\\Math", trunc,   php_ort_math_trunc_arginfo)
398

399
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\tensor", min, php_ort_math_reduce_tensor_min, php_ort_math_reduce_tensor_min_arginfo)
400
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\axis",   min, php_ort_math_reduce_axis_min,   php_ort_math_reduce_axis_min_arginfo)
401
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\tensor", max, php_ort_math_reduce_tensor_max, php_ort_math_reduce_tensor_max_arginfo)
402
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\axis",   max, php_ort_math_reduce_axis_max,   php_ort_math_reduce_axis_max_arginfo)
403
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\tensor", mean, php_ort_math_reduce_tensor_mean, php_ort_math_reduce_tensor_mean_arginfo)
404
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\axis",   mean, php_ort_math_reduce_axis_mean,   php_ort_math_reduce_axis_mean_arginfo)
405
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\tensor", sum, php_ort_math_reduce_tensor_sum, php_ort_math_reduce_tensor_sum_arginfo)
406
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\axis",   sum, php_ort_math_reduce_axis_sum,   php_ort_math_reduce_axis_sum_arginfo)
407
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\axis",   softmax, php_ort_math_reduce_axis_softmax,   php_ort_math_reduce_axis_softmax_arginfo)
408

409
    ZEND_NS_FE("ONNX\\Math", dot, php_ort_math_dot_arginfo)
410

411
    ZEND_NS_FE("ONNX\\Math", backend, php_ort_math_backend_arginfo)
412
    ZEND_NS_FE("ONNX\\Math", cores,   php_ort_math_cores_arginfo)
413
    ZEND_FE_END
414
};
415

416
/* {{{ */
417
ZEND_BEGIN_ARG_INFO_EX(php_ort_math_schema___construct_arginfo, 0, 0, 1)
418
    ZEND_ARG_TYPE_INFO(0, symbol, IS_STRING, 0)
419
ZEND_END_ARG_INFO()
420

421
PHP_METHOD(ONNX_Math_Schema, __construct)
512✔
422
{
423
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
512✔
424
    zend_string *symbol;
512✔
425

426
    ZEND_PARSE_PARAMETERS_START(1, 1)
512✔
427
        Z_PARAM_STR(symbol)
1,024✔
428
    ZEND_PARSE_PARAMETERS_END();
512✔
429

430
    if (zend_string_equals_literal_ci(symbol, "acos")) {
512✔
431
        ort->schema = &ort_math_promotion_schema_acos;
16✔
432
    } else if (zend_string_equals_literal_ci(symbol, "add")) {
496✔
433
        ort->schema = &ort_math_promotion_schema_add;
64✔
434
    } else if (zend_string_equals_literal_ci(symbol, "asin")) {
432✔
435
        ort->schema = &ort_math_promotion_schema_asin;
16✔
436
    } else if (zend_string_equals_literal_ci(symbol, "atan")) {
416✔
437
        ort->schema = &ort_math_promotion_schema_atan;
16✔
438
    } else if (zend_string_equals_literal_ci(symbol, "cos")) {
400✔
439
        ort->schema = &ort_math_promotion_schema_cos;
16✔
440
    } else if (zend_string_equals_literal_ci(symbol, "cosh")) {
384✔
441
        ort->schema = &ort_math_promotion_schema_cosh;
16✔
442
    } else if (zend_string_equals_literal_ci(symbol, "div")) {
368✔
443
        ort->schema = &ort_math_promotion_schema_div;
16✔
444
    } else if (zend_string_equals_literal_ci(symbol, "dot")) {
352✔
445
        ort->schema = &ort_math_promotion_schema_dot;
16✔
446
    } else if (zend_string_equals_literal_ci(symbol, "exp")) {
336✔
447
        ort->schema = &ort_math_promotion_schema_exp;
16✔
448
    } else if (zend_string_equals_literal_ci(symbol, "exp2")) {
320✔
449
        ort->schema = &ort_math_promotion_schema_exp2;
16✔
450
    } else if (zend_string_equals_literal_ci(symbol, "log")) {
304✔
451
        ort->schema = &ort_math_promotion_schema_log;
16✔
452
    } else if (zend_string_equals_literal_ci(symbol, "matmul")) {
288✔
453
        ort->schema = &ort_math_promotion_schema_matmul;
16✔
454
    } else if (zend_string_equals_literal_ci(symbol, "max")) {
272✔
455
        ort->schema = &ort_math_promotion_schema_max;
16✔
456
    } else if (zend_string_equals_literal_ci(symbol, "mean")) {
256✔
457
        ort->schema = &ort_math_promotion_schema_mean;
16✔
458
    } else if (zend_string_equals_literal_ci(symbol, "min")) {
240✔
459
        ort->schema = &ort_math_promotion_schema_min;
16✔
460
    } else if (zend_string_equals_literal_ci(symbol, "mod")) {
224✔
461
        ort->schema = &ort_math_promotion_schema_mod;
16✔
462
    } else if (zend_string_equals_literal_ci(symbol, "mul")) {
208✔
463
        ort->schema = &ort_math_promotion_schema_mul;
16✔
464
    } else if (zend_string_equals_literal_ci(symbol, "neg")) {
192✔
465
        ort->schema = &ort_math_promotion_schema_neg;
16✔
466
    } else if (zend_string_equals_literal_ci(symbol, "pow")) {
176✔
467
        ort->schema = &ort_math_promotion_schema_pow;
16✔
468
    } else if (zend_string_equals_literal_ci(symbol, "recip")) {
160✔
469
        ort->schema = &ort_math_promotion_schema_recip;
16✔
470
    } else if (zend_string_equals_literal_ci(symbol, "sign")) {
144✔
471
        ort->schema = &ort_math_promotion_schema_sign;
16✔
472
    } else if (zend_string_equals_literal_ci(symbol, "sin")) {
128✔
473
        ort->schema = &ort_math_promotion_schema_sin;
16✔
474
    } else if (zend_string_equals_literal_ci(symbol, "sinh")) {
112✔
475
        ort->schema = &ort_math_promotion_schema_sinh;
16✔
476
    } else if (zend_string_equals_literal_ci(symbol, "softmax")) {
96✔
477
        ort->schema = &ort_math_promotion_schema_softmax;
16✔
478
    } else if (zend_string_equals_literal_ci(symbol, "sqrt")) {
80✔
479
        ort->schema = &ort_math_promotion_schema_sqrt;
16✔
480
    } else if (zend_string_equals_literal_ci(symbol, "sub")) {
64✔
481
        ort->schema = &ort_math_promotion_schema_sub;
16✔
482
    } else if (zend_string_equals_literal_ci(symbol, "sum")) {
48✔
483
        ort->schema = &ort_math_promotion_schema_sum;
16✔
484
    } else if (zend_string_equals_literal_ci(symbol, "tan")) {
32✔
485
        ort->schema = &ort_math_promotion_schema_tan;
16✔
486
    } else if (zend_string_equals_literal_ci(symbol, "tanh")) {
16✔
487
        ort->schema = &ort_math_promotion_schema_tanh;
16✔
488
    } else {
489
        /* throw */
UNCOV
490
        return;
×
491
    }
492

493
    ort->symbol = zend_string_copy(symbol);
976✔
494
}
495

496
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(php_ort_math_schema_getSymbol_arginfo, 0, 0, MAY_BE_STRING|MAY_BE_NULL)
497
ZEND_END_ARG_INFO()
498

499
PHP_METHOD(ONNX_Math_Schema, getSymbol)
16✔
500
{
501
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
16✔
502

503
    if (!ort->symbol) {
16✔
504
        return;
505
    }
506

507
    RETURN_STR_COPY(ort->symbol);
16✔
508
}
509

510
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(php_ort_math_schema_getKind_arginfo, 0, 0, IS_LONG, 0)
511
ZEND_END_ARG_INFO()
512

513
PHP_METHOD(ONNX_Math_Schema, getKind)
16✔
514
{
515
    php_ort_math_schema_t *ort =
16✔
516
        php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
16✔
517

518
    ZEND_PARSE_PARAMETERS_NONE();
16✔
519

520
    RETURN_LONG(ort->schema->kind);
16✔
521
}
522

523
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(php_ort_math_schema_resolve_arginfo, 0, 1, IS_LONG, 0)
524
    ZEND_ARG_VARIADIC_INFO(0, types)
525
ZEND_END_ARG_INFO()
526

527
PHP_METHOD(ONNX_Math_Schema, resolve)
16✔
528
{
529
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
16✔
530
    zval *types;
16✔
531
    size_t count;
16✔
532

533
    ZEND_PARSE_PARAMETERS_START(1, -1)
16✔
534
        Z_PARAM_VARIADIC('+', types, count)
16✔
535
    ZEND_PARSE_PARAMETERS_END();
16✔
536

537
    if (count == 0) {
16✔
538
        /* throw */
539
        RETURN_LONG(-1);
×
540
    }
541

542
    if (ort->schema->kind == ORT_MATH_TYPE_PROMOTION_SCHEMA_BINARY) {
16✔
543
        if (count != 2) {
16✔
544
            /* throw */
UNCOV
545
            RETURN_LONG(-1);
×
546
        }
547

548
        if (Z_TYPE(types[0]) != IS_LONG || Z_TYPE(types[1]) != IS_LONG) {
16✔
549
            /* throw */
UNCOV
550
            RETURN_LONG(-1);
×
551
        }
552

553
        ONNXTensorElementDataType result =
16✔
554
            ort_math_type_promotion_schema_resolve_binary(
32✔
555
                ort->schema,
556
                (ONNXTensorElementDataType) Z_LVAL(types[0]),
16✔
557
                (ONNXTensorElementDataType) Z_LVAL(types[1]));
16✔
558
        RETURN_LONG(result);
16✔
UNCOV
559
    } else if (ort->schema->kind == ORT_MATH_TYPE_PROMOTION_SCHEMA_UNARY) {
×
560
        if (count != 1) {
×
UNCOV
561
            RETURN_LONG(-1);
×
562
        }
563

564
        if (Z_TYPE(types[0]) != IS_LONG) {
×
565
            /* throw */
566
            RETURN_LONG(-1);
×
567
        }
568

UNCOV
569
        ONNXTensorElementDataType result =
×
570
            ort_math_type_promotion_schema_resolve_unary(
×
571
                ort->schema,
UNCOV
572
                (ONNXTensorElementDataType) Z_LVAL(types[0]));
×
UNCOV
573
        RETURN_LONG(result);
×
574
    } else {
575
        /* throw */
UNCOV
576
        RETURN_LONG(-1);
×
577
    }
578
}
579

580
static const zend_function_entry php_ort_math_schema_methods[] = {
581
    PHP_ME(ONNX_Math_Schema, __construct, php_ort_math_schema___construct_arginfo, ZEND_ACC_PUBLIC)
582
    PHP_ME(ONNX_Math_Schema, getSymbol,   php_ort_math_schema_getSymbol_arginfo,   ZEND_ACC_PUBLIC)
583
    PHP_ME(ONNX_Math_Schema, getKind,     php_ort_math_schema_getKind_arginfo,     ZEND_ACC_PUBLIC)
584
    PHP_ME(ONNX_Math_Schema, resolve,     php_ort_math_schema_resolve_arginfo,     ZEND_ACC_PUBLIC)
585
    PHP_FE_END
586
}; /* }}} */
587

588
PHP_MINIT_FUNCTION(ORT_MATH)
3,136✔
589
{
590
    ort_math_startup();
3,136✔
591

592
    zend_class_entry ce;
3,136✔
593

594
    INIT_NS_CLASS_ENTRY(ce, "ONNX\\Math", "Schema", php_ort_math_schema_methods);
3,136✔
595
    php_ort_math_schema_ce = zend_register_internal_class(&ce);
3,136✔
596
    php_ort_math_schema_ce->create_object = php_ort_math_schema_create;
3,136✔
597
    php_ort_math_schema_ce->ce_flags |= ZEND_ACC_FINAL;
3,136✔
598

599
    zend_declare_class_constant_long(
3,136✔
600
        php_ort_math_schema_ce,
601
        ZEND_STRL("BINARY"),
602
        ORT_MATH_TYPE_PROMOTION_SCHEMA_BINARY);
603
    zend_declare_class_constant_long(
3,136✔
604
        php_ort_math_schema_ce,
605
        ZEND_STRL("UNARY"),
606
        ORT_MATH_TYPE_PROMOTION_SCHEMA_UNARY);
607

608
    memcpy(&php_ort_math_schema_handlers,
3,136✔
609
        zend_get_std_object_handlers(), sizeof(zend_object_handlers));
610
    php_ort_math_schema_handlers.offset = XtOffsetOf(php_ort_math_schema_t, std);
3,136✔
611
    php_ort_math_schema_handlers.free_obj = php_ort_math_schema_free;
3,136✔
612
    php_ort_math_schema_handlers.get_debug_info = php_ort_math_schema_debug;
3,136✔
613
    php_ort_math_schema_handlers.clone_obj = NULL; // No cloning support
3,136✔
614

615
    zend_register_functions(NULL,
3,136✔
616
        php_ort_math_functions, NULL, MODULE_PERSISTENT);
617

618
    return SUCCESS;
3,136✔
619
}
620

621
PHP_RINIT_FUNCTION(ORT_MATH)
3,136✔
622
{
623
    ort_math_activate();
3,136✔
624

625
    return SUCCESS;
3,136✔
626
}
627

628
PHP_RSHUTDOWN_FUNCTION(ORT_MATH)
3,136✔
629
{
630
    ort_math_deactivate();
3,136✔
631

632
    return SUCCESS;
3,136✔
633
}
634

635
PHP_MSHUTDOWN_FUNCTION(ORT_MATH)
3,136✔
636
{
637
    ort_math_shutdown();
3,136✔
638

639
    return SUCCESS;
3,136✔
640
}
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