• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "fcp/aggregation/core/tensor_shape.h"
18 
19 #include <utility>
20 
21 #include "fcp/base/monitoring.h"
22 
23 #ifndef FCP_NANOLIBC
24 #include "fcp/aggregation/core/tensor.pb.h"
25 #endif
26 
27 namespace fcp {
28 namespace aggregation {
29 
NumElements() const30 size_t TensorShape::NumElements() const {
31   size_t num_elements = 1;
32   for (auto dim_size : dim_sizes_) {
33     num_elements *= dim_size;
34   }
35   return num_elements;
36 }
37 
38 #ifndef FCP_NANOLIBC
39 
FromProto(const TensorShapeProto & shape_proto)40 StatusOr<TensorShape> TensorShape::FromProto(
41     const TensorShapeProto& shape_proto) {
42   TensorShape::DimSizesVector dim_sizes;
43   for (int64_t dim_size : shape_proto.dim_sizes()) {
44     if (dim_size < 0) {
45       return FCP_STATUS(INVALID_ARGUMENT)
46              << "Negative dimension size isn't supported when converting from "
47              << "shape_proto: " << shape_proto.ShortDebugString();
48     }
49     dim_sizes.push_back(dim_size);
50   }
51   return TensorShape(std::move(dim_sizes));
52 }
53 
ToProto() const54 TensorShapeProto TensorShape::ToProto() const {
55   TensorShapeProto shape_proto;
56   for (auto dim_size : dim_sizes()) {
57     shape_proto.add_dim_sizes(static_cast<int64_t>(dim_size));
58   }
59   return shape_proto;
60 }
61 
62 #endif  // FCP_NANOLIBC
63 
64 }  // namespace aggregation
65 }  // namespace fcp
66