forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Relay] [Quantization] WIP - Prototyping requantize op.
- Loading branch information
shoubhikbhatti@gmail.com
committed
Jul 8, 2019
1 parent
8d9e317
commit 5485b58
Showing
4 changed files
with
396 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/*! | ||
* Copyright (c) 2018 by Contributors | ||
* \file requantize.cc | ||
* \brief Quantized convolution operators | ||
*/ | ||
|
||
#include <tvm/relay/op_attr_types.h> | ||
#include <tvm/relay/analysis.h> | ||
#include <tvm/relay/attrs/qnn.h> | ||
#include <tvm/relay/quantize_util.h> | ||
|
||
namespace tvm { | ||
namespace relay { | ||
|
||
TVM_REGISTER_NODE_TYPE(RequantizeAttrs); | ||
|
||
|
||
bool RequantizeRel(const Array<Type>& types, | ||
int num_inputs, | ||
const Attrs& attrs, | ||
const TypeReporter& reporter) { | ||
CHECK_EQ(types.size(), 2); | ||
const auto* data = types[0].as<TensorTypeNode>(); | ||
const auto input_dtype = data->dtype; | ||
CHECK(is_valid_quantized_op_input_type(QuantizeOpType::Requantize, input_dtype)) | ||
<< "Input type should be a quantized type (u)int8 or (u)int16 but was " << input_dtype; | ||
|
||
const Array<tvm::Expr> oshape = data->shape; | ||
// assign output type | ||
const RequantizeAttrs* param = attrs.as<RequantizeAttrs>(); | ||
reporter->Assign(types[1], TensorTypeNode::make(oshape, param->out_dtype)); | ||
return true; | ||
} | ||
|
||
// Positional relay function to create quantized conv2d operator | ||
// used by frontend FFI. | ||
Expr MakeRequantize(Expr data, | ||
int32_t input_zero_point, | ||
double input_scale, | ||
int32_t output_zero_point, | ||
double output_scale, | ||
DataType out_dtype, | ||
bool use_int_compute) { | ||
auto attrs = make_node<RequantizeAttrs>(); | ||
attrs->out_dtype = std::move(out_dtype); | ||
attrs->input_zero_point = std::move(input_zero_point); | ||
attrs->output_zero_point = std::move(output_zero_point); | ||
attrs->input_scale = std::move(input_scale); | ||
attrs->output_scale = std::move(output_scale); | ||
attrs->use_int_compute = std::move(use_int_compute); | ||
static const Op& op = Op::Get("qnn.requantize"); | ||
return CallNode::make(op, {data}, Attrs(attrs), {}); | ||
} | ||
|
||
RELAY_REGISTER_OP("qnn.requantize") | ||
.describe(R"code(Requantize operator. | ||
FIXME | ||
)code" TVM_ADD_FILELINE) | ||
.set_attrs_type_key("relay.attrs.RequantizeAttrs") | ||
.set_num_inputs(1) | ||
.add_argument("data", "Tensor", "The quantized input tensor.") | ||
.set_support_level(10) | ||
.add_type_rel("Requantize", RequantizeRel); | ||
|
||
TVM_REGISTER_API("relay.op.qnn._make.requantize") | ||
.set_body_typed(MakeRequantize); | ||
|
||
} // namespace relay | ||
} // namespace tvm |
Oops, something went wrong.