wangwei created SINGA-205:
-----------------------------

             Summary: Enable slice and concatenate operations for Tensor objects
                 Key: SINGA-205
                 URL: https://issues.apache.org/jira/browse/SINGA-205
             Project: Singa
          Issue Type: New Feature
            Reporter: wangwei
            Assignee: wangwei


Slice and concatenate operations are typical array operations, and are 
necessary to support some models, e.g., GoogleLeNet.
For this ticket, we are going to implement the two operations to support 1D and 
2D tensors.  The following functions would be implemented,
1. SliceRows(Tensor, start, end), the returned tensor shares the memory with 
the origin tensor.
2. CopyRows(Tensor, start, end), CopyColumns(Tensor, start, end) the returned 
tensor copies values from the original tensor
3. ConcatenateRows(vector<Tensor>) and ConcatenateColumns(vector<Tensor>); the 
returned tensor copies values from the origin tensors.

To support SliceRows, i.e., memory sharing between two tensors of different 
sizes, we need to update the Block class as

{code}
class Block {
  Block(void* ptr, size_t size, size_t offset=0);
  void* mutable_data() const  { return data_+offset_;}
  const void* data() const  { return data_+offset_;}
  private:
    void *data_;
    size_t offset_ = 0;  // offset in terms of Bytes.
    shared_ptr<atomic<int>> ref_count_;
};
{code}
The sub-tensors (i.e., generated from SliceRows) would have a different Block 
instance as the original tensor, but would share the internal `ref_count_` and 
`data_` fields.
`offset_` is 0 for the original tensors, but non-zero for sub-tensors . atomic 
makes the inc and dec operations thread-safe. shared_ptr helps to free 
`ref_count_` safely.


SliceColumns is not supported as singa::Tensor uses row major storage which is 
difficult to support SliceColumns SliceRows(). Users can use CopyColumns().




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to