Skip to content

Commit

Permalink
graph: backend: compiler: ops: support transpose with no permutation …
Browse files Browse the repository at this point in the history
…axis
  • Loading branch information
yifeizh2 committed Jun 5, 2024
1 parent 800690a commit eb013e3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2021-2023 Intel Corporation
* Copyright 2021-2024 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -215,9 +215,10 @@ bool should_transform_reorder(const sc_op_ptr &node) {

static bool should_transform_transpose(const sc_op_ptr &node) {
assert(node->isa<transpose_op_t>());
if (node->info_.inputs_[0]->details_.get_format()
!= node->info_.outputs_[0]->details_.get_format()) {
// means that we have permuted the data format
// all transpose op can be converted to tensor_view as long as layout
// propagation is in effect
if (node->attrs_.get_or_else("layout_transformed", false)) {
// means that we have transformed the data format
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,16 +506,9 @@ void transpose_op_t::query_format(context_ptr ctx,
auto in_format = info_.inputs_[0]->details_.get_format();
auto in_strides = info_.inputs_[0]->details_.get_strides();
auto in_format_code = in_format.format_code_;
int batch_dims = info_.inputs_[0]->details_.get_plain_dims().size()
- in_format_code.norig_dims();
std::unordered_map<int, int> order_map;
for (size_t i = 0; i < order_.size(); ++i) {
COMPILE_ASSERT((order_[i] >= batch_dims)
== (static_cast<int>(i) >= batch_dims),
"Permutation on batch dims is not supported.")
if (order_[i] >= batch_dims && static_cast<int>(i) >= batch_dims) {
order_map[order_[i] - batch_dims] = i - batch_dims;
}
order_map[order_[i]] = i;
}

std::vector<int> storage_args;
Expand All @@ -525,6 +518,8 @@ void transpose_op_t::query_format(context_ptr ctx,
}
auto out_format = sc_data_format_t(storage_args, in_format.blocks_);

attrs_["layout_transformed"] = true;

supported_ins.resize(1);
supported_outs.resize(1);
supported_ins[0].emplace_back(std::make_pair(in_format, in_strides));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2020-2023 Intel Corporation
* Copyright 2020-2024 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,10 +37,11 @@ using namespace dnnl::impl::graph::gc;

template <class T>
static std::vector<T> ref_transpose(const std::vector<T> &data,
const sc_dims &input_dims, const std::vector<int> &axis) {
assert(axis.size() == 2);
const sc_dims &input_dims, std::vector<int> axis) {
assert(axis.size() == 2 || axis.empty());
const int num_of_loops = input_dims.size();
sc_dims output_dims = input_dims;
if (axis.empty()) { axis = {0, 0}; }
std::swap(output_dims[axis[0]], output_dims[axis[1]]);
std::vector<int> lp_vars(num_of_loops);
std::vector<T> ret(data.size());
Expand Down Expand Up @@ -155,4 +156,5 @@ TEST(GCCore_CPU_transpose_test, TestQueryFormat) {
TEST(GCCore_CPU_transpose_test, TestSingleTranspose) {
transpose_test({4, 8, 16, 32}, {0, 1, 3, 2}, {2, 3});
transpose_test({4, 8, 16, 32}, {0, 2, 1, 3}, {1, 2});
transpose_test({4, 8, 16, 32}, {0, 1, 2, 3}, {});
}

0 comments on commit eb013e3

Please sign in to comment.