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

krakjoe / ort / 16251964329

13 Jul 2025 06:03PM UTC coverage: 92.432% (+0.01%) from 92.421%
16251964329

push

github

krakjoe
tanh schema

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

16 existing lines in 1 file now uncovered.

5154 of 5576 relevant lines covered (92.43%)

69015.64 hits per line

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

88.18
/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/matmul.h"
36
#include "maths/schema/max.h"
37
#include "maths/schema/mean.h"
38
#include "maths/schema/min.h"
39
#include "maths/schema/mod.h"
40
#include "maths/schema/mul.h"
41
#include "maths/schema/neg.h"
42
#include "maths/schema/pow.h"
43
#include "maths/schema/recip.h"
44
#include "maths/schema/sign.h"
45
#include "maths/schema/sin.h"
46
#include "maths/schema/sinh.h"
47
#include "maths/schema/softmax.h"
48
#include "maths/schema/sqrt.h"
49
#include "maths/schema/sub.h"
50
#include "maths/schema/sum.h"
51
#include "maths/schema/tan.h"
52
#include "maths/schema/tanh.h"
53

54
typedef struct _php_ort_math_schema_t {
55
    const ort_math_type_promotion_schema_t* schema;
56
    zend_string*                            symbol;
57
    zend_object std;
58
} php_ort_math_schema_t;
59

60
zend_class_entry *php_ort_math_schema_ce;
61
zend_object_handlers php_ort_math_schema_handlers;
62

63
static zend_always_inline php_ort_math_schema_t* php_ort_math_schema_fetch(zend_object *o) {
64
    return (php_ort_math_schema_t*) (((char*) o) - XtOffsetOf(php_ort_math_schema_t, std));
65
}
66

67
static zend_object* php_ort_math_schema_create(zend_class_entry *ce) {
464✔
68
    php_ort_math_schema_t *ort = 
464✔
69
        zend_object_alloc(sizeof(php_ort_math_schema_t), ce);
464✔
70
        
71
    zend_object_std_init(&ort->std, ce);
464✔
72
    object_properties_init(&ort->std, ce);
464✔
73

74
    ort->std.handlers = &php_ort_math_schema_handlers;
464✔
75
    return &ort->std;
464✔
76
}
77

UNCOV
78
static HashTable* php_ort_math_schema_debug(zend_object *zo, int *temp) {
×
79
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(zo);
×
80
    HashTable *debug;
×
81

82
    ALLOC_HASHTABLE(debug);
×
83
    zend_hash_init(debug, 3, NULL, ZVAL_PTR_DTOR, 0);
×
84

85
    if (ort->symbol) {
×
UNCOV
86
        zval symbol;
×
87

UNCOV
88
        ZVAL_STR_COPY(&symbol, ort->symbol);
×
89

UNCOV
90
        zend_hash_str_update(debug,
×
91
            "symbol", sizeof("symbol") - 1,
92
            &symbol);
93
    }
94

95
__php_ort_tensor_debug_return:
×
UNCOV
96
    *temp = 1;
×
97

UNCOV
98
    return debug;
×
99
}
100

101
void php_ort_math_schema_free(zend_object *zo) {
464✔
102
    php_ort_math_schema_t* ort = php_ort_math_schema_fetch(zo);
464✔
103

104
    if (ort->symbol) {
464✔
105
        zend_string_release(ort->symbol);
464✔
106
    }
107

108
    zend_object_std_dtor(zo);
464✔
109
}
464✔
110

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

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

177

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

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

235
ORT_MATH_BINARY_FUNCTION_IMPL(add)
16,896✔
236
ORT_MATH_BINARY_FUNCTION_IMPL(multiply)
4,112✔
237
ORT_MATH_BINARY_FUNCTION_IMPL(subtract)
1,936✔
238
ORT_MATH_BINARY_FUNCTION_IMPL(divide)
3,456✔
239

240
ORT_MATH_BINARY_FUNCTION_IMPL(pow)
2,448✔
241
ORT_MATH_BINARY_FUNCTION_IMPL(mod)
2,496✔
242

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

268
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(min)
320✔
269
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(min)
2,176✔
270

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

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

277
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(sum)
224✔
278
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(sum)
2,304✔
279

280
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(softmax)
832✔
281

282
/* Dot reduction function */
283
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(php_ort_math_dot_arginfo, 0, 1, ONNX\\Tensor, 0)
284
    ZEND_ARG_OBJ_INFO(0, tensor_a, ONNX\\Tensor, 0)
285
    ZEND_ARG_OBJ_INFO(0, tensor_b, ONNX\\Tensor, 0)
286
ZEND_END_ARG_INFO()
287

288
PHP_FUNCTION(dot)
288✔
289
{
290
    zval *tensor_a, *tensor_b;
288✔
291

292
    ZEND_PARSE_PARAMETERS_START(2, 2)
288✔
293
        Z_PARAM_OBJECT_OF_CLASS(tensor_a, php_ort_tensor_interface_ce)
576✔
294
        Z_PARAM_OBJECT_OF_CLASS(tensor_b, php_ort_tensor_interface_ce)
576✔
295
    ZEND_PARSE_PARAMETERS_END();
288✔
296

297
    php_ort_tensor_t* tensor_a_ort = php_ort_tensor_fetch(Z_OBJ_P(tensor_a));
288✔
298
    php_ort_tensor_t* tensor_b_ort = php_ort_tensor_fetch(Z_OBJ_P(tensor_b));
288✔
299
    ort_tensor_t* result = ort_math_result_dot(
288✔
300
        tensor_a_ort->object, tensor_b_ort->object);
301

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

310
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(php_ort_math_matmul_arginfo, 0, 2, ONNX\\Tensor, 0)
311
    ZEND_ARG_OBJ_INFO(0, matrix_a, ONNX\\Tensor, 0)
312
    ZEND_ARG_OBJ_INFO(0, matrix_b, ONNX\\Tensor, 0)
313
ZEND_END_ARG_INFO()
314

315
PHP_FUNCTION(matmul)
672✔
316
{
317
    zval *matrix_a_zv, *matrix_b_zv;
672✔
318

319
    ZEND_PARSE_PARAMETERS_START(2, 2)
672✔
320
        Z_PARAM_OBJECT_OF_CLASS(matrix_a_zv, php_ort_tensor_interface_ce)
1,344✔
321
        Z_PARAM_OBJECT_OF_CLASS(matrix_b_zv, php_ort_tensor_interface_ce)
1,344✔
322
    ZEND_PARSE_PARAMETERS_END();
672✔
323

324
    php_ort_tensor_t* matrix_a_ort = php_ort_tensor_fetch(Z_OBJ_P(matrix_a_zv));
672✔
325
    php_ort_tensor_t* matrix_b_ort = php_ort_tensor_fetch(Z_OBJ_P(matrix_b_zv));
672✔
326
    
327
    ort_tensor_t* result = ort_math_result_matmul(matrix_a_ort->object, matrix_b_ort->object);
672✔
328

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

336
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(php_ort_math_backend_arginfo, 0, 0, MAY_BE_STRING|MAY_BE_FALSE)
337
ZEND_END_ARG_INFO()
338

339
PHP_FUNCTION(backend)
16✔
340
{
341
    ZEND_PARSE_PARAMETERS_NONE();
16✔
342

343
#ifdef ORT_BACKEND_ENABLED
344
    RETURN_STRING(ORT_BACKEND_NAME);
12✔
345
#else
346
    RETURN_FALSE;
4✔
347
#endif
348
}
349

350
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(php_ort_math_cores_arginfo, 0, 0, IS_LONG, 0)
351
ZEND_END_ARG_INFO()
352

353
PHP_FUNCTION(cores)
16✔
354
{
355
    ZEND_PARSE_PARAMETERS_NONE();
16✔
356

357
    RETURN_LONG(
16✔
358
        ort_pool_cores());
359
}
360

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

392
    ZEND_NS_FE("ONNX\\Math", neg,     php_ort_math_neg_arginfo)
393
    ZEND_NS_FE("ONNX\\Math", recip,   php_ort_math_recip_arginfo)
394
    ZEND_NS_FE("ONNX\\Math", trunc,   php_ort_math_trunc_arginfo)
395

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

406
    ZEND_NS_FE("ONNX\\Math", dot, php_ort_math_dot_arginfo)
407

408
    ZEND_NS_FE("ONNX\\Math", backend, php_ort_math_backend_arginfo)
409
    ZEND_NS_FE("ONNX\\Math", cores,   php_ort_math_cores_arginfo)
410
    ZEND_FE_END
411
};
412

413
/* {{{ */
414
ZEND_BEGIN_ARG_INFO_EX(php_ort_math_schema___construct_arginfo, 0, 0, 1)
415
    ZEND_ARG_TYPE_INFO(0, symbol, IS_STRING, 0)
416
ZEND_END_ARG_INFO()
417

418
PHP_METHOD(ONNX_Math_Schema, __construct)
464✔
419
{
420
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
464✔
421
    zend_string *symbol;
464✔
422

423
    ZEND_PARSE_PARAMETERS_START(1, 1)
464✔
424
        Z_PARAM_STR(symbol)
928✔
425
    ZEND_PARSE_PARAMETERS_END();
464✔
426

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

484
    ort->symbol = zend_string_copy(symbol);
880✔
485
}
486

487
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(php_ort_math_schema_getSymbol_arginfo, 0, 0, MAY_BE_STRING|MAY_BE_NULL)
488
ZEND_END_ARG_INFO()
489

490
PHP_METHOD(ONNX_Math_Schema, getSymbol)
16✔
491
{
492
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
16✔
493

494
    if (!ort->symbol) {
16✔
495
        return;
496
    }
497

498
    RETURN_STR_COPY(ort->symbol);
16✔
499
}
500

501
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(php_ort_math_schema_getKind_arginfo, 0, 0, IS_LONG, 0)
502
ZEND_END_ARG_INFO()
503

504
PHP_METHOD(ONNX_Math_Schema, getKind)
16✔
505
{
506
    php_ort_math_schema_t *ort =
16✔
507
        php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
16✔
508

509
    ZEND_PARSE_PARAMETERS_NONE();
16✔
510

511
    RETURN_LONG(ort->schema->kind);
16✔
512
}
513

514
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(php_ort_math_schema_resolve_arginfo, 0, 1, IS_LONG, 0)
515
    ZEND_ARG_VARIADIC_INFO(0, types)
516
ZEND_END_ARG_INFO()
517

518
PHP_METHOD(ONNX_Math_Schema, resolve)
16✔
519
{
520
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
16✔
521
    zval *types;
16✔
522
    size_t count;
16✔
523

524
    ZEND_PARSE_PARAMETERS_START(1, -1)
16✔
525
        Z_PARAM_VARIADIC('+', types, count)
16✔
526
    ZEND_PARSE_PARAMETERS_END();
16✔
527

528
    if (count == 0) {
16✔
529
        /* throw */
UNCOV
530
        RETURN_LONG(-1);
×
531
    }
532

533
    if (ort->schema->kind == ORT_MATH_TYPE_PROMOTION_SCHEMA_BINARY) {
16✔
534
        if (count != 2) {
16✔
535
            /* throw */
UNCOV
536
            RETURN_LONG(-1);
×
537
        }
538

539
        if (Z_TYPE(types[0]) != IS_LONG || Z_TYPE(types[1]) != IS_LONG) {
16✔
540
            /* throw */
541
            RETURN_LONG(-1);
×
542
        }
543

544
        ONNXTensorElementDataType result =
16✔
545
            ort_math_type_promotion_schema_resolve_binary(
32✔
546
                ort->schema,
547
                (ONNXTensorElementDataType) Z_LVAL(types[0]),
16✔
548
                (ONNXTensorElementDataType) Z_LVAL(types[1]));
16✔
549
        RETURN_LONG(result);
16✔
UNCOV
550
    } else if (ort->schema->kind == ORT_MATH_TYPE_PROMOTION_SCHEMA_UNARY) {
×
551
        if (count != 1) {
×
552
            RETURN_LONG(-1);
×
553
        }
554

555
        if (Z_TYPE(types[0]) != IS_LONG) {
×
556
            /* throw */
UNCOV
557
            RETURN_LONG(-1);
×
558
        }
559

UNCOV
560
        ONNXTensorElementDataType result =
×
UNCOV
561
            ort_math_type_promotion_schema_resolve_unary(
×
562
                ort->schema,
UNCOV
563
                (ONNXTensorElementDataType) Z_LVAL(types[0]));
×
UNCOV
564
        RETURN_LONG(result);
×
565
    } else {
566
        /* throw */
UNCOV
567
        RETURN_LONG(-1);
×
568
    }
569
}
570

571
static const zend_function_entry php_ort_math_schema_methods[] = {
572
    PHP_ME(ONNX_Math_Schema, __construct, php_ort_math_schema___construct_arginfo, ZEND_ACC_PUBLIC)
573
    PHP_ME(ONNX_Math_Schema, getSymbol,   php_ort_math_schema_getSymbol_arginfo,   ZEND_ACC_PUBLIC)
574
    PHP_ME(ONNX_Math_Schema, getKind,     php_ort_math_schema_getKind_arginfo,     ZEND_ACC_PUBLIC)
575
    PHP_ME(ONNX_Math_Schema, resolve,     php_ort_math_schema_resolve_arginfo,     ZEND_ACC_PUBLIC)
576
    PHP_FE_END
577
}; /* }}} */
578

579
PHP_MINIT_FUNCTION(ORT_MATH)
3,136✔
580
{
581
    ort_math_startup();
3,136✔
582

583
    zend_class_entry ce;
3,136✔
584

585
    INIT_NS_CLASS_ENTRY(ce, "ONNX\\Math", "Schema", php_ort_math_schema_methods);
3,136✔
586
    php_ort_math_schema_ce = zend_register_internal_class(&ce);
3,136✔
587
    php_ort_math_schema_ce->create_object = php_ort_math_schema_create;
3,136✔
588
    php_ort_math_schema_ce->ce_flags |= ZEND_ACC_FINAL;
3,136✔
589

590
    zend_declare_class_constant_long(
3,136✔
591
        php_ort_math_schema_ce,
592
        ZEND_STRL("BINARY"),
593
        ORT_MATH_TYPE_PROMOTION_SCHEMA_BINARY);
594
    zend_declare_class_constant_long(
3,136✔
595
        php_ort_math_schema_ce,
596
        ZEND_STRL("UNARY"),
597
        ORT_MATH_TYPE_PROMOTION_SCHEMA_UNARY);
598

599
    memcpy(&php_ort_math_schema_handlers,
3,136✔
600
        zend_get_std_object_handlers(), sizeof(zend_object_handlers));
601
    php_ort_math_schema_handlers.offset = XtOffsetOf(php_ort_math_schema_t, std);
3,136✔
602
    php_ort_math_schema_handlers.free_obj = php_ort_math_schema_free;
3,136✔
603
    php_ort_math_schema_handlers.get_debug_info = php_ort_math_schema_debug;
3,136✔
604
    php_ort_math_schema_handlers.clone_obj = NULL; // No cloning support
3,136✔
605

606
    zend_register_functions(NULL,
3,136✔
607
        php_ort_math_functions, NULL, MODULE_PERSISTENT);
608

609
    return SUCCESS;
3,136✔
610
}
611

612
PHP_RINIT_FUNCTION(ORT_MATH)
3,136✔
613
{
614
    ort_math_activate();
3,136✔
615

616
    return SUCCESS;
3,136✔
617
}
618

619
PHP_RSHUTDOWN_FUNCTION(ORT_MATH)
3,136✔
620
{
621
    ort_math_deactivate();
3,136✔
622

623
    return SUCCESS;
3,136✔
624
}
625

626
PHP_MSHUTDOWN_FUNCTION(ORT_MATH)
3,136✔
627
{
628
    ort_math_shutdown();
3,136✔
629

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