|
| 1 | +/***************************************************************************** |
| 2 | + Copyright 2022 Haiping Chen. All Rights Reserved. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +******************************************************************************/ |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.Linq; |
| 20 | +using Tensorflow.Framework; |
| 21 | +using static Tensorflow.Binding; |
| 22 | + |
| 23 | +namespace Tensorflow.Operations |
| 24 | +{ |
| 25 | + public class _EagerTensorArray : TensorArray |
| 26 | + { |
| 27 | + TF_DataType _dtype; |
| 28 | + public override TF_DataType dtype => _dtype; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Used to keep track of what tensors the TensorArray should be |
| 32 | + /// colocated with. We choose to colocate the TensorArray with the |
| 33 | + /// first tensor written to it. |
| 34 | + /// </summary> |
| 35 | + bool _colocate_with_first_write_call; |
| 36 | + public override bool colocate_with_first_write_call => _colocate_with_first_write_call; |
| 37 | + |
| 38 | + bool _infer_shape; |
| 39 | + public override bool infer_shape => _infer_shape; |
| 40 | + public bool _dynamic_size; |
| 41 | + public Shape _element_shape; |
| 42 | + |
| 43 | + public List<Tensor> _colocate_with; |
| 44 | + |
| 45 | + Tensor _handle; |
| 46 | + public override Tensor handle => _handle; |
| 47 | + Tensor _flow; |
| 48 | + public override Tensor flow => _flow; |
| 49 | + bool _clear_after_read; |
| 50 | + List<Tensor> _tensor_array; |
| 51 | + |
| 52 | + public _EagerTensorArray(TF_DataType dtype, Tensor size, bool dynamic_size = false, |
| 53 | + bool clear_after_read = true, string tensor_array_name = null, Tensor handle = null, Tensor flow = null, |
| 54 | + bool infer_shape = true, Shape? element_shape = null, |
| 55 | + bool colocate_with_first_write_call = true, string name = null) |
| 56 | + { |
| 57 | + _flow = constant_op.constant(0); |
| 58 | + _infer_shape = infer_shape; |
| 59 | + _element_shape = element_shape ?? Shape.Null; |
| 60 | + _colocate_with_first_write_call = colocate_with_first_write_call; |
| 61 | + _dtype = dtype.as_base_dtype(); |
| 62 | + _dynamic_size = dynamic_size; |
| 63 | + _clear_after_read = clear_after_read; |
| 64 | + _tensor_array = new List<Tensor>(); |
| 65 | + } |
| 66 | + |
| 67 | + public override TensorArray unstack(Tensor value, string name = null) |
| 68 | + { |
| 69 | + return tf_with(ops.name_scope(name, "TensorArrayUnstack", new { _handle, value }), delegate |
| 70 | + { |
| 71 | + var num_elements = array_ops.shape(value)[0]; |
| 72 | + return scatter(indices: math_ops.range(0, num_elements), value: value, name: name); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + public TensorArray scatter(Tensor indices, Tensor value, string name = null) |
| 77 | + { |
| 78 | + /*return tf_with(ops.name_scope(name, "TensorArrayScatter", new { _handle, value, indices }), delegate |
| 79 | + { |
| 80 | + value = ops.convert_to_tensor(value, preferred_dtype: _dtype, name: "value"); |
| 81 | + if (_infer_shape) |
| 82 | + { |
| 83 | + var shape = new Shape(value.shape.dims.Skip(1).ToArray()); |
| 84 | + _merge_element_shape(shape); |
| 85 | + } |
| 86 | +
|
| 87 | + _maybe_colocate_with(value); |
| 88 | + var flow_out = gen_data_flow_ops.tensor_array_scatter_v3( |
| 89 | + handle: _handle, |
| 90 | + indices: indices, |
| 91 | + value: value, |
| 92 | + flow_in: _flow, |
| 93 | + name: name); |
| 94 | +
|
| 95 | + var ta = new _EagerTensorArray(_dtype, |
| 96 | + infer_shape: _infer_shape, |
| 97 | + element_shape: _element_shape[0], |
| 98 | + dynamic_size: _dynamic_size, |
| 99 | + handle: _handle, |
| 100 | + flow: flow_out, |
| 101 | + colocate_with_first_write_call: _colocate_with_first_write_call); |
| 102 | +
|
| 103 | +
|
| 104 | + return ta; |
| 105 | + });*/ |
| 106 | + throw new NotImplementedException(""); |
| 107 | + } |
| 108 | + |
| 109 | + public void _merge_element_shape(Shape shape) |
| 110 | + { |
| 111 | + _element_shape.concatenate(shape); |
| 112 | + } |
| 113 | + |
| 114 | + public void _maybe_colocate_with(Tensor value) |
| 115 | + { |
| 116 | + _colocate_with.Add(value); |
| 117 | + } |
| 118 | + |
| 119 | + public override Tensor read<T>(T index, string name = null) |
| 120 | + { |
| 121 | + int index_int = -1; |
| 122 | + if (index is int int_index) |
| 123 | + index_int = int_index; |
| 124 | + else if (index is Tensor tensor_index) |
| 125 | + index_int = tensor_index.numpy(); |
| 126 | + else |
| 127 | + throw new ValueError(""); |
| 128 | + |
| 129 | + if (_clear_after_read) |
| 130 | + { |
| 131 | + _tensor_array[index_int] = null; |
| 132 | + } |
| 133 | + |
| 134 | + return _tensor_array[index_int]; |
| 135 | + } |
| 136 | + |
| 137 | + public override TensorArray write(Tensor index, Tensor value, string name = null) |
| 138 | + { |
| 139 | + if (_infer_shape) |
| 140 | + _element_shape = _element_shape.merge_with(value.shape); |
| 141 | + _tensor_array.add(value); |
| 142 | + return this; |
| 143 | + } |
| 144 | + |
| 145 | + public override TensorArray write<T>(int index, T value, string name = null) |
| 146 | + { |
| 147 | + var value_tensor = ops.convert_to_tensor(value, preferred_dtype: _dtype, name: "value"); |
| 148 | + var index_tensor = ops.convert_to_tensor(index, name: "index"); |
| 149 | + return write(index_tensor, value_tensor, name: name); |
| 150 | + } |
| 151 | + |
| 152 | + private Tensor size(string name = null) |
| 153 | + { |
| 154 | + return gen_data_flow_ops.tensor_array_size_v3(_handle, _flow, name: name); |
| 155 | + } |
| 156 | + |
| 157 | + public override Tensor stack(string name = null) |
| 158 | + { |
| 159 | + ops.colocate_with(_handle); |
| 160 | + return tf_with(ops.name_scope(name, "TensorArrayStack", new { _handle }), delegate |
| 161 | + { |
| 162 | + return gather(math_ops.range(0, size()), name: name); |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + public override Tensor gather(Tensor indices, string name = null) |
| 167 | + { |
| 168 | + var element_shape = Shape.Null; |
| 169 | + |
| 170 | + var value = gen_data_flow_ops.tensor_array_gather_v3( |
| 171 | + handle: _handle, |
| 172 | + indices: indices, |
| 173 | + flow_in: _flow, |
| 174 | + dtype: _dtype, |
| 175 | + name: name, |
| 176 | + element_shape: element_shape); |
| 177 | + |
| 178 | + //if (element_shape != null) |
| 179 | + //value.set_shape(-1, element_shape.dims); |
| 180 | + |
| 181 | + return value; |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments