-
Notifications
You must be signed in to change notification settings - Fork 65
/
cutensornet.pyx
3307 lines (2522 loc) · 167 KB
/
cutensornet.pyx
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2021-2024, NVIDIA CORPORATION & AFFILIATES
#
# SPDX-License-Identifier: BSD-3-Clause
# This code was automatically generated. Do not modify it directly.
cimport cython
cimport cpython
from libcpp.vector cimport vector
from .._utils cimport (get_resource_ptr, get_nested_resource_ptr, nested_resource, nullable_unique_ptr,
get_buffer_pointer, get_resource_ptrs, DeviceAllocType, DeviceFreeType,
cuqnt_alloc_wrapper, cuqnt_free_wrapper, logger_callback_with_data)
from enum import IntEnum as _IntEnum
import warnings as _warnings
import numpy as _numpy
###############################################################################
# Enum
###############################################################################
class Status(_IntEnum):
"""See `cutensornetStatus_t`."""
SUCCESS = CUTENSORNET_STATUS_SUCCESS
NOT_INITIALIZED = CUTENSORNET_STATUS_NOT_INITIALIZED
ALLOC_FAILED = CUTENSORNET_STATUS_ALLOC_FAILED
INVALID_VALUE = CUTENSORNET_STATUS_INVALID_VALUE
ARCH_MISMATCH = CUTENSORNET_STATUS_ARCH_MISMATCH
MAPPING_ERROR = CUTENSORNET_STATUS_MAPPING_ERROR
EXECUTION_FAILED = CUTENSORNET_STATUS_EXECUTION_FAILED
INTERNAL_ERROR = CUTENSORNET_STATUS_INTERNAL_ERROR
NOT_SUPPORTED = CUTENSORNET_STATUS_NOT_SUPPORTED
LICENSE_ERROR = CUTENSORNET_STATUS_LICENSE_ERROR
CUBLAS_ERROR = CUTENSORNET_STATUS_CUBLAS_ERROR
CUDA_ERROR = CUTENSORNET_STATUS_CUDA_ERROR
INSUFFICIENT_WORKSPACE = CUTENSORNET_STATUS_INSUFFICIENT_WORKSPACE
INSUFFICIENT_DRIVER = CUTENSORNET_STATUS_INSUFFICIENT_DRIVER
IO_ERROR = CUTENSORNET_STATUS_IO_ERROR
CUTENSOR_VERSION_MISMATCH = CUTENSORNET_STATUS_CUTENSOR_VERSION_MISMATCH
NO_DEVICE_ALLOCATOR = CUTENSORNET_STATUS_NO_DEVICE_ALLOCATOR
ALL_HYPER_SAMPLES_FAILED = CUTENSORNET_STATUS_ALL_HYPER_SAMPLES_FAILED
CUSOLVER_ERROR = CUTENSORNET_STATUS_CUSOLVER_ERROR
DEVICE_ALLOCATOR_ERROR = CUTENSORNET_STATUS_DEVICE_ALLOCATOR_ERROR
DISTRIBUTED_FAILURE = CUTENSORNET_STATUS_DISTRIBUTED_FAILURE
INTERRUPTED = CUTENSORNET_STATUS_INTERRUPTED
class ComputeType(_IntEnum):
"""See `cutensornetComputeType_t`."""
COMPUTE_16F = CUTENSORNET_COMPUTE_16F
COMPUTE_16BF = CUTENSORNET_COMPUTE_16BF
COMPUTE_TF32 = CUTENSORNET_COMPUTE_TF32
COMPUTE_3XTF32 = CUTENSORNET_COMPUTE_3XTF32
COMPUTE_32F = CUTENSORNET_COMPUTE_32F
COMPUTE_64F = CUTENSORNET_COMPUTE_64F
COMPUTE_8U = CUTENSORNET_COMPUTE_8U
COMPUTE_8I = CUTENSORNET_COMPUTE_8I
COMPUTE_32U = CUTENSORNET_COMPUTE_32U
COMPUTE_32I = CUTENSORNET_COMPUTE_32I
class GraphAlgo(_IntEnum):
"""See `cutensornetGraphAlgo_t`."""
RB = CUTENSORNET_GRAPH_ALGO_RB
KWAY = CUTENSORNET_GRAPH_ALGO_KWAY
class MemoryModel(_IntEnum):
"""See `cutensornetMemoryModel_t`."""
HEURISTIC = CUTENSORNET_MEMORY_MODEL_HEURISTIC
CUTENSOR = CUTENSORNET_MEMORY_MODEL_CUTENSOR
class OptimizerCost(_IntEnum):
"""See `cutensornetOptimizerCost_t`."""
FLOPS = CUTENSORNET_OPTIMIZER_COST_FLOPS
TIME = CUTENSORNET_OPTIMIZER_COST_TIME
TIME_TUNED = CUTENSORNET_OPTIMIZER_COST_TIME_TUNED
class ContractionOptimizerConfigAttribute(_IntEnum):
"""See `cutensornetContractionOptimizerConfigAttributes_t`."""
GRAPH_NUM_PARTITIONS = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_GRAPH_NUM_PARTITIONS
GRAPH_CUTOFF_SIZE = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_GRAPH_CUTOFF_SIZE
GRAPH_ALGORITHM = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_GRAPH_ALGORITHM
GRAPH_IMBALANCE_FACTOR = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_GRAPH_IMBALANCE_FACTOR
GRAPH_NUM_ITERATIONS = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_GRAPH_NUM_ITERATIONS
GRAPH_NUM_CUTS = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_GRAPH_NUM_CUTS
RECONFIG_NUM_ITERATIONS = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_RECONFIG_NUM_ITERATIONS
RECONFIG_NUM_LEAVES = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_RECONFIG_NUM_LEAVES
SLICER_DISABLE_SLICING = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SLICER_DISABLE_SLICING
SLICER_MEMORY_MODEL = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SLICER_MEMORY_MODEL
SLICER_MEMORY_FACTOR = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SLICER_MEMORY_FACTOR
SLICER_MIN_SLICES = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SLICER_MIN_SLICES
SLICER_SLICE_FACTOR = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SLICER_SLICE_FACTOR
HYPER_NUM_SAMPLES = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_HYPER_NUM_SAMPLES
HYPER_NUM_THREADS = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_HYPER_NUM_THREADS
SIMPLIFICATION_DISABLE_DR = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SIMPLIFICATION_DISABLE_DR
SEED = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SEED
COST_FUNCTION_OBJECTIVE = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_COST_FUNCTION_OBJECTIVE
CACHE_REUSE_NRUNS = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_CACHE_REUSE_NRUNS
SMART_OPTION = CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SMART_OPTION
class ContractionOptimizerInfoAttribute(_IntEnum):
"""See `cutensornetContractionOptimizerInfoAttributes_t`."""
PATH = CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_PATH
NUM_SLICES = CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_NUM_SLICES
NUM_SLICED_MODES = CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_NUM_SLICED_MODES
SLICED_MODE = CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_SLICED_MODE
SLICED_EXTENT = CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_SLICED_EXTENT
SLICING_CONFIG = CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_SLICING_CONFIG
SLICING_OVERHEAD = CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_SLICING_OVERHEAD
PHASE1_FLOP_COUNT = CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_PHASE1_FLOP_COUNT
FLOP_COUNT = CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_FLOP_COUNT
EFFECTIVE_FLOPS_EST = CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_EFFECTIVE_FLOPS_EST
RUNTIME_EST = CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_RUNTIME_EST
LARGEST_TENSOR = CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_LARGEST_TENSOR
INTERMEDIATE_MODES = CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_INTERMEDIATE_MODES
NUM_INTERMEDIATE_MODES = CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_NUM_INTERMEDIATE_MODES
class ContractionAutotunePreferenceAttribute(_IntEnum):
"""See `cutensornetContractionAutotunePreferenceAttributes_t`."""
MAX_ITERATIONS = CUTENSORNET_CONTRACTION_AUTOTUNE_MAX_ITERATIONS
INTERMEDIATE_MODES = CUTENSORNET_CONTRACTION_AUTOTUNE_INTERMEDIATE_MODES
class WorksizePref(_IntEnum):
"""See `cutensornetWorksizePref_t`."""
MIN = CUTENSORNET_WORKSIZE_PREF_MIN
RECOMMENDED = CUTENSORNET_WORKSIZE_PREF_RECOMMENDED
MAX = CUTENSORNET_WORKSIZE_PREF_MAX
class Memspace(_IntEnum):
"""See `cutensornetMemspace_t`."""
DEVICE = CUTENSORNET_MEMSPACE_DEVICE
HOST = CUTENSORNET_MEMSPACE_HOST
class WorkspaceKind(_IntEnum):
"""See `cutensornetWorkspaceKind_t`."""
SCRATCH = CUTENSORNET_WORKSPACE_SCRATCH
CACHE = CUTENSORNET_WORKSPACE_CACHE
class TensorSVDConfigAttribute(_IntEnum):
"""See `cutensornetTensorSVDConfigAttributes_t`."""
ABS_CUTOFF = CUTENSORNET_TENSOR_SVD_CONFIG_ABS_CUTOFF
REL_CUTOFF = CUTENSORNET_TENSOR_SVD_CONFIG_REL_CUTOFF
S_NORMALIZATION = CUTENSORNET_TENSOR_SVD_CONFIG_S_NORMALIZATION
S_PARTITION = CUTENSORNET_TENSOR_SVD_CONFIG_S_PARTITION
ALGO = CUTENSORNET_TENSOR_SVD_CONFIG_ALGO
ALGO_PARAMS = CUTENSORNET_TENSOR_SVD_CONFIG_ALGO_PARAMS
DISCARDED_WEIGHT_CUTOFF = CUTENSORNET_TENSOR_SVD_CONFIG_DISCARDED_WEIGHT_CUTOFF
class TensorSVDPartition(_IntEnum):
"""See `cutensornetTensorSVDPartition_t`."""
NONE = CUTENSORNET_TENSOR_SVD_PARTITION_NONE
US = CUTENSORNET_TENSOR_SVD_PARTITION_US
SV = CUTENSORNET_TENSOR_SVD_PARTITION_SV
UV_EQUAL = CUTENSORNET_TENSOR_SVD_PARTITION_UV_EQUAL
class TensorSVDNormalization(_IntEnum):
"""See `cutensornetTensorSVDNormalization_t`."""
NONE = CUTENSORNET_TENSOR_SVD_NORMALIZATION_NONE
L1 = CUTENSORNET_TENSOR_SVD_NORMALIZATION_L1
L2 = CUTENSORNET_TENSOR_SVD_NORMALIZATION_L2
LINF = CUTENSORNET_TENSOR_SVD_NORMALIZATION_LINF
class TensorSVDInfoAttribute(_IntEnum):
"""See `cutensornetTensorSVDInfoAttributes_t`."""
FULL_EXTENT = CUTENSORNET_TENSOR_SVD_INFO_FULL_EXTENT
REDUCED_EXTENT = CUTENSORNET_TENSOR_SVD_INFO_REDUCED_EXTENT
DISCARDED_WEIGHT = CUTENSORNET_TENSOR_SVD_INFO_DISCARDED_WEIGHT
ALGO = CUTENSORNET_TENSOR_SVD_INFO_ALGO
ALGO_STATUS = CUTENSORNET_TENSOR_SVD_INFO_ALGO_STATUS
class GateSplitAlgo(_IntEnum):
"""See `cutensornetGateSplitAlgo_t`."""
DIRECT = CUTENSORNET_GATE_SPLIT_ALGO_DIRECT
REDUCED = CUTENSORNET_GATE_SPLIT_ALGO_REDUCED
class NetworkAttribute(_IntEnum):
"""See `cutensornetNetworkAttributes_t`."""
INPUT_TENSORS_NUM_CONSTANT = CUTENSORNET_NETWORK_INPUT_TENSORS_NUM_CONSTANT
INPUT_TENSORS_CONSTANT = CUTENSORNET_NETWORK_INPUT_TENSORS_CONSTANT
INPUT_TENSORS_NUM_CONJUGATED = CUTENSORNET_NETWORK_INPUT_TENSORS_NUM_CONJUGATED
INPUT_TENSORS_CONJUGATED = CUTENSORNET_NETWORK_INPUT_TENSORS_CONJUGATED
INPUT_TENSORS_NUM_REQUIRE_GRAD = CUTENSORNET_NETWORK_INPUT_TENSORS_NUM_REQUIRE_GRAD
INPUT_TENSORS_REQUIRE_GRAD = CUTENSORNET_NETWORK_INPUT_TENSORS_REQUIRE_GRAD
class SmartOption(_IntEnum):
"""See `cutensornetSmartOption_t`."""
DISABLED = CUTENSORNET_SMART_OPTION_DISABLED
ENABLED = CUTENSORNET_SMART_OPTION_ENABLED
class TensorSVDAlgo(_IntEnum):
"""See `cutensornetTensorSVDAlgo_t`."""
GESVD = CUTENSORNET_TENSOR_SVD_ALGO_GESVD
GESVDJ = CUTENSORNET_TENSOR_SVD_ALGO_GESVDJ
GESVDP = CUTENSORNET_TENSOR_SVD_ALGO_GESVDP
GESVDR = CUTENSORNET_TENSOR_SVD_ALGO_GESVDR
class StatePurity(_IntEnum):
"""See `cutensornetStatePurity_t`."""
PURE = CUTENSORNET_STATE_PURITY_PURE
class MarginalAttribute(_IntEnum):
"""See `cutensornetMarginalAttributes_t`."""
OPT_NUM_HYPER_SAMPLES = CUTENSORNET_MARGINAL_OPT_NUM_HYPER_SAMPLES
CONFIG_NUM_HYPER_SAMPLES = CUTENSORNET_MARGINAL_CONFIG_NUM_HYPER_SAMPLES
INFO_FLOPS = CUTENSORNET_MARGINAL_INFO_FLOPS
class SamplerAttribute(_IntEnum):
"""See `cutensornetSamplerAttributes_t`."""
OPT_NUM_HYPER_SAMPLES = CUTENSORNET_SAMPLER_OPT_NUM_HYPER_SAMPLES
CONFIG_NUM_HYPER_SAMPLES = CUTENSORNET_SAMPLER_CONFIG_NUM_HYPER_SAMPLES
INFO_FLOPS = CUTENSORNET_SAMPLER_INFO_FLOPS
class AccessorAttribute(_IntEnum):
"""See `cutensornetAccessorAttributes_t`."""
OPT_NUM_HYPER_SAMPLES = CUTENSORNET_ACCESSOR_OPT_NUM_HYPER_SAMPLES
CONFIG_NUM_HYPER_SAMPLES = CUTENSORNET_ACCESSOR_CONFIG_NUM_HYPER_SAMPLES
INFO_FLOPS = CUTENSORNET_ACCESSOR_INFO_FLOPS
class ExpectationAttribute(_IntEnum):
"""See `cutensornetExpectationAttributes_t`."""
OPT_NUM_HYPER_SAMPLES = CUTENSORNET_EXPECTATION_OPT_NUM_HYPER_SAMPLES
CONFIG_NUM_HYPER_SAMPLES = CUTENSORNET_EXPECTATION_CONFIG_NUM_HYPER_SAMPLES
INFO_FLOPS = CUTENSORNET_EXPECTATION_INFO_FLOPS
class BoundaryCondition(_IntEnum):
"""See `cutensornetBoundaryCondition_t`."""
OPEN = CUTENSORNET_BOUNDARY_CONDITION_OPEN
class StateAttribute(_IntEnum):
"""See `cutensornetStateAttributes_t`."""
MPS_CANONICAL_CENTER = CUTENSORNET_STATE_MPS_CANONICAL_CENTER
MPS_SVD_CONFIG_ABS_CUTOFF = CUTENSORNET_STATE_MPS_SVD_CONFIG_ABS_CUTOFF
MPS_SVD_CONFIG_REL_CUTOFF = CUTENSORNET_STATE_MPS_SVD_CONFIG_REL_CUTOFF
MPS_SVD_CONFIG_S_NORMALIZATION = CUTENSORNET_STATE_MPS_SVD_CONFIG_S_NORMALIZATION
MPS_SVD_CONFIG_ALGO = CUTENSORNET_STATE_MPS_SVD_CONFIG_ALGO
MPS_SVD_CONFIG_ALGO_PARAMS = CUTENSORNET_STATE_MPS_SVD_CONFIG_ALGO_PARAMS
MPS_SVD_CONFIG_DISCARDED_WEIGHT_CUTOFF = CUTENSORNET_STATE_MPS_SVD_CONFIG_DISCARDED_WEIGHT_CUTOFF
NUM_HYPER_SAMPLES = CUTENSORNET_STATE_NUM_HYPER_SAMPLES
CONFIG_MPS_CANONICAL_CENTER = CUTENSORNET_STATE_CONFIG_MPS_CANONICAL_CENTER
CONFIG_MPS_SVD_ABS_CUTOFF = CUTENSORNET_STATE_CONFIG_MPS_SVD_ABS_CUTOFF
CONFIG_MPS_SVD_REL_CUTOFF = CUTENSORNET_STATE_CONFIG_MPS_SVD_REL_CUTOFF
CONFIG_MPS_SVD_S_NORMALIZATION = CUTENSORNET_STATE_CONFIG_MPS_SVD_S_NORMALIZATION
CONFIG_MPS_SVD_ALGO = CUTENSORNET_STATE_CONFIG_MPS_SVD_ALGO
CONFIG_MPS_SVD_ALGO_PARAMS = CUTENSORNET_STATE_CONFIG_MPS_SVD_ALGO_PARAMS
CONFIG_MPS_SVD_DISCARDED_WEIGHT_CUTOFF = CUTENSORNET_STATE_CONFIG_MPS_SVD_DISCARDED_WEIGHT_CUTOFF
CONFIG_MPS_MPO_APPLICATION = CUTENSORNET_STATE_CONFIG_MPS_MPO_APPLICATION
CONFIG_NUM_HYPER_SAMPLES = CUTENSORNET_STATE_CONFIG_NUM_HYPER_SAMPLES
INFO_FLOPS = CUTENSORNET_STATE_INFO_FLOPS
class StateMPOApplication(_IntEnum):
"""See `cutensornetStateMPOApplication_t`."""
INEXACT = CUTENSORNET_STATE_MPO_APPLICATION_INEXACT
EXACT = CUTENSORNET_STATE_MPO_APPLICATION_EXACT
###############################################################################
# Error handling
###############################################################################
class cuTensorNetError(Exception):
def __init__(self, status):
self.status = status
s = Status(status)
cdef str err = f"{s.name} ({s.value}): {get_error_string(status)}"
super(cuTensorNetError, self).__init__(err)
def __reduce__(self):
return (type(self), (self.status,))
@cython.profile(False)
cpdef inline check_status(int status):
if status != 0:
raise cuTensorNetError(status)
###############################################################################
# Special dtypes
###############################################################################
tensor_id_list_dtype = _numpy.dtype(
{'names':['num_tensors','data'],
'formats': (_numpy.int32, _numpy.intp),
'itemsize': sizeof(TensorIDList),
}, align=True
)
contraction_path_dtype = _numpy.dtype(
{'names':['num_contractions','data'],
'formats': (_numpy.uint32, _numpy.intp),
'itemsize': sizeof(ContractionPath),
}, align=True
)
# We need this dtype because its members are not of the same type...
slice_info_pair_dtype = _numpy.dtype(
{'names': ('sliced_mode','sliced_extent'),
'formats': (_numpy.int32, _numpy.int64),
'itemsize': sizeof(SliceInfoPair),
}, align=True
)
slicing_config_dtype = _numpy.dtype(
{'names': ('num_sliced_modes','data'),
'formats': (_numpy.uint32, _numpy.intp),
'itemsize': sizeof(SlicingConfig),
}, align=True
)
gesvdj_params_dtype = _numpy.dtype(
{'names': ('tol','max_sweeps'),
'formats': (_numpy.float64, _numpy.int32),
'itemsize': sizeof(GesvdjParams),
}, align=True
)
gesvdr_params_dtype = _numpy.dtype(
{'names': ('oversampling','niters'),
'formats': (_numpy.int64, _numpy.int64),
'itemsize': sizeof(GesvdrParams),
}, align=True
)
gesvdj_status_dtype = _numpy.dtype(
{'names': ('residual', 'sweeps'),
'formats': (_numpy.float64, _numpy.int32),
'itemsize': sizeof(GesvdjStatus),
}, align=True
)
gesvdp_status_dtype = _numpy.dtype(
{'names': ('err_sigma', ),
'formats': (_numpy.float64, ),
'itemsize': sizeof(GesvdpStatus),
}, align=True
)
tensor_qualifiers_dtype = _numpy.dtype(
{'names':('is_conjugate', 'is_constant', 'requires_gradient'),
'formats': (_numpy.int32, _numpy.int32, _numpy.int32, ),
'itemsize': sizeof(TensorQualifiers),
}, align=True
)
###############################################################################
# Wrapper functions
###############################################################################
cpdef intptr_t create() except? 0:
"""Initializes the cuTensorNet library.
Returns:
intptr_t: Pointer to ``cutensornetHandle_t``.
.. seealso:: `cutensornetCreate`
"""
cdef Handle handle
with nogil:
status = cutensornetCreate(&handle)
check_status(status)
return <intptr_t>handle
cpdef destroy(intptr_t handle):
"""Destroys the cuTensorNet library handle.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
.. seealso:: `cutensornetDestroy`
"""
with nogil:
status = cutensornetDestroy(<Handle>handle)
check_status(status)
cpdef intptr_t create_network_descriptor(intptr_t handle, int32_t num_inputs, num_modes_in, extents_in, strides_in, modes_in, qualifiers_in, int32_t num_modes_out, extents_out, strides_out, modes_out, int data_type, int compute_type) except? 0:
"""Initializes a ``cutensornetNetworkDescriptor_t``, describing the connectivity (i.e., network topology) between the tensors.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
num_inputs (int32_t): Number of input tensors.
num_modes_in (object): Array of size ``num_inputs``; ``num_modes_in[i]`` denotes the number of modes available in the i-th tensor. It can be:
- an :class:`int` as the pointer address to the array, or
- a Python sequence of ``int32_t``.
extents_in (object): Array of size ``num_inputs``; ``extents_in[i]`` has ``num_modes_in[i]`` many entries with ``extents_in[i][j]`` (``j`` < ``num_modes_in[i]``) corresponding to the extent of the j-th mode of tensor ``i``. It can be:
- an :class:`int` as the pointer address to the nested sequence, or
- a Python sequence of :class:`int`\s, each of which is a pointer address
to a valid sequence, or
- a nested Python sequence of ``int64_t``.
strides_in (object): Array of size ``num_inputs``; ``strides_in[i]`` has ``num_modes_in[i]`` many entries with ``strides_in[i][j]`` (``j`` < ``num_modes_in[i]``) corresponding to the linearized offset -- in physical memory -- between two logically-neighboring elements w.r.t the j-th mode of tensor ``i``. It can be:
- an :class:`int` as the pointer address to the nested sequence, or
- a Python sequence of :class:`int`\s, each of which is a pointer address
to a valid sequence, or
- a nested Python sequence of ``int64_t``.
modes_in (object): Array of size ``num_inputs``; ``modes_in[i]`` has ``num_modes_in[i]`` many entries -- each entry corresponds to a mode. Each mode that does not appear in the input tensor is implicitly contracted. It can be:
- an :class:`int` as the pointer address to the nested sequence, or
- a Python sequence of :class:`int`\s, each of which is a pointer address
to a valid sequence, or
- a nested Python sequence of ``int32_t``.
qualifiers_in (object): Array of size ``num_inputs``; ``qualifiers_in[i]`` denotes the qualifiers of i-th input tensor. Refer to ``cutensornetTensorQualifiers_t``. It can be:
- an :class:`int` as the pointer address to the array, or
- a Python sequence of ``cutensornetTensorQualifiers_t``.
num_modes_out (int32_t): number of modes of the output tensor. On entry, if this value is ``-1`` and the output modes are not provided, the network will infer the output modes. If this value is ``0``, the network is force reduced.
extents_out (object): Array of size ``num_modes_out``; ``extents_out[j]`` (``j`` < ``num_modes_out``) corresponding to the extent of the j-th mode of the output tensor. It can be:
- an :class:`int` as the pointer address to the array, or
- a Python sequence of ``int64_t``.
strides_out (object): Array of size ``num_modes_out``; ``strides_out[j]`` (``j`` < ``num_modes_out``) corresponding to the linearized offset -- in physical memory -- between two logically-neighboring elements w.r.t the j-th mode of the output tensor. It can be:
- an :class:`int` as the pointer address to the array, or
- a Python sequence of ``int64_t``.
modes_out (object): Array of size ``num_modes_out``; ``modes_out[j]`` denotes the j-th mode of the output tensor. output tensor. It can be:
- an :class:`int` as the pointer address to the array, or
- a Python sequence of ``int32_t``.
data_type (int): Denotes the data type for all input an output tensors.
compute_type (ComputeType): Denotes the compute type used throughout the computation.
Returns:
intptr_t: Pointer to a ``cutensornetNetworkDescriptor_t``.
.. seealso:: `cutensornetCreateNetworkDescriptor`
"""
cdef nullable_unique_ptr[ vector[int32_t] ] _num_modes_in_ = \
get_resource_ptr[int32_t](num_modes_in, <int32_t*>NULL)
cdef nested_resource[ int64_t ] _extents_in_ = \
get_nested_resource_ptr[int64_t](extents_in, <int64_t*>NULL)
cdef nested_resource[ int64_t ] _strides_in_ = \
get_nested_resource_ptr[int64_t](strides_in, <int64_t*>NULL)
cdef nested_resource[ int32_t ] _modes_in_ = \
get_nested_resource_ptr[int32_t](modes_in, <int32_t*>NULL)
cdef nullable_unique_ptr[ vector[cutensornetTensorQualifiers_t] ] _qualifiers_in_ = \
get_resource_ptr[cutensornetTensorQualifiers_t](qualifiers_in, <cutensornetTensorQualifiers_t*>NULL)
cdef nullable_unique_ptr[ vector[int64_t] ] _extents_out_ = \
get_resource_ptr[int64_t](extents_out, <int64_t*>NULL)
cdef nullable_unique_ptr[ vector[int64_t] ] _strides_out_ = \
get_resource_ptr[int64_t](strides_out, <int64_t*>NULL)
cdef nullable_unique_ptr[ vector[int32_t] ] _modes_out_ = \
get_resource_ptr[int32_t](modes_out, <int32_t*>NULL)
cdef NetworkDescriptor desc_net
with nogil:
status = cutensornetCreateNetworkDescriptor(<const Handle>handle, num_inputs, <const int32_t*>(_num_modes_in_.data()), <const int64_t* const*>(_extents_in_.ptrs.data()), <const int64_t* const*>(_strides_in_.ptrs.data()), <const int32_t* const*>(_modes_in_.ptrs.data()), <const cutensornetTensorQualifiers_t*>(_qualifiers_in_.data()), num_modes_out, <const int64_t*>(_extents_out_.data()), <const int64_t*>(_strides_out_.data()), <const int32_t*>(_modes_out_.data()), <DataType>data_type, <_ComputeType>compute_type, &desc_net)
check_status(status)
return <intptr_t>desc_net
cpdef destroy_network_descriptor(intptr_t desc):
"""Frees all the memory associated with the network descriptor.
Args:
desc (intptr_t): Opaque handle to a tensor network descriptor.
.. seealso:: `cutensornetDestroyNetworkDescriptor`
"""
with nogil:
status = cutensornetDestroyNetworkDescriptor(<NetworkDescriptor>desc)
check_status(status)
cpdef intptr_t get_output_tensor_descriptor(intptr_t handle, intptr_t desc_net) except? 0:
"""Creates a ``cutensornetTensorDescriptor_t`` representing the output tensor of the network.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
desc_net (intptr_t): Pointer to a ``cutensornetNetworkDescriptor_t``.
Returns:
intptr_t: an opaque ``cutensornetTensorDescriptor_t`` struct. Cannot be null. On return, a new ``cutensornetTensorDescriptor_t`` holds the meta-data of the ``desc_net`` output tensor.
.. seealso:: `cutensornetGetOutputTensorDescriptor`
"""
cdef TensorDescriptor output_tensor_desc
with nogil:
status = cutensornetGetOutputTensorDescriptor(<const Handle>handle, <const NetworkDescriptor>desc_net, &output_tensor_desc)
check_status(status)
return <intptr_t>output_tensor_desc
cpdef intptr_t create_workspace_descriptor(intptr_t handle) except? 0:
"""Creates a workspace descriptor that holds information about the user provided memory buffer.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
Returns:
intptr_t: Pointer to the opaque workspace descriptor.
.. seealso:: `cutensornetCreateWorkspaceDescriptor`
"""
cdef WorkspaceDescriptor work_desc
with nogil:
status = cutensornetCreateWorkspaceDescriptor(<const Handle>handle, &work_desc)
check_status(status)
return <intptr_t>work_desc
cpdef workspace_compute_contraction_sizes(intptr_t handle, intptr_t desc_net, intptr_t optimizer_info, intptr_t work_desc):
"""Computes the workspace size needed to contract the input tensor network using the provided contraction path.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
desc_net (intptr_t): Describes the tensor network (i.e., its tensors and their connectivity).
optimizer_info (intptr_t): Opaque structure.
work_desc (intptr_t): The workspace descriptor in which the information is collected.
.. seealso:: `cutensornetWorkspaceComputeContractionSizes`
"""
with nogil:
status = cutensornetWorkspaceComputeContractionSizes(<const Handle>handle, <const NetworkDescriptor>desc_net, <const ContractionOptimizerInfo>optimizer_info, <WorkspaceDescriptor>work_desc)
check_status(status)
cpdef int64_t workspace_get_memory_size(intptr_t handle, intptr_t work_desc, int work_pref, int mem_space, int work_kind) except? -1:
"""Retrieves the needed workspace size for the given workspace preference, memory space, workspace kind.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
work_desc (intptr_t): Opaque structure describing the workspace.
work_pref (WorksizePref): Preference of workspace for planning.
mem_space (Memspace): The memory space where the workspace is allocated.
work_kind (WorkspaceKind): The kind of workspace.
Returns:
int64_t: Needed workspace size.
.. seealso:: `cutensornetWorkspaceGetMemorySize`
"""
cdef int64_t memory_size
with nogil:
status = cutensornetWorkspaceGetMemorySize(<const Handle>handle, <const WorkspaceDescriptor>work_desc, <_WorksizePref>work_pref, <_Memspace>mem_space, <_WorkspaceKind>work_kind, &memory_size)
check_status(status)
return memory_size
cpdef workspace_set_memory(intptr_t handle, intptr_t work_desc, int mem_space, int work_kind, intptr_t memory_ptr, int64_t memory_size):
"""Sets the memory address and workspace size of the workspace provided by user.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
work_desc (intptr_t): Opaque structure describing the workspace.
mem_space (Memspace): The memory space where the workspace is allocated.
work_kind (WorkspaceKind): The kind of workspace.
memory_ptr (intptr_t): Workspace memory pointer, may be null.
memory_size (int64_t): Workspace size.
.. seealso:: `cutensornetWorkspaceSetMemory`
"""
with nogil:
status = cutensornetWorkspaceSetMemory(<const Handle>handle, <WorkspaceDescriptor>work_desc, <_Memspace>mem_space, <_WorkspaceKind>work_kind, <void* const>memory_ptr, memory_size)
check_status(status)
cpdef tuple workspace_get_memory(intptr_t handle, intptr_t work_desc, int mem_space, int work_kind):
"""Retrieves the memory address and workspace size of workspace hosted in the workspace descriptor.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
work_desc (intptr_t): Opaque structure describing the workspace.
mem_space (Memspace): The memory space where the workspace is allocated.
work_kind (WorkspaceKind): The kind of workspace.
Returns:
A 2-tuple containing:
- intptr_t: Workspace memory pointer.
- int64_t: Workspace size.
.. seealso:: `cutensornetWorkspaceGetMemory`
"""
cdef void* memory_ptr
cdef int64_t memory_size
with nogil:
status = cutensornetWorkspaceGetMemory(<const Handle>handle, <const WorkspaceDescriptor>work_desc, <_Memspace>mem_space, <_WorkspaceKind>work_kind, &memory_ptr, &memory_size)
check_status(status)
return (<intptr_t>memory_ptr, memory_size)
cpdef destroy_workspace_descriptor(intptr_t desc):
"""Frees the workspace descriptor.
Args:
desc (intptr_t): Opaque structure.
.. seealso:: `cutensornetDestroyWorkspaceDescriptor`
"""
with nogil:
status = cutensornetDestroyWorkspaceDescriptor(<WorkspaceDescriptor>desc)
check_status(status)
cpdef intptr_t create_contraction_optimizer_config(intptr_t handle) except? 0:
"""Sets up the required hyper-optimization parameters for the contraction order solver (see :func:`contraction_optimize`).
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
Returns:
intptr_t: This data structure holds all information about the user-requested hyper-optimization parameters.
.. seealso:: `cutensornetCreateContractionOptimizerConfig`
"""
cdef ContractionOptimizerConfig optimizer_config
with nogil:
status = cutensornetCreateContractionOptimizerConfig(<const Handle>handle, &optimizer_config)
check_status(status)
return <intptr_t>optimizer_config
cpdef destroy_contraction_optimizer_config(intptr_t optimizer_config):
"""Frees all the memory associated with ``optimizer_config``.
Args:
optimizer_config (intptr_t): Opaque structure.
.. seealso:: `cutensornetDestroyContractionOptimizerConfig`
"""
with nogil:
status = cutensornetDestroyContractionOptimizerConfig(<ContractionOptimizerConfig>optimizer_config)
check_status(status)
######################### Python specific utility #########################
cdef dict contraction_optimizer_config_attribute_sizes = {
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_GRAPH_NUM_PARTITIONS: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_GRAPH_CUTOFF_SIZE: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_GRAPH_ALGORITHM: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_GRAPH_IMBALANCE_FACTOR: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_GRAPH_NUM_ITERATIONS: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_GRAPH_NUM_CUTS: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_RECONFIG_NUM_ITERATIONS: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_RECONFIG_NUM_LEAVES: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SLICER_DISABLE_SLICING: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SLICER_MEMORY_MODEL: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SLICER_MEMORY_FACTOR: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SLICER_MIN_SLICES: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SLICER_SLICE_FACTOR: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_HYPER_NUM_SAMPLES: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_HYPER_NUM_THREADS: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SIMPLIFICATION_DISABLE_DR: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SEED: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_COST_FUNCTION_OBJECTIVE: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_CACHE_REUSE_NRUNS: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_CONFIG_SMART_OPTION: _numpy.int32,
}
cpdef get_contraction_optimizer_config_attribute_dtype(int attr):
"""Get the Python data type of the corresponding ContractionOptimizerConfigAttribute attribute.
Args:
attr (ContractionOptimizerConfigAttribute): The attribute to query.
Returns:
The data type of the queried attribute.
.. note:: This API has no C counterpart and is a convenient helper for
allocating memory for :func:`contraction_optimizer_config_get_attribute`, :func:`contraction_optimizer_config_set_attribute`.
"""
return contraction_optimizer_config_attribute_sizes[attr]
###########################################################################
cpdef contraction_optimizer_config_get_attribute(intptr_t handle, intptr_t optimizer_config, int attr, intptr_t buf, size_t size_in_bytes):
"""Gets attributes of ``optimizer_config``.
Args:
handle (intptr_t): Opaque handle holding cuTENSORNet's library context.
optimizer_config (intptr_t): Opaque structure that is accessed.
attr (ContractionOptimizerConfigAttribute): Specifies the attribute that is requested.
buf (intptr_t): On return, this buffer (of size ``size_in_bytes``) holds the value that corresponds to ``attr`` within ``optimizer_config``.
size_in_bytes (size_t): Size of ``buf`` (in bytes).
.. note:: To compute the attribute size, use the itemsize of the corresponding data
type, which can be queried using :func:`get_contraction_optimizer_config_attribute_dtype`.
.. seealso:: `cutensornetContractionOptimizerConfigGetAttribute`
"""
with nogil:
status = cutensornetContractionOptimizerConfigGetAttribute(<const Handle>handle, <const ContractionOptimizerConfig>optimizer_config, <_ContractionOptimizerConfigAttribute>attr, <void*>buf, size_in_bytes)
check_status(status)
cpdef contraction_optimizer_config_set_attribute(intptr_t handle, intptr_t optimizer_config, int attr, intptr_t buf, size_t size_in_bytes):
"""Sets attributes of ``optimizer_config``.
Args:
handle (intptr_t): Opaque handle holding cuTENSORNet's library context.
optimizer_config (intptr_t): Opaque structure that is accessed.
attr (ContractionOptimizerConfigAttribute): Specifies the attribute that is requested.
buf (intptr_t): This buffer (of size ``size_in_bytes``) determines the value to which ``attr`` will be set.
size_in_bytes (size_t): Size of ``buf`` (in bytes).
.. note:: To compute the attribute size, use the itemsize of the corresponding data
type, which can be queried using :func:`get_contraction_optimizer_config_attribute_dtype`.
.. seealso:: `cutensornetContractionOptimizerConfigSetAttribute`
"""
with nogil:
status = cutensornetContractionOptimizerConfigSetAttribute(<const Handle>handle, <ContractionOptimizerConfig>optimizer_config, <_ContractionOptimizerConfigAttribute>attr, <const void*>buf, size_in_bytes)
check_status(status)
cpdef destroy_contraction_optimizer_info(intptr_t optimizer_info):
"""Frees all the memory associated with ``optimizer_info``.
Args:
optimizer_info (intptr_t): Opaque structure.
.. seealso:: `cutensornetDestroyContractionOptimizerInfo`
"""
with nogil:
status = cutensornetDestroyContractionOptimizerInfo(<ContractionOptimizerInfo>optimizer_info)
check_status(status)
cpdef intptr_t create_contraction_optimizer_info(intptr_t handle, intptr_t desc_net) except? 0:
"""Allocates resources for ``optimizerInfo``.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
desc_net (intptr_t): Describes the tensor network (i.e., its tensors and their connectivity) for which ``optimizerInfo`` is created.
Returns:
intptr_t: Pointer to ``cutensornetContractionOptimizerInfo_t``.
.. seealso:: `cutensornetCreateContractionOptimizerInfo`
"""
cdef ContractionOptimizerInfo optimizer_info
with nogil:
status = cutensornetCreateContractionOptimizerInfo(<const Handle>handle, <const NetworkDescriptor>desc_net, &optimizer_info)
check_status(status)
return <intptr_t>optimizer_info
cpdef contraction_optimize(intptr_t handle, intptr_t desc_net, intptr_t optimizer_config, uint64_t workspace_size_constraint, intptr_t optimizer_info):
"""Computes an "optimized" contraction order as well as slicing info (for more information see Overview section) for a given tensor network such that the total time to solution is minimized while adhering to the user-provided memory constraint.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
desc_net (intptr_t): Describes the topology of the tensor network (i.e., all tensors, their connectivity and modes).
optimizer_config (intptr_t): Holds all hyper-optimization parameters that govern the search for an "optimal" contraction order.
workspace_size_constraint (uint64_t): Maximal device memory that will be provided by the user (i.e., cuTensorNet has to find a viable path/slicing solution within this user-defined constraint).
optimizer_info (intptr_t): On return, this object will hold all necessary information about the optimized path and the related slicing information. ``optimizer_info`` will hold information including (see ``cutensornetContractionOptimizerInfoAttributes_t``):.
.. seealso:: `cutensornetContractionOptimize`
"""
with nogil:
status = cutensornetContractionOptimize(<const Handle>handle, <const NetworkDescriptor>desc_net, <const ContractionOptimizerConfig>optimizer_config, workspace_size_constraint, <ContractionOptimizerInfo>optimizer_info)
check_status(status)
######################### Python specific utility #########################
cdef dict contraction_optimizer_info_attribute_sizes = {
CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_PATH: contraction_path_dtype,
CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_NUM_SLICES: _numpy.int64,
CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_NUM_SLICED_MODES: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_SLICED_MODE: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_SLICED_EXTENT: _numpy.int64,
CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_SLICING_CONFIG: slicing_config_dtype,
CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_SLICING_OVERHEAD: _numpy.float64,
CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_PHASE1_FLOP_COUNT: _numpy.float64,
CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_FLOP_COUNT: _numpy.float64,
CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_EFFECTIVE_FLOPS_EST: _numpy.float64,
CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_RUNTIME_EST: _numpy.float64,
CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_LARGEST_TENSOR: _numpy.float64,
CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_INTERMEDIATE_MODES: _numpy.int32,
CUTENSORNET_CONTRACTION_OPTIMIZER_INFO_NUM_INTERMEDIATE_MODES: _numpy.int32,
}
cpdef get_contraction_optimizer_info_attribute_dtype(int attr):
"""Get the Python data type of the corresponding ContractionOptimizerInfoAttribute attribute.
Args:
attr (ContractionOptimizerInfoAttribute): The attribute to query.
Returns:
The data type of the queried attribute.
.. note:: This API has no C counterpart and is a convenient helper for
allocating memory for :func:`contraction_optimizer_info_get_attribute`, :func:`contraction_optimizer_info_set_attribute`.
"""
return contraction_optimizer_info_attribute_sizes[attr]
###########################################################################
cpdef contraction_optimizer_info_get_attribute(intptr_t handle, intptr_t optimizer_info, int attr, intptr_t buf, size_t size_in_bytes):
"""Gets attributes of ``optimizer_info``.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
optimizer_info (intptr_t): Opaque structure that is accessed.
attr (ContractionOptimizerInfoAttribute): Specifies the attribute that is requested.
buf (intptr_t): On return, this buffer (of size ``size_in_bytes``) holds the value that corresponds to ``attr`` within ``optimizeInfo``.
size_in_bytes (size_t): Size of ``buf`` (in bytes).
.. note:: To compute the attribute size, use the itemsize of the corresponding data
type, which can be queried using :func:`get_contraction_optimizer_info_attribute_dtype`.
.. seealso:: `cutensornetContractionOptimizerInfoGetAttribute`
"""
with nogil:
status = cutensornetContractionOptimizerInfoGetAttribute(<const Handle>handle, <const ContractionOptimizerInfo>optimizer_info, <_ContractionOptimizerInfoAttribute>attr, <void*>buf, size_in_bytes)
check_status(status)
cpdef contraction_optimizer_info_set_attribute(intptr_t handle, intptr_t optimizer_info, int attr, intptr_t buf, size_t size_in_bytes):
"""Sets attributes of optimizer_info.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
optimizer_info (intptr_t): Opaque structure that is accessed.
attr (ContractionOptimizerInfoAttribute): Specifies the attribute that is requested.
buf (intptr_t): This buffer (of size ``size_in_bytes``) determines the value to which ``attr`` will be set.
size_in_bytes (size_t): Size of ``buf`` (in bytes).
.. note:: To compute the attribute size, use the itemsize of the corresponding data
type, which can be queried using :func:`get_contraction_optimizer_info_attribute_dtype`.
.. seealso:: `cutensornetContractionOptimizerInfoSetAttribute`
"""
with nogil:
status = cutensornetContractionOptimizerInfoSetAttribute(<const Handle>handle, <ContractionOptimizerInfo>optimizer_info, <_ContractionOptimizerInfoAttribute>attr, <const void*>buf, size_in_bytes)
check_status(status)
cpdef size_t contraction_optimizer_info_get_packed_size(intptr_t handle, intptr_t optimizer_info) except? 0:
"""Gets the packed size of the ``optimizer_info`` object.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
optimizer_info (intptr_t): Opaque structure of type cutensornetContractionOptimizerInfo_t.
Returns:
size_t: The packed size (in bytes).
.. seealso:: `cutensornetContractionOptimizerInfoGetPackedSize`
"""
cdef size_t size_in_bytes
with nogil:
status = cutensornetContractionOptimizerInfoGetPackedSize(<const Handle>handle, <const ContractionOptimizerInfo>optimizer_info, &size_in_bytes)
check_status(status)
return size_in_bytes
cpdef contraction_optimizer_info_pack_data(intptr_t handle, intptr_t optimizer_info, buffer, size_t size_in_bytes):
"""Packs the ``optimizer_info`` object into the provided buffer.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
optimizer_info (intptr_t): Opaque structure of type cutensornetContractionOptimizerInfo_t.
buffer (bytes): On return, this buffer holds the contents of optimizer_info in packed form.
size_in_bytes (size_t): The size of the buffer (in bytes).
.. seealso:: `cutensornetContractionOptimizerInfoPackData`
"""
cdef void* _buffer_ = get_buffer_pointer(buffer, size_in_bytes, readonly=False)
with nogil:
status = cutensornetContractionOptimizerInfoPackData(<const Handle>handle, <const ContractionOptimizerInfo>optimizer_info, <void*>_buffer_, size_in_bytes)
check_status(status)
cpdef intptr_t create_contraction_optimizer_info_from_packed_data(intptr_t handle, intptr_t desc_net, buffer, size_t size_in_bytes) except? 0:
"""Create an optimizerInfo object from the provided buffer.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
desc_net (intptr_t): Describes the tensor network (i.e., its tensors and their connectivity) for which ``optimizerInfo`` is created.
buffer (bytes): A buffer with the contents of optimizerInfo in packed form.
size_in_bytes (size_t): The size of the buffer (in bytes).
Returns:
intptr_t: Pointer to ``cutensornetContractionOptimizerInfo_t``.
.. seealso:: `cutensornetCreateContractionOptimizerInfoFromPackedData`
"""
cdef void* _buffer_ = get_buffer_pointer(buffer, size_in_bytes, readonly=True)
cdef ContractionOptimizerInfo optimizer_info
with nogil:
status = cutensornetCreateContractionOptimizerInfoFromPackedData(<const Handle>handle, <const NetworkDescriptor>desc_net, <const void*>_buffer_, size_in_bytes, &optimizer_info)
check_status(status)
return <intptr_t>optimizer_info
cpdef update_contraction_optimizer_info_from_packed_data(intptr_t handle, buffer, size_t size_in_bytes, intptr_t optimizer_info):
"""Update the provided ``optimizer_info`` object from the provided buffer.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
buffer (bytes): A buffer with the contents of optimizer_info in packed form.
size_in_bytes (size_t): The size of the buffer (in bytes).
optimizer_info (intptr_t): Opaque object of type ``cutensornetContractionOptimizerInfo_t`` that will be updated.
.. seealso:: `cutensornetUpdateContractionOptimizerInfoFromPackedData`
"""
cdef void* _buffer_ = get_buffer_pointer(buffer, size_in_bytes, readonly=True)
with nogil:
status = cutensornetUpdateContractionOptimizerInfoFromPackedData(<const Handle>handle, <const void*>_buffer_, size_in_bytes, <ContractionOptimizerInfo>optimizer_info)
check_status(status)
cpdef intptr_t create_contraction_plan(intptr_t handle, intptr_t desc_net, intptr_t optimizer_info, intptr_t work_desc) except? 0:
"""Initializes a ``cutensornetContractionPlan_t``.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
desc_net (intptr_t): Describes the tensor network (i.e., its tensors and their connectivity).
optimizer_info (intptr_t): Opaque structure.
work_desc (intptr_t): Opaque structure describing the workspace. At the creation of the contraction plan, only the workspace size is needed; the pointer to the workspace memory may be left null. If a device memory handler is set, ``work_desc`` can be set either to null (in which case the "recommended" workspace size is inferred, see ``CUTENSORNET_WORKSIZE_PREF_RECOMMENDED``) or to a valid ``cutensornetWorkspaceDescriptor_t`` with the desired workspace size set and a null workspace pointer, see Memory Management API section.
Returns:
intptr_t: cuTensorNet's contraction plan holds all the information required to perform the tensor contractions; to be precise, it initializes a ``cutensorContractionPlan_t`` for each tensor contraction that is required to contract the entire tensor network.
.. seealso:: `cutensornetCreateContractionPlan`
"""
cdef ContractionPlan plan
with nogil:
status = cutensornetCreateContractionPlan(<const Handle>handle, <const NetworkDescriptor>desc_net, <const ContractionOptimizerInfo>optimizer_info, <const WorkspaceDescriptor>work_desc, &plan)
check_status(status)
return <intptr_t>plan
cpdef destroy_contraction_plan(intptr_t plan):
"""Frees all resources owned by ``plan``.
Args:
plan (intptr_t): Opaque structure.
.. seealso:: `cutensornetDestroyContractionPlan`
"""
with nogil:
status = cutensornetDestroyContractionPlan(<ContractionPlan>plan)
check_status(status)
cpdef contraction_autotune(intptr_t handle, intptr_t plan, raw_data_in, intptr_t raw_data_out, intptr_t work_desc, intptr_t pref, intptr_t stream):
"""Auto-tunes the contraction plan to find the best ``cutensorContractionPlan_t`` for each pair-wise contraction.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
plan (intptr_t): The plan must already be created (see :func:`create_contraction_plan`); the individual contraction plans will be fine-tuned.
raw_data_in (object): Array of N pointers (N being the number of input tensors specified :func:`create_network_descriptor`); ``raw_data_in[i]`` points to the data associated with the i-th input tensor (in device memory). It can be:
- an :class:`int` as the pointer address to the array, or
- a Python sequence of :class:`int`\s (as pointer addresses).
raw_data_out (intptr_t): Points to the raw data of the output tensor (in device memory).
work_desc (intptr_t): Opaque structure describing the workspace. The provided workspace must be ``valid`` (the workspace size must be the same as or larger than both the minimum needed and the value provided at plan creation). See :func:`create_contraction_plan`, :func:`workspace_get_memory_size` & :func:`workspace_set_memory`. If a device memory handler is set, the ``work_desc`` can be set to null, or the workspace pointer in ``work_desc`` can be set to null, and the workspace size can be set either to 0 (in which case the "recommended" size is used, see ``CUTENSORNET_WORKSIZE_PREF_RECOMMENDED``) or to a ``valid`` size. A workspace of the specified size will be drawn from the user's mempool and released back once done.
pref (intptr_t): Controls the auto-tuning process and gives the user control over how much time is spent in this routine.
stream (intptr_t): The CUDA stream on which the computation is performed.
.. seealso:: `cutensornetContractionAutotune`
"""
cdef nullable_unique_ptr[ vector[void*] ] _raw_data_in_ = \
get_resource_ptrs[void](raw_data_in, <void*>NULL)
with nogil:
status = cutensornetContractionAutotune(<const Handle>handle, <ContractionPlan>plan, <const void* const*>(_raw_data_in_.data()), <void*>raw_data_out, <WorkspaceDescriptor>work_desc, <const ContractionAutotunePreference>pref, <Stream>stream)
check_status(status)
cpdef intptr_t create_contraction_autotune_preference(intptr_t handle) except? 0:
"""Sets up the required auto-tune parameters for the contraction plan.
Args:
handle (intptr_t): Opaque handle holding cuTensorNet's library context.
Returns:
intptr_t: This data structure holds all information about the user-requested auto-tune parameters.
.. seealso:: `cutensornetCreateContractionAutotunePreference`
"""
cdef ContractionAutotunePreference autotune_preference
with nogil:
status = cutensornetCreateContractionAutotunePreference(<const Handle>handle, &autotune_preference)
check_status(status)
return <intptr_t>autotune_preference
######################### Python specific utility #########################
cdef dict contraction_autotune_preference_attribute_sizes = {