Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose stride from disc ral to torch tensor #1124

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,16 @@ at::List<at::Tensor> RalContext::CreateAndBindingOutputs(
out_tensor = torch::from_blob(
const_cast<void*>(out_buf->data()),
out_buf->shape(),
out_buf->strides(),
deleter,
option);
out_buf->release();
} else {
out_tensor =
torch::from_blob(
const_cast<void*>(out_buf->data()), out_buf->shape(), option)
.clone();
out_tensor = torch::from_blob(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need to copy the underline buffer? @wyzero Pls double check if it is safe.

const_cast<void*>(out_buf->data()),
out_buf->shape(),
out_buf->strides(),
option);
}
outputs.push_back(out_tensor);
}
Expand Down
7 changes: 5 additions & 2 deletions tao_compiler/mlir/xla/ral/context/base/base_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ void BaseExecutionContext::bindOutput(
return;
}

output->reset(
new BaseOutputBufferWrapper(it->second.buffer, it->second.shape));
output->reset(new BaseOutputBufferWrapper(it->second.buffer, it->second.shape,
it->second.strides));
if (!output_ptr_set.insert(it->second.buffer).second) {
// This buffer is used as output before, thus is already set a deleter.
return;
Expand Down Expand Up @@ -192,6 +192,9 @@ void ral_base_cuda_send_output(ExecutionContext* ctx, int64_t output_idx,
for (int i = 0; i < N; ++i) {
tensor.shape.push_back(memref.sizes[i]);
}
for (int i = 0; i < N; ++i) {
tensor.strides.push_back(memref.strides[i]);
}
for (int i = 0; i < N; ++i) {
TAO_VLOG(1) << "tensor dim size = " << tensor.shape[i];
}
Expand Down
6 changes: 6 additions & 0 deletions tao_compiler/mlir/xla/ral/context/base/base_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class BaseOutputBufferWrapper : public OutputBufferWrapper {
public:
BaseOutputBufferWrapper(buffer_t data, buffer_shape_t shape)
: data_(data), shape_(shape) {}
BaseOutputBufferWrapper(buffer_t data, buffer_shape_t shape,
buffer_shape_t strides)
: data_(data), shape_(shape), strides_(strides) {}
~BaseOutputBufferWrapper() {
if (deleter_) deleter_(data_);
}
Expand All @@ -51,6 +54,7 @@ class BaseOutputBufferWrapper : public OutputBufferWrapper {

const_buffer_t data() override { return data_; }
const buffer_shape_t& shape() override { return shape_; }
const buffer_shape_t& strides() override { return strides_; }
void set_deleter(Deleter deleter) { deleter_ = deleter; }

// Returns true if this wrapper is the exclusive owner
Expand All @@ -69,6 +73,7 @@ class BaseOutputBufferWrapper : public OutputBufferWrapper {
private:
buffer_t data_;
buffer_shape_t shape_;
buffer_shape_t strides_;
Deleter deleter_;
bool owned_ = false;
};
Expand All @@ -92,6 +97,7 @@ class InternalAllocator : public Allocator {
struct Tensor {
void* buffer;
std::vector<int64_t> shape;
std::vector<int64_t> strides;
};

struct BaseExecutionContext : public tao::ral::ExecutionContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ void BaseCpuExecutionContext::setOutputDeleter(OutputBufferWrapper& output) {
// This buffer is a pesistent buffer, thus no need to set a deleter.
return;
}

auto hid = host_ptr_map.find(buffer);
if (hid != host_ptr_map.end()) {
if (--hid->second == 0) {
Expand Down
22 changes: 22 additions & 0 deletions tao_compiler/mlir/xla/ral/context/context_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ tao::ral::MemRefType<T, N> assignMemRef(void* ptr, const ShapeTy& shape) {
return memref;
}

template <typename T, int N, typename ShapeTy>
tao::ral::MemRefType<T, N> assignMemRef(void* ptr, const ShapeTy& shape,
const ShapeTy& strides) {
tao::ral::MemRefType<T, N> memref;
memref.basePtr = reinterpret_cast<T*>(ptr);
memref.data = reinterpret_cast<T*>(ptr);
memref.offset = 0;
for (int i = 0; i < N; ++i) {
memref.sizes[i] = shape[i];
}

for (int i = 0; i < N; ++i) {
memref.strides[i] = strides[i];
}

if (TAO_VLOG_IS_ON(1)) {
print_memref(memref, "assigned");
}

return memref;
}

template <typename T>
tao::ral::MemRefType<T, 0> assignMemRef_0d(void* ptr) {
tao::ral::MemRefType<T, 0> memref;
Expand Down
1 change: 1 addition & 0 deletions tao_compiler/mlir/xla/ral/ral_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class OutputBufferWrapper {
virtual ~OutputBufferWrapper() {}
virtual const_buffer_t data() = 0;
virtual const buffer_shape_t& shape() = 0;
virtual const buffer_shape_t& strides() = 0;
// Returns true if this wrapper is the exclusive owner
virtual bool owned() const = 0;
// mark that this wrapper exclusively owns the underlying buffer.
Expand Down