This repository has been archived by the owner on Nov 25, 2022. It is now read-only.
forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ETHOSN] Transpose fully connected weights (apache#12970)
* [ETHOSN] Transpose fully connected weights The NPU driver stack expects weights in IO (HWIO) format, however, Relay uses an OI representation. Although the shape of the weight tensor was correctly changed during codegen, the values in the weights tensor were not being transposed. This lead to an output mismatch when the output "units" was > 1. The tests didn't catch this due to using a weights tensor of all 1's. Change-Id: I51b2bcd14b677280ef3b6a6845d56b7dfacc7d6a * Address comments * Refactor use of weight transpose to common file between contrib codegens. * Make function areguments more explicit. * Update network hashes. Change-Id: Ib53bc7d2837b62908b92fd09062cbe9a8bb4ab30 * Fix lint Change-Id: I6a1d9ffa8e3a747b7c77c9b27aa1b1c0d4c5cbff * Fix cmsis-nn weights transpose Change-Id: Ie89e429da222ffe17bc8faf831bf59217008a68a * Address comments Change-Id: Ie5ded2db3024b9e2c5095f01adea65798fc1da55
- Loading branch information
Showing
11 changed files
with
180 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#include "constant_transforms.h" | ||
|
||
#include <string> | ||
|
||
#include "../../transforms/pattern_utils.h" | ||
#include "../../transforms/simplify_expr.h" | ||
|
||
/*! | ||
* \file src/relay/backend/contrib/constant_transforms.cc | ||
* \brief Transforms applied to constant operations during codegen for BYOC backends. | ||
*/ | ||
|
||
namespace tvm { | ||
namespace relay { | ||
namespace contrib { | ||
|
||
Expr FoldConstantExpr(const Expr& expr, bool fold_qnn) { | ||
auto mod = IRModule::FromExpr(expr); | ||
mod = transform::FoldConstant(fold_qnn)(mod); | ||
auto entry_func = Downcast<Function>(mod->Lookup("main")); | ||
return expr.as<FunctionNode>() == nullptr ? entry_func->body : entry_func; | ||
} | ||
|
||
Constant TransposeWeights(const Constant& data, const std::string& source_layout, | ||
const std::string& target_layout) { | ||
Array<Integer> transpose_matrix; | ||
for (const char& c : target_layout) { | ||
int pos = source_layout.find(c); | ||
transpose_matrix.push_back(pos); | ||
} | ||
Expr transpose = MakeTranspose(data, transpose_matrix); | ||
transpose = InferType(FoldConstantExpr(transpose)); | ||
Constant transposed_data = Downcast<Constant>(transpose); | ||
return transposed_data; | ||
} | ||
|
||
} // namespace contrib | ||
} // namespace relay | ||
} // namespace tvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* \file src/relay/backend/contrib/constant_transforms.h | ||
* \brief Transforms applied to constant operations during codegen for BYOC backends. | ||
*/ | ||
|
||
#ifndef TVM_RELAY_BACKEND_CONTRIB_CONSTANT_TRANSFORMS_H_ | ||
#define TVM_RELAY_BACKEND_CONTRIB_CONSTANT_TRANSFORMS_H_ | ||
|
||
#include <tvm/relay/expr.h> | ||
|
||
#include <string> | ||
|
||
namespace tvm { | ||
namespace relay { | ||
namespace contrib { | ||
|
||
/*! | ||
* \brief Apply constant folding on an expression. | ||
* | ||
* \param expr The expression to fold. | ||
* \param fold_qnn Whether to fold constants for QNN operations. | ||
* \returns The new folded expression. | ||
*/ | ||
Expr FoldConstantExpr(const Expr& expr, bool fold_qnn = true); | ||
|
||
/*! | ||
*\brief Transpose weights from `source_layout` to `target_layout` | ||
* | ||
* \param data The constant expression to transpose. | ||
* \param source_layout The current layout of the constant e.g. "OHWI". | ||
* \param target_layout The target layout of the constant e.g. "HWIO". | ||
*/ | ||
Constant TransposeWeights(const Constant& data, const std::string& source_layout, | ||
const std::string& target_layout); | ||
|
||
} // namespace contrib | ||
} // namespace relay | ||
} // namespace tvm | ||
|
||
#endif // TVM_RELAY_BACKEND_CONTRIB_CONSTANT_TRANSFORMS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.