Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 21499a0

Browse files
committed
Merge remote-tracking branch 'origin/trigonometric-ops' into f/arduino-sine
2 parents 735a65a + 278a929 commit 21499a0

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

src/uTensor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "uTensor/ops/symmetric_quantization/convolution2d.hpp"
2828
#include "uTensor/ops/symmetric_quantization/depthwise_separable_convolution.hpp"
2929
#include "uTensor/ops/symmetric_quantization/fully_connected.hpp"
30-
#include "uTensor/ops/tanh.hpp"
30+
#include "uTensor/ops/trigonometric.hpp"
3131

3232
/*
3333
* Tensors

src/uTensor/ops/Arithmetic.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ class AddOperator : public OperatorInterface<2, 1> {
2323
}
2424
};
2525

26-
}
26+
template <typename T>
27+
class MulOperator : public OperatorInterface<2, 1> {
28+
public:
29+
enum names_in : uint8_t { a, b };
30+
enum names_out : uint8_t { c };
31+
// AddOperator(FixedTensorMap<2> inputs, FixedTensorMap<1> outputs) :
32+
// OperatorBase(inputs, outputs) {}
33+
34+
protected:
35+
virtual void compute() {
36+
mul_kernel<T>(outputs[c].tensor(), inputs[a].tensor(), inputs[b].tensor());
37+
}
38+
};
39+
40+
} // namespace ReferenceOperators
2741
} // namespace uTensor
2842
#endif

src/uTensor/ops/Arithmetic_kernels.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,18 @@ void add_kernel(Tensor& c, const Tensor& a, const Tensor& b) {
1616
c(i) = static_cast<T>(static_cast<T>(a(i)) + static_cast<T>(b(i)));
1717
}
1818

19+
template <typename T>
20+
void mul_kernel(Tensor& c, const Tensor& a, const Tensor& b) {
21+
// Decide on c shape
22+
TensorShape c_shape = c->get_shape();
23+
uint32_t c_size = c_shape.get_linear_size();
24+
// TensorInterface& C = reinterpret_cast<TensorInterface*>(*c);
25+
// const TensorInterface& A = reinterpret_cast<TensorInterface*>(*a);
26+
// const TensorInterface& B = reinterpret_cast<TensorInterface*>(*b);
27+
28+
for (uint32_t i = 0; i < c_size; i++)
29+
c(i) = static_cast<T>(static_cast<T>(a(i)) * static_cast<T>(b(i)));
30+
}
31+
1932
} // namespace uTensor
2033
#endif

src/uTensor/ops/tanh.hpp renamed to src/uTensor/ops/trigonometric.hpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef UTENSOR_TANH_H
2-
#define UTENSOR_TANH_H
1+
#ifndef UTENSOR_TRIGONOMETRIC_H
2+
#define UTENSOR_TRIGONOMETRIC_H
33
#include <math.h>
44

55
#include "uTensor/core/context.hpp"
@@ -44,6 +44,39 @@ class TanhOperator : public OperatorInterface<1, 1> {
4444
}
4545
};
4646

47+
template <typename OutputT, typename InputT>
48+
class SinOperator : public OperatorInterface<1, 1> {
49+
public:
50+
enum names_in : uint8_t { act_in };
51+
enum names_out : uint8_t { act_out };
52+
53+
protected:
54+
virtual void compute() {
55+
Tensor& in = inputs[act_in].tensor();
56+
Tensor& out = outputs[act_out].tensor();
57+
58+
const uint32_t flat_size = in->get_shape().get_linear_size();
59+
60+
int32_t in_zero_point =
61+
in->get_quantization_params().get_zeroP_for_channel(0);
62+
float in_scale = in->get_quantization_params().get_scale_for_channel(0);
63+
64+
int32_t out_zero_point =
65+
out->get_quantization_params().get_zeroP_for_channel(0);
66+
float out_scale = out->get_quantization_params().get_scale_for_channel(0);
67+
68+
for (uint32_t i = 0; i < flat_size; i++) {
69+
const int32_t in_val = static_cast<InputT>(in(i));
70+
float float_in_val = static_cast<float>(
71+
in_scale * static_cast<float>((in_val - in_zero_point)));
72+
float element_activation = sin(float_in_val);
73+
OutputT result =
74+
static_cast<OutputT>(element_activation / out_scale + out_zero_point);
75+
out(i) = result;
76+
}
77+
}
78+
};
79+
4780
} // namespace ReferenceOperators
4881
} // namespace uTensor
49-
#endif
82+
#endif // UTENSOR_TRIGONOMETRIC_H

0 commit comments

Comments
 (0)