| 32 | // tensor. |
| 33 | |
| 34 | class TensorSlice { |
| 35 | public: |
| 36 | // Construct a tensor slice: you have a number of ways: |
| 37 | // -- creating an empty slice |
| 38 | // -- from just a dimension (in this case it will create a full slice) |
| 39 | // -- from an array of pairs of integers. |
| 40 | // -- from a TensorSliceProto protocol buffer |
| 41 | // -- from a string format of "start,length:start,length..." where each |
| 42 | // "start,length" pair represents the slice on one dimension. We allow a |
| 43 | // special "-" that means "everything for this dimension". One such example |
| 44 | // is: 0,10:-:14,1:-:- |
| 45 | TensorSlice() {} |
| 46 | explicit TensorSlice(int dim); |
| 47 | explicit TensorSlice(const TensorSliceProto& proto); |
| 48 | explicit TensorSlice(std::initializer_list<std::pair<int64, int64>> extents); |
| 49 | |
| 50 | // This factory methods should be used instead of the constructor that takes a |
| 51 | // `TensorSliceProto` if calling code cannot validate that the sizes specify a |
| 52 | // valid `TensorSlice`. |
| 53 | static Status BuildTensorSlice(const TensorSliceProto& proto, |
| 54 | TensorSlice* output); |
| 55 | |
| 56 | static Status Parse(const string& str, TensorSlice* output); |
| 57 | static TensorSlice ParseOrDie(const string& str) { |
| 58 | TensorSlice ret; |
| 59 | Status s = Parse(str, &ret); |
| 60 | if (!s.ok()) { |
| 61 | LOG(FATAL) << "Could not parse TensorSlice"; |
| 62 | } |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | void Clear(); |
| 67 | |
| 68 | // Accessors |
| 69 | int dims() const { return starts_.size(); } |
| 70 | |
| 71 | int64 start(int d) const { |
| 72 | DCHECK_GE(d, 0); |
| 73 | DCHECK_LT(d, dims()); |
| 74 | return starts_[d]; |
| 75 | } |
| 76 | |
| 77 | int64 length(int d) const { |
| 78 | DCHECK_GE(d, 0); |
| 79 | DCHECK_LT(d, dims()); |
| 80 | return lengths_[d]; |
| 81 | } |
| 82 | |
| 83 | int64 end(int d) const { |
| 84 | DCHECK_GE(d, 0); |
| 85 | DCHECK_LT(d, dims()); |
| 86 | return start(d) + length(d); |
| 87 | } |
| 88 | |
| 89 | void set_start(int d, int64 x) { |
| 90 | DCHECK_GE(d, 0); |
| 91 | DCHECK_LT(d, dims()); |
no outgoing calls