orka_b455160b/orka_tensors_gpu/src/orka-numerics-tensors-cs_gpu.ads

  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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
--  SPDX-License-Identifier: Apache-2.0
--
--  Copyright (c) 2023 onox <denkpadje@gmail.com>
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.

private with Ada.Finalization;

private with Orka.Rendering.Buffers;

with Orka.Resources.Locations;

generic
package Orka.Numerics.Tensors.CS_GPU is
   pragma Preelaborate;

   procedure Initialize_Shaders
     (Prefix_Sum, Tensors_GPU : Resources.Locations.Location_Ptr);

   type GPU_Tensor (<>) is new Tensor with private;

   overriding procedure Materialize (Object : in out GPU_Tensor);

   overriding function Is_Materialized (Object : GPU_Tensor) return Boolean;

   overriding function Kind (Object : GPU_Tensor) return Data_Type;

   overriding function Get (Object : GPU_Tensor; Index : Index_Type) return Element;
   overriding function Get (Object : GPU_Tensor; Index : Index_Type) return Boolean;
   overriding function Get (Object : GPU_Tensor; Index : Index_Type) return GPU_Tensor;

   overriding function Get (Object : GPU_Tensor; Index : Range_Type) return GPU_Tensor;

   overriding function Get (Object : GPU_Tensor; Index : Tensor_Index) return Element;
   overriding function Get (Object : GPU_Tensor; Index : Tensor_Index) return Boolean;
   overriding function Get (Object : GPU_Tensor; Index : Tensor_Range) return GPU_Tensor;

   overriding function Get (Object : GPU_Tensor; Index : GPU_Tensor) return GPU_Tensor;

   ----------------------------------------------------------------------------

   overriding procedure Set (Object : in out GPU_Tensor; Index : Index_Type; Value : Element);
   overriding procedure Set (Object : in out GPU_Tensor; Index : Index_Type; Value : Boolean);

   overriding procedure Set (Object : in out GPU_Tensor; Index : Tensor_Index; Value : Element);
   overriding procedure Set (Object : in out GPU_Tensor; Index : Tensor_Index; Value : Boolean);

   overriding procedure Set (Object : in out GPU_Tensor; Index : Index_Type;   Value : GPU_Tensor);
   overriding procedure Set (Object : in out GPU_Tensor; Index : Range_Type;   Value : GPU_Tensor);
   overriding procedure Set (Object : in out GPU_Tensor; Index : Tensor_Range; Value : GPU_Tensor);

   ----------------------------------------------------------------------------

   overriding
   function Image (Object : GPU_Tensor) return String;

   overriding
   function Shape (Object : GPU_Tensor) return Tensor_Shape;

   overriding
   function Elements (Object : GPU_Tensor) return Natural;

   overriding
   function Axes (Object : GPU_Tensor) return Tensor_Axis;

   ----------------------------------------------------------------------------

   overriding
   function Empty (Shape : Tensor_Shape) return GPU_Tensor;

   overriding
   function Fill (Shape : Tensor_Shape; Value : Element) return GPU_Tensor;

   overriding
   function Zeros (Elements : Positive) return GPU_Tensor;

   overriding
   function Zeros (Shape : Tensor_Shape) return GPU_Tensor;

   overriding
   function Ones (Elements : Positive) return GPU_Tensor;

   overriding
   function Ones (Shape : Tensor_Shape) return GPU_Tensor;

   overriding
   function To_Tensor (Elements : Element_Array; Shape : Tensor_Shape) return GPU_Tensor;

   overriding
   function To_Tensor (Elements : Element_Array) return GPU_Tensor;

   overriding
   function To_Boolean_Tensor
     (Booleans : Boolean_Array;
      Shape    : Tensor_Shape) return GPU_Tensor;

   overriding
   function To_Boolean_Tensor (Booleans : Boolean_Array) return GPU_Tensor;

   overriding
   function Array_Range (Stop : Element) return GPU_Tensor;

   overriding
   function Array_Range (Start, Stop : Element; Step : Element := 1.0) return GPU_Tensor;

   overriding
   function Linear_Space
     (Start, Stop : Element;
      Count       : Positive;
      Interval    : Interval_Kind := Closed) return GPU_Tensor;

   overriding
   function Log_Space
     (Start, Stop : Element;
      Count       : Positive;
      Interval    : Interval_Kind := Closed;
      Base        : Element := 10.0) return GPU_Tensor;

   overriding
   function Geometric_Space
     (Start, Stop : Element;
      Count       : Positive;
      Interval    : Interval_Kind := Closed;
      Base        : Element := 10.0) return GPU_Tensor;

   overriding
   function Identity (Size : Positive; Offset : Integer := 0) return GPU_Tensor;

   overriding
   function Identity (Rows, Columns : Positive; Offset : Integer := 0) return GPU_Tensor;

   overriding
   function Upper_Triangular (Object : GPU_Tensor; Offset : Integer := 0) return GPU_Tensor;

   overriding
   function Main_Diagonal (Object : GPU_Tensor; Offset : Integer := 0) return GPU_Tensor;

   overriding
   function Diagonal (Elements : Element_Array; Offset : Integer := 0) return GPU_Tensor;

   overriding
   function Diagonal (Elements : GPU_Tensor; Offset : Integer := 0) return GPU_Tensor;

   overriding
   function Trace (Object : GPU_Tensor; Offset : Integer := 0) return Element;

   overriding
   function Reshape (Object : GPU_Tensor; Shape : Tensor_Shape) return GPU_Tensor;

   overriding
   function Reshape (Object : GPU_Tensor; Elements : Positive) return GPU_Tensor;

   overriding
   function Flatten (Object : GPU_Tensor) return GPU_Tensor;

   overriding
   function Concatenate
     (Left, Right : GPU_Tensor;
      Axis   : Tensor_Axis) return GPU_Tensor;

   overriding
   function "&" (Left, Right : GPU_Tensor) return GPU_Tensor;

   ----------------------------------------------------------------------------
   --                            Matrix operations                           --
   ----------------------------------------------------------------------------

   overriding
   function "*" (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding
   function "*" (Left, Right : GPU_Tensor) return Element;

   overriding
   function "**" (Left : GPU_Tensor; Right : Integer) return GPU_Tensor;

   overriding
   function Outer (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding
   function Inverse (Object : GPU_Tensor) return GPU_Tensor;

   overriding
   function Transpose (Object : GPU_Tensor) return GPU_Tensor;

   overriding
   function Solve (A, B : GPU_Tensor; Solution : out Solution_Kind) return GPU_Tensor;

   overriding
   function Solve (A, B : GPU_Tensor; Form : Triangular_Form) return GPU_Tensor;

   overriding
   function Divide_By (B, A : GPU_Tensor) return GPU_Tensor;

   overriding
   function Divide_By (B, A : GPU_Tensor; Form : Triangular_Form) return GPU_Tensor;

   type GPU_QR_Factorization (<>) is new QR_Factorization with private;

   overriding
   function Determinancy (Object : GPU_QR_Factorization) return Matrix_Determinancy;

   function Q (Object : GPU_QR_Factorization'Class) return GPU_Tensor;
   function R (Object : GPU_QR_Factorization'Class) return GPU_Tensor;

   overriding
   function QR (Object : GPU_Tensor) return GPU_Tensor;

   overriding
   function QR (Object : GPU_Tensor; Mode : QR_Mode := Reduced) return QR_Factorization'Class;

   overriding
   function QR_For_Least_Squares (Object : GPU_Tensor) return QR_Factorization'Class;

   overriding
   function Least_Squares (Object : QR_Factorization'Class; B : GPU_Tensor) return GPU_Tensor;
--     with Pre'Class => GPU_QR_Factorization (Object).Q.Rows = B.Rows;
   --  Note: commented to avoid GNAT bug box

   overriding
   function Least_Squares (A, B : GPU_Tensor) return GPU_Tensor;

   overriding
   function Constrained_Least_Squares (A, B, C, D : GPU_Tensor) return GPU_Tensor;

   overriding
   function Cholesky (Object : GPU_Tensor; Form : Triangular_Form := Lower) return GPU_Tensor;

   overriding
   function Cholesky_Update
     (R, V : GPU_Tensor;
      Mode : Update_Mode) return GPU_Tensor;

   ----------------------------------------------------------------------------
   --                            Vector operations                           --
   ----------------------------------------------------------------------------

   overriding function Norm (Object : GPU_Tensor) return Element;

   overriding function Normalize (Object : GPU_Tensor) return GPU_Tensor;

   overriding function Standardize (Object : GPU_Tensor) return GPU_Tensor;

   overriding
   function Correlation_Coefficient (Left, Right : GPU_Tensor) return Correlation_Element;

   ----------------------------------------------------------------------------
   --                         Element-wise operations                        --
   ----------------------------------------------------------------------------

   overriding function "+" (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding function "-" (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding function "/" (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding function Divide_Or_Zero (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding function "**" (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding function "**" (Left : GPU_Tensor; Right : Element) return GPU_Tensor;
   overriding function "**" (Left : Element; Right : GPU_Tensor) return GPU_Tensor;

   overriding function "*" (Left : Element; Right : GPU_Tensor) return GPU_Tensor;
   overriding function "*" (Left : GPU_Tensor; Right : Element) return GPU_Tensor;

   overriding function "/" (Left : Element; Right : GPU_Tensor) return GPU_Tensor;
   overriding function "/" (Left : GPU_Tensor; Right : Element) return GPU_Tensor;

   overriding function "+" (Left : Element; Right : GPU_Tensor) return GPU_Tensor;
   overriding function "+" (Left : GPU_Tensor; Right : Element) return GPU_Tensor;

   overriding function "-" (Left : Element; Right : GPU_Tensor) return GPU_Tensor;
   overriding function "-" (Left : GPU_Tensor; Right : Element) return GPU_Tensor;

   overriding function "-" (Object : GPU_Tensor) return GPU_Tensor;

   overriding function "mod" (Left, Right : GPU_Tensor) return GPU_Tensor;
   overriding function "rem" (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding function "mod" (Left : GPU_Tensor; Right : Element) return GPU_Tensor;
   overriding function "rem" (Left : GPU_Tensor; Right : Element) return GPU_Tensor;

   overriding function "abs" (Object : GPU_Tensor) return GPU_Tensor;

   ----------------------------------------------------------------------------

   overriding function Add (Left, Right : GPU_Tensor) return GPU_Tensor renames "+";

   overriding function Subtract (Left, Right : GPU_Tensor) return GPU_Tensor renames "-";

   overriding function Multiply (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding function Power (Left : GPU_Tensor; Right : Integer) return GPU_Tensor;

   overriding function Divide (Left, Right : GPU_Tensor) return GPU_Tensor renames "/";

   ----------------------------------------------------------------------------

   overriding function Min (Left : Element; Right : GPU_Tensor) return GPU_Tensor;
   overriding function Min (Left : GPU_Tensor; Right : Element) return GPU_Tensor;

   overriding function Max (Left : Element; Right : GPU_Tensor) return GPU_Tensor;
   overriding function Max (Left : GPU_Tensor; Right : Element) return GPU_Tensor;

   overriding function Sqrt (Object : GPU_Tensor) return GPU_Tensor;

   overriding function Ceil  (Object : GPU_Tensor) return GPU_Tensor;
   overriding function Floor (Object : GPU_Tensor) return GPU_Tensor;
   overriding function Round (Object : GPU_Tensor) return GPU_Tensor;
   overriding function Truncate (Object : GPU_Tensor) return GPU_Tensor;

   overriding function Exp   (Object : GPU_Tensor) return GPU_Tensor;
   overriding function Log   (Object : GPU_Tensor) return GPU_Tensor;
   overriding function Log10 (Object : GPU_Tensor) return GPU_Tensor;
   overriding function Log2  (Object : GPU_Tensor) return GPU_Tensor;

   ----------------------------------------------------------------------------
   --                              Trigonometry                              --
   ----------------------------------------------------------------------------

   overriding function Sin (Object : GPU_Tensor) return GPU_Tensor;
   overriding function Cos (Object : GPU_Tensor) return GPU_Tensor;
   overriding function Tan (Object : GPU_Tensor) return GPU_Tensor;

   overriding function Arcsin (Object : GPU_Tensor) return GPU_Tensor;
   overriding function Arccos (Object : GPU_Tensor) return GPU_Tensor;
   overriding function Arctan (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding function Degrees (Object : GPU_Tensor) return GPU_Tensor;
   overriding function Radians (Object : GPU_Tensor) return GPU_Tensor;

   ----------------------------------------------------------------------------

   overriding
   function Reduce_Associative
     (Object  : GPU_Tensor;
      Subject : Expression'Class;
      Initial : Element) return Element;

   overriding
   function Reduce_Associative
     (Object  : GPU_Tensor;
      Subject : Expression'Class;
      Initial : Element;
      Axis    : Tensor_Axis) return GPU_Tensor;

   overriding
   function Reduce
     (Object  : GPU_Tensor;
      Subject : Expression'Class;
      Initial : Element) return Element;

   overriding
   function Reduce
     (Object  : GPU_Tensor;
      Subject : Expression'Class;
      Initial : Element;
      Axis    : Tensor_Axis) return GPU_Tensor;

   overriding function Sum (Object : GPU_Tensor) return Element;

   overriding function Product (Object : GPU_Tensor) return Element;

   overriding
   function Sum (Object : GPU_Tensor; Axis : Tensor_Axis) return GPU_Tensor;

   overriding
   function Product (Object : GPU_Tensor; Axis : Tensor_Axis) return GPU_Tensor;

   ----------------------------------------------------------------------------
   --                               Statistics                               --
   ----------------------------------------------------------------------------

   overriding function Min (Object : GPU_Tensor) return Element;
   overriding function Max (Object : GPU_Tensor) return Element;

   overriding function Quantile (Object : GPU_Tensor; P : Probability) return Element;
   overriding function Median (Object : GPU_Tensor) return Element;

   overriding function Mean (Object : GPU_Tensor) return Element;

   overriding
   function Variance (Object : GPU_Tensor; Offset : Natural := 0) return Element;

   overriding
   function Standard_Deviation (Object : GPU_Tensor; Offset : Natural := 0) return Element;

   overriding
   function Min (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding
   function Max (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding
   function Min (Object : GPU_Tensor; Axis : Tensor_Axis) return GPU_Tensor;

   overriding
   function Max (Object : GPU_Tensor; Axis : Tensor_Axis) return GPU_Tensor;

   overriding
   function Quantile
     (Object : GPU_Tensor;
      P      : Probability;
      Axis   : Tensor_Axis) return GPU_Tensor;

   overriding
   function Median (Object : GPU_Tensor; Axis : Tensor_Axis) return GPU_Tensor;

   overriding
   function Mean (Object : GPU_Tensor; Axis : Tensor_Axis) return GPU_Tensor;

   overriding
   function Variance
     (Object : GPU_Tensor;
      Axis   : Tensor_Axis;
      Offset : Natural := 0) return GPU_Tensor;

   overriding
   function Standard_Deviation
     (Object : GPU_Tensor;
      Axis   : Tensor_Axis;
      Offset : Natural := 0) return GPU_Tensor;

   ----------------------------------------------------------------------------
   --                                Logical                                 --
   ----------------------------------------------------------------------------

   overriding function And_Not (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding function "and" (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding function "and" (Left : Element; Right : GPU_Tensor) return GPU_Tensor;

   overriding function "or"  (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding function "xor" (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding function "not" (Object : GPU_Tensor) return GPU_Tensor;

   ----------------------------------------------------------------------------
   --                              Comparisons                               --
   ----------------------------------------------------------------------------

   overriding function "="  (Left : GPU_Tensor; Right : Element) return GPU_Tensor;
   overriding function "/=" (Left : GPU_Tensor; Right : Element) return GPU_Tensor;

   overriding function ">"  (Left : GPU_Tensor; Right : Element) return GPU_Tensor;
   overriding function "<"  (Left : GPU_Tensor; Right : Element) return GPU_Tensor;

   overriding function ">=" (Left : GPU_Tensor; Right : Element) return GPU_Tensor;
   overriding function "<=" (Left : GPU_Tensor; Right : Element) return GPU_Tensor;

   ----------------------------------------------------------------------------

   overriding function "="  (Left : Element; Right : GPU_Tensor) return GPU_Tensor;
   overriding function "/=" (Left : Element; Right : GPU_Tensor) return GPU_Tensor;

   overriding function ">"  (Left : Element; Right : GPU_Tensor) return GPU_Tensor;
   overriding function "<"  (Left : Element; Right : GPU_Tensor) return GPU_Tensor;

   overriding function ">=" (Left : Element; Right : GPU_Tensor) return GPU_Tensor;
   overriding function "<=" (Left : Element; Right : GPU_Tensor) return GPU_Tensor;

   ----------------------------------------------------------------------------

   overriding function "=" (Left, Right : GPU_Tensor) return Boolean;

   overriding function "="  (Left, Right : GPU_Tensor) return GPU_Tensor;
   overriding function "/=" (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding function ">"  (Left, Right : GPU_Tensor) return GPU_Tensor;
   overriding function "<"  (Left, Right : GPU_Tensor) return GPU_Tensor;

   overriding function ">=" (Left, Right : GPU_Tensor) return GPU_Tensor;
   overriding function "<=" (Left, Right : GPU_Tensor) return GPU_Tensor;

   ----------------------------------------------------------------------------

   overriding
   function All_Close
     (Left, Right        : GPU_Tensor;
      Relative_Tolerance : Element := 1.0e-05;
      Absolute_Tolerance : Element := Element_Type'Model_Epsilon) return Boolean;

   overriding
   function Any_True (Object : GPU_Tensor; Axis : Tensor_Axis) return GPU_Tensor;

   overriding
   function Any_True (Object : GPU_Tensor) return Boolean;

   overriding
   function All_True (Object : GPU_Tensor; Axis : Tensor_Axis) return GPU_Tensor;

   overriding
   function All_True (Object : GPU_Tensor) return Boolean;

   ----------------------------------------------------------------------------

   procedure Reset_Random (Seed : Duration);

   overriding function Random_Uniform (Shape : Tensor_Shape) return GPU_Tensor;
   --  Return a tensor with elements from a uniform distribution in [0.0, 1.0)

private

   type Buffer_Access is access all Orka.Rendering.Buffers.Buffer;

   type GPU_Tensor_Reference is record
      References   : Natural;
      Materialized : Boolean;
      Data         : Buffer_Access;
   end record
     with Dynamic_Predicate => (if Materialized then Data /= null);

   type GPU_Tensor_Reference_Access is access all GPU_Tensor_Reference;

   ----------------------------------------------------------------------------

   type Value_Kind is (Tensor_Value, Scalar_Value);

   package Tensor_Holders is new Ada.Containers.Indefinite_Holders (Tensor'Class);

   type Value_Type (Kind : Value_Kind := Scalar_Value) is record
      case Kind is
         when Tensor_Value =>
            Tensor : Tensor_Holders.Holder;
         when Scalar_Value =>
            Value : Element;
      end case;
   end record
     with Dynamic_Predicate => (if Kind = Tensor_Value then not Tensor.Is_Empty);

   ----------------------------------------------------------------------------

   type Binary_Operation_Kind is
     (Add, Subtract, Multiply, Divide,
      Equal, Not_Equal, Greater_Than, Greater_Equal, Less_Than, Less_Equal,
      Logical_And, Logical_Or, Logical_Xor,
      Divide_Or_Zero, Power, Modulus, Min, Max, Arctan);

   subtype Binary_Operator is Binary_Operation_Kind range Add .. Logical_Xor;
   subtype Binary_Function is Binary_Operation_Kind range Power .. Arctan;

   subtype Boolean_Operator is Binary_Operator range Equal .. Logical_Xor;

   type Unary_Operation_Kind is
     (Minus, Absolute, Sqrt, Ceil, Floor, Round, Truncate, Exp, Log, Log2,
      Sin, Cos, Tan, Arcsin, Arccos, Logical_Not, Reshape);

   type Constructor_Kind is (Identity, Fill, Linear_Space, Log_Space);

   type Constructor_Type (Kind : Constructor_Kind := Identity) is record
      case Kind is
         when Identity =>
            Offset : Integer;
         when Fill =>
            Value : Element;
         when Linear_Space | Log_Space =>
            Start, Step : Element;
            Base        : Element := 0.0;  --  Only used by Log_Space
      end case;
   end record;

   type Matrix_Operation_Kind is
     (Main_Diagonal, Diagonal, Transpose, Any_True, All_True, Matrix_Matrix, Random);

   type Matrix_Operation_Type (Kind : Matrix_Operation_Kind := Matrix_Matrix) is record
      case Kind is
         when Main_Diagonal | Diagonal | Transpose | Any_True | All_True =>
            Value  : Tensor_Holders.Holder;
            Offset : Integer := 0;  --  Not used by Transpose | Any_True | All_True
         when Matrix_Matrix =>
            Left, Right : Tensor_Holders.Holder;
         when Random =>
            null;
      end case;
   end record;

   ----------------------------------------------------------------------------

   type Operation_Kind is
     (None, Constructor_Operation, Binary_Operation, Unary_Operation, Matrix_Operation);

   type Operation_Type (Kind : Operation_Kind := None) is record
      case Kind is
         when Binary_Operation =>
            Binary_Operator : Binary_Operation_Kind;
            Left, Right     : Value_Type;
         when Unary_Operation =>
            Unary_Operator  : Unary_Operation_Kind;
            Value           : Value_Type;
         when Constructor_Operation =>
            Constructor : Constructor_Type;
         when Matrix_Operation =>
            Matrix_Operation : Matrix_Operation_Type;
         when None =>
            null;
      end case;
   end record;

   type GPU_Tensor
     (Axes : Tensor_Axis;
      Kind : Data_Type)
   is new Ada.Finalization.Controlled and Tensor with record
      Reference : GPU_Tensor_Reference_Access;
      Shape     : Tensor_Shape (1 .. Axes);
      Operation : Operation_Type;
   end record;

   overriding procedure Adjust   (Object : in out GPU_Tensor);
   overriding procedure Finalize (Object : in out GPU_Tensor);

   ----------------------------------------------------------------------------

   type GPU_QR_Factorization is new QR_Factorization with record
      Q, R         : Tensor_Holders.Holder;
      Determinancy : Matrix_Determinancy;
   end record;

   function Q (Object : GPU_QR_Factorization'Class) return GPU_Tensor is
     (GPU_Tensor (Object.Q.Element));
   function R (Object : GPU_QR_Factorization'Class) return GPU_Tensor is
     (GPU_Tensor (Object.R.Element));

   overriding
   function Determinancy (Object : GPU_QR_Factorization) return Matrix_Determinancy is
     (Object.Determinancy);

end Orka.Numerics.Tensors.CS_GPU;