Skip to content

Commit

Permalink
Add debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiago Crepaldi committed Aug 2, 2022
1 parent 4cfde71 commit 72e056c
Show file tree
Hide file tree
Showing 24 changed files with 140 additions and 60 deletions.
83 changes: 44 additions & 39 deletions onnxruntime/core/providers/cpu/tensor/col2im.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,55 +27,60 @@ Status Col2Im<T>::Compute(OpKernelContext* context) const {
const auto* col_input = context->Input<Tensor>(0);
const auto* image_shape = context->Input<Tensor>(1);
const auto* kernel_shape = context->Input<Tensor>(2);
std::cout << "Status Col2Im<T>::Compute(OpKernelContext* context)" << std::endl;

TensorShape col_shape = col_input->Shape();
const auto num_image_channels = image_shape->Shape()[1];
const auto batch_size = col_shape[0];
const T* col_input_data = col_input->template Data<T>();
TensorShape col_input_shape = col_input->Shape();
int64_t col_input_C = col_input_shape[1];
const auto col_input_N = col_input_shape[0];

const int64_t image_size = image_shape->Shape().Size();

AllocatorPtr alloc;
ORT_RETURN_IF_ERROR(context->GetTempSpaceAllocator(&alloc));
const int64_t col_buffer_size = col_input->Shape().Size();
auto col_data = alloc->Alloc(SafeInt<size_t>(sizeof(T)) * col_buffer_size);

BufferUniquePtr col_buffer(col_data, BufferDeleter(std::move(alloc)));
T* col_buffer_data = static_cast<T*>(col_buffer.get());
int64_t image_shape_size = 1;
int64_t kernel_shape_size = 1;
for (auto i=0; i < image_shape->Shape().Size(); ++i) {
image_shape_size *= image_shape->Data<int64_t>()[i];
kernel_shape_size *= kernel_shape->Data<int64_t>()[i];
// col_input_C computed as => (C*n-ary-prod{kernel_shape}) / n-ary-prod{kernel_shape}
col_input_C /= kernel_shape->Data<int64_t>()[i];
}

TensorShapeVector Y_dims;
Y_dims.insert(Y_dims.begin(), {batch_size, num_image_channels});
Y_dims.insert(Y_dims.begin(), {col_input_N, col_input_C});
for (auto i=0; i < image_shape->Shape()[0]; ++i) {
Y_dims.push_back(image_shape->Data<int64_t>()[i]);
}
TensorShape Yshape(Y_dims);
Tensor* Y = context->Output(0, Yshape);
T* Ydata = Y->template MutableData<T>();

// template <typename T, class Provider, int order>
// void Col2imNd(
// const T* data_col,
// const int64_t* img_shape,
// const int64_t* output_shape,
// int64_t channels_col,
// int64_t img_size,
// const int64_t* kernel_shape,
// const int64_t* stride,
// const int64_t* dilation,
// const int64_t* pad,
// ptrdiff_t N,
// T* data_img,
// Provider* provider);
std::cout << "\n\tInput 0: col_input = ("; for (auto i=0; i < Yshape.Size(); ++i) std::cout << col_input_data[i] << ", "; std::cout << ") with shape "<< Yshape << std::endl;
std::cout << "\tInput 1: image_shape = ("; for (auto i=0; i < image_shape->Shape().Size(); ++i) std::cout << image_shape->Data<int64_t>()[i] << ", "; std::cout << ")" << std::endl;
std::cout << "\tInput 2: kernel_shape = ("; for (auto i=0; i < kernel_shape->Shape().Size(); ++i) std::cout << kernel_shape->Data<int64_t>()[i] << ", "; std::cout << ")" << std::endl;
std::cout << "\tAttribute strides = ("; for (size_t i=0; i < col2im_attrs_.strides.size(); ++i) std::cout << col2im_attrs_.strides[i] << ", "; std::cout << ")"<< std::endl;
std::cout << "\tAttribute dilations = ("; for (size_t i=0; i < col2im_attrs_.dilations.size(); ++i) std::cout << col2im_attrs_.dilations[i] << ", "; std::cout << ")"<< std::endl;
std::cout << "\tAttribute pads = ("; for (size_t i=0; i < col2im_attrs_.pads.size(); ++i) std::cout << col2im_attrs_.pads[i] << ", "; std::cout << ")"<< std::endl;

std::cout << "\tVariable col_input_C: " << col_input_C << std::endl;
std::cout << "\tVariable col_input_N = " << col_input_N << std::endl;
std::cout << "\tVariable image_shape_size: " << image_shape_size << std::endl;
std::cout << "\tVariable kernel_shape_size: " << kernel_shape_size << std::endl;

std::cout << "\n\tStatus Col2Im<T>::Compute() --> math::Col2imNd<>()" << std::endl;

math::Col2imNd<T, CPUMathUtil, StorageOrder::NCHW>(
col_buffer_data,
image_shape->Shape().GetDims().data(),
col_shape.GetDims().data(),
num_image_channels,
image_size,
kernel_shape->Shape().GetDims().data(),
col2im_attrs_.strides.data(),
col2im_attrs_.dilations.data(),
col2im_attrs_.pads.data(),
static_cast<int>(kernel_shape->Shape().Size()),
Ydata,
&CPUMathUtil::Instance());
col_input_data, // const T* data_col,
image_shape->Data<int64_t>(), // const int64_t* img_shape,
Yshape.Slice(2).GetDims().data(), // const int64_t* output_shape,
col_input_C, // int64_t channels_col, --> output_num_channels * kernel_shape_size
image_shape_size, // int64_t img_size,
kernel_shape->Data<int64_t>(), // const int64_t* kernel_shape,
col2im_attrs_.strides.data(), // const int64_t* stride,
col2im_attrs_.dilations.data(), // const int64_t* dilation,
col2im_attrs_.pads.data(), // const int64_t* pad,
kernel_shape->Shape().Size(), // ptrdiff_t N, --> number of spatial dims for image
Ydata, // T* data_img,
&CPUMathUtil::Instance() // Provider* provider
);
std::cout << "\n\n Return Col2Im<T>::Compute() --> "; for (auto i=0; i < Yshape.Size(); ++i) std::cout << Ydata[i] << ", "; std::cout << ") with shape " << Yshape << std::endl << std::endl;

return Status::OK();
}
Expand Down
6 changes: 3 additions & 3 deletions onnxruntime/core/providers/cpu/tensor/col2im_attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ struct Col2ImAttributes {

explicit Col2ImAttributes(const OpKernelInfo& info) {
auto status = info.GetAttrs("strides", strides);
ORT_ENFORCE(status.IsOK());
// ORT_ENFORCE(status.IsOK());

gsl::span<const int64_t> pads_span;
status = info.GetAttrsAsSpan("pads", pads_span);
ORT_ENFORCE(status.IsOK());
// ORT_ENFORCE(status.IsOK());
pads.assign(pads_span.cbegin(), pads_span.cend());

status = info.GetAttrs("dilations", dilations);
ORT_ENFORCE(status.IsOK());
// ORT_ENFORCE(status.IsOK());
}

~Col2ImAttributes() = default;
Expand Down
83 changes: 65 additions & 18 deletions onnxruntime/core/util/math_cpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#pragma GCC diagnostic pop
#endif
using onnxruntime::concurrency::ThreadPool;
#include <iostream>

namespace onnxruntime {
namespace math {
Expand Down Expand Up @@ -370,7 +371,27 @@ void Im2col<T, StorageOrder::NCHW>::operator()(
T* data_col,
bool accumulate_output,
T padding_value) {
int64_t kernel_size = std::accumulate(kernel_shape, kernel_shape + rank, 1LL, std::multiplies<int64_t>());

int64_t im_shape_size = std::accumulate(im_shape, im_shape + rank, 1LL, std::multiplies<int64_t>());
int64_t output_shape_size = std::accumulate(output_shape, output_shape + rank, 1LL, std::multiplies<int64_t>());
int64_t kernel_shape_size = std::accumulate(kernel_shape, kernel_shape + rank, 1LL, std::multiplies<int64_t>());

std::cout << "\n\nCalled void Im2col<T, StorageOrder::NCHW>::operator()(";
std::cout << ",\n\tconst T* data_im={"; for (auto i=0; i < im_shape_size; ++i) std::cout << data_im[i] << ", "; std::cout << "}";
std::cout << ",\n\tconst int64_t* im_shape={"; for (auto i=0; i < rank; ++i) std::cout << im_shape[i] << ", "; std::cout << "}";
std::cout << ",\n\tconst int64_t* output_shape={"; for (auto i=0; i < rank; ++i) std::cout << output_shape[i] << ", "; std::cout << "}";
std::cout << ",\n\tint64_t channels_col=" << channels_col;
std::cout << ",\n\tconst int64_t* kernel_shape={"; for (auto i=0; i < rank; ++i) std::cout << kernel_shape[i] << ", "; std::cout << "}";
std::cout << ",\n\tconst int64_t* stride={"; for (auto i=0; i < rank; ++i) std::cout << stride[i] << ", "; std::cout << "}";
std::cout << ",\n\tconst int64_t* dilation={"; for (auto i=0; i < rank; ++i) std::cout << dilation[i] << ", "; std::cout << "}";
std::cout << ",\n\tconst int64_t* pad={"; for (auto i=0; i < rank; ++i) std::cout << pad[i] << ", "; std::cout << "}";
std::cout << ",\n\tptrdiff_t rank=" << rank;
std::cout << ",\n\tT* data_col= preallocated pointer to write at {"; for (auto i=0; i < output_shape_size; ++i) std::cout << data_col[i] << ", "; std::cout << "}";
std::cout << ",\n\tbool accumulate_output=" << accumulate_output;
std::cout << ",\n\tT padding_value=" << padding_value << ")";

std::cout << "\n\n\tVariable im_shape_size: " << im_shape_size << "\n\tVariable output_shape_size: "<<output_shape_size << "\n\tVariable kernel_shape_size: " << kernel_shape_size << std::endl<< std::endl;

std::vector<int64_t> d_offset(rank, 0);
std::vector<int64_t> d_iter(rank, 0);
for (int64_t c_col = 0; c_col < channels_col; ++c_col) {
Expand All @@ -386,7 +407,7 @@ void Im2col<T, StorageOrder::NCHW>::operator()(
// Loop over spatial axes in forward order to compute the indices in the
// image and column, and whether the index lies in the padding.
int64_t index_col = c_col;
int64_t index_im = c_col / kernel_size;
int64_t index_im = c_col / kernel_shape_size;
bool is_padding = false;
for (ptrdiff_t d_i = 0; d_i < rank; ++d_i) {
int64_t d = d_iter[d_i];
Expand All @@ -408,6 +429,8 @@ void Im2col<T, StorageOrder::NCHW>::operator()(
}
} while (NextPosition(rank, output_shape, d_iter.data()));
} // for (int c = 0; c < channels_col; ++c) {

std::cout << "Return void Im2col -> T* data_col={"; for (auto i=0; i < output_shape_size; ++i) std::cout << data_col[i] << ", "; std::cout << "}\n";
}

template struct Im2col<float, StorageOrder::NCHW>;
Expand Down Expand Up @@ -780,24 +803,48 @@ void Col2im<float, CPUMathUtil, StorageOrder::NHWC>(const float* data_col, int64
}

template <>
void Col2imNd<float, CPUMathUtil, StorageOrder::NCHW>(const float* data_col, const int64_t* img_shape,
const int64_t* output_shape, int64_t channels_col, int64_t img_size,
const int64_t* kernel_shape, const int64_t* stride,
const int64_t* dilation, const int64_t* pad, ptrdiff_t N,
float* data_img, CPUMathUtil* context) {
void Col2imNd<float, CPUMathUtil, StorageOrder::NCHW>(const float* data_col,
const int64_t* img_shape,
const int64_t* output_shape,
int64_t channels_col,
int64_t img_size,
const int64_t* kernel_shape,
const int64_t* stride,
const int64_t* dilation,
const int64_t* pad,
ptrdiff_t N,
float* data_img,
CPUMathUtil* context) {
std::cout << "\n\nCalled void Col2imNd<float, CPUMathUtil, StorageOrder::NCHW>(";
std::cout << ",\n\tconst float* data_col={"; for (auto i=0; i < img_size; ++i) std::cout << data_col[i] << ", "; std::cout << "}";
std::cout << ",\n\tconst int64_t* img_shape={"; for (auto i=0; i < N; ++i) std::cout << img_shape[i] << ", "; std::cout << "}";
std::cout << ",\n\tconst int64_t* output_shape={"; for (auto i=0; i < N; ++i) std::cout << output_shape[i] << ", "; std::cout << "}";
std::cout << ",\n\tint64_t channels_col=" << channels_col;
std::cout << ",\n\tint64_t img_size=" << img_size;
std::cout << ",\n\tconst int64_t* kernel_shape={"; for (auto i=0; i < N; ++i) std::cout << kernel_shape[i] << ", "; std::cout << "}";
std::cout << ",\n\tconst int64_t* stride={"; for (auto i=0; i < N; ++i) std::cout << stride[i] << ", "; std::cout << "}";
std::cout << ",\n\tconst int64_t* dilation={"; for (auto i=0; i < N; ++i) std::cout << dilation[i] << ", "; std::cout << "}";
std::cout << ",\n\tconst int64_t* pad={"; for (auto i=0; i < 2*N; ++i) std::cout << pad[i] << ", "; std::cout << "}";
std::cout << ",\n\tptrdiff_t N=" << N;
std::cout << ",\n\tfloat* data_img= preallocated pointer to save at {"; for (auto i=0; i < img_size; ++i) std::cout << data_img[i] << ", "; std::cout << "}";
std::cout << ",\n\tCPUMathUtil* context=...)" << std::endl;

Set<float, CPUMathUtil>(gsl::narrow<ptrdiff_t>(img_size), 0, data_img, context);
Im2col<float, StorageOrder::NCHW>()(
data_col,
img_shape,
output_shape,
channels_col,
kernel_shape,
stride,
dilation,
pad,
N,
data_img,
true);
data_col, // const T* data_im,
img_shape, // const int64_t* im_shape,
output_shape, // const int64_t* output_shape,
channels_col, // int64_t channels_col,
kernel_shape, // const int64_t* kernel_shape,
stride, // const int64_t* stride,
dilation, // const int64_t* dilation,
pad, // const int64_t* pad,
N, // ptrdiff_t rank,
data_img, // T* data_col,
true // bool accumulate_output,
);

std::cout << "Return void Col2imNd --> float* data_img= {"; for (auto i=0; i < img_size; ++i) std::cout << data_img[i] << ", "; std::cout << "}";
}

#define SPECIALIZED_COPYVECTOR(T) \
Expand Down
28 changes: 28 additions & 0 deletions onnxruntime/test/contrib_ops/col2im_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#include "gtest/gtest.h"
#include "test/providers/provider_test_utils.h"
#include "core/util/math.h"

namespace onnxruntime {
namespace test {

TEST(Col2ImContribOpTest, simple) {
OpTester test("Col2Im", 1, kMSDomain);

test.AddAttribute("strides", std::vector<int64_t>{1, 1});
test.AddAttribute("dilations", std::vector<int64_t>{1, 1});
test.AddAttribute("pads", std::vector<int64_t>{0, 0, 0, 0});

test.AddInput<float>("input", {1, 5, 5}, std::vector<float>{1.f, 6.f, 11.f, 16.f, 21.f, 2.f, 7.f, 12.f, 17.f, 22.f, 3.f, 8.f, 13.f, 18.f, 23.f, 4.f, 9.f, 14.f, 19.f, 24.f, 5.f, 0.f, 15.f, 20.f, 25.f});
test.AddInput<int64_t>("image_shape", {2}, std::vector<int64_t>{5, 5});
test.AddInput<int64_t>("block_shape", {2}, std::vector<int64_t>{1, 5});

test.AddOutput<float>("output", {1, 1, 5, 5}, std::vector<float>{1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f, 12.f, 13.f, 14.f, 15.f, 16.f, 17.f, 18.f, 19.f, 20.f, 21.f, 22.f, 23.f, 24.f, 25.f});
test.Run();
}


} // namespace test
} // namespace onnxruntime
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 72e056c

Please sign in to comment.