-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
032372c
commit de5eee5
Showing
21 changed files
with
254 additions
and
147 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,135 @@ | ||
// SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
#include "prod.hpp" | ||
#include "prod_nc_op.hpp" | ||
#include "prod_op_all.hpp" | ||
#include "ttnn/deprecated/tt_dnn/op_library/auto_format.hpp" | ||
#include "ttnn/cpp/ttnn/operations/creation.hpp" | ||
#include "ttnn/operations/data_movement/slice/slice.hpp" | ||
#include "ttnn/operations/data_movement/permute/permute.hpp" | ||
#include "tt_numpy/functions.hpp" | ||
|
||
|
||
namespace ttnn { | ||
|
||
namespace operations { | ||
|
||
namespace prod { | ||
|
||
inline Tensor zeros( | ||
const tt::tt_metal::Shape shape, DataType data_type, Layout layout, Device* device, const MemoryConfig& output_mem_config) { | ||
return tt::numpy::zeros(shape, data_type, layout, device, output_mem_config); | ||
} | ||
|
||
// Autoformat support | ||
inline Tensor change_layout_to_tile(const Tensor& temp, const MemoryConfig& output_mem_config) { | ||
auto formatted_input_tensor = temp; | ||
if(formatted_input_tensor.get_layout()==Layout::ROW_MAJOR){ | ||
auto a_pad_shape = AutoFormat::pad_to_tile_shape(temp.get_legacy_shape(), false, false, true, true); | ||
if (!AutoFormat::check_input_tensor_format(temp, a_pad_shape)) { | ||
formatted_input_tensor = AutoFormat::format_input_tensor(temp, temp.device(), a_pad_shape, 1.0, Layout::TILE); | ||
} | ||
} | ||
return formatted_input_tensor; | ||
} | ||
|
||
inline Tensor prod_all(const Tensor& input_a, const MemoryConfig& output_mem_config) { | ||
auto formatted_input_tensor = input_a; | ||
if (formatted_input_tensor.get_layout() == Layout::ROW_MAJOR) { | ||
auto a_pad_shape = AutoFormat::pad_to_tile_shape(input_a.get_legacy_shape(), false, false, true, true); | ||
auto out_shape = input_a.get_legacy_shape(); | ||
out_shape = {out_shape[0], out_shape[1], out_shape[2], out_shape[3]}; | ||
if (!AutoFormat::check_input_tensor_format(input_a, a_pad_shape)) { | ||
formatted_input_tensor = | ||
AutoFormat::format_input_tensor(input_a, input_a.device(), a_pad_shape, 1.0, Layout::TILE); | ||
} | ||
} | ||
return tt::operations::primary::prod_all(formatted_input_tensor, output_mem_config); | ||
} | ||
|
||
inline Tensor prod_nc(const Tensor& temp, int64_t dim, const MemoryConfig& output_mem_config) { | ||
// layout conversion | ||
auto formatted_input_tensor = temp; | ||
if (formatted_input_tensor.get_layout() == Layout::ROW_MAJOR) { | ||
auto a_pad_shape = AutoFormat::pad_to_tile_shape(temp.get_legacy_shape(), false, false, true, true); | ||
auto out_shape = temp.get_legacy_shape(); | ||
out_shape = {out_shape[0], out_shape[1], out_shape[2], out_shape[3]}; | ||
if (!AutoFormat::check_input_tensor_format(temp, a_pad_shape)) { | ||
formatted_input_tensor = | ||
AutoFormat::format_input_tensor(temp, temp.device(), a_pad_shape, 1.0, Layout::TILE); | ||
} | ||
} | ||
// Apply prod | ||
std::vector<int64_t> dimension = {(dim == 1 || dim == -3) ? 1 : 0}; | ||
tt::tt_metal::Shape input_shape = formatted_input_tensor.get_legacy_shape(); | ||
tt::tt_metal::Shape required = { | ||
((dim == 1 || dim == -3) ? input_shape[0] : 1), | ||
((dim == 1 || dim == -3) ? 1 : input_shape[1]), | ||
input_shape[2], | ||
input_shape[3]}; | ||
return tt::operations::primary::prod_nc( | ||
formatted_input_tensor, | ||
zeros( | ||
required, | ||
formatted_input_tensor.get_dtype(), | ||
formatted_input_tensor.get_layout(), | ||
formatted_input_tensor.device(), | ||
output_mem_config), | ||
dimension, | ||
output_mem_config); | ||
} | ||
|
||
|
||
Tensor ProdOp::_prod(const Tensor& input_a, bool all_dimensions, int64_t dim, const MemoryConfig& output_mem_config) { | ||
if (all_dimensions) { | ||
return prod_all(input_a, output_mem_config); | ||
} | ||
TT_FATAL(dim >= -4 && dim <= 3 && "Dimension out of range (expected to be in range of [-4, 3]"); | ||
Tensor temp = input_a; | ||
// Permute for dim 2,3 | ||
if (dim == 2 || dim == -2) { | ||
std::vector<int64_t> permute_dims = {2, 0, 1, 3}; | ||
temp = ttnn::permute(input_a, permute_dims, output_mem_config); | ||
} else if (dim == 3 || dim == -1) { | ||
std::vector<int64_t> permute_dims = {3, 0, 1, 2}; | ||
temp = ttnn::permute(input_a, permute_dims, output_mem_config); | ||
} | ||
Tensor result = prod_nc(temp, dim, output_mem_config); | ||
// Permute and unpad result for dim 2,3 | ||
if (dim == 0 || dim == 1 || dim == -4 || dim == -3) { | ||
return result; | ||
} else if (dim == 2 || dim == -2) { | ||
std::vector<int64_t> after_permute_dims = {1, 2, 0, 3}; | ||
Tensor required = ttnn::permute(result, after_permute_dims, output_mem_config); | ||
tt::tt_metal::Shape input_shape = input_a.get_legacy_shape(); | ||
std::vector<uint32_t> start_index = {0, 0, 0, 0}; | ||
std::vector<uint32_t> end_index = {input_shape[0] - 1, input_shape[1] - 1, 0, input_shape[3] - 1}; | ||
return ttnn::slice(0, required, start_index, end_index, std::nullopt); | ||
} else { // dim 3 | ||
// permute | ||
std::vector<int64_t> after_permute_dims = {1, 2, 0, 3}; | ||
Tensor required = ttnn::permute(result, after_permute_dims, output_mem_config); | ||
// unpad | ||
tt::tt_metal::Shape input_shape = input_a.get_legacy_shape(); | ||
std::vector<uint32_t> start_index = {0, 0, 0, 0}; | ||
std::vector<uint32_t> end_index = {input_shape[0] - 1, input_shape[1] - 1, 0, input_shape[2] - 1}; | ||
Tensor new_unpad_tensor = ttnn::slice(0, required, start_index, end_index, std::nullopt); | ||
// permute back | ||
after_permute_dims = {0, 1, 3, 2}; | ||
Tensor res_host = ttnn::permute(new_unpad_tensor, after_permute_dims, output_mem_config); | ||
if(res_host.storage_type() != StorageType::DEVICE or res_host.storage_type() != StorageType::MULTI_DEVICE) { | ||
res_host = res_host.pad_to_tile(0.0f); | ||
res_host = res_host.to(Layout::TILE); | ||
res_host = res_host.to(input_a.device()); | ||
} | ||
return res_host; | ||
} | ||
} | ||
|
||
|
||
} // namespace prod | ||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <optional> | ||
#include <functional> | ||
|
||
#include "ttnn/decorators.hpp" | ||
#include "ttnn/operations/core/core.hpp" | ||
|
||
namespace ttnn { | ||
|
||
namespace operations { | ||
|
||
namespace prod { | ||
|
||
struct ProdOp { | ||
static Tensor _prod( | ||
const Tensor& input_a, | ||
bool all_dimensions, | ||
int64_t dim, | ||
const MemoryConfig& output_mem_config); | ||
}; | ||
|
||
|
||
struct ExecuteProdOp { | ||
static Tensor execute_on_worker_thread( | ||
const Tensor& input, | ||
bool all_dimensions = false, | ||
int64_t dim = 0, | ||
const std::optional<MemoryConfig>& memory_config = std::nullopt) { | ||
|
||
return ProdOp::_prod(input, all_dimensions, dim, memory_config.value_or(input.memory_config())); | ||
} | ||
}; | ||
|
||
} // namespace prod | ||
} // namespace operations | ||
|
||
constexpr auto prod = ttnn::register_operation<operations::prod::ExecuteProdOp>("ttnn::prod"); | ||
|
||
} // 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
Oops, something went wrong.