Jared Duke | 13689fe | 2019-04-16 16:22:07 -0400 | [diff] [blame] | 1 | /* Copyright 2019 Google LLC. All Rights Reserved. |
| 2 | |
| 3 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | you may not use this file except in compliance with the License. |
| 5 | You may obtain a copy of the License at |
| 6 | |
| 7 | http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | |
| 9 | Unless required by applicable law or agreed to in writing, software |
| 10 | distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | See the License for the specific language governing permissions and |
| 13 | limitations under the License. |
| 14 | ==============================================================================*/ |
| 15 | |
Benoit Jacob | a0ba3ac | 2019-04-08 12:00:37 -0400 | [diff] [blame] | 16 | #include <iostream> |
| 17 | |
| 18 | #include "ruy.h" |
| 19 | |
Benoit Jacob | f5b8108 | 2019-04-11 15:59:22 -0400 | [diff] [blame] | 20 | void ExampleMulFloat(ruy::Context *context) { |
| 21 | const float lhs_data[] = {1, 2, 3, 4}; |
| 22 | const float rhs_data[] = {1, 2, 3, 4}; |
| 23 | float dst_data[4]; |
| 24 | |
| 25 | ruy::Matrix<float> lhs; |
| 26 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kRowMajor, &lhs.layout); |
| 27 | lhs.data = lhs_data; |
| 28 | ruy::Matrix<float> rhs; |
| 29 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &rhs.layout); |
| 30 | rhs.data = rhs_data; |
| 31 | ruy::Matrix<float> dst; |
| 32 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &dst.layout); |
| 33 | dst.data = dst_data; |
| 34 | |
| 35 | ruy::BasicSpec<float, float> spec; |
| 36 | ruy::Mul<ruy::kAllPaths>(lhs, rhs, spec, context, &dst); |
| 37 | |
| 38 | std::cout << "Example Mul, float:\n"; |
| 39 | std::cout << "LHS:\n" << lhs; |
| 40 | std::cout << "RHS:\n" << rhs; |
| 41 | std::cout << "Result:\n" << dst << "\n"; |
| 42 | } |
| 43 | |
| 44 | void ExampleMulFloatWithBiasAddAndClamp(ruy::Context *context) { |
| 45 | const float lhs_data[] = {1, 2, 3, 4}; |
| 46 | const float rhs_data[] = {1, 2, 3, 4}; |
| 47 | const float bias_data[] = {1, 0}; |
| 48 | float dst_data[4]; |
| 49 | |
| 50 | ruy::Matrix<float> lhs; |
| 51 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kRowMajor, &lhs.layout); |
| 52 | lhs.data = lhs_data; |
| 53 | ruy::Matrix<float> rhs; |
| 54 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &rhs.layout); |
| 55 | rhs.data = rhs_data; |
| 56 | ruy::Matrix<float> dst; |
| 57 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &dst.layout); |
| 58 | dst.data = dst_data; |
| 59 | |
| 60 | ruy::BasicSpec<float, float> spec; |
| 61 | spec.bias = bias_data; |
| 62 | spec.clamp_min = 0; |
| 63 | spec.clamp_max = 15; |
| 64 | ruy::Mul<ruy::kAllPaths>(lhs, rhs, spec, context, &dst); |
| 65 | |
| 66 | std::cout << "Example Mul, float with bias addition and clamp:\n"; |
| 67 | std::cout << "LHS:\n" << lhs; |
| 68 | std::cout << "RHS:\n" << rhs; |
| 69 | std::cout << "Result:\n" << dst << "\n"; |
| 70 | } |
| 71 | |
| 72 | void ExampleMulUint8Quantized(ruy::Context *context) { |
| 73 | const std::uint8_t lhs_data[] = {1, 2, 3, 4}; |
| 74 | const std::uint8_t rhs_data[] = {1, 2, 3, 4}; |
| 75 | std::uint8_t dst_data[4]; |
| 76 | |
| 77 | ruy::Matrix<std::uint8_t> lhs; |
| 78 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kRowMajor, &lhs.layout); |
| 79 | lhs.data = lhs_data; |
| 80 | ruy::Matrix<std::uint8_t> rhs; |
| 81 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &rhs.layout); |
| 82 | rhs.data = rhs_data; |
| 83 | ruy::Matrix<std::uint8_t> dst; |
| 84 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &dst.layout); |
| 85 | dst.data = dst_data; |
| 86 | |
| 87 | ruy::BasicSpec<std::int32_t, std::uint8_t> spec; |
| 88 | spec.multiplier_fixedpoint = 1 << 30; |
| 89 | spec.multiplier_exponent = 0; |
| 90 | ruy::Mul<ruy::kAllPaths>(lhs, rhs, spec, context, &dst); |
| 91 | |
| 92 | std::cout << "Example Mul, uint8 quantized:\n"; |
| 93 | std::cout << "LHS:\n" << lhs; |
| 94 | std::cout << "RHS:\n" << rhs; |
| 95 | std::cout << "Result:\n" << dst << "\n"; |
| 96 | } |
| 97 | |
| 98 | void ExampleMulUint8QuantizedWithZeroPoints(ruy::Context *context) { |
| 99 | const std::uint8_t lhs_data[] = {1, 2, 3, 4}; |
| 100 | const std::uint8_t rhs_data[] = {1, 2, 3, 4}; |
| 101 | std::uint8_t dst_data[4]; |
| 102 | |
| 103 | ruy::Matrix<std::uint8_t> lhs; |
| 104 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kRowMajor, &lhs.layout); |
| 105 | lhs.data = lhs_data; |
| 106 | lhs.zero_point = 2; |
| 107 | ruy::Matrix<std::uint8_t> rhs; |
| 108 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &rhs.layout); |
| 109 | rhs.data = rhs_data; |
| 110 | rhs.zero_point = 3; |
| 111 | ruy::Matrix<std::uint8_t> dst; |
| 112 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &dst.layout); |
| 113 | dst.data = dst_data; |
| 114 | dst.zero_point = 1; |
| 115 | |
| 116 | ruy::BasicSpec<std::int32_t, std::uint8_t> spec; |
| 117 | spec.multiplier_fixedpoint = 1 << 30; |
| 118 | spec.multiplier_exponent = 0; |
| 119 | ruy::Mul<ruy::kAllPaths>(lhs, rhs, spec, context, &dst); |
| 120 | |
| 121 | std::cout << "Example Mul, uint8 quantized with zero_point's:\n"; |
| 122 | std::cout << "LHS:\n" << lhs; |
| 123 | std::cout << "RHS:\n" << rhs; |
| 124 | std::cout << "Result:\n" << dst << "\n"; |
| 125 | } |
| 126 | |
| 127 | void ExampleMulInt8PerChannelQuantized(ruy::Context *context) { |
| 128 | const std::int8_t lhs_data[] = {1, 2, 3, 4}; |
| 129 | const std::int8_t rhs_data[] = {1, 2, 3, 4}; |
| 130 | const std::int32_t multiplier_data[] = {3 << 28, 5 << 28}; |
| 131 | const int exponent_data[] = {1, -2}; |
| 132 | std::int8_t dst_data[4]; |
| 133 | |
| 134 | ruy::Matrix<std::int8_t> lhs; |
| 135 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kRowMajor, &lhs.layout); |
| 136 | lhs.data = lhs_data; |
| 137 | ruy::Matrix<std::int8_t> rhs; |
| 138 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &rhs.layout); |
| 139 | rhs.data = rhs_data; |
| 140 | ruy::Matrix<std::int8_t> dst; |
| 141 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &dst.layout); |
| 142 | dst.data = dst_data; |
| 143 | |
| 144 | ruy::BasicSpec<std::int32_t, std::uint8_t> spec; |
| 145 | spec.multiplier_fixedpoint_perchannel = multiplier_data; |
| 146 | spec.multiplier_exponent_perchannel = exponent_data; |
| 147 | ruy::Mul<ruy::kAllPaths>(lhs, rhs, spec, context, &dst); |
| 148 | |
| 149 | std::cout << "Example Mul, int8 quantized with per-channel multipliers\n"; |
| 150 | std::cout << "LHS:\n" << lhs; |
| 151 | std::cout << "RHS:\n" << rhs; |
| 152 | std::cout << "Result:\n" << dst << "\n"; |
| 153 | } |
| 154 | |
Benoit Jacob | a0ba3ac | 2019-04-08 12:00:37 -0400 | [diff] [blame] | 155 | int main() { |
| 156 | ruy::Context context; |
Benoit Jacob | f5b8108 | 2019-04-11 15:59:22 -0400 | [diff] [blame] | 157 | ExampleMulFloat(&context); |
| 158 | ExampleMulFloatWithBiasAddAndClamp(&context); |
| 159 | ExampleMulUint8Quantized(&context); |
| 160 | ExampleMulUint8QuantizedWithZeroPoints(&context); |
| 161 | ExampleMulInt8PerChannelQuantized(&context); |
Benoit Jacob | a0ba3ac | 2019-04-08 12:00:37 -0400 | [diff] [blame] | 162 | } |