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 | |
Benoit Jacob | e91fa44 | 2019-05-29 15:15:44 -0400 | [diff] [blame^] | 72 | void ExampleMulUint8AsymmetricQuantized(ruy::Context *context) { |
| 73 | const std::uint8_t lhs_data[] = {124, 125, 126, 127}; |
| 74 | const std::uint8_t rhs_data[] = {129, 130, 131, 132}; |
Benoit Jacob | f5b8108 | 2019-04-11 15:59:22 -0400 | [diff] [blame] | 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; |
Benoit Jacob | e91fa44 | 2019-05-29 15:15:44 -0400 | [diff] [blame^] | 80 | lhs.zero_point = 125; |
Benoit Jacob | f5b8108 | 2019-04-11 15:59:22 -0400 | [diff] [blame] | 81 | ruy::Matrix<std::uint8_t> rhs; |
| 82 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &rhs.layout); |
| 83 | rhs.data = rhs_data; |
Benoit Jacob | e91fa44 | 2019-05-29 15:15:44 -0400 | [diff] [blame^] | 84 | rhs.zero_point = 132; |
Benoit Jacob | f5b8108 | 2019-04-11 15:59:22 -0400 | [diff] [blame] | 85 | ruy::Matrix<std::uint8_t> dst; |
| 86 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &dst.layout); |
| 87 | dst.data = dst_data; |
Benoit Jacob | e91fa44 | 2019-05-29 15:15:44 -0400 | [diff] [blame^] | 88 | dst.zero_point = 129; |
Benoit Jacob | f5b8108 | 2019-04-11 15:59:22 -0400 | [diff] [blame] | 89 | |
| 90 | ruy::BasicSpec<std::int32_t, std::uint8_t> spec; |
| 91 | spec.multiplier_fixedpoint = 1 << 30; |
| 92 | spec.multiplier_exponent = 0; |
| 93 | ruy::Mul<ruy::kAllPaths>(lhs, rhs, spec, context, &dst); |
| 94 | |
Benoit Jacob | e91fa44 | 2019-05-29 15:15:44 -0400 | [diff] [blame^] | 95 | std::cout << "Example Mul, uint8 quantized with asymmetric zero points:\n"; |
Benoit Jacob | f5b8108 | 2019-04-11 15:59:22 -0400 | [diff] [blame] | 96 | std::cout << "LHS:\n" << lhs; |
| 97 | std::cout << "RHS:\n" << rhs; |
| 98 | std::cout << "Result:\n" << dst << "\n"; |
| 99 | } |
Benoit Jacob | f5b8108 | 2019-04-11 15:59:22 -0400 | [diff] [blame] | 100 | void ExampleMulInt8PerChannelQuantized(ruy::Context *context) { |
| 101 | const std::int8_t lhs_data[] = {1, 2, 3, 4}; |
| 102 | const std::int8_t rhs_data[] = {1, 2, 3, 4}; |
| 103 | const std::int32_t multiplier_data[] = {3 << 28, 5 << 28}; |
| 104 | const int exponent_data[] = {1, -2}; |
| 105 | std::int8_t dst_data[4]; |
| 106 | |
| 107 | ruy::Matrix<std::int8_t> lhs; |
| 108 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kRowMajor, &lhs.layout); |
| 109 | lhs.data = lhs_data; |
| 110 | ruy::Matrix<std::int8_t> rhs; |
| 111 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &rhs.layout); |
| 112 | rhs.data = rhs_data; |
| 113 | ruy::Matrix<std::int8_t> dst; |
| 114 | ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &dst.layout); |
| 115 | dst.data = dst_data; |
| 116 | |
| 117 | ruy::BasicSpec<std::int32_t, std::uint8_t> spec; |
| 118 | spec.multiplier_fixedpoint_perchannel = multiplier_data; |
| 119 | spec.multiplier_exponent_perchannel = exponent_data; |
| 120 | ruy::Mul<ruy::kAllPaths>(lhs, rhs, spec, context, &dst); |
| 121 | |
| 122 | std::cout << "Example Mul, int8 quantized with per-channel multipliers\n"; |
| 123 | std::cout << "LHS:\n" << lhs; |
| 124 | std::cout << "RHS:\n" << rhs; |
| 125 | std::cout << "Result:\n" << dst << "\n"; |
| 126 | } |
| 127 | |
Benoit Jacob | a0ba3ac | 2019-04-08 12:00:37 -0400 | [diff] [blame] | 128 | int main() { |
| 129 | ruy::Context context; |
Benoit Jacob | f5b8108 | 2019-04-11 15:59:22 -0400 | [diff] [blame] | 130 | ExampleMulFloat(&context); |
| 131 | ExampleMulFloatWithBiasAddAndClamp(&context); |
Benoit Jacob | e91fa44 | 2019-05-29 15:15:44 -0400 | [diff] [blame^] | 132 | ExampleMulUint8AsymmetricQuantized(&context); |
Benoit Jacob | f5b8108 | 2019-04-11 15:59:22 -0400 | [diff] [blame] | 133 | ExampleMulInt8PerChannelQuantized(&context); |
Benoit Jacob | a0ba3ac | 2019-04-08 12:00:37 -0400 | [diff] [blame] | 134 | } |