-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#8117: Move global_avg_pool2d to C++
- Loading branch information
1 parent
87a78d6
commit 5618f7f
Showing
12 changed files
with
136 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// SPDX-FileCopyrightText: © 2023 Tenstorrent Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
||
#include "ttnn/cpp/pybind11/decorators.hpp" | ||
#include "ttnn/operations/pool.hpp" | ||
#include "ttnn/types.hpp" | ||
|
||
namespace py = pybind11; | ||
|
||
namespace ttnn { | ||
namespace operations { | ||
namespace pool { | ||
|
||
namespace detail { | ||
|
||
void bind_global_avg_pool2d(py::module& module) { | ||
auto doc = fmt::format( | ||
R"doc({0}(input_tensor: ttnn.Tensor, *, memory_config: Optional[ttnn.MemoryConfig] = None, dtype: Optional[ttnn.DataType] = None) -> ttnn.Tensor | ||
Applies {0} to :attr:`input_tensor` by performing a 2D adaptive average pooling over an input signal composed of several input planes. This operation computes the average of all elements in each channel across the entire spatial dimensions. | ||
.. math:: | ||
{0}(\\mathrm{{input\\_tensor}}_i) | ||
Args: | ||
* :attr:`input_tensor` (ttnn.Tensor): The input tensor to be pooled. Typically of shape (batch_size, channels, height, width). | ||
Keyword Args: | ||
* :attr:`memory_config` (Optional[ttnn.MemoryConfig]): Memory configuration for the operation. | ||
* :attr:`dtype` (Optional[ttnn.DataType]): data type for the output tensor | ||
Returns: | ||
ttnn.Tensor: The tensor with the averaged values. The output tensor shape is (batch_size, channels, 1, 1). | ||
Example:: | ||
>>> tensor = ttnn.from_torch(torch.randn((10, 3, 32, 32), dtype=ttnn.bfloat16), device=device) | ||
>>> output = {1}(tensor) | ||
)doc", | ||
ttnn::operations::pool::global_avg_pool2d.name(), | ||
ttnn::operations::pool::global_avg_pool2d.python_fully_qualified_name()); | ||
|
||
bind_registered_operation( | ||
module, | ||
ttnn::operations::pool::global_avg_pool2d, | ||
doc, | ||
ttnn::pybind_arguments_t{ | ||
py::arg("input_tensor"), | ||
py::kw_only(), | ||
py::arg("memory_config") = std::nullopt, | ||
py::arg("dtype") = std::nullopt}); | ||
} | ||
|
||
} // namespace detail | ||
|
||
void py_module(py::module& module) { | ||
detail::bind_global_avg_pool2d(module); | ||
} | ||
|
||
} // namespace pool | ||
} // namespace operations | ||
} // namespace ttnn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// SPDX-FileCopyrightText: © 2023 Tenstorrent Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include "ttnn/decorators.hpp" | ||
#include "ttnn/operations/core.hpp" | ||
#include "tt_eager/tt_dnn/op_library/pool/average_pool.hpp" | ||
|
||
namespace ttnn { | ||
namespace operations { | ||
namespace pool { | ||
|
||
namespace detail { | ||
inline const std::array<ttnn::TensorSchema, 1> input_tensor_schemas() { | ||
return {ttnn::TensorSchema{ | ||
4, // min rank | ||
4, // max rank | ||
{ttnn::bfloat16, ttnn::bfloat8_b, ttnn::uint16, ttnn::uint32}, | ||
{ttnn::TILE_LAYOUT}, | ||
true, // can_be_on_device | ||
false, // can_be_on_cpu | ||
false, // can_be_scalar | ||
false // is_optional} | ||
}}; | ||
} | ||
} // namespace details | ||
|
||
struct GlobalAveragePool2D { | ||
static const std::array<TensorSchema, 1> input_tensor_schemas() { return detail::input_tensor_schemas(); } | ||
|
||
template <typename... Args> | ||
static auto input_tensors_to_validate(const Tensor& input_tensor, Args&&... args) { | ||
return std::make_tuple(input_tensor); | ||
} | ||
|
||
static Tensor execute(const Tensor& input, const std::optional<MemoryConfig>& memory_config_arg = std::nullopt, const std::optional<DataType>& output_dtype = std::nullopt) { | ||
auto memory_config = memory_config_arg.value_or(input.memory_config()); | ||
auto result = tt::tt_metal::average_pool_2d(input, memory_config, output_dtype); | ||
return result; | ||
} | ||
}; | ||
constexpr auto global_avg_pool2d = ttnn::register_operation<ttnn::operations::pool::GlobalAveragePool2D>("ttnn::pool::global_avg_pool2d"); | ||
} // namespace pool | ||
} // namespace operations | ||
} // namespace ttnn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters