/* * Copyright (c) Meta Platforms, Inc. and affiliates. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */ #include #include #include #include #include namespace executorch { namespace ET_RUNTIME_NAMESPACE { namespace { Result calculate_nbytes( const Span& sizes, const executorch::aten::ScalarType& scalar_type) { ssize_t n = 1; for (const auto i : c10::irange(sizes.size())) { if (sizes[i] < 0) { return Error::InvalidArgument; } n *= sizes[i]; } // Use the full namespace to disambiguate from c10::elementSize. return n * executorch::runtime::elementSize(scalar_type); } } // namespace Result TensorLayout::create( Span sizes, Span dim_order, executorch::aten::ScalarType scalar_type) { auto nbytes = calculate_nbytes(sizes, scalar_type); if (!nbytes.ok()) { return nbytes.error(); } if (dim_order.size() != sizes.size()) { return Error::InvalidArgument; } for (const auto i : c10::irange(dim_order.size())) { if (dim_order[i] >= sizes.size()) { return Error::InvalidArgument; } } return TensorLayout(sizes, dim_order, scalar_type, nbytes.get()); } } // namespace ET_RUNTIME_NAMESPACE } // namespace executorch