-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDType.php
More file actions
382 lines (338 loc) · 10.8 KB
/
Copy pathDType.php
File metadata and controls
382 lines (338 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
declare(strict_types=1);
namespace PhpMlKit\NDArray;
use FFI\CData;
use PhpMlKit\NDArray\FFI\Lib;
/**
* Data type enumeration for NDArray elements.
*
* Integer values must stay in sync with rust/src/dtype.rs
*/
enum DType: int
{
// Signed integers
case Int8 = 0;
case Int16 = 1;
case Int32 = 2;
case Int64 = 3;
// Unsigned integers
case UInt8 = 4;
case UInt16 = 5;
case UInt32 = 6;
case UInt64 = 7;
// Floating-point
case Float32 = 8;
case Float64 = 9;
// Boolean
case Bool = 10;
// Complex numbers
case Complex64 = 11;
case Complex128 = 12;
/**
* Get the C FFI type string for this dtype.
*/
public function ffiType(): string
{
return match ($this) {
self::Int8 => 'int8_t',
self::Int16 => 'int16_t',
self::Int32 => 'int32_t',
self::Int64 => 'int64_t',
self::UInt8 => 'uint8_t',
self::UInt16 => 'uint16_t',
self::UInt32 => 'uint32_t',
self::UInt64 => 'uint64_t',
self::Float32 => 'float',
self::Float64 => 'double',
self::Bool => 'uint8_t',
self::Complex64 => 'float',
self::Complex128 => 'double',
};
}
/**
* Get the size of each element in bytes.
*/
public function itemSize(): int
{
return match ($this) {
self::Int8, self::UInt8, self::Bool => 1,
self::Int16, self::UInt16 => 2,
self::Int32, self::UInt32, self::Float32 => 4,
self::Int64, self::UInt64, self::Float64 => 8,
self::Complex64 => 8,
self::Complex128 => 16,
};
}
/**
* Check if this dtype is a signed integer type.
*/
public function isSigned(): bool
{
return match ($this) {
self::Int8, self::Int16, self::Int32, self::Int64 => true,
default => false,
};
}
/**
* Check if this dtype is an unsigned integer type.
*/
public function isUnsigned(): bool
{
return match ($this) {
self::UInt8, self::UInt16, self::UInt32, self::UInt64 => true,
default => false,
};
}
/**
* Check if this dtype is an integer type (signed or unsigned).
*/
public function isInteger(): bool
{
return $this->isSigned() || $this->isUnsigned();
}
/**
* Check if this dtype is a floating-point type.
*/
public function isFloat(): bool
{
return match ($this) {
self::Float32, self::Float64 => true,
default => false,
};
}
/**
* Check if this dtype is a complex number type.
*/
public function isComplex(): bool
{
return match ($this) {
self::Complex64, self::Complex128 => true,
default => false,
};
}
/**
* Check if this dtype is a boolean type.
*/
public function isBool(): bool
{
return self::Bool === $this;
}
/**
* Infer dtype from a PHP value.
*
* Uses NumPy conventions: bool -> Bool, int -> Int64, float -> Float64
*/
public static function fromValue(mixed $value): self
{
if ($value instanceof Complex) {
return self::Complex128;
}
if (\is_bool($value)) {
return self::Bool;
}
if (\is_int($value)) {
return self::Int64;
}
if (\is_float($value)) {
return self::Float64;
}
throw new \InvalidArgumentException(
\sprintf(
"Cannot infer dtype from value of type '%s'. Expected bool, int, float, or Complex.",
get_debug_type($value)
)
);
}
/**
* Infer dtype from a PHP array, checking all elements.
*
* Type promotion: float > int > bool. Empty arrays default to Float64.
*
* @param array<mixed> $data
*/
public static function fromArray(array $data): self
{
if (empty($data)) {
return self::Float64;
}
$hasComplex = false;
$hasFloat = false;
$hasInt = false;
$hasBool = false;
$stack = [$data];
while ($stack) {
$current = array_pop($stack);
foreach ($current as $item) {
if (\is_array($item)) {
$stack[] = $item;
} elseif ($item instanceof Complex) {
$hasComplex = true;
} elseif (\is_float($item)) {
$hasFloat = true;
} elseif (\is_int($item)) {
$hasInt = true;
} elseif (\is_bool($item)) {
$hasBool = true;
} else {
throw new \InvalidArgumentException(
\sprintf(
"Cannot infer dtype: array contains unsupported type '%s'.",
get_debug_type($item)
)
);
}
}
}
if ($hasComplex) {
return self::Complex128;
}
if ($hasFloat) {
return self::Float64;
}
if ($hasInt) {
return self::Int64;
}
if ($hasBool) {
return self::Bool;
}
return self::Float64;
}
/**
* Get the minimum value representable by this dtype.
*/
public function minValue(): Complex|float|int
{
return match ($this) {
self::Int8 => -128,
self::Int16 => -32768,
self::Int32 => -2147483648,
self::Int64 => \PHP_INT_MIN,
self::UInt8, self::UInt16, self::UInt32, self::UInt64 => 0,
self::Float32 => -3.4028235e+38,
self::Float64 => -\PHP_FLOAT_MAX,
self::Bool => 0,
self::Complex64 => new Complex(-3.4028235e+38, -3.4028235e+38),
self::Complex128 => new Complex(-\PHP_FLOAT_MAX, -\PHP_FLOAT_MAX),
};
}
/**
* Get the maximum value representable by this dtype.
*/
public function maxValue(): Complex|float|int
{
return match ($this) {
self::Int8 => 127,
self::Int16 => 32767,
self::Int32 => 2147483647,
self::Int64 => \PHP_INT_MAX,
self::UInt8 => 255,
self::UInt16 => 65535,
self::UInt32 => 4294967295,
self::UInt64 => \PHP_INT_MAX,
self::Float32 => 3.4028235e+38,
self::Float64 => \PHP_FLOAT_MAX,
self::Bool => 1,
self::Complex64 => new Complex(3.4028235e+38, 3.4028235e+38),
self::Complex128 => new Complex(\PHP_FLOAT_MAX, \PHP_FLOAT_MAX),
};
}
/**
* Create an FFI C value container for this dtype.
*
* If a value is provided, it will be stored in the C value with appropriate
* type handling (e.g., bool is converted to 1/0).
*
* @param null|bool|Complex|float|int $value Optional value to store
*
* @return CData FFI CData representing this dtype
*/
public function createCValue(bool|Complex|float|int|null $value = null): CData
{
$lib = Lib::get();
if ($this->isComplex()) {
$cArray = $lib->new("{$this->ffiType()}[2]");
if (null !== $value) {
$complex = $value instanceof Complex ? $value : new Complex((float) $value);
$cArray[0] = $complex->real;
$cArray[1] = $complex->imag;
}
return $cArray;
}
$cValue = $lib->new($this->ffiType());
if (null !== $value) {
$cValue->cdata = $this->isBool() ? ($value ? 1 : 0) : $value;
}
return $cValue;
}
/**
* Create an FFI C array for this dtype.
*
* If $values is non-empty, the array will be initialized with those values;
* otherwise, a zero-filled array of the given element count will be allocated.
* For complex types, the array length is $elementCount * 2 (real, imag).
*
* @param int $elementCount Number of logical elements (not primitives); e.g., 4 means 4 floats or 4 complex numbers.
* @param array<mixed> $values Optional values to initialize the array with. Ignored if empty.
*
* @return CData allocated FFI C array for this dtype (primitive count, not logical elements, for complex types)
*/
public function createCArray(int $elementCount, array $values = []): CData
{
$lib = Lib::get();
if (!empty($values)) {
$cArray = $lib->new("{$this->ffiType()}[".\count($values).']');
foreach ($values as $i => $value) {
$cArray[$i] = $value;
}
return $cArray;
}
$primitiveCount = $this->isComplex() ? $elementCount * 2 : $elementCount;
return $lib->new("{$this->ffiType()}[{$primitiveCount}]");
}
/**
* Cast an FFI C value to the appropriate PHP type for this dtype.
*
* Handles type conversion: bools are cast from integer representation,
* floats are cast to float, integers to int, complex to Complex object.
*
* @param null|bool|CData|float|int|string $cValue FFI CData containing the value
*
* @return bool|Complex|float|int The value cast to the appropriate PHP type
*/
public function castFromCValue(bool|CData|float|int|string|null $cValue): bool|Complex|float|int
{
return match ($this) {
self::Float64, self::Float32 => (float) $cValue,
self::Bool => (bool) $cValue,
self::Complex64 => new Complex((float) $cValue[0], (float) $cValue[1]),
self::Complex128 => new Complex((float) $cValue[0], (float) $cValue[1]),
default => (int) $cValue,
};
}
/**
* Prepare array values for FFI by converting bools to 1/0 and complex to flat pairs.
*
* This is necessary because FFI bools are represented as uint8_t,
* so boolean values must be converted to integers.
*
* @param array<bool|Complex|float|int> $values Array of values to prepare
*
* @return array<float|int> Prepared values with bools converted and complex flattened
*/
public function prepareArrayValues(array $values): array
{
if ($this->isComplex()) {
$result = [];
foreach ($values as $value) {
$complex = $value instanceof Complex ? $value : new Complex((float) $value);
$result[] = $complex->real;
$result[] = $complex->imag;
}
return $result;
}
if (!$this->isBool()) {
return $values;
}
return array_map(static fn ($v) => $v ? 1 : 0, $values);
}
}