blob: 514043e07b8d1c662e1e6838d9286d4f58ca080b [file] [log] [blame]
Jared Duke13689fe2019-04-16 16:22:07 -04001/* Copyright 2019 Google LLC. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
Benoit Jacoba0ba3ac2019-04-08 12:00:37 -040016#include <iostream>
17
18#include "ruy.h"
19
Benoit Jacobf5b81082019-04-11 15:59:22 -040020void 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
44void 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
72void 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
98void 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
127void 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 Jacoba0ba3ac2019-04-08 12:00:37 -0400155int main() {
156 ruy::Context context;
Benoit Jacobf5b81082019-04-11 15:59:22 -0400157 ExampleMulFloat(&context);
158 ExampleMulFloatWithBiasAddAndClamp(&context);
159 ExampleMulUint8Quantized(&context);
160 ExampleMulUint8QuantizedWithZeroPoints(&context);
161 ExampleMulInt8PerChannelQuantized(&context);
Benoit Jacoba0ba3ac2019-04-08 12:00:37 -0400162}