Skip to content
Merged
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
12 changes: 12 additions & 0 deletions src/backend/common/jit/BufferNodeBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ class BufferNodeBase : public common::Node {
}
return false;
}

virtual void modDims(const af::dim4 &newDim) override {
af::dim4 strides(1, 1, 1, 1);
for(dim_t i = 1; i < 4; ++i) {
strides[i] = strides[i - 1] * newDim[i - 1];
}

for(dim_t i = 0; i < 4; ++i) {
m_param.dims[i] = newDim[i];
m_param.strides[i] = strides[i];
}
}
};

} // namespace common
Expand Down
5 changes: 5 additions & 0 deletions src/backend/common/jit/Node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <optypes.hpp>
#include <types.hpp>
#include <af/defines.h>
#include <af/dim4.hpp>

#include <nonstd/span.hpp>
#include <algorithm>
Expand Down Expand Up @@ -311,6 +312,10 @@ class Node {
}
virtual std::unique_ptr<Node> clone() = 0;

virtual void modDims(const af::dim4 &newDim) {
UNUSED(newDim);
}

#ifdef AF_CPU
template<typename U>
friend void arrayfire::cpu::kernel::evalMultiple(
Expand Down
29 changes: 18 additions & 11 deletions src/backend/common/moddims.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,27 @@ using detail::createNodeArray;

using std::make_shared;
using std::shared_ptr;
using std::array;
using arrayfire::common::Node;
using arrayfire::common::Node_ptr;
using std::vector;

namespace arrayfire {
namespace common {

Node_ptr copyModdims(const Node_ptr &in, const af::dim4 &newDim) {

Node_ptr out = in->clone();
for(int i = 0; i < in->kMaxChildren && in->m_children[i] != nullptr; ++i) {
out->m_children[i] = copyModdims(in->m_children[i], newDim);
}
if(out->isBuffer()) out->modDims(newDim);

return out;
}

template<typename T>
Array<T> moddimOp(const Array<T> &in, af::dim4 outDim) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like moddims is not a jittable operation anymore with this change. what are the performance implications of this?

using arrayfire::common::Node;
using arrayfire::common::Node_ptr;
using std::array;

auto createModdim = [outDim](array<Node_ptr, 1> &operands) {
return make_shared<ModdimNode>(
outDim, static_cast<af::dtype>(af::dtype_traits<T>::af_type),
operands[0]);
};

const auto &node = in.getNode();

Expand All @@ -49,8 +55,9 @@ Array<T> moddimOp(const Array<T> &in, af::dim4 outDim) {
}
if (all_linear == false) in.eval();

Node_ptr out = createNaryNode<T, 1>(outDim, createModdim, {&in});
return createNodeArray<T>(outDim, out);
Array<T> out = createNodeArray<T>(outDim, copyModdims(in.getNode(), outDim));

return out;
}

template<typename T>
Expand Down
13 changes: 13 additions & 0 deletions src/backend/cpu/jit/BufferNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ class BufferNode : public TNode<T> {
}
return false;
}

virtual void modDims(const af::dim4 &newDim) override {
af::dim4 strides(1, 1, 1, 1);
for(dim_t i = 1; i < 4; ++i) {
strides[i] = strides[i - 1] * newDim[i - 1];
}

for(dim_t i = 0; i < 4; ++i) {
m_dims[i] = newDim[i];
m_strides[i] = strides[i];
}
}

};

} // namespace jit
Expand Down
11 changes: 10 additions & 1 deletion src/backend/cuda/jit/BufferNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ bool BufferNodeBase<DataType, ParamType>::operator==(
// clang-format off
return m_data.get() == other.m_data.get() &&
m_bytes == other.m_bytes &&
m_param.ptr == other.m_param.ptr;
m_param.ptr == other.m_param.ptr &&
m_linear_buffer == other.m_linear_buffer &&
m_param.dims[0] == other.m_param.dims[0] &&
m_param.dims[1] == other.m_param.dims[1] &&
m_param.dims[2] == other.m_param.dims[2] &&
m_param.dims[3] == other.m_param.dims[3] &&
m_param.strides[0] == other.m_param.strides[0] &&
m_param.strides[1] == other.m_param.strides[1] &&
m_param.strides[2] == other.m_param.strides[2] &&
m_param.strides[3] == other.m_param.strides[3];
// clang-format on
}

Expand Down
11 changes: 10 additions & 1 deletion src/backend/oneapi/jit/BufferNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ bool BufferNodeBase<DataType, ParamType>::operator==(
// clang-format off
return m_data.get() == other.m_data.get() &&
m_bytes == other.m_bytes &&
m_param.offset == other.m_param.offset;
m_param.offset == other.m_param.offset &&
m_linear_buffer == other.m_linear_buffer &&
m_param.dims[0] == other.m_param.dims[0] &&
m_param.dims[1] == other.m_param.dims[1] &&
m_param.dims[2] == other.m_param.dims[2] &&
m_param.dims[3] == other.m_param.dims[3] &&
m_param.strides[0] == other.m_param.strides[0] &&
m_param.strides[1] == other.m_param.strides[1] &&
m_param.strides[2] == other.m_param.strides[2] &&
m_param.strides[3] == other.m_param.strides[3];
// clang-format on
}

Expand Down
11 changes: 10 additions & 1 deletion src/backend/opencl/jit/BufferNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ bool BufferNodeBase<DataType, ParamType>::operator==(
// clang-format off
return m_data.get() == other.m_data.get() &&
m_bytes == other.m_bytes &&
m_param.offset == other.m_param.offset;
m_param.offset == other.m_param.offset &&
m_linear_buffer == other.m_linear_buffer &&
m_param.dims[0] == other.m_param.dims[0] &&
m_param.dims[1] == other.m_param.dims[1] &&
m_param.dims[2] == other.m_param.dims[2] &&
m_param.dims[3] == other.m_param.dims[3] &&
m_param.strides[0] == other.m_param.strides[0] &&
m_param.strides[1] == other.m_param.strides[1] &&
m_param.strides[2] == other.m_param.strides[2] &&
m_param.strides[3] == other.m_param.strides[3];
// clang-format on
}

Expand Down
40 changes: 40 additions & 0 deletions test/jit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,3 +814,43 @@ TEST(JIT, setKernelCacheDirectory) {
// Reset to the old path
ASSERT_SUCCESS(af_set_kernel_cache_directory(old_path.c_str(), false));
}

// Ensure that a correct result is obtained when evaluating an expression
// that contains both an array and its transpose - see ISSUE 3660
TEST(JIT, evaluateBothArrayAndItsTranspose) {
float X2_ptr[25] = { -1., -1., -1., -1., -1.,
-0.5, -0.5, -0.5, -0.5, -0.5,
0., 0., 0., 0., 0.,
0.5, 0.5, 0.5, 0.5, 0.5,
1., 1., 1., 1., 1. };
array X2_gold(5, 5, X2_ptr);

float Y2_ptr[25] = { -1., -0.5, 0., 0.5, 1.,
-1., -0.5, 0., 0.5, 1.,
-1., -0.5, 0., 0.5, 1.,
-1., -0.5, 0., 0.5, 1.,
-1., -0.5, 0., 0.5, 1. };
array Y2_gold(5, 5, Y2_ptr);

float X2Y2_ptr[25] = { -2., -1.5, -1., -0.5, 0.,
-1.5, -1., -0.5, 0., 0.5,
-1., -0.5, 0., 0.5, 1.,
-0.5, 0., 0.5, 1., 1.5,
0., 0.5, 1., 1.5, 2. };
array X2Y2_gold(5, 5, X2Y2_ptr);

int n = 5;
int half = (n - 1) / 2;
double delta = 1.0 / half;

array coord = delta * (af::range(n) - half);

array X2 = tile(coord.T(), n, 1);
array Y2 = tile(coord, 1, n);

array X2Y2 = X2 + Y2;

ASSERT_ARRAYS_EQ(X2_gold, X2);
ASSERT_ARRAYS_EQ(Y2_gold, Y2);
ASSERT_ARRAYS_EQ(X2Y2_gold, X2Y2);
}