blob: a6f6f6fdce4ae18939f7101cb015e72e3ca3e4c4 [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
Benoit Jacobe91fa442019-05-29 15:15:44 -040072void 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 Jacobf5b81082019-04-11 15:59:22 -040075 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 Jacobe91fa442019-05-29 15:15:44 -040080 lhs.zero_point = 125;
Benoit Jacobf5b81082019-04-11 15:59:22 -040081 ruy::Matrix<std::uint8_t> rhs;
82 ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &rhs.layout);
83 rhs.data = rhs_data;
Benoit Jacobe91fa442019-05-29 15:15:44 -040084 rhs.zero_point = 132;
Benoit Jacobf5b81082019-04-11 15:59:22 -040085 ruy::Matrix<std::uint8_t> dst;
86 ruy::MakeSimpleLayout(2, 2, ruy::Order::kColMajor, &dst.layout);
87 dst.data = dst_data;
Benoit Jacobe91fa442019-05-29 15:15:44 -040088 dst.zero_point = 129;
Benoit Jacobf5b81082019-04-11 15:59:22 -040089
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 Jacobe91fa442019-05-29 15:15:44 -040095 std::cout << "Example Mul, uint8 quantized with asymmetric zero points:\n";
Benoit Jacobf5b81082019-04-11 15:59:22 -040096 std::cout << "LHS:\n" << lhs;
97 std::cout << "RHS:\n" << rhs;
98 std::cout << "Result:\n" << dst << "\n";
99}
Benoit Jacobf5b81082019-04-11 15:59:22 -0400100void 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 Jacoba0ba3ac2019-04-08 12:00:37 -0400128int main() {
129 ruy::Context context;
Benoit Jacobf5b81082019-04-11 15:59:22 -0400130 ExampleMulFloat(&context);
131 ExampleMulFloatWithBiasAddAndClamp(&context);
Benoit Jacobe91fa442019-05-29 15:15:44 -0400132 ExampleMulUint8AsymmetricQuantized(&context);
Benoit Jacobf5b81082019-04-11 15:59:22 -0400133 ExampleMulInt8PerChannelQuantized(&context);
Benoit Jacoba0ba3ac2019-04-08 12:00:37 -0400134}