Skip to content

Commit

Permalink
[luci] Rename input_dtype and output_dtype (Samsung#7837)
Browse files Browse the repository at this point in the history
* [luci] Rename input_dtype and output_dtype

Rename input_dtype and output_dtype to model_dtype.

ONE-DCO-1.0-Signed-off-by: Hyukjin Jeong <[email protected]>

* Make link to input_dtype and input_model_dtype

* Move enum params
  • Loading branch information
jinevening authored Oct 15, 2021
1 parent de83a7c commit 0fb144c
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 73 deletions.
7 changes: 5 additions & 2 deletions compiler/luci/pass/include/luci/CircleOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ class CircleOptimizer final
enum AlgorithmParameters
{
// quantize
Quantize_input_dtype,
Quantize_output_dtype,
Quantize_input_model_dtype,
Quantize_output_model_dtype,
Quantize_granularity, // layer-wise or channel-wise
Quantize_tensor_names,
Quantize_scales,
Expand All @@ -104,6 +104,9 @@ class CircleOptimizer final
// convert NCHW to NHWC
NCHW_to_NHWC_input_shape,
NCHW_to_NHWC_output_shape,

Quantize_input_dtype = Quantize_input_model_dtype, // TODO Remove this
Quantize_output_dtype = Quantize_output_model_dtype, // TODO Remove this
};

virtual ~Options() = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ namespace luci
class QuantizeDequantizeWeightsPass : public logo::Pass
{
public:
QuantizeDequantizeWeightsPass(loco::DataType input_dtype, loco::DataType output_dtype,
QuantizeDequantizeWeightsPass(loco::DataType input_model_dtype, loco::DataType output_model_dtype,
QuantizationGranularity granularity)
: _input_dtype{input_dtype}, _output_dtype{output_dtype}, _granularity{granularity}
: _input_model_dtype{input_model_dtype}, _output_model_dtype{output_model_dtype}, _granularity{
granularity}
{
// DO NOTHING
}
Expand All @@ -44,8 +45,8 @@ class QuantizeDequantizeWeightsPass : public logo::Pass
bool run(loco::Graph *graph);

private:
loco::DataType _input_dtype;
loco::DataType _output_dtype;
loco::DataType _input_model_dtype;
loco::DataType _output_model_dtype;
QuantizationGranularity _granularity;
};

Expand Down
9 changes: 5 additions & 4 deletions compiler/luci/pass/include/luci/Pass/QuantizeWithMinMaxPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ namespace luci
class QuantizeWithMinMaxPass : public logo::Pass
{
public:
QuantizeWithMinMaxPass(loco::DataType input_dtype, loco::DataType output_dtype,
QuantizeWithMinMaxPass(loco::DataType input_model_dtype, loco::DataType output_model_dtype,
QuantizationGranularity granularity)
: _input_dtype{input_dtype}, _output_dtype{output_dtype}, _granularity{granularity}
: _input_model_dtype{input_model_dtype}, _output_model_dtype{output_model_dtype}, _granularity{
granularity}
{
// DO NOTHING
}
Expand All @@ -44,8 +45,8 @@ class QuantizeWithMinMaxPass : public logo::Pass
bool run(loco::Graph *graph);

private:
loco::DataType _input_dtype;
loco::DataType _output_dtype;
loco::DataType _input_model_dtype;
loco::DataType _output_model_dtype;
QuantizationGranularity _granularity;
};

Expand Down
71 changes: 40 additions & 31 deletions compiler/luci/pass/src/CircleOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,28 +422,30 @@ void CircleOptimizer::quantize(loco::Graph *g) const
// Fake quantization of weights
if (_options->query(Options::Algorithm::QuantizeDequantizeWeights))
{
static const std::vector<std::string> fakeq_supported_input_dtype{"float32"};
static const std::vector<std::string> fakeq_supported_output_dtype{"uint8", "int16"};
static const std::vector<std::string> fakeq_supported_input_model_dtype{"float32"};
static const std::vector<std::string> fakeq_supported_output_model_dtype{"uint8", "int16"};
static const std::vector<std::string> fakeq_supported_granularity{"layer", "channel"};

auto input_dtype = _options->param(Options::AlgorithmParameters::Quantize_input_dtype);
auto output_dtype = _options->param(Options::AlgorithmParameters::Quantize_output_dtype);
auto input_model_dtype =
_options->param(Options::AlgorithmParameters::Quantize_input_model_dtype);
auto output_model_dtype =
_options->param(Options::AlgorithmParameters::Quantize_output_model_dtype);
auto granularity = _options->param(Options::AlgorithmParameters::Quantize_granularity);

if (!in_array(to_lower_case(input_dtype), fakeq_supported_input_dtype))
if (!in_array(to_lower_case(input_model_dtype), fakeq_supported_input_model_dtype))
throw std::runtime_error("Unsupported input type. List of supported input type: " +
to_string(fakeq_supported_input_dtype));
to_string(fakeq_supported_input_model_dtype));

if (!in_array(to_lower_case(output_dtype), fakeq_supported_output_dtype))
if (!in_array(to_lower_case(output_model_dtype), fakeq_supported_output_model_dtype))
throw std::runtime_error("Unsupported output type. List of supported output type: " +
to_string(fakeq_supported_output_dtype));
to_string(fakeq_supported_output_model_dtype));

if (!in_array(to_lower_case(granularity), fakeq_supported_granularity))
throw std::runtime_error("Unsupported granularity. List of supported granularity: " +
to_string(fakeq_supported_granularity));

if (str_to_granularity(granularity) == QuantizationGranularity::LayerWise &&
str_to_dtype(output_dtype) != loco::DataType::U8)
str_to_dtype(output_model_dtype) != loco::DataType::U8)
throw std::runtime_error("Layer-wise quantization only supports uint8 dtype.");

// Clear existing quantparams before doing fake quantization
Expand All @@ -454,39 +456,43 @@ void CircleOptimizer::quantize(loco::Graph *g) const
circle_node->quantparam(nullptr);
}

luci::QuantizeDequantizeWeightsPass fake_quantizer(
str_to_dtype(input_dtype), str_to_dtype(output_dtype), str_to_granularity(granularity));
luci::QuantizeDequantizeWeightsPass fake_quantizer(str_to_dtype(input_model_dtype),
str_to_dtype(output_model_dtype),
str_to_granularity(granularity));
fake_quantizer.run(g);
}

// Actual quantization of weights, bias, and activation
if (_options->query(Options::Algorithm::QuantizeWithMinMax))
{
static const std::vector<std::string> qwmm_supported_input_dtype{"float32"};
static const std::vector<std::string> qwmm_supported_output_dtype{"uint8", "int16"};
static const std::vector<std::string> qwmm_supported_input_model_dtype{"float32"};
static const std::vector<std::string> qwmm_supported_output_model_dtype{"uint8", "int16"};
static const std::vector<std::string> qwmm_supported_granularity{"layer", "channel"};

auto input_dtype = _options->param(Options::AlgorithmParameters::Quantize_input_dtype);
auto output_dtype = _options->param(Options::AlgorithmParameters::Quantize_output_dtype);
auto input_model_dtype =
_options->param(Options::AlgorithmParameters::Quantize_input_model_dtype);
auto output_model_dtype =
_options->param(Options::AlgorithmParameters::Quantize_output_model_dtype);
auto granularity = _options->param(Options::AlgorithmParameters::Quantize_granularity);

if (!in_array(to_lower_case(input_dtype), qwmm_supported_input_dtype))
if (!in_array(to_lower_case(input_model_dtype), qwmm_supported_input_model_dtype))
throw std::runtime_error("Unsupported input type. List of supported input types: " +
to_string(qwmm_supported_input_dtype));
to_string(qwmm_supported_input_model_dtype));

if (!in_array(to_lower_case(output_dtype), qwmm_supported_output_dtype))
if (!in_array(to_lower_case(output_model_dtype), qwmm_supported_output_model_dtype))
throw std::runtime_error("Unsupported output type. List of supported output types: " +
to_string(qwmm_supported_output_dtype));
to_string(qwmm_supported_output_model_dtype));

if (!in_array(to_lower_case(granularity), qwmm_supported_granularity))
throw std::runtime_error("Unsupported granularity. List of supported granularity: " +
to_string(qwmm_supported_granularity));

if (str_to_granularity(granularity) == QuantizationGranularity::LayerWise &&
str_to_dtype(output_dtype) != loco::DataType::U8)
str_to_dtype(output_model_dtype) != loco::DataType::U8)
throw std::runtime_error("Layer-wise quantization only supports uint8 dtype.");

luci::QuantizeWithMinMaxPass quantizer(str_to_dtype(input_dtype), str_to_dtype(output_dtype),
luci::QuantizeWithMinMaxPass quantizer(str_to_dtype(input_model_dtype),
str_to_dtype(output_model_dtype),
str_to_granularity(granularity));
quantizer.run(g);

Expand All @@ -505,29 +511,32 @@ void CircleOptimizer::quantize(loco::Graph *g) const
phase_runner.run(phase);

// Verify the type/granularity of the quantized model
luci::QuantizedModelVerifier verifier(str_to_dtype(output_dtype),
luci::QuantizedModelVerifier verifier(str_to_dtype(output_model_dtype),
str_to_granularity(granularity));
verifier.verify(g);
}

// Requantize
if (_options->query(Options::Algorithm::Requantize))
{
static const std::vector<std::string> rq_supported_input_dtype{"int8"};
static const std::vector<std::string> rq_supported_output_dtype{"uint8"};
static const std::vector<std::string> rq_supported_input_model_dtype{"int8"};
static const std::vector<std::string> rq_supported_output_model_dtype{"uint8"};

auto input_dtype = _options->param(Options::AlgorithmParameters::Quantize_input_dtype);
auto output_dtype = _options->param(Options::AlgorithmParameters::Quantize_output_dtype);
auto input_model_dtype =
_options->param(Options::AlgorithmParameters::Quantize_input_model_dtype);
auto output_model_dtype =
_options->param(Options::AlgorithmParameters::Quantize_output_model_dtype);

if (!in_array(to_lower_case(input_dtype), rq_supported_input_dtype))
if (!in_array(to_lower_case(input_model_dtype), rq_supported_input_model_dtype))
throw std::runtime_error("Unsupported input type. List of supported input types: " +
to_string(rq_supported_input_dtype));
to_string(rq_supported_input_model_dtype));

if (!in_array(to_lower_case(output_dtype), rq_supported_output_dtype))
if (!in_array(to_lower_case(output_model_dtype), rq_supported_output_model_dtype))
throw std::runtime_error("Unsupported output type. List of supported output types: " +
to_string(rq_supported_output_dtype));
to_string(rq_supported_output_model_dtype));

luci::RequantizePass requantizer(str_to_dtype(input_dtype), str_to_dtype(output_dtype));
luci::RequantizePass requantizer(str_to_dtype(input_model_dtype),
str_to_dtype(output_model_dtype));
requantizer.run(g);
}

Expand Down
44 changes: 22 additions & 22 deletions compiler/luci/pass/src/CircleOptimizer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ TEST(CircleOptimizerTest, quantize_quantdequant_simple)
auto options = o.options();

options->enable(Algorithms::QuantizeDequantizeWeights);
options->param(AlgorithmParameters::Quantize_input_dtype, "float32");
options->param(AlgorithmParameters::Quantize_output_dtype, "uint8");
options->param(AlgorithmParameters::Quantize_input_model_dtype, "float32");
options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
options->param(AlgorithmParameters::Quantize_granularity, "layer");

o.quantize(&g);
Expand All @@ -97,8 +97,8 @@ TEST(CircleOptimizerTest, quantize_quantdequant_input_NEG)
auto options = o.options();

options->enable(Algorithms::QuantizeDequantizeWeights);
options->param(AlgorithmParameters::Quantize_input_dtype, "invalid");
options->param(AlgorithmParameters::Quantize_output_dtype, "uint8");
options->param(AlgorithmParameters::Quantize_input_model_dtype, "invalid");
options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
options->param(AlgorithmParameters::Quantize_granularity, "layer");

EXPECT_THROW(o.quantize(&g), std::runtime_error);
Expand All @@ -112,8 +112,8 @@ TEST(CircleOptimizerTest, quantize_quantdequant_output_NEG)
auto options = o.options();

options->enable(Algorithms::QuantizeDequantizeWeights);
options->param(AlgorithmParameters::Quantize_input_dtype, "float32");
options->param(AlgorithmParameters::Quantize_output_dtype, "invalid");
options->param(AlgorithmParameters::Quantize_input_model_dtype, "float32");
options->param(AlgorithmParameters::Quantize_output_model_dtype, "invalid");
options->param(AlgorithmParameters::Quantize_granularity, "layer");

EXPECT_THROW(o.quantize(&g), std::runtime_error);
Expand All @@ -127,8 +127,8 @@ TEST(CircleOptimizerTest, quantize_quantdequant_gran_NEG)
auto options = o.options();

options->enable(Algorithms::QuantizeDequantizeWeights);
options->param(AlgorithmParameters::Quantize_input_dtype, "float32");
options->param(AlgorithmParameters::Quantize_output_dtype, "uint8");
options->param(AlgorithmParameters::Quantize_input_model_dtype, "float32");
options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
options->param(AlgorithmParameters::Quantize_granularity, "invalid");

EXPECT_THROW(o.quantize(&g), std::runtime_error);
Expand All @@ -142,8 +142,8 @@ TEST(CircleOptimizerTest, quantize_minmax_simple)
auto options = o.options();

options->enable(Algorithms::QuantizeWithMinMax);
options->param(AlgorithmParameters::Quantize_input_dtype, "float32");
options->param(AlgorithmParameters::Quantize_output_dtype, "uint8");
options->param(AlgorithmParameters::Quantize_input_model_dtype, "float32");
options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
options->param(AlgorithmParameters::Quantize_granularity, "layer");

o.quantize(&g);
Expand All @@ -159,8 +159,8 @@ TEST(CircleOptimizerTest, quantize_minmax_input_NEG)
auto options = o.options();

options->enable(Algorithms::QuantizeWithMinMax);
options->param(AlgorithmParameters::Quantize_input_dtype, "invalid");
options->param(AlgorithmParameters::Quantize_output_dtype, "uint8");
options->param(AlgorithmParameters::Quantize_input_model_dtype, "invalid");
options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
options->param(AlgorithmParameters::Quantize_granularity, "layer");

EXPECT_THROW(o.quantize(&g), std::runtime_error);
Expand All @@ -174,8 +174,8 @@ TEST(CircleOptimizerTest, quantize_minmax_output_NEG)
auto options = o.options();

options->enable(Algorithms::QuantizeWithMinMax);
options->param(AlgorithmParameters::Quantize_input_dtype, "float32");
options->param(AlgorithmParameters::Quantize_output_dtype, "invalid");
options->param(AlgorithmParameters::Quantize_input_model_dtype, "float32");
options->param(AlgorithmParameters::Quantize_output_model_dtype, "invalid");
options->param(AlgorithmParameters::Quantize_granularity, "layer");

EXPECT_THROW(o.quantize(&g), std::runtime_error);
Expand All @@ -189,8 +189,8 @@ TEST(CircleOptimizerTest, quantize_minmax_gran_NEG)
auto options = o.options();

options->enable(Algorithms::QuantizeWithMinMax);
options->param(AlgorithmParameters::Quantize_input_dtype, "float32");
options->param(AlgorithmParameters::Quantize_output_dtype, "uint8");
options->param(AlgorithmParameters::Quantize_input_model_dtype, "float32");
options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
options->param(AlgorithmParameters::Quantize_granularity, "invalid");

EXPECT_THROW(o.quantize(&g), std::runtime_error);
Expand All @@ -204,8 +204,8 @@ TEST(CircleOptimizerTest, quantize_requant_simple)
auto options = o.options();

options->enable(Algorithms::Requantize);
options->param(AlgorithmParameters::Quantize_input_dtype, "int8");
options->param(AlgorithmParameters::Quantize_output_dtype, "uint8");
options->param(AlgorithmParameters::Quantize_input_model_dtype, "int8");
options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");

o.quantize(&g);

Expand All @@ -220,8 +220,8 @@ TEST(CircleOptimizerTest, quantize_requant_input_NEG)
auto options = o.options();

options->enable(Algorithms::Requantize);
options->param(AlgorithmParameters::Quantize_input_dtype, "invalid");
options->param(AlgorithmParameters::Quantize_output_dtype, "uint8");
options->param(AlgorithmParameters::Quantize_input_model_dtype, "invalid");
options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");

EXPECT_THROW(o.quantize(&g), std::runtime_error);
}
Expand All @@ -234,8 +234,8 @@ TEST(CircleOptimizerTest, quantize_requant_output_NEG)
auto options = o.options();

options->enable(Algorithms::Requantize);
options->param(AlgorithmParameters::Quantize_input_dtype, "int8");
options->param(AlgorithmParameters::Quantize_output_dtype, "invalid");
options->param(AlgorithmParameters::Quantize_input_model_dtype, "int8");
options->param(AlgorithmParameters::Quantize_output_model_dtype, "invalid");

EXPECT_THROW(o.quantize(&g), std::runtime_error);
}
2 changes: 1 addition & 1 deletion compiler/luci/pass/src/QuantizeDequantizeWeightsPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ bool QuantizeDequantizeWeightsPass::run(loco::Graph *g)
// Quantize weights
for (auto node : loco::active_nodes(loco::output_nodes(g)))
{
QuantizeDequantizeWeights qw(_input_dtype, _output_dtype, _granularity);
QuantizeDequantizeWeights qw(_input_model_dtype, _output_model_dtype, _granularity);
auto circle_node = loco::must_cast<luci::CircleNode *>(node);
circle_node->accept(&qw);
}
Expand Down
Loading

0 comments on commit 0fb144c

Please sign in to comment.