Skip to content

Commit

Permalink
Remove deprecated Tensor constructor with Shape (#16955)
Browse files Browse the repository at this point in the history
### Ticket

### Problem description
We're continuing to remove all usages of Shape/LegacyShape from the
codebase

### What's changed
Removed Tensor constructor which takes Shape
Removed all usages

### Checklist
- [x] [Post commit CI
passes](https://github.com/tenstorrent/tt-metal/actions/runs/12913833779)
- [x] [Model regression CI testing
passes](https://github.com/tenstorrent/tt-metal/actions/runs/12899489935)
- [x] [Device performance regression CI testing
passes](https://github.com/tenstorrent/tt-metal/actions/runs/12899491700)
- [x] [T3K unit tests CI
passes](https://github.com/tenstorrent/tt-metal/actions/runs/12899497750)
- [x] [T3K frequent tests CI
passes](https://github.com/tenstorrent/tt-metal/actions/runs/12899494794)
- [x] [Nightly model and ttnn tests CI
passes](https://github.com/tenstorrent/tt-metal/actions/runs/12899505501)
- [x] New/Existing tests provide coverage for changes
  • Loading branch information
sminakov-tt authored Jan 22, 2025
1 parent de53975 commit ba35246
Show file tree
Hide file tree
Showing 22 changed files with 413 additions and 234 deletions.
2 changes: 1 addition & 1 deletion tests/tt_eager/ops/test_eltwise_binary_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Tensor host_function(const Tensor& input_tensor_a, const Tensor& input_tensor_b)
}
return Tensor(
OwnedStorage{output_buffer},
input_tensor_a.get_legacy_shape(),
input_tensor_a.get_logical_shape(),
input_tensor_a.get_dtype(),
input_tensor_a.get_layout());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/tt_eager/ops/test_eltwise_unary_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Tensor host_function(const Tensor& input_tensor) {

return Tensor(
OwnedStorage{output_buffer},
input_tensor.get_legacy_shape(),
input_tensor.get_logical_shape(),
input_tensor.get_dtype(),
input_tensor.get_layout());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/tt_eager/ops/test_transpose_wh_multi_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using namespace constants;

Tensor perform_transpose_wh(Tensor& input_tensor) {
TT_FATAL(input_tensor.storage_type() == StorageType::OWNED, "Error");
auto ashape = input_tensor.get_legacy_shape();
auto ashape = input_tensor.get_padded_shape();
TT_FATAL(ashape.rank() == 4, "Error");
auto bshape = ashape;
bshape[2] = ashape[3];
Expand Down
2 changes: 1 addition & 1 deletion tests/tt_eager/ops/test_transpose_wh_single_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using namespace constants;

Tensor perform_transpose_wh(Tensor& input_tensor) {
TT_FATAL(input_tensor.storage_type() == StorageType::OWNED, "Error");
auto ashape = input_tensor.get_legacy_shape();
auto ashape = input_tensor.get_padded_shape();
TT_FATAL(ashape.rank() == 4, "Error");
auto bshape = ashape;
bshape[2] = ashape[3];
Expand Down
22 changes: 8 additions & 14 deletions tests/tt_eager/tensors/test_raw_host_memory_pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@

template <typename DataType>
struct NDArray {
tt::tt_metal::LegacyShape shape;
ttnn::SimpleShape shape;
void* data;

NDArray(tt::tt_metal::LegacyShape shape) :
shape(shape), data(malloc(tt::tt_metal::compute_volume(shape) * sizeof(DataType))) {}
NDArray(const ttnn::SimpleShape& shape) : shape(shape), data(malloc(shape.volume() * sizeof(DataType))) {}
~NDArray() { free(data); }

std::size_t size() const { return tt::tt_metal::compute_volume(shape); }
std::size_t size() const { return shape.volume(); }
};

void test_raw_host_memory_pointer() {
Expand All @@ -66,14 +65,11 @@ void test_raw_host_memory_pointer() {
int device_id = 0;
tt::tt_metal::IDevice* device = tt::tt_metal::CreateDevice(device_id);

tt::tt_metal::LegacyShape shape = {1, 1, tt::constants::TILE_HEIGHT, tt::constants::TILE_WIDTH};
ttnn::SimpleShape shape({1, 1, tt::constants::TILE_HEIGHT, tt::constants::TILE_WIDTH});

// Host tensor to print the output
Tensor tensor_for_printing = Tensor(
OwnedStorage{owned_buffer::create<bfloat16>(tt::tt_metal::compute_volume(shape))},
shape,
DataType::BFLOAT16,
Layout::TILE);
Tensor tensor_for_printing =
Tensor(OwnedStorage{owned_buffer::create<bfloat16>(shape.volume())}, shape, DataType::BFLOAT16, Layout::TILE);

/* Borrow Data from Numpy Start */
// Create some
Expand Down Expand Up @@ -128,14 +124,12 @@ void test_raw_host_memory_pointer() {
/* Alternative Way to Print Start */
// Alternatively, we could allocate memory manually and create Tensors with BorrowedStorage on the fly to print the
// data
void* storage_of_alternative_tensor_for_printing = malloc(tt::tt_metal::compute_volume(shape) * sizeof(bfloat16));
void* storage_of_alternative_tensor_for_printing = malloc(shape.volume() * sizeof(bfloat16));
tt::tt_metal::memcpy(storage_of_alternative_tensor_for_printing, c_dev);

Tensor alternative_tensor_for_printing = Tensor(
BorrowedStorage{
borrowed_buffer::Buffer(
static_cast<bfloat16*>(storage_of_alternative_tensor_for_printing),
tt::tt_metal::compute_volume(shape)),
borrowed_buffer::Buffer(static_cast<bfloat16*>(storage_of_alternative_tensor_for_printing), shape.volume()),
on_creation_callback,
on_destruction_callback},
shape,
Expand Down
2 changes: 1 addition & 1 deletion tests/ttnn/unit_tests/gtests/test_async_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ TEST_F(MultiCommandQueueSingleDeviceFixture, TestAsyncPreallocatedOutputs) {
Tensor input_tensor = Tensor(
input_storage,
TensorSpec(input_shape, TensorLayout(DataType::BFLOAT16, PageConfig(Layout::TILE), MemoryConfig{})));
Tensor output_tensor = Tensor(output_storage, np_out.get_shape(), DataType::BFLOAT16, Layout::TILE);
Tensor output_tensor = Tensor(output_storage, np_out.get_logical_shape(), DataType::BFLOAT16, Layout::TILE);
// Populate input_tensor with data
ttnn::write_buffer(io_cq, input_tensor, {host_data});
// Record the completion of the write event
Expand Down
6 changes: 1 addition & 5 deletions tests/ttnn/unit_tests/gtests/test_multi_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ Tensor create_host_multi_device_tensor(const Tensor& tensor, const ReplicateTens
specs.push_back(tensor.get_tensor_spec());
}

return Tensor{
MultiDeviceHostStorage(strategy, owned_buffers, specs),
tensor.get_legacy_shape(),
tensor.get_dtype(),
tensor.get_layout()};
return Tensor{MultiDeviceHostStorage(strategy, owned_buffers, specs), tensor.get_tensor_spec()};
}

TEST_F(T3kMultiDeviceFixture, TestGetTensorsFromMultiDeviceStorage) {
Expand Down
2 changes: 1 addition & 1 deletion tt-train/sources/examples/sample_app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int main() {
// Let the tensor take ownership of the buffer
OwnedStorage{std::move(buffer)},
// IMPORTANT: SHAPE MUST BE 4D ELSE EVERYTHING WILL BREAK during the PAD operation
{1, 1, tensor_width, tensor_height},
ttnn::SimpleShape({1, 1, tensor_width, tensor_height}),
// The data type of the tensor
tt::tt_metal::DataType::BFLOAT16,
// The layout of the tensor. We don't care about the layout in this demo. But the valid options are TILE and
Expand Down
2 changes: 1 addition & 1 deletion tt-train/sources/ttml/core/tt_tensor_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ tt::tt_metal::Tensor ttml_create_owned_tensor(
std::vector<T>&& data, const ttnn::Shape& shape, tt::tt_metal::DataType data_type, tt::tt_metal::Layout layout) {
auto buffer = tt::tt_metal::owned_buffer::create(std::move(data));
auto storage = OwnedStorage{std::move(buffer)};
return {std::move(storage), shape, data_type, layout};
return {std::move(storage), shape.logical_shape(), data_type, layout};
}

} // namespace
Expand Down
47 changes: 26 additions & 21 deletions ttnn/cpp/ttnn/distributed/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,7 @@ Tensor aggregate_as_tensor(
}
}
auto storage = MultiDeviceHostStorage{config, std::move(host_owned_buffers), specs};
return Tensor(
std::move(storage),
reference_shard.get_legacy_shape(),
reference_shard.get_dtype(),
reference_shard.get_layout(),
tile);
return Tensor(std::move(storage), reference_shard.get_tensor_spec());
} else {
std::vector<int> ordered_device_ids;
std::unordered_map<int, ttnn::TensorSpec> specs;
Expand All @@ -122,12 +117,7 @@ Tensor aggregate_as_tensor(
}
}
auto storage = MultiDeviceStorage{config, ordered_device_ids, std::move(device_buffers), specs};
return Tensor(
std::move(storage),
reference_shard.get_legacy_shape(),
reference_shard.get_dtype(),
reference_shard.get_layout(),
tile);
return Tensor(std::move(storage), reference_shard.get_tensor_spec());
}
}

Expand Down Expand Up @@ -194,9 +184,14 @@ Tensor get_device_tensor(const Tensor& multi_device_tensor, const int device_id)
tensor_storage != nullptr && tensor_storage->has_buffer_for_device_id(device_id)) {
return Tensor{
DeviceStorage{tensor_storage->get_buffer_for_device_id(device_id)},
multi_device_tensor.get_shape(),
multi_device_tensor.get_dtype(),
multi_device_tensor.get_layout()};
TensorSpec(
multi_device_tensor.get_logical_shape(),
TensorLayout::fromPaddedShape(
multi_device_tensor.get_dtype(),
PageConfig(multi_device_tensor.get_layout()),
MemoryConfig{},
multi_device_tensor.get_logical_shape(),
multi_device_tensor.get_padded_shape()))};
} else if (std::holds_alternative<tt::tt_metal::DeviceStorage>(multi_device_tensor.get_storage())) {
return multi_device_tensor;
}
Expand Down Expand Up @@ -266,9 +261,14 @@ Tensor create_multi_device_tensor(
}
return Tensor{
MultiDeviceStorage{strategy, ordered_device_ids, device_buffers, specs},
tensors.at(0).get_legacy_shape(),
tensors.at(0).get_dtype(),
tensors.at(0).get_layout()};
TensorSpec(
tensors.at(0).get_logical_shape(),
TensorLayout::fromPaddedShape(
tensors.at(0).get_dtype(),
PageConfig(tensors.at(0).get_layout()),
MemoryConfig{},
tensors.at(0).get_logical_shape(),
tensors.at(0).get_padded_shape()))};
} else if (storage_type == StorageType::MULTI_DEVICE_HOST) {
std::vector<OwnedBuffer> owned_buffers;
std::vector<ttnn::TensorSpec> specs;
Expand All @@ -282,9 +282,14 @@ Tensor create_multi_device_tensor(
}
return Tensor{
MultiDeviceHostStorage{strategy, owned_buffers, specs},
tensors.at(0).get_legacy_shape(),
tensors.at(0).get_dtype(),
tensors.at(0).get_layout()};
TensorSpec(
tensors.at(0).get_logical_shape(),
TensorLayout::fromPaddedShape(
tensors.at(0).get_dtype(),
PageConfig(tensors.at(0).get_layout()),
MemoryConfig{},
tensors.at(0).get_logical_shape(),
tensors.at(0).get_padded_shape()))};
} else {
TT_THROW("Invalid storage type for multi-device tensor");
}
Expand Down
119 changes: 76 additions & 43 deletions ttnn/cpp/ttnn/operations/core/to_dtype/to_dtype_op.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,27 @@ inline Tensor convert_to_cpp_supported_dtype(const Tensor& input_tensor) {
[&](auto&& buffer) -> Tensor {
using T = std::decay_t<decltype(buffer)>;
if constexpr (std::is_same_v<T, tt::tt_metal::OwnedBuffer>) {
return Tensor{
return Tensor(
tt::tt_metal::OwnedStorage{buffer},
input_tensor.get_shape(),
input_dtype,
input_tensor.get_layout()};
TensorSpec(
input_tensor.get_logical_shape(),
TensorLayout::fromPaddedShape(
input_dtype,
PageConfig(input_tensor.get_layout()),
MemoryConfig{},
input_tensor.get_logical_shape(),
input_tensor.get_padded_shape())));
} else if constexpr (std::is_same_v<T, tt::tt_metal::BorrowedBuffer>) {
return Tensor{
tt::tt_metal::BorrowedStorage{buffer, []() {}, []() {}},
input_tensor.get_shape(),
input_dtype,
input_tensor.get_layout()};
TensorSpec(
input_tensor.get_logical_shape(),
TensorLayout::fromPaddedShape(
input_dtype,
PageConfig(input_tensor.get_layout()),
MemoryConfig{},
input_tensor.get_logical_shape(),
input_tensor.get_padded_shape()))};
} else {
TT_THROW("Unsupported buffer type");
}
Expand Down Expand Up @@ -112,46 +122,65 @@ inline std::vector<NewT> cast(const tt::tt_metal::borrowed_buffer::Buffer<OldT>&
}

template <typename T>
Tensor create_owned_tensor(std::vector<T>&& data, const Shape& shape, DataType data_type, Layout layout) {
Tensor create_owned_tensor(
std::vector<T>&& data,
const SimpleShape& logical_shape,
const SimpleShape& padded_shape,
DataType data_type,
Layout layout) {
auto buffer = tt::tt_metal::owned_buffer::create(std::move(data));
auto storage = tt::tt_metal::OwnedStorage{std::move(buffer)};
return Tensor(std::move(storage), shape, data_type, layout);
return Tensor(
std::move(storage),
TensorSpec(
logical_shape,
TensorLayout::fromPaddedShape(data_type, PageConfig(layout), MemoryConfig{}, logical_shape, padded_shape)));
}

template <typename T>
inline Tensor create_tensor_from_buffer(
const tt::tt_metal::borrowed_buffer::Buffer<T>& input_buffer,
const Shape& shape,
const SimpleShape& logical_shape,
const SimpleShape& padded_shape,
const Layout& input_layout,
const DataType& dtype) {
switch (dtype) {
case DataType::UINT16: {
auto data = cast<uint16_t, T>(input_buffer);
return create_owned_tensor(std::move(data), shape, dtype, Layout::ROW_MAJOR).to(input_layout);
return create_owned_tensor(std::move(data), logical_shape, padded_shape, dtype, Layout::ROW_MAJOR)
.to(input_layout);
}
case DataType::INT32: {
auto data = cast<int32_t, T>(input_buffer);
return create_owned_tensor(std::move(data), shape, dtype, Layout::ROW_MAJOR).to(input_layout);
return create_owned_tensor(std::move(data), logical_shape, padded_shape, dtype, Layout::ROW_MAJOR)
.to(input_layout);
}
case DataType::UINT32: {
auto data = cast<uint32_t, T>(input_buffer);
return create_owned_tensor(std::move(data), shape, dtype, Layout::ROW_MAJOR).to(input_layout);
return create_owned_tensor(std::move(data), logical_shape, padded_shape, dtype, Layout::ROW_MAJOR)
.to(input_layout);
}
case DataType::FLOAT32: {
auto data = cast<float, T>(input_buffer);
return create_owned_tensor(std::move(data), shape, dtype, Layout::ROW_MAJOR).to(input_layout);
return create_owned_tensor(std::move(data), logical_shape, padded_shape, dtype, Layout::ROW_MAJOR)
.to(input_layout);
}
case DataType::BFLOAT16: {
auto data = cast<::bfloat16, T>(input_buffer);
return create_owned_tensor(std::move(data), shape, dtype, Layout::ROW_MAJOR).to(input_layout);
return create_owned_tensor(std::move(data), logical_shape, padded_shape, dtype, Layout::ROW_MAJOR)
.to(input_layout);
}
case DataType::BFLOAT8_B:
case DataType::BFLOAT4_B: {
auto data = cast<float, T>(input_buffer);
auto buffer = tt::tt_metal::owned_buffer::create<float>(std::move(data));
auto tensor =
Tensor(tt::tt_metal::OwnedStorage{std::move(buffer)}, shape, DataType::FLOAT32, Layout::ROW_MAJOR)
.to(Layout::TILE);
auto tensor = Tensor(
tt::tt_metal::OwnedStorage{std::move(buffer)},
logical_shape,
padded_shape,
DataType::FLOAT32,
Layout::ROW_MAJOR)
.to(Layout::TILE);
auto output_float_data = tt::tt_metal::owned_buffer::get_as<float>(tensor).get();
auto output_packed_data =
dtype == DataType::BFLOAT8_B
Expand All @@ -160,7 +189,8 @@ inline Tensor create_tensor_from_buffer(
auto output_buffer = tt::tt_metal::owned_buffer::create<uint32_t>(std::move(output_packed_data));
return Tensor(
tt::tt_metal::OwnedStorage{std::move(output_buffer)},
shape,
logical_shape,
padded_shape,
dtype,
Layout::TILE); // has to be in tile layout
}
Expand All @@ -173,32 +203,35 @@ inline Tensor create_tensor_from_buffer(

inline Tensor convert_to_dtype(const Tensor& input_tensor, const Layout& input_layout, const DataType& dtype) {
auto input_dtype = input_tensor.get_dtype();
const auto& logical_shape = input_tensor.get_logical_shape();
const auto& padded_shape = input_tensor.get_padded_shape();

auto convert_dtype = [&input_layout, &input_dtype, &dtype](const Tensor& input_tensor) {
switch (input_dtype) {
case DataType::UINT16: {
auto buffer = host_buffer::get_as<uint16_t>(input_tensor);
return create_tensor_from_buffer(buffer, input_tensor.get_shape(), input_layout, dtype);
}
case DataType::INT32: {
auto buffer = host_buffer::get_as<int32_t>(input_tensor);
return create_tensor_from_buffer(buffer, input_tensor.get_shape(), input_layout, dtype);
}
case DataType::UINT32: {
auto buffer = host_buffer::get_as<uint32_t>(input_tensor);
return create_tensor_from_buffer(buffer, input_tensor.get_shape(), input_layout, dtype);
}
case DataType::FLOAT32: {
auto buffer = host_buffer::get_as<float>(input_tensor);
return create_tensor_from_buffer(buffer, input_tensor.get_shape(), input_layout, dtype);
auto convert_dtype =
[&input_layout, &input_dtype, &dtype, &logical_shape, &padded_shape](const Tensor& input_tensor) {
switch (input_dtype) {
case DataType::UINT16: {
auto buffer = host_buffer::get_as<uint16_t>(input_tensor);
return create_tensor_from_buffer(buffer, logical_shape, padded_shape, input_layout, dtype);
}
case DataType::INT32: {
auto buffer = host_buffer::get_as<int32_t>(input_tensor);
return create_tensor_from_buffer(buffer, logical_shape, padded_shape, input_layout, dtype);
}
case DataType::UINT32: {
auto buffer = host_buffer::get_as<uint32_t>(input_tensor);
return create_tensor_from_buffer(buffer, logical_shape, padded_shape, input_layout, dtype);
}
case DataType::FLOAT32: {
auto buffer = host_buffer::get_as<float>(input_tensor);
return create_tensor_from_buffer(buffer, logical_shape, padded_shape, input_layout, dtype);
}
case DataType::BFLOAT16: {
auto buffer = host_buffer::get_as<::bfloat16>(input_tensor);
return create_tensor_from_buffer(buffer, logical_shape, padded_shape, input_layout, dtype);
}
default: TT_THROW("Unsupported DataType: {}", input_dtype); break;
}
case DataType::BFLOAT16: {
auto buffer = host_buffer::get_as<::bfloat16>(input_tensor);
return create_tensor_from_buffer(buffer, input_tensor.get_shape(), input_layout, dtype);
}
default: TT_THROW("Unsupported DataType: {}", input_dtype); break;
}
};
};
return distributed::is_multi_device_tensor(input_tensor) ? transform(input_tensor, convert_dtype)
: convert_dtype(input_tensor);
}
Expand Down
Loading

0 comments on commit ba35246

Please sign in to comment.