33namespace itlab_2023 {
44
55void MulLayer::run (const Tensor& A, const Tensor& B, Tensor& output) {
6- if (B.get_shape ().dims ().empty () || B.get_shape () == Shape{1 }) {
7- run_with_scalar (A, B.as <float >()->at (0 ), output);
6+ if (B.get_shape ().dims () == 0 ||
7+ (B.get_shape ().dims () == 1 && B.get_shape ()[0 ] == 1 )) {
8+ if (B.get_type () == Type::kFloat ) {
9+ run_with_scalar (A, B.as <float >()->at (0 ), output);
10+ } else {
11+ run_with_scalar (A, static_cast <float >(B.as <int >()->at (0 )), output);
12+ }
813 return ;
914 }
10- if (A.get_shape ().dims ().empty () || A.get_shape () == Shape{1 }) {
11- run_with_scalar (B, A.as <float >()->at (0 ), output);
15+
16+ if (A.get_shape ().dims () == 0 ||
17+ (A.get_shape ().dims () == 1 && A.get_shape ()[0 ] == 1 )) {
18+ if (A.get_type () == Type::kFloat ) {
19+ run_with_scalar (B, A.as <float >()->at (0 ), output);
20+ } else {
21+ run_with_scalar (B, static_cast <float >(A.as <int >()->at (0 )), output);
22+ }
1223 return ;
1324 }
1425
@@ -19,23 +30,23 @@ void MulLayer::run(const Tensor& A, const Tensor& B, Tensor& output) {
1930 if (A.get_shape () == B.get_shape ()) {
2031 switch (A.get_type ()) {
2132 case Type::kFloat : {
22- auto a_data = A.as <float >()-> get_data ();
23- auto b_data = B.as <float >()-> get_data ();
33+ const auto & a_data = * A.as <float >();
34+ const auto & b_data = * B.as <float >();
2435 std::vector<float > result;
2536 result.reserve (a_data.size ());
2637 std::transform (a_data.begin (), a_data.end (), b_data.begin (),
2738 std::back_inserter (result), std::multiplies<float >());
28- output = Tensor (result, A.get_shape ());
39+ output = make_tensor (result, A.get_shape ());
2940 break ;
3041 }
3142 case Type::kInt : {
32- auto a_data = A.as <int >()-> get_data ();
33- auto b_data = B.as <int >()-> get_data ();
43+ const auto & a_data = * A.as <int >();
44+ const auto & b_data = * B.as <int >();
3445 std::vector<int > result;
3546 result.reserve (a_data.size ());
3647 std::transform (a_data.begin (), a_data.end (), b_data.begin (),
3748 std::back_inserter (result), std::multiplies<int >());
38- output = Tensor (result, A.get_shape ());
49+ output = make_tensor (result, A.get_shape ());
3950 break ;
4051 }
4152 default :
@@ -49,29 +60,29 @@ void MulLayer::run(const Tensor& A, const Tensor& B, Tensor& output) {
4960
5061 switch (A.get_type ()) {
5162 case Type::kFloat : {
52- auto a_data = A.as <float >()-> get_data ();
53- auto b_data = B.as <float >()-> get_data ();
63+ const auto & a_data = * A.as <float >();
64+ const auto & b_data = * B.as <float >();
5465 std::vector<float > result (output_shape.count ());
5566
5667 for (size_t i = 0 ; i < result.size (); ++i) {
5768 size_t a_idx = get_broadcasted_index (i, A.get_shape (), output_shape);
5869 size_t b_idx = get_broadcasted_index (i, B.get_shape (), output_shape);
5970 result[i] = a_data[a_idx] * b_data[b_idx];
6071 }
61- output = Tensor (result, output_shape);
72+ output = make_tensor (result, output_shape);
6273 break ;
6374 }
6475 case Type::kInt : {
65- auto a_data = A.as <int >()-> get_data ();
66- auto b_data = B.as <int >()-> get_data ();
76+ const auto & a_data = * A.as <int >();
77+ const auto & b_data = * B.as <int >();
6778 std::vector<int > result (output_shape.count ());
6879
6980 for (size_t i = 0 ; i < result.size (); ++i) {
7081 size_t a_idx = get_broadcasted_index (i, A.get_shape (), output_shape);
7182 size_t b_idx = get_broadcasted_index (i, B.get_shape (), output_shape);
7283 result[i] = a_data[a_idx] * b_data[b_idx];
7384 }
74- output = Tensor (result, output_shape);
85+ output = make_tensor (result, output_shape);
7586 break ;
7687 }
7788 default :
@@ -84,21 +95,23 @@ void MulLayer::run_with_scalar(const Tensor& input, float scalar,
8495 const auto & shape = input.get_shape ();
8596 switch (input.get_type ()) {
8697 case Type::kFloat : {
87- auto input_data = input.as <float >()->get_data ();
88- std::vector<float > result (shape.count ());
89- for (size_t i = 0 ; i < result.size (); ++i) {
90- result[i] = input_data[i] * scalar;
98+ const auto & input_data = *input.as <float >();
99+ std::vector<float > result;
100+ result.reserve (shape.count ());
101+ for (auto val : input_data) {
102+ result.push_back (val * scalar);
91103 }
92- output = Tensor (result, shape);
104+ output = make_tensor (result, shape);
93105 break ;
94106 }
95107 case Type::kInt : {
96- auto input_data = input.as <int >()->get_data ();
97- std::vector<int > result (shape.count ());
98- for (size_t i = 0 ; i < result.size (); ++i) {
99- result[i] = input_data[i] * static_cast <int >(scalar);
108+ const auto & input_data = *input.as <int >();
109+ std::vector<int > result;
110+ result.reserve (shape.count ());
111+ for (auto val : input_data) {
112+ result.push_back (val * static_cast <int >(scalar));
100113 }
101- output = Tensor (result, shape);
114+ output = make_tensor (result, shape);
102115 break ;
103116 }
104117 default :
@@ -143,7 +156,7 @@ std::vector<size_t> MulLayer::get_strides(const Shape& shape) {
143156 if (strides.empty ()) return strides;
144157
145158 strides.back () = 1 ;
146- for (int i = shape.dims () - 2 ; i >= 0 ; --i) {
159+ for (int i = ( int ) shape.dims () - 2 ; i >= 0 ; --i) {
147160 strides[i] = strides[i + 1 ] * shape[i + 1 ];
148161 }
149162 return strides;
0 commit comments