Skip to content

Commit

Permalink
!45234 mindir serializer support compression_type
Browse files Browse the repository at this point in the history
Merge pull request !45234 from yeyunpeng2020/master_serialize_ci
  • Loading branch information
it-is-a-robot authored and gitee-org committed Nov 8, 2022
2 parents 658d048 + 30ab13a commit bbff57b
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 5 deletions.
5 changes: 5 additions & 0 deletions mindspore/ccsrc/transform/express_ir/mindir_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,11 @@ bool IrExportBuilder::BuildParameters(const FuncGraphPtr &func_graph, mind_ir::G
MS_LOG(ERROR) << "Set parameter " << param->DebugString() << " to TensorProto failed.";
return false;
}
auto tensor = param->default_param()->cast<tensor::TensorPtr>();
if (tensor != nullptr) {
parameter_proto->set_compression_type(
static_cast<mind_ir::TensorProto_CompressionType>(tensor->compression_type()));
}
} else {
mind_ir::ValueInfoProto *input_proto = graph_proto->add_input();
input_proto->set_name(param_name);
Expand Down
35 changes: 35 additions & 0 deletions mindspore/core/ir/quantization_param.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2022 Huawei Technologies Co., Ltd
*
* Licensed 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 "ir/quantization_param.h"
#include "utils/ms_utils.h"

namespace mindspore {
bool QuantizationParam::operator==(const QuantizationParam &other) const {
if (quant_algo_name() != other.quant_algo_name()) {
return false;
}
return common::IsAttrsEqual(attrs_, other.attrs_);
}

bool QuantizationParam::operator==(const mindspore::Value &other) const {
if (other.isa<QuantizationParam>()) {
auto other_prim = static_cast<const QuantizationParam &>(other);
return *this == other_prim;
} else {
return false;
}
}
} // namespace mindspore
107 changes: 107 additions & 0 deletions mindspore/core/ir/quantization_param.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* Copyright 2022 Huawei Technologies Co., Ltd
*
* Licensed 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.
*/

#ifndef MINDSPORE_CORE_IR_QUANTIZATION_PARAM_H_
#define MINDSPORE_CORE_IR_QUANTIZATION_PARAM_H_

#include <string>
#include <memory>
#include "ir/named.h"
#include "ir/primal_attr.h"

namespace mindspore {
/// \brief QuantizationParam defines tensor quantization param of MindSpore.
class MS_CORE_API QuantizationParam : public Value {
public:
explicit QuantizationParam(const std::string &quant_algo_name) : quant_algo_name_(quant_algo_name) {}
~QuantizationParam() = default;

/// \brief Add attribute to QuantizationParam attribute map.
///
/// \param[in] name The name of attribute.
/// \param[in] attr The value of attribute.
/// \return The QuantizationParam to which attribute has been added.
QuantizationParam &AddAttr(const std::string &name, const ValuePtr &attr) {
attrs_[name] = attr;
return *this;
}

/// \brief Delete the attribute.
///
/// \param[in] name The name of attribute to be delete.
/// \return The QuantizationParam to which attribute has been added.
QuantizationParam &DelAttr(const std::string &name) {
(void)attrs_.erase(name);
return *this;
}

/// \brief Set attribute to the quant param attribute map.
void SetAttr(const std::string &attrName, const ValuePtr &attr) { attrs_[attrName] = attr; }
/// \brief Get QuantizationParam's attribute.
///
/// \param[in] attrName QuantizationParam attribute name.
/// \return The value of attribute in QuantizationParam attribute map, if the map is not
ValuePtr GetAttr(const std::string &attrName) const {
auto iter = attrs_.find(attrName);
return iter == attrs_.cend() ? nullptr : iter->second;
}

/// \brief Use add attribute by using a map,all elements of the map will be added in the QuantizationParam's attribute
/// map.
///
/// \param[in] attrs The attribute map needs to be added in the QuantizationParam attribute.
/// \return The QuantizationParam to which attribute has been added.
QuantizationParam &set_attrs(const mindspore::HashMap<std::string, ValuePtr> &attrs) {
for (auto &attr : attrs) {
attrs_[attr.first] = attr.second;
}
return *this;
}

/// \brief Get QuantizationParam's all attributes.
///
/// \return The QuantizationParam's all attribute.
const mindspore::HashMap<std::string, ValuePtr> &attrs() const { return attrs_; }

/// \brief Get QuantizationParam's algorithm name.
///
/// \return The QuantizationParam's algorithm name.
std::string quant_algo_name() const { return quant_algo_name_; }

/// \brief Set quantization algorithm name.
///
/// \param[in] quant_algo_name The QuantizationParam's algorithm name.
/// \return The QuantizationParam to which algorithm name has been added.
QuantizationParam &set_quant_algo_name(const std::string &quant_algo_name) {
this->quant_algo_name_ = quant_algo_name;
return *this;
}
MS_DECLARE_PARENT(QuantizationParam, Value);

bool operator==(const Value &other) const override;
/// \brief To compare whether two Primitive objects are equal.
///
/// \param[in] other The other QuantizationParam be compared with.
/// \return return true if the name and attributes of primitives are the same,otherwise return false.
bool operator==(const QuantizationParam &other) const;

private:
std::string quant_algo_name_;
mindspore::HashMap<std::string, ValuePtr> attrs_;
};
using QuantizationParamPtr = std::shared_ptr<QuantizationParam>;
} // namespace mindspore
#endif // MINDSPORE_CORE_IR_QUANT_PARAM_H
15 changes: 14 additions & 1 deletion mindspore/core/ir/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "utils/ms_exception.h"
#include "ir/device_event.h"
#include "utils/os.h"
#include "ir/quantization_param.h"

// brief mindspore namespace.
//
Expand Down Expand Up @@ -755,6 +756,18 @@ class MS_CORE_API Tensor : public MetaTensor {
/// \return tensor name.
const std::string &name() const { return tensor_name_; }

/// \brief Set tensor quant param.
///
/// \param[in] quant_param The tensor quant param.
void set_quant_param(const std::vector<std::shared_ptr<QuantizationParam>> &quant_params) {
quant_params_.assign(quant_params.begin(), quant_params.end());
}

/// \brief Get the tensor quant param.
///
/// \return tensor quant param.
const std::vector<std::shared_ptr<QuantizationParam>> &quant_params() const { return quant_params_; }

private:
void ExecuteLazyTask() const;

Expand All @@ -781,7 +794,7 @@ class MS_CORE_API Tensor : public MetaTensor {
std::function<void(void)> lazy_callback_{nullptr};
UserData user_data_;
TensorCompressionType compression_type_{kNoCompression};

std::vector<std::shared_ptr<QuantizationParam>> quant_params_;
std::string tensor_name_;
};

Expand Down
32 changes: 30 additions & 2 deletions mindspore/core/load_mindir/anf_model_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,21 @@ tensor::TensorPtr MSANFModelParser::GenerateTensorPtrFromTensorProto(const mind_
for (int i = 0; i < attr_tensor.dims_size(); ++i) {
shape.push_back(attr_tensor.dims(i));
}
tensor::TensorPtr tensor = std::make_shared<tensor::Tensor>(kDefaultValueSwitchMap[attr_tensor_type], shape);
tensor::TensorPtr tensor = nullptr;
if (!attr_tensor.has_compression_type() ||
attr_tensor.compression_type() == mind_ir::TensorProto_CompressionType_NO_COMPRESSION) {
tensor = std::make_shared<tensor::Tensor>(kDefaultValueSwitchMap[attr_tensor_type], shape);
} else {
auto compression_type = static_cast<TensorCompressionType>(static_cast<int>(attr_tensor.compression_type()));
size_t data_size = 0;
if (!attr_tensor.has_external_data()) {
data_size = attr_tensor.raw_data().size();
} else {
data_size = attr_tensor.external_data().length();
}
tensor =
std::make_shared<tensor::Tensor>(kDefaultValueSwitchMap[attr_tensor_type], shape, data_size, compression_type);
}

if (!IsIncLoad() || load_tensor_map_.find(attr_tensor.name()) == load_tensor_map_.end()) {
load_tensor_map_[attr_tensor.name()] = tensor;
Expand Down Expand Up @@ -974,7 +988,21 @@ bool MSANFModelParser::ObtainValueNodeInTupleTensorForm(const std::string &value
for (int j = 0; j < attr_tensor.dims_size(); ++j) {
shape.push_back(attr_tensor.dims(j));
}
tensor::TensorPtr tensor_info = std::make_shared<tensor::Tensor>(kDefaultValueSwitchMap[attr_tensor_type], shape);
tensor::TensorPtr tensor_info = nullptr;
if (!attr_tensor.has_compression_type() ||
attr_tensor.compression_type() == mind_ir::TensorProto_CompressionType_NO_COMPRESSION) {
tensor_info = std::make_shared<tensor::Tensor>(kDefaultValueSwitchMap[attr_tensor_type], shape);
} else {
auto compression_type = static_cast<TensorCompressionType>(static_cast<int>(attr_tensor.compression_type()));
size_t data_size = 0;
if (!attr_tensor.has_external_data()) {
data_size = attr_tensor.raw_data().size();
} else {
data_size = attr_tensor.external_data().length();
}
tensor_info =
std::make_shared<tensor::Tensor>(kDefaultValueSwitchMap[attr_tensor_type], shape, data_size, compression_type);
}
const std::string &tensor_buf = attr_tensor.raw_data();
auto *tensor_data_buf = reinterpret_cast<uint8_t *>(tensor_info->data_c());
errno_t ret = memcpy_s(tensor_data_buf, tensor_info->data().nbytes(), tensor_buf.data(), tensor_buf.size());
Expand Down
1 change: 1 addition & 0 deletions mindspore/core/load_mindir/load_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ FuncGraphPtr MindIRLoader::LoadMindIR(const std::string &file_name) {
param_proto->set_name(param_graph.parameter(param_index).name());
param_proto->set_data_type(param_graph.parameter(param_index).data_type());
param_proto->set_raw_data(param_graph.parameter(param_index).raw_data());
param_proto->set_compression_type(param_graph.parameter(param_index).compression_type());
for (const auto &dim : param_graph.parameter(param_index).dims()) {
param_proto->add_dims(dim);
}
Expand Down
4 changes: 2 additions & 2 deletions mindspore/core/proto/mind_ir.proto
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ message TensorProto {
}
message QuantParamProto {
required string quant_algo_name = 1;
repeated AttributeProto attrs = 2;
repeated AttributeProto attribute = 2;
}
repeated int64 dims = 1;
optional int32 data_type = 2;
Expand All @@ -186,7 +186,7 @@ message TensorProto {
repeated int64 min_dims = 14;
repeated int64 max_dims = 15;
optional CompressionType compression_type = 16;
repeated QuantParamProto quant_param = 17;
repeated QuantParamProto quant_params = 17;
}

message ParallelProto {
Expand Down

0 comments on commit bbff57b

Please sign in to comment.