Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing get_shape() #16786

Open
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

jbedichekTT
Copy link

@jbedichekTT jbedichekTT commented Jan 15, 2025

Ticket

None

Problem description

Replacing the legacy Tensor::get_shape() with new Tensor::get_padded_shape and Tensor::get_logical_shape from Moreh folder.

What's changed

  • Remove usage of Tensor::get_shape() in more places, replacing with Tensor::get_logical_shape() or Tensor::get_padded_shape()
  • Shift from copying a value, to getting a const reference to avoid any copies

Checklist

  • Post commit CI passes
  • Device performance regression CI testing passes (if applicable)
  • New/Existing tests provide coverage for changes

@jbedichekTT jbedichekTT changed the title #0: Replacing get_shape() Replacing get_shape() Jan 15, 2025
for (int i = 0; i < input_shape.rank(); ++i) {
TT_FATAL(
input_shape[i] == output_shape[i],
"Input shape must match output shape. Received input_shape = {} and output_shape = {}.",
input_shape[i],
output_shape[i]);
TT_FATAL(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@razorback3 can someone from the team please check?

Comment on lines 24 to 26
// Unnecessary? ->
//const auto input_shape_wo_padding = input.get_logical_shape();
//const auto output_shape_wo_padding = input.get_logical_shape();
Copy link
Member

@ayerofieiev-tt ayerofieiev-tt Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My recommendation is to remove things you believe are not needed, instead of commenting them
You can then ping related parties in the PR to clarify with them.

Comment on lines 119 to 120
uint32_t index_size = index.get_logical_shape()[-1];
uint32_t index_size_without_padding = index.get_logical_shape()[-1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@razorback3 , both values are without a padding here. I think someone from Moreh team will have to take a look

}

uint32_t index_size = index_tensors.front().get_shape().value[-1];
const uint32_t& index_size = index_tensors.front().get_logical_shape()[-1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_shape().value is returns a LegacyShape. its operator[] will give padded value.
So the proper replacement here is get_padded_shape()

Comment on lines 45 to 46
const auto& input_shape = input.get_padded_shape();
const auto& input_shape_without_padding = input.get_logical_shape()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@razorback3 , another place where there seem to be an issue. get_shape and input_shape.value.without_padding() are basically returning the same thing. One can probably be removed.

auto input_shape_without_padding = input_shape.value.without_padding();
auto output_shape = output.get_shape();
auto output_shape_without_padding = output_shape.value.without_padding();
const auto& input_shape = input.get_padded_shape();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbedichekTT input_shape variable here seem to have had a semantics of get_logical_shape()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    auto input_shape = input.get_shape();

@@ -31,34 +31,37 @@ void MorehGroupNormOperation::validate_tensors(
check_tensor(beta, "moreh_group_norm", "beta");

// input (N, C, H, W)
auto C = input.get_shape().value[1];
auto C = input.get_logical_shape()[1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_padded_shape()

TT_FATAL(C % num_groups == 0, "input_shape[1] must be divisible by num_groups.");
// output (N, C, H, W)
if (output.has_value()) {
C = output.value().get_shape().value[1];
C = output.value().get_logical_shape()[1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_padded_shape()

TT_FATAL(C % num_groups == 0, "beta_shape[-1] must be divisible by num_groups.");
}

// mean (1, 1, N, num_groups)
if (mean.has_value()) {
TT_FATAL(
mean.value().get_shape().value.without_padding()[-1] == num_groups,
//"mean_shape[-1] must match num_groups.");
mean.value().get_logical_shape()[-1] == num_groups,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mean->get_logical_shape()[-1]

TT_FATAL(C % num_groups == 0, "beta_shape[-1] must be divisible by num_groups.");
}

// mean (1, 1, N, num_groups)
if (mean.has_value()) {
TT_FATAL(
mean.value().get_shape().value.without_padding()[-1] == num_groups,
//"mean_shape[-1] must match num_groups.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

TT_FATAL(C % num_groups == 0, "input_shape[1] must be divisible by num_groups.");
// input_grad (N, C, H, W)
if (input_grad.has_value()) {
C = input_grad.value().get_shape().value[1];
C = input_grad.value().get_logical_shape()[1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_padded_value

Comment on lines 68 to 69
const auto& input_shape = input.get_padded_shape();
const auto& input_shape_without_padding = input.get_logical_shape();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is flipped. I recommend to rename/refactor variable names.
input_shape --> input_shape_padded
input_shape_without_padding --> input_shape

Comment on lines 67 to 68
const auto& mean_rstd_shape = mean.get_padded_shape();
const auto mean_rstd_shape_without_padding = mean.get_logical_shape();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name mismatch

const auto mean_rstd_shape = mean.get_shape().value;
const auto mean_rstd_shape_without_padding = mean_rstd_shape.without_padding();

const auto& mean_rstd_shape_without_padding = mean.get_logical_shape();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name mismatch

Comment on lines 33 to 34
const auto& input_wo_shape = input.get_logical_shape();
const auto& other_wo_shape = other.get_logical_shape();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should just be input_shape and other_shape

Comment on lines 121 to 122
const auto& input_shape = tensor_args.input.get_padded_shape();
const auto& other_shape = tensor_args.other.get_padded_shape();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be input_shape_padded and other_shape_padded

Comment on lines 126 to 127
const auto& input_shape_wo_padding = tensor_args.input.get_logical_shape();
const auto& other_shape_wo_padding = tensor_args.other.get_logical_shape();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const auto& input_shape_wo_padding = tensor_args.input.get_logical_shape();
const auto& other_shape_wo_padding = tensor_args.other.get_logical_shape();
const auto& input_shape = tensor_args.input.get_logical_shape();
const auto& other_shape = tensor_args.other.get_logical_shape();

Comment on lines 54 to 55
const auto& a_shape = tensor_a.get_logical_shape();
const auto& b_shape = tensor_b.get_logical_shape();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_padded_shape()

Comment on lines 140 to 141
const auto& input_shape = input.get_padded_shape();
const auto& input_shape_wo_padding = input.get_logical_shape();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name mismatch

auto rank = shape.rank();
// Replace? ->
auto padding = shape.value.padding();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this likely does not compile.
Please revert this change. Someone will later make a pass here.

CC @sminakov-tt

const auto& input_grad_shape = input_grad.get_shape();
const auto& input_grad_shape_wo_padding = input_grad_shape.value.without_padding();
const auto& input_grad_shape = input_grad.get_logical_shape();
//const auto& input_grad_shape_wo_padding = input_grad_shape.value.without_padding();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this file changed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants