| 1 | package AI::TensorFlow::Libtensorflow::Lib::_Alloc; | ||
| 2 | # ABSTRACT: [private] Allocation utilities | ||
| 3 | $AI::TensorFlow::Libtensorflow::Lib::_Alloc::VERSION = '0.0.7'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use AI::TensorFlow::Libtensorflow::Lib; | ||
| 7 | use AI::TensorFlow::Libtensorflow::Tensor; | ||
| 8 | use AI::TensorFlow::Libtensorflow::DataType qw(INT8); | ||
| 9 | use FFI::Platypus::Memory qw(malloc free strcpy); | ||
| 10 | use FFI::Platypus::Buffer qw(buffer_to_scalar window); | ||
| 11 | use Sub::Quote qw(quote_sub); | ||
| 12 | |||
| 13 | use Feature::Compat::Defer; | ||
| 14 | |||
| 15 | # If _aligned_alloc() implementation needs the size to be a multiple of the | ||
| 16 | # alignment. | ||
| 17 | our $_ALIGNED_ALLOC_ALIGNMENT_MULTIPLE = 0; | ||
| 18 | |||
| 19 | my $ffi = FFI::Platypus->new; | ||
| 20 | $ffi->lib(undef); | ||
| 21 | if( $ffi->find_symbol('aligned_alloc') ) { | ||
| 22 | # C11 aligned_alloc() | ||
| 23 | # NOTE: C11 aligned_alloc not available on Windows. | ||
| 24 | # void *aligned_alloc(size_t alignment, size_t size); | ||
| 25 | $ffi->attach( [ 'aligned_alloc' => '_aligned_alloc' ] => | ||
| 26 | [ 'size_t', 'size_t' ] => 'opaque' ); | ||
| 27 | *_aligned_free = *free; | ||
| 28 | $_ALIGNED_ALLOC_ALIGNMENT_MULTIPLE = 1; | ||
| 29 | } else { | ||
| 30 | # Pure Perl _aligned_alloc() | ||
| 31 | quote_sub '_aligned_alloc', q{ | ||
| 32 | my ($alignment, $size) = @_; | ||
| 33 | |||
| 34 | # $alignment must fit in 8-bits | ||
| 35 | die "\$alignment must be <= 255" if $alignment > 0xFF; | ||
| 36 | |||
| 37 | my $requested_size = $alignment + $size; # size_t | ||
| 38 | my $ptr = malloc($requested_size); # void* | ||
| 39 | my $offset = $alignment - $ptr % $alignment; # size_t | ||
| 40 | my $aligned = $ptr + $offset; # void* | ||
| 41 | |||
| 42 | strcpy $aligned - 1, chr($offset); | ||
| 43 | |||
| 44 | return $aligned; | ||
| 45 | }; | ||
| 46 | quote_sub '_aligned_free', q{ | ||
| 47 | my ($aligned) = @_; | ||
| 48 | my $offset = ord(buffer_to_scalar($aligned - 1, 1)); | ||
| 49 | free( $aligned - $offset ); | ||
| 50 | }; | ||
| 51 | $_ALIGNED_ALLOC_ALIGNMENT_MULTIPLE = 0; | ||
| 52 | } | ||
| 53 | |||
| 54 | use Const::Fast; | ||
| 55 | # See <https://github.com/tensorflow/tensorflow/issues/58112>. | ||
| 56 | # This is a power-of-two. | ||
| 57 | const our $EIGEN_MAX_ALIGN_BYTES => do { _tf_alignment(); }; | ||
| 58 | |||
| 59 | sub _tf_alignment { | ||
| 60 | # Bytes of alignment sorted in descending order: | ||
| 61 | # NOTE Alignment can not currently be larger than 128-bytes as the pure | ||
| 62 | # Perl implementation of _aligned_alloc() only supports alignment of up | ||
| 63 | # to 255 bytes (which means 128 bytes is the maximum power-of-two | ||
| 64 | # alignment). | ||
| 65 | my @alignments = map 2**$_, reverse 0..7; | ||
| 66 | |||
| 67 | # 1-byte element | ||
| 68 | my $el = INT8; | ||
| 69 | my $el_size = $el->Size; | ||
| 70 | |||
| 71 | my $max_alignment = $alignments[0]; | ||
| 72 | my $req_size = 2 * $max_alignment + $el_size; | ||
| 73 | # All data that is sent to TF_NewTensor here is within the block of | ||
| 74 | # memory allocated at $ptr_base. | ||
| 75 | my $ptr_base = malloc($req_size); | ||
| 76 | defer { free($ptr_base); } | ||
| 77 | |||
| 78 | # start at offset that is aligned with $max_alignment | ||
| 79 | my $ptr = $ptr_base + ( $max_alignment - $ptr_base % $max_alignment ); | ||
| 80 | |||
| 81 | my $create_tensor_at_alignment = sub { | ||
| 82 | my ($n, $dealloc_called) = @_; | ||
| 83 | my $offset = $n - $ptr % $n; | ||
| 84 | my $ptr_offset = $ptr + $offset; | ||
| 85 | my $space_for_data = $req_size - $offset; | ||
| 86 | |||
| 87 | window(my $data, $ptr_offset, $space_for_data); | ||
| 88 | |||
| 89 | return AI::TensorFlow::Libtensorflow::Tensor->New( | ||
| 90 | $el, [int($space_for_data/$el_size)], \$data, sub { | ||
| 91 | $$dealloc_called = 1 | ||
| 92 | } | ||
| 93 | ); | ||
| 94 | }; | ||
| 95 | |||
| 96 | for my $a_idx (0..@alignments-2) { | ||
| 97 | my @dealloc = (0, 0); | ||
| 98 | my @t = map { | ||
| 99 | $create_tensor_at_alignment->($alignments[$a_idx + $_], \$dealloc[$_]); | ||
| 100 | } (0..1); | ||
| 101 | return $alignments[$a_idx] if $dealloc[0] == 0 && $dealloc[1] == 1; | ||
| 102 | } | ||
| 103 | |||
| 104 | return 1; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub _tf_aligned_alloc { | ||
| 108 | my ($class, $size) = @_; | ||
| 109 | return _aligned_alloc($EIGEN_MAX_ALIGN_BYTES, | ||
| 110 | $_ALIGNED_ALLOC_ALIGNMENT_MULTIPLE | ||
| 111 | # since $EIGEN_MAX_ALIGN_BYTES is a power-of-two, use | ||
| 112 | # two's complement bit arithmetic | ||
| 113 | ? ($size + $EIGEN_MAX_ALIGN_BYTES - 1 ) & -$EIGEN_MAX_ALIGN_BYTES | ||
| 114 | : $size | ||
| 115 | ); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub _tf_aligned_free { | ||
| 119 | my ($class, $ptr) = @_; | ||
| 120 | _aligned_free($ptr); | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; | ||
| 124 | |||
| 125 | __END__ |